Make a call
Pick a method, fill in the path's parameters and build the query string.
With a client in hand, every request is a single method call. You pass a path and an object of options, and you get back an Effect that sends the request when it runs. This page covers what every call has in common: the method, the path and its parameters, and the query string.
Pick a method
The client has a method for each common HTTP method, named in lowercase:
get,headandoptionssend no body.post,put,patchanddelcan send one. It'sdel, notdelete, becausedeleteis a reserved word in JavaScript.
Each one takes the path first, then the options:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
export const = .("/todos/1", { : });
export const = .("/todos/1", {
: { : "Write the docs" },
: ,
});Three other methods cover the rest:
api.requesttakes the HTTP method as its first argument. Use it when the method is only known at runtime, or for one with no shorthand, such asTRACEorQUERY(see When the methods aren't enough).api.sseandapi.ndjsonread a response that arrives a piece at a time (see Streaming responses).
Fill in path parameters
To put a value in the path, write that segment as a name that starts with
:, and pass the value in params:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
// GET /todos/42
export const = .("/todos/:id", { : { : 42 }, : });TypeScript reads the parameter names from the path, so it asks for exactly
params: { id }, with no names missing and none extra. A path without
parameters doesn't take params at all.
A value can be a string or a number. Effetch converts it with String() and
encodes it with encodeURIComponent, so "a/b" stays one segment (a%2Fb)
and can't add a level to the path.
A parameter is always a whole segment, from the : to the next /. In
/files/:name.json, the parameter is called name.json, not name. To send
a file name with its extension, make the whole segment the parameter, as in
/files/:file, and pass "report.json".
Build a query string
Pass the query as a plain object, and Effetch builds the string for you:
import * as from "effect/Schema";
import type { } from "@monarcode/effetch";
declare const : .;
const = .({ : ., : . });
// GET /todos?page=1&tags=work&tags=home
export const = .("/todos", {
: { : 1, : ["work", "home"], : },
: .(),
});The rules are made so that you can pass optional filters as they are:
- An array repeats the key, as in
tags=work&tags=home. nullandundefinedvalues are left out, sosearchsends nothing.- Numbers, booleans and bigints are converted with
String().
With Effect 4.1 or later, a value can also be Redacted, such as
Redacted.make(apiKey). It's sent as its value, and Effect keeps it out of
its own logs and traces.
The query object isn't checked against anything, so a typo in a key
compiles. To type a query, or to send values that need encoding, such as
dates, give an endpoint a query schema. See
Typed query parameters.
What's next
- Send a body: send JSON, a form, text or bytes.
- Upload files: send files in a typed multipart form.
- Read the response: decode what comes back with a schema.
- Call options: look up every option a call takes.
- When the methods aren't enough: pick the method
at runtime, or use the client's
HttpClientdirectly.