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
4 changes: 4 additions & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ to static files. It's a package in the pnpm workspace and isn't published.
skips it. It compiles `ts` only, so a JSX example needs the check
extended first.
- Pages are `.mdx` only: the site and the docs check read no other files.
- The guides live on the site, once. The README is an overview that links
to them, so when a feature changes, update its page, and the README only
if its quick start or links change. Link between pages with root-relative
paths (`/docs/errors/status-errors`).
- `pnpm run check` doesn't build the site. After changing `site/`, run
`pnpm run build:site`, which prerenders the site into `site/dist/client`.
`pnpm --dir site run dev` serves it while you write.
Expand Down
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@

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

## Unreleased

### Added

- Documentation site at [effetch.pages.dev/docs](https://effetch.pages.dev/docs),
with a page for each guide that was in the README, and search. The
package's `homepage` points to it.

### Changed

- The README is now an overview: what Effetch is, how to install it, a
quick start and links to the site's guides. The sections that earlier
entries name, such as "Upgrading from 0.1.0-rc.3" and "Uploading files",
are now [Upgrade from 0.1.0-rc.3](https://effetch.pages.dev/docs/upgrading)
and [Upload files](https://effetch.pages.dev/docs/calls/uploads).

## 0.1.0-rc.4

### Changed
Expand Down
3,206 changes: 43 additions & 3,163 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"react-native",
"schema"
],
"homepage": "https://github.com/monarcode/effetch#readme",
"homepage": "https://effetch.pages.dev/docs",
"bugs": {
"url": "https://github.com/monarcode/effetch/issues"
},
Expand Down
3 changes: 2 additions & 1 deletion scripts/smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,8 @@ export const isHttp = (u: unknown): u is HttpError => u instanceof HttpError;
export type Api = Http.Client;
export type Remote = import("@monarcode/effetch").Client;
TS
# The command the README documents, without reaching the registry.
# The command the upgrade guide (/docs/upgrading) documents, without reaching
# the registry.
npx --offline -p @monarcode/effetch effetch-migrate legacy >/dev/null
if grep -q '\* as Http from\|{ HttpError, make' legacy/api.ts; then
echo "✗ effetch-migrate left rc.3-style imports in legacy/api.ts:" >&2
Expand Down
67 changes: 67 additions & 0 deletions site/content/docs/(latest)/calls/body.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,70 @@
title: Send a body
description: Send JSON, form data, text or bytes, encoded with a schema or as is.
---

Send a request body with `post`, `put`, `patch` or `del`. Effetch sends the
`body` according to its type, and an `input` schema types and encodes it.

## Send JSON or a form

The following calls send a body encoded with an `input` schema, a plain JSON
body, and a `FormData` you build yourself:

```ts
import * as Schema from "effect/Schema";

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

declare const api: Fetch.Client;

const Todo = Schema.Struct({
id: Schema.Finite,
title: Schema.String,
done: Schema.Boolean,
due: Schema.Date,
});
const NewTodo = Schema.Struct({ title: Schema.String, due: Schema.Date });

export const create = api.post("/todos", {
input: NewTodo,
body: { title: "Write docs", due: new Date() },
output: Todo,
});

export const complete = api.patch("/todos/:id", {
params: { id: 1 },
body: { done: true },
output: Todo,
});

export const upload = (file: Blob) => {
const form = new FormData();
form.append("file", file);
return api.post("/uploads", { body: form });
};
```

## How a body is sent

A `body` is sent according to its type:

- an `HttpBody`, a string, an `ArrayBuffer` or typed array, a `Blob` or
`File`, `FormData` and `URLSearchParams` are sent as they are;
- anything else is sent as JSON.

## Encode the body with a schema

Add an `input` schema, and `body` is typed by it and encoded with it before
it's sent as JSON. In `create`, `due` is a `Date` in your code and an ISO
string on the wire.

To send a typed body as a form instead, such as a file upload, see
[Upload files](/docs/calls/uploads).

## What's next

- [Upload files](/docs/calls/uploads): send a typed body as
`multipart/form-data`.
- [Read the response](/docs/calls/responses): decode what the server sends
back.
- [Call options](/docs/calls/options): look up every option a call takes.
42 changes: 40 additions & 2 deletions site/content/docs/(latest)/calls/http-client.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
---
title: Use HttpClient directly
description: Use Effect's HttpClient for what the methods don't cover.
title: When the methods aren't enough
description: Choose the method at runtime, read a stream, or use the HttpClient underneath a client.
---

When the methods aren't enough, pick a method at runtime with `api.request`,
read a stream with `api.sse` or `api.ndjson`, or use 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, before the path and the options:

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

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

declare const api: Fetch.Client;

export const send = (method: HttpMethod.HttpMethod, path: string) =>
api.request(method, path);
```

## Read a stream of events or lines

When the response is a stream of events or lines, use `api.sse` or
`api.ndjson`. See [Streaming responses](/docs/streaming).

## Use the underlying HttpClient

When you need something the methods don't do, use `api.httpClient`. It's the
configured `HttpClient` underneath, with the base URL, the default headers and
the plugins, but no status check and no decoding.

## What's next

- [Streaming responses](/docs/streaming): read Server-Sent Events and
newline-delimited JSON.
- [Call errors](/docs/errors): see what can go wrong in a call.
- [Call options](/docs/calls/options): look up every option the methods take.
19 changes: 19 additions & 0 deletions site/content/docs/(latest)/calls/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,22 @@
title: Methods
description: Call a route with get, head, options, post, put, patch or del.
---

With a client in hand, every call is one method: you pass the path and an
object of options, and get an Effect back.

## Pick a method

A client has a method for each HTTP method:

- `get`, `head` and `options` send no body.
- `post`, `put`, `patch` and `del` can send one. It's `del` because `delete`
is a reserved word.

## What's next

- [Path and query parameters](/docs/calls/parameters): fill in the path and
build a query.
- [Send a body](/docs/calls/body): send JSON, forms, text or bytes.
- [Read the response](/docs/calls/responses): decode what comes back.
- [Call options](/docs/calls/options): look up every option a call takes.
21 changes: 21 additions & 0 deletions site/content/docs/(latest)/calls/options.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,24 @@
title: Call options
description: Look up an option a call accepts.
---

Every method takes the path and an object of options. This page lists every
option a call accepts.

## Options

The following options control how a call builds its request and reads its
response:

| Option | What it does |
| -------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `params` | Values for the path's `:params`. Required, with exactly those names, when the path has any; not allowed otherwise. Values are URI-encoded. |
| `query` | An object of query parameters. Arrays repeat the key, and `null` and `undefined` are dropped. |
| `headers` | Headers for this call. They win over the client's, and over the body's `content-type`. |
| `body` | Sent by its type: `HttpBody`, string, `ArrayBuffer` and typed arrays, `Blob`/`File`, `FormData` and `URLSearchParams` as they are, anything else as JSON. |
| `input` | A schema `body` is encoded with. `body` is then typed by it. Sent as JSON, or as `multipart/form-data` when wrapped in `Fetch.multipart` (see [Upload files](/docs/calls/uploads)). |
| `output` | A schema the 2xx body is decoded with: read as JSON, or as text with `Fetch.text()` and bytes with `Fetch.bytes()`. The call then sends a matching `Accept` header (unless you set `accept`). Without it, the call succeeds with the raw `HttpClientResponse`. See [Read the response](/docs/calls/responses). |
| `responseMode` | `"decoded-and-response"` makes the call succeed with `[value, response]`, to read the status and headers too. Defaults to `"decoded"`, the value alone. |
| `error` | A schema for the body of error responses, replacing the client's `defaultError`. |
| `statusErrors` | Your own errors for error responses, by status. See [Errors by status](/docs/errors/status-errors). |
| `plugins` | Plugin overrides for this call (see [Change plugins for one call](/docs/plugins/per-call)). |
52 changes: 52 additions & 0 deletions site/content/docs/(latest)/calls/parameters.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,55 @@
title: Path and query parameters
description: Fill in a path's parameters and build a query.
---

Fill in a path's `:name` parameters from `params`, and build the query string
from a plain `query` object.

## Fill in path parameters and a query

The following calls fill in an `:id` parameter, and send a query with a
number, a repeated key and a dropped value:

```ts
import * as Schema from "effect/Schema";

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

declare const api: Fetch.Client;

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

// GET /todos/1
export const one = api.get("/todos/:id", { params: { id: 1 }, output: Todo });

// GET /todos?page=1&tags=work&tags=home
export const some = api.get("/todos", {
query: { page: 1, tags: ["work", "home"], search: undefined },
output: Schema.Array(Todo),
});
```

## Path parameters

Every `:name` in a path is a parameter, filled in from `params`. When a path
has parameters, `params` is required, with exactly those names, and when it
has none, `params` isn't allowed. Effetch URI-encodes the values for you.

## Query parameters

`query` is a plain object. Arrays repeat the key, and `null` and `undefined`
are dropped, so you can pass optional filters as they are.

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.

To type an endpoint's query and send values such as dates, give the endpoint
a schema for it. See [Typed query parameters](/docs/endpoints/typed-query).

## What's next

- [Send a body](/docs/calls/body): send data with `post`, `put` and `patch`.
- [Read the response](/docs/calls/responses): decode the body with a schema.
- [Typed query parameters](/docs/endpoints/typed-query): type a query with a
schema.
Loading
Loading