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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
@tanstack/react-router@1.132.0 @tanstack/react-start@1.169.0 \
react-router@7.0.0 next@16.0.0 expo-router@5.0.2 \
expo-secure-store@14.2.0 react-native@0.79.0 effect@4.1.0 \
@monarcode/effetch@0.1.0-rc.2
@monarcode/effetch@0.1.0-rc.3

- name: Build and test
run: pnpm run build && pnpm test && pnpm run test:types
Expand Down
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
# Changelog

## 0.1.0-rc.1

The Effect guide and example move to effetch and effetch-query `0.1.0-rc.3`.
The public API is unchanged.

### Changed

- The `@monarcode/effetch` peer range is `^0.1.0-rc.3`, up from `rc.2`.
effetch-query 0.1.0-rc.3, which the Effect guide uses, needs it, and the
guide, the example and the tests use effetch's rc.3 APIs.

### Docs

- The Effect guide and the TanStack Start example with effetch use
effetch's and effetch-query's rc.3 APIs: the API client is a
`Fetch.Service`, effetch-query's `fromClient` reads its `defaultError`,
and the refresh turns a 401 or 403 into `null` with `Fetch.catchStatus`.
- The Effect guide says what `sessionBearer` does that effetch's own
`bearer` doesn't, and that after a refresh it sends only a `GET` or `HEAD`
again, where `bearer` with a `refresh` option sends any method again.
- `sessionBearer` says that streams work through it up to the response's
headers, and that a stream open when the user signs out keeps reading.

## 0.1.0-rc.0

The first release candidate for `0.1.0`. The public API is now frozen, and
Expand Down
4 changes: 2 additions & 2 deletions examples/tanstack-start-spa-effetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
},
"dependencies": {
"@effect/atom-react": "4.1.3",
"@monarcode/effetch": "0.1.0-rc.2",
"@monarcode/effetch-query": "0.1.0-rc.2",
"@monarcode/effetch": "0.1.0-rc.3",
"@monarcode/effetch-query": "0.1.0-rc.3",
"@monarcode/session-kit": "workspace:*",
"@tanstack/react-router": "1.170.46",
"@tanstack/react-start": "1.169.5",
Expand Down
21 changes: 7 additions & 14 deletions examples/tanstack-start-spa-effetch/src/api.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as Fetch from "@monarcode/effetch";
import { retry } from "@monarcode/effetch/plugins";
import { sessionBearer } from "@monarcode/session-kit/effetch";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import * as Layer from "effect/Layer";

import { API_BASE_URL, ApiError } from "./backend";

const make = Fetch.make({
/** The app's API client: effetch's `get`, `post` and the rest, for DummyJSON. */
export class ApiClient extends Fetch.Service<ApiClient>()("ApiClient", {
baseUrl: API_BASE_URL,
defaultError: ApiError,
// `retry` resends a GET after a network error or a status such as 503.
Expand All @@ -17,17 +16,11 @@ const make = Fetch.make({
// sends a GET or HEAD again once. `optional`: a call made while signed out
// goes without a token.
plugins: [retry(), sessionBearer({ baseUrl: API_BASE_URL, optional: true })],
}).pipe(
}) {}

/** The API client, over whichever `Auth` is provided. */
export const ApiClientLive = ApiClient.layer.pipe(
// Effect adds tracing headers to every request, which fail the preflight
// of an API that doesn't allow them.
Effect.provideService(HttpClient.TracerPropagationEnabled, false),
Layer.provide(Layer.succeed(HttpClient.TracerPropagationEnabled)(false)),
);

/** The app's API client: effetch's `get`, `post` and the rest, for DummyJSON. */
export class ApiClient extends Context.Service<
ApiClient,
Effect.Success<typeof make>
>()("ApiClient") {}

/** The API client, over whichever `Auth` is provided. */
export const ApiClientLive = Layer.effect(ApiClient, make);
5 changes: 2 additions & 3 deletions examples/tanstack-start-spa-effetch/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { QueryClient } from "@monarcode/effetch-query";
import { endpoints } from "@monarcode/effetch-query/effetch";
import { fromClient } from "@monarcode/effetch-query/effetch";
import type * as ManagedRuntime from "effect/ManagedRuntime";

import { ApiClient } from "./api";
import { ApiError } from "./backend";
import { appRuntime, type AppRuntime } from "./runtime";

declare module "@monarcode/effetch-query" {
Expand All @@ -13,7 +12,7 @@ declare module "@monarcode/effetch-query" {
}

/** Turns endpoints of the API client into queries and mutations. */
export const api = endpoints(ApiClient, { defaultError: ApiError });
export const api = fromClient(ApiClient);

/**
* Makes the query cache, which runs queries and mutations with `appRuntime`'s
Expand Down
6 changes: 1 addition & 5 deletions examples/tanstack-start-spa-effetch/src/refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@ export const refreshSession = (refreshToken: string, signal: AbortSignal) =>
output: SessionTokens,
});
}).pipe(
Effect.catchTag("HttpError", (error) =>
error.status === 401 || error.status === 403
? Effect.succeed(null)
: Effect.fail(error),
),
Fetch.catchStatus([401, 403], () => Effect.succeed(null)),
Effect.provide(FetchHttpClient.layer),
),
{ signal },
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@monarcode/session-kit",
"version": "0.1.0-rc.0",
"version": "0.1.0-rc.1",
"description": "Schema-driven, type-safe sessions for React, with TanStack Router, React Router, Expo Router, Next.js, TanStack Start, and Effect support",
"keywords": [
"auth",
Expand Down Expand Up @@ -123,8 +123,8 @@
"use-sync-external-store": "^1.6.0"
},
"devDependencies": {
"@monarcode/effetch": "0.1.0-rc.2",
"@monarcode/effetch-query": "0.1.0-rc.2",
"@monarcode/effetch": "0.1.0-rc.3",
"@monarcode/effetch-query": "0.1.0-rc.3",
"@tanstack/react-router": "1.170.46",
"@tanstack/react-start": "1.169.5",
"@types/jsdom": "21.1.7",
Expand All @@ -151,7 +151,7 @@
"zod": "4.6.7"
},
"peerDependencies": {
"@monarcode/effetch": "^0.1.0-rc.2",
"@monarcode/effetch": "^0.1.0-rc.3",
"@tanstack/react-router": "^1.132.0",
"@tanstack/react-start": "^1.169.0",
"effect": "^4.1.0",
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 26 additions & 28 deletions site/content/docs/(latest)/effect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ npm install @monarcode/session-kit effect @monarcode/effetch

`effect` and `@monarcode/effetch` are optional peer dependencies of Session
kit. `./effect` and `./effetch` need `effect` 4.1 or newer, and `./effetch`
also needs `@monarcode/effetch`.
also needs `@monarcode/effetch` 0.1.0-rc.3 or newer.

To [cache with effetch-query](#cache-with-effetch-query), also install
effetch-query 0.1.0-rc.2 or newer, and its `@effect/atom-react` peer for
effetch-query 0.1.0-rc.3 or newer, and its `@effect/atom-react` peer for
React:

```npm
Expand Down Expand Up @@ -126,11 +126,7 @@ export const refreshSession = (refreshToken: string, signal: AbortSignal) =>
output: SessionTokens,
});
}).pipe(
Effect.catchTag("HttpError", (error) =>
error.status === 401 || error.status === 403
? Effect.succeed(null)
: Effect.fail(error),
),
Fetch.catchStatus([401, 403], () => Effect.succeed(null)),
Effect.provide(FetchHttpClient.layer),
),
{ signal },
Expand Down Expand Up @@ -208,50 +204,54 @@ export const showGreeting = () =>

## 6. Call your API with effetch

Make the app's API client with effetch's `Fetch.make`: the backend's base
Make the app's API client as an effetch `Fetch.Service`: the backend's base
URL, its error body, and the plugins every call goes through. The
`sessionBearer` plugin does what [`createAuthFetch`](/docs/client/requests)
does: it sends the access token, and refreshes after a 401.

Put `sessionBearer` last, so that a retry reads the current token. Give it the
client's `baseUrl`, since the token goes only to that origin. `Fetch.make`
then needs the `Auth` service, once:
client's `baseUrl`, since the token goes only to that origin. The client's
layer then needs the `Auth` service, once:

```ts title="src/api.ts"
import * as Fetch from "@monarcode/effetch";
import { retry } from "@monarcode/effetch/plugins";
import { sessionBearer } from "@monarcode/session-kit/effetch";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as HttpClient from "effect/http/HttpClient";
import * as Layer from "effect/Layer";

import { API_BASE_URL, ApiError } from "./backend.js";

const make = Fetch.make({
export class ApiClient extends Fetch.Service<ApiClient>()("ApiClient", {
baseUrl: API_BASE_URL,
defaultError: ApiError,
plugins: [retry(), sessionBearer({ baseUrl: API_BASE_URL, optional: true })],
}).pipe(
}) {}

/** The API client, over whichever `Auth` is provided. */
export const ApiClientLive = ApiClient.layer.pipe(
// Effect adds tracing headers to every request, which fail the preflight
// of an API that doesn't allow them.
Effect.provideService(HttpClient.TracerPropagationEnabled, false),
Layer.provide(Layer.succeed(HttpClient.TracerPropagationEnabled)(false)),
);

export class ApiClient extends Context.Service<
ApiClient,
Effect.Success<typeof make>
>()("ApiClient") {}

/** The API client, over whichever `Auth` is provided. */
export const ApiClientLive = Layer.effect(ApiClient, make);
```

Calls can fail with `AuthFailure`. When one client serves public and private
endpoints, as this one does, give the plugin `optional: true`: a call made
while signed out is then sent without a token, and a private endpoint
answers with its own 401. A locked client still fails with `LOCKED`.

effetch's own `bearer` plugin can refresh after a 401 too, with its
`refresh` option, but it doesn't know the session. `sessionBearer` reads it
from Session kit. A call fails with `SESSION_CHANGED` if the account changes
while it runs, and a second 401 signs the user out. Its
[reference](/docs/reference/effetch#sessionbearer) lists the rest.

After a refresh, `bearer` sends a request of any method again, a `POST`
included. `sessionBearer` sends only a `GET` or `HEAD` again, as
`createAuthFetch` does, and returns the 401 of any other request for you to
retry.

Effect code yields the client and calls its methods, such as `get` and
`post`. The sign-in call skips the plugin with `sessionBearer: false`,
because it's how the user gets a token. To read the current user and to sign
Expand Down Expand Up @@ -376,16 +376,14 @@ export const logStatus = Auth.changes.pipe(
effetch-query caches endpoints: a route and its schemas, defined with
`Fetch.endpoint`, which it calls with the app's API client. Its queries and
mutations run with the app's runtime, so components never run an Effect by
hand. Give it the client's `defaultError`, which an endpoint can't read from
the client:
hand. `fromClient` reads the client's `defaultError` from its class:

```ts title="src/query.ts"
import { QueryClient } from "@monarcode/effetch-query";
import { endpoints } from "@monarcode/effetch-query/effetch";
import { fromClient } from "@monarcode/effetch-query/effetch";
import type * as ManagedRuntime from "effect/ManagedRuntime";

import { ApiClient } from "./api.js";
import { ApiError } from "./backend.js";
import { appRuntime, type AppRuntime } from "./runtime.js";

declare module "@monarcode/effetch-query" {
Expand All @@ -394,7 +392,7 @@ declare module "@monarcode/effetch-query" {
}
}

export const api = endpoints(ApiClient, { defaultError: ApiError });
export const api = fromClient(ApiClient);

export const makeQueryClient = () => QueryClient.make({ runtime: appRuntime });
```
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/(latest)/reference/compatibility.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import use:
| `react-router` | `^7.0.0 \|\| ^8.0.0` | `/react-router`, `/react-router/server` |
| `next` | `^16.0.0` | `/next` |
| `effect` | `^4.1.0` | `/effect`, `/effetch` |
| `@monarcode/effetch` | `^0.1.0-rc.2` | `/effetch` |
| `@monarcode/effetch` | `^0.1.0-rc.3` | `/effetch` |
| `expo-router` | `^5.0.2` through `^58` | `/expo` |
| `expo-secure-store` | `^14.2.0` through `^58` | `/expo` |
| `expo-file-system` | `^18.1.0` through `^58` | `/expo` |
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/(latest)/reference/effetch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ Options for [`sessionBearer`](#sessionbearer).

### SessionBearerPlugin

The plugin [`sessionBearer`](#sessionbearer) returns. Its calls can fail with `AuthFailure`, and `Fetch.make` needs the `Auth` service.
The plugin [`sessionBearer`](#sessionbearer) returns. Its calls can fail with `AuthFailure`, and a client made with it needs the `Auth` service.
Loading
Loading