Call an endpoint
Pass what changes on each call, and bind endpoints to a client with bind, bindAll or api.endpoint.
This page shows you what to pass when you call an endpoint, and how to bind
endpoints to a client yourself when Fetch.Endpoints doesn't fit.
Pass what changes on each call
What's fixed goes in the definition, and what changes goes in each call:
| Defined once, on the endpoint | Passed with each call |
|---|---|
output, error, input, query, statusErrors | params, query, body |
headers, responseMode, stream | headers, responseMode |
plugins (when binding) | plugins |
A call follows these rules:
users.get({ params: { id } })sends exactly whatapi.get("/users/:id", { output: User, params: { id } })does, and returns the same Effect.paramsare required when the route's path has any, as with the methods. An endpoint that needs nothing more is called with no arguments:users.list().- With an
inputschema,bodyis required and typed by it. GET,HEAD,OPTIONSandTRACEendpoints take nobodyand noinput, and TypeScript rejects them.- With a
queryschema,queryis typed by it, and required when the schema has required fields. See Typed query parameters.
Bind endpoints with bind and bindAll
Fetch.Endpoints is built on two client methods, which you can also use
directly, such as inside your own service's layer:
api.bind(definition, options?)binds one definition.optionsare the binding'sheadersandpluginsoverrides.api.bindAll(record)binds a record of definitions, each under its own name. An entry can also be a[definition, options]pair, with the optionsapi.bindtakes. Each endpoint has the typeapi.bindgives it.
The following example binds the same two endpoints both ways:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
const = .({ : ., : . });
const = .({ : . });
const = .("GET /users/:id", { : });
const = .("POST /auth/login", { : });
class extends .<>()("Api", {
: "https://api.example.com",
: [({ : "token" })],
}) {}
export const = .(function* () {
const = yield* ;
return {
: .(),
: .(, { : { : false } }),
};
});
export const = .(function* () {
const = yield* ;
return .({
,
: [, { : { : false } }],
});
});Define and bind in one step
api.endpoint(route, definition) defines and binds in one step, where
you already have the client. Its definition can take plugins directly,
since it knows the client's plugins:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
import { } from "@monarcode/effetch/plugins";
const = .({ : . });
class extends .<>()("Api", {
: "https://api.example.com",
: [({ : "token" })],
}) {}
export const = .(function* () {
const = yield* ;
return {
: .("POST /auth/login", {
: ,
: { : false },
}),
};
});Choose between Fetch.endpoint and api.endpoint
api.bind(Fetch.endpoint(route, definition)) is the endpoint
api.endpoint(route, definition) makes, with the same type and the same
calls. Prefer Fetch.endpoint when:
- endpoints should be listed next to their schemas, in a file with no client (see A feature-sliced app);
- one definition serves several clients, such as staging and production;
- a tool works from your endpoints, and needs their routes and schemas
without a client, as
FetchTestdoes (see Test with a fake backend).
What's next
- Typed query parameters: give an endpoint a schema for its query.
- Build an endpoint's URL: get the URL a call would go to, without sending it.
- Endpoints or methods: decide which calls deserve an endpoint.