Skip to content

Commit 9547e50

Browse files
authored
Merge branch 'main' into main
2 parents c9984e6 + 75399f8 commit 9547e50

File tree

158 files changed

+50008
-16291
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+50008
-16291
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
- main
77
pull_request:
88

9+
env:
10+
GITHUB_TOKEN: ${{ secrets.OPENAPI_TS_BOT_GITHUB_TOKEN }}
11+
912
concurrency:
1013
group: ci-${{ github.ref }}
1114
cancel-in-progress: true
@@ -20,7 +23,7 @@ jobs:
2023
node-version: 22
2124
- uses: pnpm/action-setup@v4
2225
with:
23-
version: latest
26+
version: 9.14.4
2427
run_install: true
2528
- run: pnpm run lint
2629
test-node-versions:
@@ -35,9 +38,8 @@ jobs:
3538
node-version: ${{ matrix.node-version }}
3639
- uses: pnpm/action-setup@v4
3740
with:
38-
version: latest
41+
version: 9.14.4
3942
run_install: true
40-
- run: pnpm run build
4143
- run: pnpm test
4244
test-e2e:
4345
runs-on: ubuntu-latest
@@ -48,7 +50,7 @@ jobs:
4850
node-version: 22
4951
- uses: pnpm/action-setup@v4
5052
with:
51-
version: latest
53+
version: 9.14.4
5254
run_install: true
5355
- run: pnpm exec playwright install --with-deps
5456
- run: pnpm run test-e2e
@@ -61,9 +63,8 @@ jobs:
6163
node-version: 22
6264
- uses: pnpm/action-setup@v4
6365
with:
64-
version: latest
66+
version: 9.14.4
6567
run_install: true
66-
- run: pnpm run build
6768
- run: pnpm test
6869
test-windows:
6970
runs-on: windows-latest
@@ -74,7 +75,6 @@ jobs:
7475
node-version: 22
7576
- uses: pnpm/action-setup@v4
7677
with:
77-
version: latest
78+
version: 9.14.4
7879
run_install: true
79-
- run: pnpm run build
8080
- run: pnpm test

.github/workflows/release.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ on:
55
branches:
66
- main
77

8+
env:
9+
GITHUB_TOKEN: ${{ secrets.OPENAPI_TS_BOT_GITHUB_TOKEN }}
10+
811
jobs:
912
changelog:
1013
runs-on: ubuntu-latest
@@ -19,15 +22,13 @@ jobs:
1922
node-version: 22
2023
- uses: pnpm/action-setup@v4
2124
with:
22-
version: latest
25+
version: 9.14.4
2326
run_install: true
24-
- run: pnpm run build
2527
- uses: changesets/action@v1
2628
with:
2729
version: pnpm run version
2830
publish: pnpm exec changeset publish
2931
commit: "[ci] release"
3032
title: "[ci] release"
3133
env:
32-
GITHUB_TOKEN: ${{ secrets.OPENAPI_TS_BOT_GITHUB_TOKEN }}
3334
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/size-limit.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
name: "Size Limit"
2+
23
on:
34
pull_request:
45
branches: [main]
56
types: [opened, synchronize]
67

8+
env:
9+
GITHUB_TOKEN: ${{ secrets.OPENAPI_TS_BOT_GITHUB_TOKEN }}
10+
711
jobs:
812
size-limit:
913
permissions:
@@ -17,7 +21,7 @@ jobs:
1721
node-version: 22
1822
- uses: pnpm/action-setup@v4
1923
with:
20-
version: latest
24+
version: 9.14.4
2125
run_install: true
2226
- uses: andresz1/[email protected]
2327
with:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.DS_Store
2+
.turbo
23
dist
34
node_modules
45
coverage

CONTRIBUTING.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
# Contributing
22

3-
Contributions are welcome! T
3+
Contributions are welcome!
44

55
## Setup
66

7-
This monorepo uses [pnpm workspaces](https://pnpm.io/) that lets packages pull from local versions rather than remote. After installing pnpm, running this command from the root will set everything up:
7+
This monorepo uses [pnpm workspaces](https://pnpm.io/) that lets packages pull from local versions rather than remote. It also uses [Turborepo](https://turbo.build/repo/docs) to run commands more easily. After installing pnpm, running the following commands from the root will set everything up:
88

99
```sh
1010
pnpm i
11+
pnpm run build
1112
```
1213

14+
## Workspace vs local commands
15+
16+
The “workspace“ is the root `package.json`. Running commands here will run on all packages. For example, running `pnpm run build` in the workspace root will build all packages. But running `cd packages/openapi-typescript && pnpm run build` will only build that one package.
17+
18+
It’s worth noting that some packages depend on others in the monorepo. For example, `openapi-fetch` depends on `openapi-typescript` building built. `openapi-react-query` relies on `openapi-fetch` which relies on `openapi-typescript`. If running commands per-package, you’d have to manually build each package before the test suite could run. But running `pnpm run build` in the workspace root once will build all packages for you, so that you can run any test suite.
19+
20+
> ![NOTE]
21+
>
22+
> Note that if any package changes, be sure to rebuild before running tests! Of course, CI will always do this, but in case you see differences between local vs CI, this is usually the culprit—a stale build.
23+
24+
Again, Turborepo is there so that you don’t have to think about all the packages as much, if you’re just contributing to one. But these tips may help you troubleshoot if something unexpected happens.
25+
1326
## Structure
1427

1528
This monorepo has 2 main sections: the `docs/` that deploy to `openapi-ts.dev`, and `packages/` which are all the npm packages. Each npm package has its own `CONTRIBUTING.md` doc that describes setup needed for that package.
1629

1730
```
18-
┬── docs/ # openapi-ts.dev
19-
31+
┬── docs/
2032
└── packages/
21-
├── openapi-fetch/ # openapi-fetch package
33+
├── openapi-fetch/
34+
├── openapi-react-query/
35+
├── openapi-typescript/
2236
├── openapi-typescript-helpers/
23-
│ # openapi-typescript package
24-
└── openapi-typescript/ # openapi-typescript package
37+
└── swr-openapi/
2538
```

docs/cli.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ The following flags are supported in the CLI:
120120
| `--immutable` | | `false` | Generates immutable types (readonly properties and readonly array) |
121121
| `--path-params-as-types` | | `false` | Allow dynamic string lookups on the `paths` object |
122122
| `--root-types` | | `false` | Exports types from `components` as root level type aliases |
123+
| `--root-types-no-schema-prefix` | | `false` | Do not add "Schema" prefix to types at the root level (should only be used with --root-types) |
123124

124125
### pathParamsAsTypes
125126

docs/data/contributors.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/openapi-fetch/api.md

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ openapi-fetch supports path serialization as [outlined in the 3.1 spec](https://
207207

208208
## Middleware
209209

210-
Middleware is an object with `onRequest()` and `onResponse()` callbacks that can observe and modify requests and responses.
210+
Middleware is an object with `onRequest()`, `onResponse()` and `onError()` callbacks that can observe and modify requests, responses and errors.
211211

212212
```ts
213213
import createClient from "openapi-fetch";
@@ -224,6 +224,12 @@ const myMiddleware: Middleware = {
224224
// change status of response
225225
return new Response(body, { ...resOptions, status: 200 });
226226
},
227+
async onError({ error }) {
228+
// wrap errors thrown by fetch
229+
onError({ error }) {
230+
return new Error("Oops, fetch failed", { cause: error });
231+
},
232+
},
227233
};
228234

229235
const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
@@ -238,21 +244,33 @@ client.use(myMiddleware);
238244

239245
Each middleware callback receives the following `options` object with the following:
240246

241-
| Name | Type | Description |
242-
| :----------- | :-------------- | :------------------------------------------------------------------------------------------ |
243-
| `request` | `Request` | The current `Request` to be sent to the endpoint. |
244-
| `response` | `Response` | The `Response` returned from the endpoint (note: this will be `undefined` for `onRequest`). |
245-
| `schemaPath` | `string` | The original OpenAPI path called (e.g. `/users/{user_id}`) |
246-
| `params` | `Object` | The original `params` object passed to `GET()` / `POST()` / etc. |
247-
| `id` | `string` | A random, unique ID for this request. |
248-
| `options` | `ClientOptions` | The readonly options passed to `createClient()`. |
247+
| Name | Type | Description |
248+
| :----------- | :-------------- | :----------------------------------------------------------------|
249+
| `request` | `Request` | The current `Request` to be sent to the endpoint. |
250+
| `schemaPath` | `string` | The original OpenAPI path called (e.g. `/users/{user_id}`) |
251+
| `params` | `Object` | The original `params` object passed to `GET()` / `POST()` / etc. |
252+
| `id` | `string` | A random, unique ID for this request. |
253+
| `options` | `ClientOptions` | The readonly options passed to `createClient()`. |
254+
255+
In addition to these, the `onResponse` callback receives an additional `response` property:
256+
257+
| Name | Type | Description |
258+
| :----------- | :-------------- | :------------------------------------------|
259+
| `response` | `Response` | The `Response` returned from the endpoint. |
260+
261+
And the `onError` callback receives an additional `error` property:
262+
263+
| Name | Type | Description |
264+
| :----------- | :-------------- | :------------------------------------------------------------------------|
265+
| `error` | `unknown` | The error thrown by `fetch`, probably a `TypeError` or a `DOMException`. |
249266

250267
#### Response
251268

252269
Each middleware callback can return:
253270

254271
- **onRequest**: Either a `Request` to modify the request, or `undefined` to leave it untouched (skip)
255-
- **onResponse** Either a `Response` to modify the response, or `undefined` to leave it untouched (skip)
272+
- **onResponse**: Either a `Response` to modify the response, or `undefined` to leave it untouched (skip)
273+
- **onError**: Either an `Error` to modify the error that is thrown, a `Response` which means that the `fetch` call will proceed as successful, or `undefined` to leave the error untouched (skip)
256274

257275
### Ejecting middleware
258276

docs/openapi-fetch/middleware-auth.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ title: Middleware & Auth
44

55
# Middleware & Auth
66

7-
Middleware allows you to modify either the request, response, or both for all fetches. One of the most common usecases is authentication, but can also be used for logging/telemetry, throwing errors, or handling specific edge cases.
7+
Middleware allows you to modify either the request, response, or both for all fetches as well as handling errors thrown by `fetch`. One of the most common usecases is authentication, but can also be used for logging/telemetry, throwing errors, or handling specific edge cases.
88

99
## Middleware
1010

11-
Each middleware can provide `onRequest()` and `onResponse()` callbacks, which can observe and/or mutate requests and responses.
11+
Each middleware can provide `onRequest()`, `onResponse()` and `onError` callbacks, which can observe and/or mutate requests, responses and `fetch` errors.
1212

1313
::: code-group
1414

@@ -27,6 +27,12 @@ const myMiddleware: Middleware = {
2727
// change status of response
2828
return new Response(body, { ...resOptions, status: 200 });
2929
},
30+
async onError({ error }) {
31+
// wrap errors thrown by fetch
32+
onError({ error }) {
33+
return new Error("Oops, fetch failed", { cause: error });
34+
},
35+
},
3036
};
3137

3238
const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
@@ -71,6 +77,44 @@ onResponse({ response }) {
7177
}
7278
```
7379

80+
### Error Handling
81+
82+
The `onError` callback allows you to handle errors thrown by `fetch`. Common errors are `TypeError`s which can occur when there is a network or CORS error or `DOMException`s when the request is aborted using an `AbortController`.
83+
84+
Depending on the return value, `onError` can handle errors in three different ways:
85+
86+
**Return nothing** which means that the error will still be thrown. This is useful for logging.
87+
88+
```ts
89+
onError({ error }) {
90+
console.error(error);
91+
return;
92+
},
93+
```
94+
95+
**Return another instance of `Error`** which is thrown instead of the original error.
96+
97+
```ts
98+
onError({ error }) {
99+
return new Error("Oops", { cause: error });
100+
},
101+
```
102+
103+
**Return a new instance of `Response`** which means that the `fetch` call will proceed as successful.
104+
105+
```ts
106+
onError({ error }) {
107+
return Response.json({ message: 'nothing to see' });
108+
},
109+
```
110+
111+
::: tip
112+
113+
`onError` _does not_ handle error responses with `4xx` or `5xx` HTTP status codes, since these are considered "successful" responses but with a bad status code. In these cases you need to check the response's status property or `ok()` method via the `onResponse` callback.
114+
115+
:::
116+
117+
74118
### Ejecting middleware
75119

76120
To remove middleware, call `client.eject(middleware)`:

docs/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
"scripts": {
77
"build": "pnpm run update-contributors && vitepress build && cp _redirects .vitepress/dist",
88
"dev": "pnpm run update-contributors && vitepress dev",
9-
"tokens": "co build",
109
"update-contributors": "node scripts/update-contributors.js"
1110
},
1211
"devDependencies": {
13-
"vitepress": "1.3.2"
12+
"vitepress": "1.5.0"
1413
}
1514
}

package.json

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,30 @@
77
"name": "Drew Powers",
88
"email": "[email protected]"
99
},
10+
"packageManager": "[email protected]",
1011
"scripts": {
11-
"build": "pnpm run -r --filter \"!*docs\" --aggregate-output build",
12-
"lint": "pnpm run -r --parallel --aggregate-output lint",
12+
"build": "turbo run build",
13+
"lint": "turbo run lint",
1314
"dev": "pnpm run -r --parallel --filter \"{packages/*}\" --aggregate-output dev",
14-
"format": "pnpm run -r --parallel --aggregate-output format",
15-
"test": "pnpm run -r --parallel --aggregate-output test",
16-
"test-e2e": "pnpm run -r --parallel --aggregate-output test-e2e",
17-
"size": "pnpm run build && size-limit",
15+
"format": "turbo run format",
16+
"test": "turbo run test",
17+
"test-e2e": "turbo run test-e2e",
18+
"size": "turbo run size-limit",
1819
"version": "pnpm run build && changeset version && pnpm i"
1920
},
2021
"devDependencies": {
2122
"@biomejs/biome": "^1.9.4",
2223
"@changesets/changelog-github": "^0.5.0",
23-
"@changesets/cli": "^2.27.9",
24-
"@playwright/test": "^1.48.1",
24+
"@changesets/cli": "^2.27.10",
25+
"@playwright/test": "^1.49.0",
2526
"@size-limit/preset-small-lib": "^11.1.6",
26-
"@types/node": "^22.8.0",
27+
"@types/node": "^22.10.1",
2728
"del-cli": "^5.1.0",
28-
"prettier": "^3.3.3",
29+
"prettier": "^3.4.1",
2930
"size-limit": "^11.1.6",
30-
"typescript": "^5.6.3",
31-
"vitest": "^2.1.3"
31+
"turbo": "^2.3.3",
32+
"typescript": "^5.7.2",
33+
"vitest": "^2.1.6"
3234
},
3335
"size-limit": [
3436
{

packages/openapi-fetch/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# openapi-fetch
22

3+
## 0.13.1
4+
5+
### Patch Changes
6+
7+
- [#1974](https://github.com/openapi-ts/openapi-typescript/pull/1974) [`35c576c`](https://github.com/openapi-ts/openapi-typescript/commit/35c576c8b2852f66e641014d13ffcfdeb21e98a1) Thanks [@luchsamapparat](https://github.com/luchsamapparat)! - add onError handler to middleware
8+
39
## 0.13.0
410

511
### Minor Changes

packages/openapi-fetch/examples/nextjs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"dev": "next dev"
77
},
88
"dependencies": {
9-
"next": "15.0.1",
9+
"next": "15.0.3",
1010
"openapi-fetch": "workspace:^",
1111
"react": "^18.3.1",
1212
"react-dom": "^18.3.1"
@@ -15,6 +15,6 @@
1515
"@types/react": "^18.3.12",
1616
"@types/react-dom": "^18.3.1",
1717
"openapi-typescript": "workspace:^",
18-
"typescript": "5.4.5"
18+
"typescript": "^5.7.2"
1919
}
2020
}

0 commit comments

Comments
 (0)