teqo/src/lib/campaignJsonRequest.ts
Francisco Ignácio Fontoura Solla 2cfb7d5e67 Simplify the WebAuthn adoption: an error branch that could not reach a screen.
`postJson` caught a non-JSON response and threw a plain `Error` carrying
"Resposta inválida do servidor.", but all three islands read `message` only off
a `CampaignWebAuthnError` — so the sentence was written for the person and then
thrown down the channel that discards messages. Deleted rather than re-typed:
the islands' own fallback is better copy for a field worker, which is the same
reason the five list-cell controls substitute their own. Nothing user-visible
changes, because the string was already unreachable; the comment now records
why the branch is absent, so it does not come back.

The login failure sentence was also spelled twice for one slot on one screen —
the route's `genericMessage` and the button's fallback — so it became
`CAMPAIGN_BIOMETRIC_LOGIN_ERROR_MESSAGE`, twin of the enroll constant the
register half already shares between its route and its card.

Also widened two doc comments this branch made stale: `campaignJsonMutationRoute`
and `campaignJsonRequest` both claimed to serve only the list cells' quick edits,
and four of nine callers are now WebAuthn ceremonies.
2026-07-28 12:30:36 -03:00

26 lines
1 KiB
TypeScript

/**
* Transport half of every `/campanha` JSON POST — the list cells' quick edits
* and the WebAuthn ceremonies: the preamble each one repeated around `fetch`
* (same-origin credentials, JSON headers, the cast of the discriminated
* response). Deliberately knows nothing about revert, retry or debounce — that
* is `useCampaignCellAutosave`'s, or the caller's.
*
* `ok` travels next to the payload because a route can answer a real body with
* a non-200 status (`engagement-level` returns its `blocked` state as 409), so
* the two are not the same question.
*/
export const postCampaignJson = async <TResponse>(
url: string,
body: unknown,
signal?: AbortSignal,
): Promise<{ ok: boolean; payload: TResponse }> => {
const response = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
credentials: 'same-origin',
signal,
body: JSON.stringify(body),
})
return { ok: response.ok, payload: (await response.json()) as TResponse }
}