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

Lines changed: 0 additions & 1 deletion
This file was deleted.

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
22

biome.json

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 7 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 19 additions & 52 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 21 additions & 23 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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 推荐

0 commit comments

Comments
 (0)