Skip to main content
Version: v10.5.0

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.

What you'll learn
  1. How to type a payload with the payload generic.
  2. How to set it with setPayload or pass it to send().
  3. How the payload is encoded: JSON, FormData and binary bodies.
  4. How to send a typed object as FormData with setPayloadMapper.

Type the payload​

Add a payload generic next to response. A request with a payload type requires one at send().

src/api/users.ts
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:

src/app.ts
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.

src/app.ts
// 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:

PayloadBody sentContent-Type
Object or arrayJSON.stringifyapplication/json, added for you
FormData, Blob, URLSearchParams, ArrayBuffer, streamAs it isSet by fetch, such as multipart
Primitive (string, number)JSON.stringifynone

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.

src/api/users.ts
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().


Congratulations!

You've learned how to send typed payloads.

  • You can type a payload with the payload generic and let TypeScript require it.
  • You can set it with setPayload or pass it to send().
  • You can send FormData from a typed object with setPayloadMapper.

Runnable examples: Forms, File upload and Payload validation.

Payload types
Type the payload and the response data in depth.
Validation
Validate the payload before it is sent.
Request
Params, query params and payload in one place.