Payload
The payload is the body a request sends, usually with POST, PUT or PATCH. In HyperFetch you type it once on the
request, so every place that sends it is checked by TypeScript.
- How to type a payload with the
payloadgeneric. - How to set it with
setPayloador pass it tosend(). - How the payload is encoded: JSON,
FormDataand binary bodies. - How to send a typed object as
FormDatawithsetPayloadMapper.
Type the payload
Add a payload generic next to response. A request with a payload type requires one at send().
type User = { id: number; name: string; email: string };
type CreateUserPayload = Omit<User, "id">;
export const createUser = client.createRequest<{
response: User;
payload: CreateUserPayload;
}>()({
endpoint: "/users",
method: "POST",
});
A payload of the wrong shape is a compile error:
createUser.setPayload({ name: "Ann" }); // email is missing
Set it with setPayload or send()
Pass the payload to send() when it comes from a form or user input at call time. Use setPayload to prepare a request
and send it later, or hand it to a hook.
// At call time, the common case
const { data, error } = await createUser.send({
payload: { name: "Ann", email: "ann@example.com" },
});
// Prepared ahead
const prepared = createUser.setPayload({
name: "Ann",
email: "ann@example.com",
});
await prepared.send();
As with params, a request with a payload already set no longer
accepts one in send(). Falsy values such as 0, false or "" are sent as they are.
How the payload is encoded
The fetch adapter decides the body from the payload's type:
| Payload | Body sent | Content-Type |
|---|---|---|
| Object or array | JSON.stringify | application/json, added for you |
FormData, Blob, URLSearchParams, ArrayBuffer, stream | As it is | Set by fetch, such as multipart |
| Primitive (string, number) | JSON.stringify | none |
A Content-Type header you set yourself replaces the default one, see headers.
Send FormData with a typed payload
Uploading files needs FormData, which has no type for its fields. Type the payload as a plain object and convert it in
setPayloadMapper, which runs right before the request is sent.
export const updateAvatar = client
.createRequest<{
response: User;
payload: { name: string; avatar: File };
}>()({ endpoint: "/users/:userId/avatar", method: "POST" })
.setPayloadMapper((payload) => {
const formData = new FormData();
formData.append("name", payload.name);
formData.append("avatar", payload.avatar);
return formData;
});
// Callers still pass a typed object
await updateAvatar.send({
params: { userId: 1 },
payload: { name: "Ann", avatar: file },
});
The mapper does not change the payload type, so callers keep the object shape. To map the payload of every request,
use client.adapter.setPayloadMapper instead. For upload progress, pass onUploadProgress to send().
You've learned how to send typed payloads.
- You can type a payload with the
payloadgeneric and let TypeScript require it. - You can set it with
setPayloador pass it tosend(). - You can send
FormDatafrom a typed object withsetPayloadMapper.
Runnable examples: Forms, File upload and Payload validation.