Skip to content
monarcodePublic

About

Typed, schema-driven HTTP calls on Effect v4's HttpClient: path params, object queries, decoded responses and plugins

Topics

Resources

Stars

11 stars

Watchers

0 watching

Forks

Repository files navigation

Effetch: schema-driven HTTP calls on Effect v4's HttpClient, typed end to end

npm CI bundle size, without effect types provenance license

Effect Browsers React Native Expo Node.js Bun Deno

Typed HTTP calls for Effect, without the boilerplate.

You give Effetch a path and a schema. It fills in the path, builds the query, sends the request, checks the status and decodes the response. What you get back is an Effect that's typed end to end, errors included, so TypeScript tells you what a call returns and every way it can fail.

import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";

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

const Todo = Schema.Struct({ id: Schema.Finite, title: Schema.String });

class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
}) {}

export const getTodo = (id: number) =>
  Effect.gen(function* () {
    const api = yield* Api;
    return yield* api.get("/todos/:id", { params: { id }, output: Todo });
  });
// Effect<{ readonly id: number; readonly title: string },
//        NetworkError | DecodeError | HttpError, Api>

Versioning. An update within 0.1.x doesn't break code written for 0.1.0, and breaking changes wait for 0.2.0, with notes in the changelog. The exceptions follow Effect, which marks its HTTP modules and Server-Sent Events parser unstable. Versioning has the details.

Documentation

The guides and the API reference are in the Effetch website.

Install

pnpm add @monarcode/effetch effect

You'll need:

  • effect 4.x, which is a peer dependency;
  • an ESM project, since the package is ESM only;
  • TypeScript 5.7 or newer.

Compatibility lists the runtimes Effetch is built for, and which of them CI tests.

Quick start

This program fetches a todo from JSONPlaceholder, a free fake API, and decodes it with a schema:

import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as Schema from "effect/Schema";

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

// 1. What a todo looks like
const Todo = Schema.Struct({
  id: Schema.Finite,
  title: Schema.String,
  completed: Schema.Boolean,
});

// 2. The client, declared once for the whole app
class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://jsonplaceholder.typicode.com",
}) {}

// 3. A call
const program = Effect.gen(function* () {
  const api = yield* Api;
  const todo = yield* api.get("/todos/:id", {
    params: { id: 1 },
    output: Todo,
  });
  console.log(todo.title);
});

// 4. Run it, with the client and the HttpClient that sends the requests
const ApiLive = Api.layer.pipe(Layer.provide(FetchHttpClient.layer));

await Effect.runPromise(program.pipe(Effect.provide(ApiLive)));

Api is your client, declared once for the whole app. api.get fills in :id from params, sends the request and decodes the body with Todo. Api.layer builds the client, with the HttpClient that sends the requests. Getting started walks through each part.

Calling an API from a web page on another origin? Read Platform notes first.

Guides

Examples

examples/ has programs that use Effetch for a real task, against free public APIs, with no key needed. Run one with pnpm example <name>:

  • github/: a repository and its open issues, with endpoints, errors of your own, paging and a plugin.
  • sign-in/: signing in, and renewing the access token when it's refused.
  • live/: Wikipedia's edits and Lichess TV's moves, read as they stream in.
  • upload/: a file and form fields, as a multipart body.
  • bff/: a server's call errors sent to the browser over Effect RPC, with Fetch.errorCodec.
  • store/: an app in feature modules, with a workflow and its tests.
  • store-react/: the same modules behind a React app, whose TanStack Router loaders cancel their requests when you leave the page.

Each one has tests that run offline, against a fake backend. Its README says how to run them, and what each shows.

Development

You need Node 24 and pnpm 10.12.1 (corepack enable picks the pinned version).

pnpm install
pnpm run check
Script What it does
pnpm run build Builds dist/ (ESM and .d.ts) with tsdown.
pnpm test Runs the tests and type tests with Vitest.
pnpm run typecheck Typechecks src, test and examples.
pnpm run lint Lints with oxlint (lint:effect runs the Effect language service).
pnpm run format Formats with oxfmt (format:check only checks).
pnpm run check:docs Checks every public export has a description, @example and @since, compiles every example in the JSDoc, this README and the docs site's pages, and checks the site's API reference matches the JSDoc.
pnpm run smoke Packs the package, installs it in a fresh project, typechecks and runs a consumer, and checks go-to-definition, hovers and completions.
pnpm run check All of the above, plus publint and attw. CI runs it on every push to main and every pull request.
pnpm run compat After smoke, checks the installed package (not part of check; CI runs it) with another TypeScript version or runtime: pnpm run compat typescript 5.7, or runtime node, bun or deno.
pnpm run build:site Builds the package, then prerenders the docs site in site/ to site/dist/client, failing on a type error in an example. check doesn't build the site; CI does.
pnpm run docs:reference Writes the site's API reference pages from the JSDoc. Run it after changing a public export or its docs.
pnpm run docs:snapshot Freezes an older release line's docs into the site, such as pnpm run docs:snapshot 0.1 when 0.2 starts.

To work on the docs site, run pnpm run build, then pnpm --dir site run dev. The site's type hovers and tables read the built package, so build again after changing src/.

The pre-commit hook runs lint and format:check.

License

MIT

About

Typed, schema-driven HTTP calls on Effect v4's HttpClient: path params, object queries, decoded responses and plugins

Topics

Resources

Stars

11 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages