Resilient Interfaces & Progressive Enhancement

Form Actions & API Outcomes


Learning Objectives

  • You can connect protected form input to one authoritative API operation.
  • You can preserve raw values and translate rejection into useful feedback.
  • You can distinguish a lost response from a confirmed rejection or acceptance.

The previous form checked input without storing it. A reservation form adds a real operation: its name and attendee count reach SvelteKit, then Hono decides whether the requested places are available. The page action preserves input and translates the result; the API owns capacity and transaction rules.

Separate parsing from acceptance

The exercise supplies the form, session hook and request-protection helper. readForm(request, locals) checks method, session, browser origin, CSRF and schema. If it rejects, return its status and raw-field feedback without calling the API. Hidden controls remain caller-controlled input even when this page rendered them.

A successful helper result keeps the native action’s raw/parsed distinction: values retains attendee input " 2 " for feedback, while parsed supplies number 2 to the API. The fixed client forwards only the approved session cookie and mutation headers. The page server must preserve origin and CSRF protection.

Follow the outcome of one reservation request:

API outcomeBrowser-facing action result
201 acceptedA fixed 303 to /reservation/received; GET reads the stored reservation.
409 capacity rejectionRetain the raw fields and say “No capacity available”.
401 or 403 access rejectionPreserve the status and say “Access no longer available”.
Other status or lost responseReturn 503 with “Reservation could not be confirmed”.

The API evaluates current capacity and can reject a schema-valid request with 409. Return that outcome with the original submitted fields.

Keep the transport catch narrow

Catch a rejected API call before mapping its response, leaving the accepted branch’s redirect outside the catch. Otherwise the action can report unavailability after the API accepted the reservation.

A redirect and an error handler

0 / 5 points

A page action uses this control flow. The API returns status 201. SvelteKit’s redirect throws the framework result described in Chapter 9.4.

try {
const response = await api.create(parsed);
if (response.status === 201) {
redirect(303, "/request/received");
}
return fail(409, { values, error: "Request rejected" });
} catch {
return fail(503, { values, error: "Outcome unconfirmed" });
}

Which result does this action actually return?

If the response is lost, the API may have committed. Keep acceptance unconfirmed and do not automatically request another reservation. A fresh read can establish current state; it may not identify what happened to the earlier request.

The checks follow these outcomes in the supplied reservation form:

  1. With an established session, a visitor submits Name and Attendees using Reserve. A confirmed acceptance leads to “Reservation received”, including the stored name, even with JavaScript disabled.
  2. Under a controlled capacity rejection, the form stays available, says “No capacity available” and retains the raw name and attendee strings.
  3. Under a lost response, it says “Reservation could not be confirmed” after the single API call and retains the raw fields.

Complete the action for these outcomes. Both submission paths use the same protected operation.

A reservation form action

0 / 40 points

Complete the action in routes/reservations/+page.server.ts, keeping its supplied load export. The path is relative to src/. The native/enhanced form, session hook, API and result page are supplied.

Supplied interfaces

Await readForm(request, locals) before requesting a reservation. It resolves to one of these results:

  • { ok: false, status, values, error } for a method, session, origin, CSRF or schema rejection. Return fail(status, { values, error }) without calling the API.
  • { ok: true, values, parsed }. values contains the exact raw name and attendees strings. parsed contains a validated name string and numeric attendee count.

Construct the API client with createApi(fetch, cookie), where cookie is selected from this request by selectedCookie. Its reservation(parsed, locals.csrfToken) method performs the authoritative request. The client already provides the approved Origin, CSRF, cookie and redirect policy.

Action outcomes

Request the reservation once with the parsed values. Use only the response status, mapped as follows:

API outcome Action result
201 redirect(303, "/reservation/received")
409 fail(409, { values, error: "No capacity available" })
401 or 403 Preserve that status with { values, error: "Access no longer available" }
Other status or transport rejection fail(503, { values, error: "Reservation could not be confirmed" })

Keep the raw values in rejection feedback. For example, attendee input " 2 " reaches the API as number 2 but remains " 2 " in capacity-rejection feedback.

If the response is lost, acceptance is unconfirmed; do not request another reservation automatically. After a confirmed acceptance, the redirect leads to a GET that reads the stored reservation.

Choose Submit solution. Protected parsed forwarding: 15 points; rejection mapping/raw fields: 10; accepted native 303/GET: 5; enhanced feedback and uncertainty without replay: 10.

Check Your Understanding

  1. Which checks precede the API call, and which rules remain with Hono?
  2. Why keep both raw and parsed attendee values?
  3. Why must an accepted redirect escape the transport catch?
  4. How could retrying after a lost response create a duplicate reservation?