Skip to Content
Calls

When the methods aren't enough

Choose the method at runtime, read a stream, or use the HttpClient underneath a client.

The client's methods cover most calls, but a few needs fall outside them: an HTTP method you only know at runtime, a response that arrives a piece at a time, or a request that Effetch doesn't model at all. This page shows the way out for each one: api.request, api.sse and api.ndjson, and the HttpClient underneath the client.

Choose the method at runtime

When the method is only known at runtime, use api.request. It takes the method as its first argument, then the path and the same options as the other methods:

import type * as  from "effect/http/HttpMethod";

import type {  } from "@monarcode/effetch";

declare const : .;

export const  = (: ., : string) =>
  .(, );

api.request also sends the methods that have no shorthand, such as TRACE or QUERY. The other methods are shorthand for it.

api.request types body the same way for every method, a literal "GET" included, so TypeScript doesn't stop a body on a GET. When the call runs, a body on a GET or HEAD request makes it die, since those requests can't carry one.

Read a stream of events or lines

When the response is a stream of Server-Sent Events or newline-delimited JSON, use api.sse or api.ndjson. Each gives you an Effect Stream of decoded items. See Streaming responses.

Use the underlying HttpClient

When you need something the methods don't do, such as reading a response your own way, use api.httpClient. It's the configured Effect HttpClient underneath the client, and it takes Effect's own requests:

import * as  from "effect/Effect";
import * as  from "effect/http/HttpClientRequest";

import type {  } from "@monarcode/effetch";

declare const : .;

// Reads the export as text, whatever its status.
export const  = .(
  ..(.("/export.csv")),
  () => .,
);

api.httpClient adds the base URL and the client's default headers to each request, and runs every plugin with its default options. Transport failures fail with NetworkError, next to the plugins' own errors.

It leaves out the rest of what a call does:

  • No status check. A 404 succeeds with its response, so check response.status yourself.
  • No decoding. There's no output, no Accept header picked for you, and no DecodeError.
  • No error schemas. The client's defaultError and statusErrors don't apply.
  • No onError or onExit hooks. A plugin that works from those, such as consoleLogger, which logs from onExit, isn't run for these requests.
  • No per-call plugin options. You can't turn a plugin off, or change its options, for one request.

For a streamed body, use api.sse or api.ndjson rather than api.httpClient.

What's next

On this page