Read the response
Decode the body with a schema, or read text, bytes or the whole response.
Most calls come back with a JSON body that you want as a typed value. Give
the call an output schema, and Effetch decodes the body with it, so the
call succeeds with the value your code expects. This page shows how, and
how to read a body that isn't JSON, or the status and headers next to it.
Decode the body with a schema
Pass the schema as output:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
// Succeeds with { id: number, title: string }
export const = .("/todos/:id", { : { : 1 }, : });The call succeeds with the decoded value, or fails with DecodeError if
the body doesn't match. It also sends Accept: application/json, unless an
accept header is set on the call, the endpoint or the client.
Only a 2xx response is decoded. Any other status, 3xx included, fails with
HttpError before output runs (see Call errors). fetch
follows redirects itself, so a 3xx only arrives with redirect: "manual"
or from a conditional request, such as 304 Not Modified.
Handle an empty body
Some responses have no body, such as a 204 No Content or any HEAD
response. An empty body decodes as JSON null, so give such a call
output: Schema.Void:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
export const = .("/todos/:id/archive", {
: { : 1 },
: .,
});Schema.Undefined and Schema.NullOr(…) accept an empty body too. A JSON
null body and an empty one decode the same way, so a schema can't tell
them apart.
Skip decoding
Without output, the call succeeds with the raw HttpClientResponse.
That's handy when you only care that the call worked, or want to read the
body yourself:
import type { } from "@monarcode/effetch";
declare const : .;
// Succeeds with the HttpClientResponse.
export const = .("/todos/:id", { : { : 1 } });The status is still checked: a response outside 2xx fails with HttpError
whether or not the call has an output.
Read text or bytes
output reads the body as JSON. For other bodies, wrap the schema in one of
these:
Fetch.text()reads the body as a string, as it was sent.Fetch.text(schema)reads a single value, such as a count or a status. It trims the text, then decodes it with a schema whose encoded side is a string, such asSchema.NumberFromString.Fetch.bytes()reads the body as aUint8Array, a copy of the body, for images, PDFs and other binary bodies.
The following program reads a CSV export as text, a count as a number, and a PDF as bytes:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
declare const : .;
export const = .(function* () {
const = yield* .("/export.csv", { : .() });
const = yield* .("/todos/count", {
: .(.),
});
const = yield* .("/invoices/:id", {
: { : 7 },
: .(),
});
// In a browser, Node, Bun or Deno. React Native's Blob doesn't take arrays.
return { , , : new ([], { : "application/pdf" }) };
});They work wherever output does, endpoints included. The one exception is
Fetch.bytes(), which a stream (api.sse, api.ndjson or a stream
endpoint) can't take.
The following rules apply to Fetch.text and Fetch.bytes:
- The call sends a matching
Acceptheader:text/*, */*;q=0.1for text, and*/*for bytes, unless anacceptheader is set on the call, the endpoint or the client. - An empty body is
""forFetch.text(), and aUint8Arrayof length 0 forFetch.bytes(). - For
Fetch.text(schema), an empty body fails withDecodeErrorbefore it reaches the schema, soSchema.NumberFromStringnever reads it as0. - A body that can't be read, or doesn't match the schema, fails with
DecodeError. - Error responses are still read as JSON for
errorandstatusErrors. - Pass the schema they return to
outputas it is, or with more checks or annotations. A schema built around it, such as withSchema.NullOr, is read as JSON again.
Read the body and the response
To read the status or headers next to the decoded body, such as a total count
or an ETag, set responseMode: "decoded-and-response". The call succeeds
with a pair:
import * as from "effect/Effect";
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
export const = .(function* () {
const [, ] = yield* .("/todos", {
: { : 1 },
: .(),
: "decoded-and-response",
});
return { , : (.["x-total-count"]) };
});The following rules apply to responseMode:
- The default,
"decoded", gives the decoded body alone. - It's allowed on calls, on endpoint definitions, and on each call of an endpoint, where the call's own mode wins.
- Without
output, both items are the response. - The body has already been read, so use the decoded value rather than reading the response's body again.
What's next
- Call errors: handle
DecodeErrorand the other ways a call fails. - Errors by status: turn error responses into your own errors.
- Streaming responses: read Server-Sent Events and newline-delimited JSON.