Endpoints
Build an endpoint's URL
Get the URL a call to an endpoint would go to, without sending a request.
This page shows you how to get the URL an endpoint call would go to, as a
string, for a link, an <img> source, or a cache key.
Get the URL
endpoint.url(options) returns the URL a call with the same params and
query would go to. It works on a definition and on a bound endpoint:
import * as from "effect/Effect";
import * as from "effect/Schema";
import { } from "@monarcode/effetch";
const = .("GET /files/:name", {
: .({ : .(.) }),
});
class extends .<>()("Api", {
: "https://api.example.com",
}) {}
// "/files/report.pdf?download=true"
export const = .({
: { : "report.pdf" },
: { : true },
});
export const = .(function* () {
const = yield* ;
// "https://api.example.com/files/report.pdf?download=true"
return .().({
: { : "report.pdf" },
: { : true },
});
});What the URL contains
The URL contains the following:
- On a bound endpoint, the URL starts with the client's
baseUrl. - On a definition from
Fetch.endpoint, which has no client, it's the path alone, unless the route is a full URL. That makes it a stable cache key for one backend, whichever client calls it. - The URL is built before plugins run, so anything a plugin adds, such as a token in the query, isn't in it.
When url throws
url throws when a path parameter is missing or the query doesn't match the
schema, as a call would die. It encodes the query synchronously, so on an
endpoint whose query schema needs services to encode, calling it doesn't
compile.
What's next
- Typed query parameters: see how the query is encoded.
- Write a plugin: read the route a call goes to from a plugin.
- Call an endpoint: send the request the URL describes.