Skip to content

Commit d7494ba

Browse files
authored
openapi-typescript@7, [email protected] 🎉 (#1706)
* Breaking 0.10.0 changes * Update docs * Update lockfile
1 parent 41ea873 commit d7494ba

File tree

80 files changed

+12726
-8614
lines changed

Some content is hidden

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

80 files changed

+12726
-8614
lines changed

.nvm

-1
This file was deleted.

.nvmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22

biome.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/1.7.0/schema.json",
2+
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
33
"organizeImports": {
44
"enabled": false
55
},

docs/introduction.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ description: Quickstart
55

66
<img src="/assets/openapi-ts.svg" alt="openapi-typescript" width="200" height="40" />
77

8-
::: warning
9-
10-
The 7.x docs are for a beta release that’s not production-ready yet. See the [6.x docs](/6.x/introduction) for the stable version.
11-
12-
:::
13-
148
openapi-typescript turns [OpenAPI 3.0 & 3.1](https://spec.openapis.org/oas/latest.html) schemas into TypeScript quickly using Node.js. No Java/node-gyp/running OpenAPI servers necessary.
159

1610
The code is [MIT-licensed](https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/LICENSE") and free for use.
@@ -39,7 +33,7 @@ _Note: OpenAPI 2.x is supported with versions `5.x` and previous_
3933
This library requires the latest version of [Node.js](https://nodejs.org) installed (20.x or higher recommended). With that present, run the following in your project:
4034

4135
```bash
42-
npm i -D openapi-typescript@next typescript
36+
npm i -D openapi-typescript typescript
4337
```
4438

4539
And in your `tsconfig.json`, to load the types properly:

docs/node.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The Node API may be useful if dealing with dynamically-created schemas, or you
1010
## Setup
1111

1212
```bash
13-
npm i --save-dev openapi-typescript@next typescript
13+
npm i --save-dev openapi-typescript typescript
1414
```
1515

1616
::: tip Recommended

docs/openapi-fetch/api.md

+19-52
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,12 @@ import createClient from "openapi-fetch";
165165
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript
166166

167167
const myMiddleware: Middleware = {
168-
async onRequest(req, options) {
168+
async onRequest({ request, options }) {
169169
// set "foo" header
170-
req.headers.set("foo", "bar");
171-
return req;
170+
request.headers.set("foo", "bar");
171+
return request;
172172
},
173-
async onResponse(res, options) {
173+
async onResponse({ request, response, options }) {
174174
const { body, ...resOptions } = res;
175175
// change status of response
176176
return new Response(body, { ...resOptions, status: 200 });
@@ -183,60 +183,27 @@ const client = createClient<paths>({ baseUrl: "https://myapi.dev/v1/" });
183183
client.use(myMiddleware);
184184
```
185185

186-
### onRequest
186+
### API
187187

188-
```ts
189-
onRequest(req, options) {
190-
//
191-
}
192-
```
193-
194-
`onRequest()` takes 2 params:
195-
196-
| Name | Type | Description |
197-
| :-------- | :-----------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
198-
| `req` | `MiddlewareRequest` | A standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) with `schemaPath` (OpenAPI pathname) and `params` ([params](/openapi-fetch/api#fetch-options) object) |
199-
| `options` | `MergedOptions` | Combination of [createClient](/openapi-fetch/api#create-client) options + [fetch overrides](/openapi-fetch/api#fetch-options) |
188+
#### Parameters
200189

201-
And it expects either:
190+
Each middleware callback receives the following `options` object with the following:
202191

203-
- **If modifying the request:** A [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request)
204-
- **If not modifying:** `undefined` (void)
192+
| Name | Type | Description |
193+
| :----------- | :-------------- | :------------------------------------------------------------------------------------------ |
194+
| `request` | `Request` | The current `Request` to be sent to the endpoint. |
195+
| `response` | `Response` | The `Response` returned from the endpoint (note: this will be `undefined` for `onRequest`). |
196+
| `schemaPath` | `string` | The original OpenAPI path called (e.g. `/users/{user_id}`) |
197+
| `params` | `Object` | The original `params` object passed to `GET()` / `POST()` / etc. |
198+
| `id` | `string` | A random, unique ID for this request. |
199+
| `options` | `ClientOptions` | The readonly options passed to `createClient()`. |
205200

206-
### onResponse
207-
208-
```ts
209-
onResponse(res, options, req) {
210-
//
211-
}
212-
```
201+
#### Response
213202

214-
`onResponse()` also takes 3 params:
215-
| Name | Type | Description |
216-
| :-------- | :-----------------: | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
217-
| `req` | `Response` | A standard [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response). |
218-
| `options` | `MergedOptions` | Combination of [createClient](/openapi-fetch/api#create-client) options + [fetch overrides](/openapi-fetch/api#fetch-options) |
219-
| `req` | `MiddlewareRequest` | A standard [Request](https://developer.mozilla.org/en-US/docs/Web/API/Request) with `schemaPath` (OpenAPI pathname) and `params` ([params](/openapi-fetch/api#fetch-options) object) |
220-
221-
And it expects either:
222-
223-
- **If modifying the response:** A [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response)
224-
- **If not modifying:** `undefined` (void)
225-
226-
### Skipping
227-
228-
If you want to skip the middleware under certain conditions, just `return` as early as possible:
229-
230-
```ts
231-
onRequest(req) {
232-
if (req.schemaPath !== "/projects/{project_id}") {
233-
return undefined;
234-
}
235-
//
236-
}
237-
```
203+
Each middleware callback can return:
238204

239-
This will leave the request/response unmodified, and pass things off to the next middleware handler (if any). There’s no internal callback or observer library needed.
205+
- **onRequest**: Either a `Request` to modify the request, or `undefined` to leave it untouched (skip)
206+
- **onResponse** Either a `Response` to modify the response, or `undefined` to leave it untouched (skip)
240207

241208
### Ejecting middleware
242209

docs/openapi-fetch/index.md

-6
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,6 @@ Notice there are no generics, and no manual typing. Your endpoint’s request an
5959

6060
## Setup
6161

62-
::: warning
63-
64-
openapi-fetch currently requires [email protected] (latest). An upcoming breaking release will support [email protected].
65-
66-
:::
67-
6862
Install this library along with [openapi-typescript](/introduction):
6963

7064
```bash

docs/openapi-fetch/middleware-auth.md

+21-23
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ import createClient from "openapi-fetch";
1717
import type { paths } from "./my-openapi-3-schema"; // generated by openapi-typescript
1818

1919
const myMiddleware: Middleware = {
20-
async onRequest(req, options) {
20+
async onRequest({ request, options }) {
2121
// set "foo" header
22-
req.headers.set("foo", "bar");
23-
return req;
22+
request.headers.set("foo", "bar");
23+
return request;
2424
},
25-
async onResponse(res, options) {
26-
const { body, ...resOptions } = res;
25+
async onResponse({ request, response, options }) {
26+
const { body, ...resOptions } = response;
2727
// change status of response
2828
return new Response(body, { ...resOptions, status: 200 });
2929
},
@@ -48,8 +48,8 @@ The order in which middleware are registered matters. For requests, `onRequest()
4848
If you want to skip the middleware under certain conditions, just `return` as early as possible:
4949

5050
```ts
51-
onRequest(req) {
52-
if (req.schemaPath !== "/projects/{project_id}") {
51+
onRequest({ schemaPath }) {
52+
if (schemaPath !== "/projects/{project_id}") {
5353
return undefined;
5454
}
5555
//
@@ -63,9 +63,9 @@ This will leave the request/response unmodified, and pass things off to the next
6363
Middleware can also be used to throw an error that `fetch()` wouldn’t normally, useful in libraries like [TanStack Query](https://tanstack.com/query/latest):
6464

6565
```ts
66-
onResponse(res) {
67-
if (res.error) {
68-
throw new Error(res.error.message);
66+
onResponse({ response }) {
67+
if (response.error) {
68+
throw new Error(response.error.message);
6969
}
7070
}
7171
```
@@ -98,12 +98,10 @@ By default, `openapi-fetch` will **NOT** arbitrarily clone requests/responses fo
9898
<!-- prettier-ignore -->
9999
```ts
100100
const myMiddleware: Middleware = {
101-
onResponse(res) {
102-
if (res) {
103-
const data = await res.json(); // [!code --]
104-
const data = await res.clone().json(); // [!code ++]
105-
return undefined;
106-
}
101+
onResponse({ response }) {
102+
const data = await response.json(); // [!code --]
103+
const data = await response.clone().json(); // [!code ++]
104+
return undefined;
107105
},
108106
};
109107
```
@@ -125,7 +123,7 @@ import type { paths } from "./my-openapi-3-schema";
125123
let accessToken: string | undefined = undefined;
126124

127125
const authMiddleware: Middleware = {
128-
async onRequest(req) {
126+
async onRequest({ request }) {
129127
// fetch token, if it doesn’t exist
130128
if (!accessToken) {
131129
const authRes = await someAuthFunc();
@@ -139,8 +137,8 @@ const authMiddleware: Middleware = {
139137
// (optional) add logic here to refresh token when it expires
140138

141139
// add Authorization header to every request
142-
req.headers.set("Authorization", `Bearer ${accessToken}`);
143-
return req;
140+
request.headers.set("Authorization", `Bearer ${accessToken}`);
141+
return request;
144142
},
145143
};
146144

@@ -162,14 +160,14 @@ If authorization isn’t needed for certain routes, you could also handle that w
162160
const UNPROTECTED_ROUTES = ["/v1/login", "/v1/logout", "/v1/public/"];
163161

164162
const authMiddleware = {
165-
onRequest(req) {
166-
if (UNPROTECTED_ROUTES.some((pathname) => req.url.startsWith(pathname))) {
163+
onRequest({ url, request }) {
164+
if (UNPROTECTED_ROUTES.some((pathname) => url.startsWith(pathname))) {
167165
return undefined; // don’t modify request for certain paths
168166
}
169167

170168
// for all other paths, set Authorization header as expected
171-
req.headers.set("Authorization", `Bearer ${accessToken}`);
172-
return req;
169+
request.headers.set("Authorization", `Bearer ${accessToken}`);
170+
return request;
173171
},
174172
};
175173
```

docs/zh/introduction.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ _注意:OpenAPI 2.x 在版本 `5.x` 及更早版本中受支持_
3333
此库需要安装最新版本的 [Node.js](https://nodejs.org)(建议使用 20.x 或更高版本)。安装完成后,在项目中运行以下命令:
3434

3535
```bash
36-
npm i -D openapi-typescript@next typescript
36+
npm i -D openapi-typescript typescript
3737
```
3838

3939
::: tip 强烈推荐

docs/zh/node.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Node.js API 对于处理动态创建的模式或在较大应用程序上下文
1010
## 安装
1111

1212
```bash
13-
npm i --save-dev openapi-typescript@next typescript
13+
npm i --save-dev openapi-typescript typescript
1414
```
1515

1616
::: tip 推荐

packages/openapi-fetch/CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# openapi-fetch
22

3+
## 0.10.0
4+
5+
### Minor Changes
6+
7+
- ⚠️ **Breaking Change**: `openapi-typescript@7` is needed to work. You’ll get type errors with `openapi-typescript@6` and below.
8+
- ⚠️ **Breaking Change**: The Middleware API has changed to be an object rather than `(request, options)` or `(response, options)`. [See Middleware docs](https://openapi-ts.dev/openapi-fetch/middleware-auth) for updated API.
9+
- ⚠️ **Breaking Change**: The `Content-Type` header is no longer sent by default if a body payload is attached.
10+
- ⚠️ **Breaking Change**: The `customFetch` type now calls `fetch(input: string, init: RequestInit)` and the types have been updated, rather than `fetch(input: Request)` introduced in `0.9.0`.
11+
12+
- Added `id` to middleware handlers that create a unique ID per-fetch
13+
314
## 0.9.8
415

516
### Patch Changes

packages/openapi-fetch/biome.json

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
{
2-
"$schema": "https://biomejs.dev/schemas/1.7.0/schema.json",
2+
"$schema": "https://biomejs.dev/schemas/1.8.1/schema.json",
33
"extends": ["../../biome.json"],
44
"files": {
55
"include": ["./src/", "./test/"],
66
"ignore": ["**/fixtures/**/*"]
7+
},
8+
"linter": {
9+
"rules": {
10+
"complexity": {
11+
"noBannedTypes": "off"
12+
},
13+
"suspicious": {
14+
"noConfusingVoidType": "off"
15+
}
16+
}
717
}
818
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-dom": "18.3.1"
1313
},
1414
"devDependencies": {
15-
"@types/node": "20.12.12",
15+
"@types/node": "20.14.6",
1616
"@types/react": "18.3.1",
1717
"@types/react-dom": "18.3.0",
1818
"openapi-typescript": "workspace:^",

packages/openapi-fetch/examples/vue-3/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
},
1717
"devDependencies": {
1818
"@tsconfig/node20": "^20.1.4",
19-
"@types/node": "^20.12.12",
19+
"@types/node": "^20.14.6",
2020
"@vitejs/plugin-vue": "^5.0.4",
2121
"@vue/tsconfig": "^0.5.1",
2222
"openapi-typescript": "workspace:^",

packages/openapi-fetch/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,24 @@
5454
"build:cjs": "esbuild --bundle src/index.js --format=cjs --outfile=dist/cjs/index.cjs && cp dist/index.d.ts dist/cjs/index.d.cts",
5555
"format": "biome format . --write",
5656
"lint": "biome check .",
57-
"generate-types": "node ./scripts/generate-types.js",
57+
"generate-types": "openapi-typescript test/fixtures/api.yaml -o test/fixtures/api.d.ts",
5858
"pretest": "pnpm run generate-types",
5959
"test": "pnpm run \"/^test:/\"",
6060
"test:js": "vitest run",
6161
"test:ts": "tsc --noEmit",
6262
"version": "pnpm run prepare && pnpm run build"
6363
},
6464
"dependencies": {
65+
"nanoid": "^5.0.7",
6566
"openapi-typescript-helpers": "workspace:^"
6667
},
6768
"devDependencies": {
68-
"axios": "^1.6.8",
69+
"axios": "^1.7.2",
6970
"del-cli": "^5.1.0",
7071
"esbuild": "^0.20.2",
7172
"execa": "^8.0.1",
72-
"msw": "^2.3.0",
73-
"openapi-typescript": "^6.x",
73+
"msw": "^2.3.1",
74+
"openapi-typescript": "workspace:^",
7475
"openapi-typescript-codegen": "^0.25.0",
7576
"openapi-typescript-fetch": "^2.0.0",
7677
"superagent": "^9.0.2",

packages/openapi-fetch/scripts/generate-types.js

-21
This file was deleted.

0 commit comments

Comments
 (0)