Read the response
Decode the body with a schema, or read text, bytes or the whole response.
Decode a response body with a schema, read it as text or bytes, or read the status and headers next to the decoded body.
Decode the body with a schema
Give a call an output schema, and the response body is decoded with it. The
call succeeds with the decoded value, or fails with DecodeError if the body
doesn't match. It also sends Accept: application/json, unless you set an
accept header yourself.
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 * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
// No output: succeeds with the HttpClientResponse.
export const = .("/todos/:id", { : { : 1 } });
// Nothing to decode: an empty body (204, HEAD) decodes as JSON null.
export const = .("/todos/:id/archive", {
: { : 1 },
: .,
});Read text or bytes
output reads the body as JSON. For other bodies, wrap the schema:
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 call sends a matching Accept header: text/*, */*;q=0.1 for text, and
*/* for bytes, unless you set one.
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.
- An empty body is
""forFetch.text()and aUint8Arrayof length 0 forFetch.bytes(). ForFetch.text(schema), it fails withDecodeError, as for JSON. - 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. 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"]) };
});- 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: use the decoded value.
- The default,
"decoded", gives the decoded body alone.
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.