Skip to Content
Endpoints

Define endpoints

Save a route with its schemas and defaults, and see how they combine with the client's and the call's.

You define an endpoint once, and every call to it uses what you saved. This page shows how to write a definition with Fetch.endpoint, what it can hold, how its defaults combine with the client's and the call's, and what you can read back from it.

Define an endpoint

Fetch.endpoint takes a route, which is 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"
.; // User
.; // undefined, so the client's defaultError applies

A definition is plain data. It holds the route, the schemas and the defaults, but it can't make a call until you bind it to a client. To bind it, see Share endpoints as a service.

What a definition holds

Everything in a definition is optional. It takes the following options:

OptionWhat it does
outputDecodes a 2xx body. Without it, a call succeeds with the raw HttpClientResponse.
errorDecodes an error body into HttpError.body, in place of the client's defaultError.
statusErrorsTurns error responses into your own errors, by status. See Errors by status.
inputEncodes body, and makes body required on every call. GET, HEAD, OPTIONS and TRACE endpoints can't take one.
headersHeaders sent with every call.
responseModeWhat every call succeeds with. See Read the response.
queryA schema that types and encodes each call's query. See Typed query parameters.
stream"sse" or "ndjson", so every call returns a Stream. See Streaming responses.

A Fetch.endpoint definition takes no plugins, because plugin overrides are checked against a client's plugins, and a definition has no client yet. You give them when you bind it, or in the definition of api.endpoint, which defines and binds in one step (see Call an endpoint).

Write the route

The route is an uppercase method, one space, then a path that starts with / or a full http:// or https:// URL. TypeScript checks it as you type: "GTE /users" and "get /users" don't compile, and your editor suggests the methods.

Fetch.endpoint also checks the definition when it runs, and throws on what can't work. A bad definition then fails as soon as its module is imported, not on its first call. It throws on:

  • a malformed route, which TypeScript only lets through after a cast;
  • a query schema with a field that can't be encoded to a string, such as Schema.instanceOf(Uint8Array);
  • a stream that isn't "sse" or "ndjson", or that comes with a responseMode or a Fetch.bytes() output.

How defaults combine

An endpoint holds defaults, the client holds others, and each call can add its own. They combine 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 ones given when binding, over the definition's. Unlike the client's, an endpoint's headers can set content-type, such as application/merge-patch+json for a PATCH, since one endpoint sends the same kind of body on every call.
  • Plugin overrides. An override turns a plugin off for the endpoint, or changes its options, as in { bearer: false } for a login that sends no token (see Built-in plugins). The endpoint's overrides come from binding, or from api.endpoint's definition. A call's override for the same plugin wins, and when both give options, they're merged. So an endpoint bound with retry: false can still be retried in one call with plugins: { retry: { times: 2 } }.
  • Schemas. output, input and query belong to the endpoint, and a call can't change them.
  • Error body. The endpoint's error schema replaces the client's defaultError. Without one, the endpoint uses the defaultError of the client it's bound to.
  • Errors by status. The endpoint's statusErrors are tried first, then the client's. A call to an endpoint can't add a map of its own.
  • Response mode. A call's responseMode wins over the endpoint's. A stream endpoint's calls can't take one.

Read an endpoint's properties

An endpoint carries what it was defined with, so other code can read it. route, method and path are there for log labels and cache keys, and plugins get the same route, as ctx.route (see Write a plugin). The schemas are there for things such as decoding cached data or building test fixtures.

A definition from Fetch.endpoint and a bound endpoint have mostly the same properties. A few differ, because only a bound endpoint has a client to fall back on:

PropertyOn a definitionOn a bound endpoint
routeThe route, such as "GET /users/:id"The same
methodThe method, such as "GET"The same
pathThe path, such as "/users/:id"The same
outputThe schema, or undefined without oneThe same
inputThe schema, or undefined without oneThe same
errorThe schema, or undefined without oneThe endpoint's schema, else the client's defaultError, else Schema.Unknown
statusErrorsThe map as given, or undefined without oneFor each status, a list of the endpoint's schemas, then the client's. {} when neither has any
headersThe headers, or undefined without anyNot there
responseModeThe mode, or undefined without oneThe same
queryThe schema, or undefined without oneThe same
stream"sse", "ndjson", or undefinedThe same
url(options)The URL a call would go to, without a base URLThe same, starting with the client's baseUrl

For url, see Build an endpoint's URL.

What's next

On this page