Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ on:
push:
branches: [main]
pull_request:
# Weekly, so a new Effect 4.x or TypeScript 7.x that breaks Effetch shows
# up without waiting for a push: those jobs install the newest versions.
schedule:
- cron: "0 6 * * 1"
workflow_dispatch:

permissions:
contents: read
Expand Down Expand Up @@ -68,6 +73,130 @@ jobs:
- name: Build and test
run: pnpm run typecheck && pnpm test && pnpm run build

newest-effect:
name: check (newest effect)
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Check out repository
uses: actions/checkout@v7

- name: Set up pnpm
uses: pnpm/action-setup@v6
with:
version: 10.12.1

- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: 24
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

# The peer range is `^4.0.0`, so a new 4.x reaches users without a
# release of Effetch. The versions are looked up, because pnpm keeps
# the lockfile's version for a range it already satisfies.
# npm lists versions in publish order, so a backport can come last:
# sort them first.
- name: Install newest effect 4.x
shell: bash
run: |
set -euo pipefail
newest() {
npm view "$1@^4" version --json |
node -p '[].concat(JSON.parse(require("fs").readFileSync(0, "utf8"))).sort((a, b) => a.localeCompare(b, "en", { numeric: true })).pop()'
}
effect="$(newest effect)"
vitest="$(newest @effect/vitest)"
for version in "$effect" "$vitest"; do
if ! [[ "$version" =~ ^4\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::Couldn't look up the newest 4.x version (got '$version')"
exit 1
fi
done
echo "Testing effect@$effect with @effect/vitest@$vitest"
pnpm add --workspace-root --save-dev --save-exact \
"effect@$effect" "@effect/vitest@$vitest"

- name: Build and test
run: pnpm run typecheck && pnpm test && pnpm run build

# The installed package against the TypeScript versions and runtimes the
# docs name, beyond the `check` job's TypeScript 6 and Node.js 24. Each
# reuses the smoke test's consumer program (scripts/compat.sh).
compatibility:
name: ${{ matrix.name }}
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
include:
- name: typescript 5.7 (oldest supported)
mode: typescript
target: "5.7"
- name: typescript 7
mode: typescript
target: "7"
- name: node.js 20
mode: runtime
target: node
node: 20
- name: node.js 22
mode: runtime
target: node
node: 22
- name: bun
mode: runtime
target: bun
- name: deno
mode: runtime
target: deno
steps:
- name: Check out repository
uses: actions/checkout@v7

- name: Set up pnpm
uses: pnpm/action-setup@v6
with:
version: 10.12.1

- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: 24
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

# Builds, packs and writes the consumer program the check reuses.
- name: Smoke test
run: pnpm run smoke

- name: Set up Node.js ${{ matrix.node }}
if: matrix.node
uses: actions/setup-node@v7
with:
node-version: ${{ matrix.node }}

- name: Set up Bun
if: matrix.target == 'bun'
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.x

- name: Set up Deno
if: matrix.target == 'deno'
uses: denoland/setup-deno@v2
with:
deno-version: 2.x

- name: Check ${{ matrix.mode }} ${{ matrix.target }}
run: bash scripts/compat.sh ${{ matrix.mode }} ${{ matrix.target }}

site:
name: build docs site
runs-on: ubuntu-latest
Expand Down
16 changes: 16 additions & 0 deletions .oxlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@
]
}
},
{
"files": ["examples/store-react/**"],
"plugins": [
"import",
"promise",
"typescript",
"unicorn",
"react",
"jsx-a11y"
],
"env": { "browser": true, "node": true },
"rules": {
"react/exhaustive-deps": "warn",
"jsx-a11y/alt-text": "error"
}
},
{
"files": ["site/**"],
"plugins": [
Expand Down
3 changes: 2 additions & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
inline conditional types. One exception: `CallEffect` and the call
signatures (`NoBodyMethod`, `BodyMethod`, `RequestMethod`, `Endpoint`)
write out the members of `CallError` and `CallServices`, so hovers show
the errors rather than the alias. Keep them in sync; the type tests check
the errors rather than the alias, and `Fetch.errorCodec` writes out
those of `ErrorCodecErrors`. Keep them in sync; the type tests check
each one against the aliases.
4. **Runtime targets: browsers, React Native (Hermes/Metro), Node ≥ 20, Bun,
Deno.** No `node:` imports, no Node globals, **no `new URL()`** (React
Expand Down
88 changes: 87 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,98 @@

All notable changes to `@monarcode/effetch`, newest first.

## Unreleased
## 0.1.0

The first stable release. From here, an update within 0.1.x doesn't break
code written for 0.1.0, apart from the few exceptions that
[Versioning](https://effetch.monarcode.dev/docs/reference/compatibility#versioning)
lists. The changes since 0.1.0-rc.5:

### Added

- `Fetch.errorCodec` builds a codec (a schema with a JSON encoding) for
every error a call can fail with, to send its failures where only data
goes: an Effect RPC's `error`, a server-rendered page, a worker. The
other side gets the same errors back, typed as on the server: an
`HttpError`'s body is decoded again from the response text with the
call's error schema, and the `statusErrors` errors come back as their own
classes.
- Give it a bound endpoint (`Fetch.errorCodec(todos.get)`), a definition
and its client (`Fetch.errorCodec(getTodo, { client: Api })`), or the
schemas of a plain method call
(`Fetch.errorCodec({ error: ApiError, statusErrors: { 404: NotFound } })`).
- Add plugins' errors, such as `TimeoutError`, as `errors`.
- Headers, a `NetworkError`'s `cause` and a `DecodeError`'s `issue` stay
behind.
- An error whose schema sends no `_tag` goes with its schema's name,
the `_tag` it leaves out or its identifier, such as a class's name, so
it decodes with its own schema, even when the other side lists its
errors in another order. A schema without a name of its own goes by
its place among those, so both sides must list them in the same order.

It comes with the types `ErrorCodec`, `ErrorCodecErrors`,
`ErrorCodecSource`, `ErrorCodecClient` and `ErrorCodecOptions`. A new
page,
[Send errors across the wire](https://effetch.monarcode.dev/docs/errors/across-the-wire),
shows it with an RPC.

### Fixed

- `HttpError`, `NetworkError` and `DecodeError` now encode to JSON and
decode back into the same classes, so they can be an Effect RPC's `error`
or cross from the server to the browser. Before,
`Schema.toCodecJson(Fetch.HttpError)` couldn't decode any `HttpError`, and
the other two couldn't be encoded. What's sent:
- an `HttpError` goes without its headers, which decode as empty, so
`set-cookie` and the like stay where they were. Its `body` goes when
it's JSON. A body decoded into something else, such as a class,
doesn't, and decodes as `undefined`.
- a `NetworkError` goes without its `cause`, and a `DecodeError` without
its `issue`. Both decode as `undefined`.

Constructing the errors and reading their fields are unchanged. Their
schemas' `Encoded` types now match what's sent.

### Changed

- The documentation site moved to https://effetch.monarcode.dev. The old
address, https://effetch.pages.dev, still serves it.
- CI now runs the installed package on Node.js 20 and 22, Bun 1 and Deno 2,
compiles it with TypeScript 5.7 (the oldest supported) and 7, and runs the
tests against the newest Effect 4.x. The
[Compatibility](https://effetch.monarcode.dev/docs/reference/compatibility)
page lists what each job covers.

### Documentation

- Effetch has a versioning policy, in the README and on the
[Compatibility](https://effetch.monarcode.dev/docs/reference/compatibility#versioning)
page. An update within 0.1.x doesn't break code written for 0.1.0: the
API, the fields errors encode to and the supported TypeScript versions
and runtimes stay as they are, and breaking changes wait for 0.2.0. The
exceptions follow Effect, which marks its HTTP modules and `encoding/Sse`
unstable: when they change, a 0.1.x update can change the types that come
from them, how Server-Sent Events behave, or the oldest `effect` it
supports. In 0.2.0, `retry` waits as a `Retry-After` header asks by
default. The "release candidate" notes are gone.
- The examples are new. Each one does a real task against a free public
API that needs no key, and runs with `pnpm example <name>`:
- `github/` shows a repository and its open issues;
- `sign-in/` signs in and renews the access token;
- `live/` reads Wikipedia's edits and Lichess TV's moves as they stream;
- `upload/` sends a multipart body;
- `bff/` sends a server's call errors to the browser with
`Fetch.errorCodec`;
- `store/` is an app in feature modules. It replaces `app/`, with the
same layout;
- `store-react/` puts `store/`'s modules behind a React app routed with
TanStack Router. Its loaders cancel their requests when you leave the
page, and `pnpm example store-react` starts its dev server.

Their tests run offline against `FetchTest`, as part of the test suite.
The old examples, which called an API that doesn't exist and were only
typechecked, are gone. See
[Examples](https://effetch.monarcode.dev/docs/examples).

## 0.1.0-rc.5

Expand Down
59 changes: 30 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,17 @@ export const getTodo = (id: number) =>
// NetworkError | DecodeError | HttpError, Api>
```

> **Release candidate.** Effetch is in release candidates (`0.1.0-rc.N`)
> on the way to `0.1.0`. A release candidate can still change the API, and
> when one does, the [changelog](./CHANGELOG.md) says what to change in your
> code.
> **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](./CHANGELOG.md). The exceptions follow Effect, which marks its
> HTTP modules and Server-Sent Events parser unstable.
> [Versioning](https://effetch.monarcode.dev/docs/reference/compatibility#versioning)
> has the details.

## Documentation

The guides and the API reference are at
**[effetch.monarcode.dev/docs](https://effetch.monarcode.dev/docs)**.
The guides and the API reference are in the
**[Effetch website](https://effetch.monarcode.dev/)**.

## Install

Expand Down Expand Up @@ -153,29 +155,27 @@ Calling an API from a web page on another origin? Read

## Examples

[`examples/`](examples) has complete programs, typechecked with the package so
they always match the current API:

- [`basic.ts`](examples/basic.ts): a client in a service, the methods, and
handling each error.
- [`builtins.ts`](examples/builtins.ts): the built-in plugins, and changing
them for one call.
- [`plugins.ts`](examples/plugins.ts): writing plugins, from auth with a
service to a `wrap` with its own error.
- [`endpoints.ts`](examples/endpoints.ts): the same app as `builtins.ts`, with
endpoints.
- [`backends.ts`](examples/backends.ts): two backends in one file, with shared
plugins.
- [`status-errors.ts`](examples/status-errors.ts): your own errors by status,
for every call, per endpoint and for one call.
- [`streams.ts`](examples/streams.ts): Server-Sent Events, newline-delimited
JSON, and a list paged through by its `Link` header.
- [`testing.ts`](examples/testing.ts): tests for the `app/` todos module,
against a fake backend made from its endpoints.
- [`app/`](examples/app): a feature-sliced app, with clients as shared
infrastructure and each module defining its own endpoints.

Its [README](examples/README.md) walks through each one.
[`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/`](examples/github): a repository and its open issues, with
endpoints, errors of your own, paging and a plugin.
- [`sign-in/`](examples/sign-in): signing in, and renewing the access token
when it's refused.
- [`live/`](examples/live): Wikipedia's edits and Lichess TV's moves, read
as they stream in.
- [`upload/`](examples/upload): a file and form fields, as a multipart body.
- [`bff/`](examples/bff): a server's call errors sent to the browser over
Effect RPC, with `Fetch.errorCodec`.
- [`store/`](examples/store): an app in feature modules, with a workflow and
its tests.
- [`store-react/`](examples/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](examples/README.md) says how to run them, and what each shows.

## Development

Expand All @@ -197,6 +197,7 @@ pnpm run check
| `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. |
Expand Down
Loading