Define endpoints
Save a route with its schemas, defaults and overrides.
This page shows you how to define an endpoint with Fetch.endpoint, how
its defaults combine with the client's and the call's, and which properties
you can read from it.
Define an endpoint
Fetch.endpoint takes a route, the method and the path in one string, and
what every call to it shares. It needs no client, so you can define endpoints
next to their schemas:
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
const = .({ : ., : . });
const = .({ : . });
export const = .("GET /users/:id", { : });
export const = .("GET /users", {
: .(),
});
export const = .("POST /users", {
: ,
: ,
});
.; // "GET /users/:id"
.; // UserA definition is plain data: it holds the route, the schemas and the defaults, but it can't make a call until it's bound to a client. To bind it, see Share endpoints as a service.
Write the route
The route is an uppercase method, one space, then a path that starts with /
or a full http(s):// URL. TypeScript checks it as you type: "GTE /users"
and "get /users" don't compile, and your editor suggests the methods.
Fetch.endpoint also throws on a malformed route, so a bad one fails as soon
as its module is imported.
Set defaults and overrides
An endpoint's headers, plugin overrides and schemas combine with the client's and the call's as follows:
- Headers. A call's headers win over the endpoint's, which win over the
client's. A bound endpoint's headers are the binding's over the
definition's. Unlike the client's, an endpoint's headers can set
content-type. - Plugins. A call's plugin override wins over the endpoint's (given when
binding, or to
api.endpoint), and when both give options, they're merged. An endpoint bound withretry: falsecan still be retried in one call withplugins: { retry: { times: 2 } }. - Schemas belong to the endpoint, and can't be changed per call.
Read an endpoint's properties
Each endpoint also has route, method and path properties, for log labels
or cache keys. Plugins see the same route, as ctx.route (see
Write a plugin).
Its output, error and input properties are its schemas, for things like
decoding cached data or building test fixtures:
outputandinputareundefinedwhen the endpoint has none.erroris the schema error bodies are decoded with: the endpoint's, else the client'sdefaultError, elseSchema.Unknown.
What's next
- Share endpoints as a service: bind your definitions to a client and share them as a service.
- Call an endpoint: see what goes in each call, and other ways to bind.
- Typed query parameters: give an endpoint a schema for its query.