Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add OpenAPI 2.0.x experimental parser #1525

Merged
merged 2 commits into from
Jan 6, 2025
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
5 changes: 5 additions & 0 deletions .changeset/empty-numbers-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': minor
---

feat: add OpenAPI 2.0 support to experimental parser
21 changes: 21 additions & 0 deletions .changeset/flat-brooms-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@hey-api/client-fetch': minor
---

**BREAKING**: please update `@hey-api/openapi-ts` to the latest version

feat: replace accessToken and apiKey functions with auth

### Added `auth` option

Client package functions `accessToken` and `apiKey` were replaced with a single `auth` function for fetching auth tokens. If your API supports multiple auth mechanisms, you can use the `auth` argument to return the appropriate token.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
accessToken: () => '<my_token>', // [!code --]
apiKey: () => '<my_token>', // [!code --]
auth: (auth) => '<my_token>', // [!code ++]
});
```
6 changes: 6 additions & 0 deletions .changeset/mean-moles-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@hey-api/client-axios': minor
'@hey-api/client-fetch': minor
---

**BREAKING**: rename exported Security interface to Auth
5 changes: 5 additions & 0 deletions .changeset/nasty-comics-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/openapi-ts': patch
---

fix: preserve leading indicators in enum keys
8 changes: 6 additions & 2 deletions .changeset/old-gorillas-speak.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ export default {
client: '@hey-api/client-fetch',
input: 'path/to/openapi.json',
output: 'src/client',
watch: true, // [!code ++]
watch: true,
};
```

### CLI

```sh
npx @hey-api/openapi-ts -i path/to/openapi.json -o src/client -c @hey-api/client-fetch -w
npx @hey-api/openapi-ts \
-c @hey-api/client-fetch \
-i path/to/openapi.json \
-o src/client \
-w
```
7 changes: 7 additions & 0 deletions .changeset/tender-bananas-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@hey-api/openapi-ts': minor
---

**BREAKING**: please update `@hey-api/client-*` packages to the latest version

feat: add support for basic http auth
5 changes: 5 additions & 0 deletions .changeset/twenty-walls-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hey-api/client-axios': minor
---

**BREAKING**: remove support for passing auth to Axios instance
2 changes: 1 addition & 1 deletion .changeset/wise-poets-flow.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@hey-api/client-fetch': minor
---

[BREAKING] return raw response body (of type `ReadableStream`) when `Content-Type` response header is not provided and `parseAs` is set to `auto`
**BREAKING**: return raw response body (of type `ReadableStream`) when `Content-Type` response header is not provided and `parseAs` is set to `auto`
23 changes: 23 additions & 0 deletions .changeset/witty-wasps-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
'@hey-api/client-axios': minor
---

**BREAKING**: please update `@hey-api/openapi-ts` to the latest version

feat: replace accessToken and apiKey functions with auth

### Added `auth` option

Client package functions `accessToken` and `apiKey` were replaced with a single `auth` function for fetching auth tokens. If your API supports multiple auth mechanisms, you can use the `auth` argument to return the appropriate token.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
accessToken: () => '<my_token>', // [!code --]
apiKey: () => '<my_token>', // [!code --]
auth: (auth) => '<my_token>', // [!code ++]
});
```

Due to conflict with the Axios native `auth` option, we removed support for configuring Axios auth. Please let us know if you require this feature added back.
51 changes: 44 additions & 7 deletions docs/openapi-ts/clients/axios.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,26 @@ bun add @hey-api/client-axios

:::

Ensure you have already [configured](/openapi-ts/get-started) `@hey-api/openapi-ts`. Update your configuration to use the Axios client package.
In your [configuration](/openapi-ts/get-started), set `client` to `@hey-api/client-axios` and you'll be ready to use the Axios client. :tada:

```js
::: code-group

```js [config]
export default {
client: '@hey-api/client-axios', // [!code ++]
input: 'path/to/openapi.json',
output: 'src/client',
};
```

You can now run `openapi-ts` to use the new Axios client. 🎉
```sh [cli]
npx @hey-api/openapi-ts \
-c @hey-api/client-axios \ # [!code ++]
-i path/to/openapi.json \
-o src/client
```

:::

## Configuration

Expand All @@ -79,15 +88,15 @@ const client = createClient({

## Interceptors

Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to the rest of your application. Axios provides interceptors, please refer to their documentation on [interceptors](https://axios-http.com/docs/interceptors).
Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application. Axios provides interceptors, please refer to their documentation on [interceptors](https://axios-http.com/docs/interceptors).

We expose the Axios instance through the `instance` field.

```js
import { client } from 'client/sdk.gen';

client.instance.interceptors.request.use((config) => {
config.headers.set('Authorization', 'Bearer <my_token>');
// do something
return config;
});
```
Expand All @@ -98,7 +107,7 @@ The Axios client is built as a thin wrapper on top of Axios, extending its funct

### SDKs

This is the most common requirement. Our generated SDKs consume an internal Axios instance, so you will want to configure that.
This is the most common requirement. The generated SDKs consume an internal client instance, so you will want to configure that.

```js
import { client } from 'client/sdk.gen';
Expand All @@ -108,7 +117,7 @@ client.setConfig({
});
```

You can pass any Axios configuration option to `setConfig()`, and even your own Axios implementation.
You can pass any Axios configuration option to `setConfig()` (except for `auth`), and even your own Axios implementation.

### Client

Expand Down Expand Up @@ -140,6 +149,34 @@ const response = await getFoo({
});
```

## Auth

::: warning
To use this feature, you must opt in to the [experimental parser](/openapi-ts/configuration#parser).
:::

The SDKs include auth mechanisms for every endpoint. You will want to configure the `auth` field to pass the right token for each request. The `auth` field can be a string or a function returning a string representing the token. The returned value will be attached only to requests that require auth.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
auth: () => '<my_token>', // [!code ++]
baseURL: 'https://example.com',
});
```

If you're not using SDKs or generating auth, using interceptors is a common approach to configuring auth for each request.

```js
import { client } from 'client/sdk.gen';

client.instance.interceptors.request.use((config) => {
config.headers.set('Authorization', 'Bearer <my_token>'); // [!code ++]
return config;
});
```

## Build URL

::: warning
Expand Down
63 changes: 50 additions & 13 deletions docs/openapi-ts/clients/fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,26 @@ bun add @hey-api/client-fetch

:::

Ensure you have already [configured](/openapi-ts/get-started) `@hey-api/openapi-ts`. Update your configuration to use the Fetch API client package.
In your [configuration](/openapi-ts/get-started), set `client` to `@hey-api/client-fetch` and you'll be ready to use the Fetch API client. :tada:

```js
::: code-group

```js [config]
export default {
client: '@hey-api/client-fetch', // [!code ++]
input: 'path/to/openapi.json',
output: 'src/client',
};
```

You can now run `openapi-ts` to use the new Fetch API client. 🎉
```sh [cli]
npx @hey-api/openapi-ts \
-c @hey-api/client-fetch \ # [!code ++]
-i path/to/openapi.json \
-o src/client
```

:::

## Configuration

Expand All @@ -79,24 +88,24 @@ const client = createClient({

## Interceptors

Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to the rest of your application. They can be added with `use` or removed with `eject`. Fetch API does not have the interceptor functionality, so we implement our own. Below is an example request interceptor
Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application. They can be added with `use` and removed with `eject`. Fetch API does not have the interceptor functionality, so we implement our own. Below is an example request interceptor

::: code-group

```js [use]
import { client } from 'client/sdk.gen';

client.interceptors.request.use((request, options) => {
request.headers.set('Authorization', 'Bearer <my_token>');
client.interceptors.request.use((request) => {
// do something
return request;
});
```

```js [eject]
import { client } from 'client/sdk.gen';

client.interceptors.request.eject((request, options) => {
request.headers.set('Authorization', 'Bearer <my_token>');
client.interceptors.request.eject((request) => {
// do something
return request;
});
```
Expand All @@ -110,17 +119,17 @@ and an example response interceptor
```js [use]
import { client } from 'client/sdk.gen';

client.interceptors.response.use((response, request, options) => {
trackAnalytics(response);
client.interceptors.response.use((response) => {
// do something
return response;
});
```

```js [eject]
import { client } from 'client/sdk.gen';

client.interceptors.response.eject((response, request, options) => {
trackAnalytics(response);
client.interceptors.response.eject((response) => {
// do something
return response;
});
```
Expand All @@ -137,7 +146,7 @@ The Fetch client is built as a thin wrapper on top of Fetch API, extending its f

### SDKs

This is the most common requirement. The generated SDKs consume an internal Fetch instance, so you will want to configure that.
This is the most common requirement. The generated SDKs consume an internal client instance, so you will want to configure that.

```js
import { client } from 'client/sdk.gen';
Expand Down Expand Up @@ -179,6 +188,34 @@ const response = await getFoo({
});
```

## Auth

::: warning
To use this feature, you must opt in to the [experimental parser](/openapi-ts/configuration#parser).
:::

The SDKs include auth mechanisms for every endpoint. You will want to configure the `auth` field to pass the right token for each request. The `auth` field can be a string or a function returning a string representing the token. The returned value will be attached only to requests that require auth.

```js
import { client } from 'client/sdk.gen';

client.setConfig({
auth: () => '<my_token>', // [!code ++]
baseUrl: 'https://example.com',
});
```

If you're not using SDKs or generating auth, using interceptors is a common approach to configuring auth for each request.

```js
import { client } from 'client/sdk.gen';

client.interceptors.request.use((request, options) => {
request.headers.set('Authorization', 'Bearer <my_token>'); // [!code ++]
return request;
});
```

## Build URL

::: warning
Expand Down
6 changes: 5 additions & 1 deletion docs/openapi-ts/clients/legacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import { embedProject } from '../../embed'

# Legacy Clients

::: warning
This feature is deprecated and no longer maintained. Please migrate to one of the client packages.
:::

Before client packages, clients were generated using `@hey-api/openapi-ts`. In fact, `@hey-api/openapi-ts` still supports generating legacy clients. You can generate them with the `client` config option.

::: code-group
Expand Down Expand Up @@ -78,7 +82,7 @@ You might not need a `node` client. Fetch API is [experimental](https://nodejs.o

## Interceptors

Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to the rest of your application.
Interceptors (middleware) can be used to modify requests before they're sent or responses before they're returned to your application.

Below is an example request interceptor

Expand Down
14 changes: 11 additions & 3 deletions docs/openapi-ts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ You can learn more on the [Output](/openapi-ts/output) page.

## Parser

If you're using OpenAPI 3.0 or newer, we encourage you to try out the experimental parser. In the future this will become the default parser, but until it's been tested it's an opt-in feature. To try it out, set the `experimentalParser` flag in your configuration to `true`.
If you're NOT using a legacy client, we encourage you to try out the experimental parser. Soon, it will become the default parser, but until it's been tested it's an opt-in feature. To try it out, set the `experimentalParser` flag in your configuration to `true`.

::: code-group

Expand All @@ -110,7 +110,11 @@ export default {
```

```sh [cli]
npx @hey-api/openapi-ts -i path/to/openapi.json -o src/client -c @hey-api/client-fetch -e
npx @hey-api/openapi-ts \
-c @hey-api/client-fetch \
-e \ # [!code ++]
-i path/to/openapi.json \
-o src/client
```

:::
Expand Down Expand Up @@ -274,7 +278,11 @@ export default {
```

```sh [cli]
npx @hey-api/openapi-ts -i path/to/openapi.json -o src/client -c @hey-api/client-fetch -w
npx @hey-api/openapi-ts \
-c @hey-api/client-fetch \
-i path/to/openapi.json \
-o src/client \
-w # [!code ++]
```

:::
Expand Down
5 changes: 4 additions & 1 deletion docs/openapi-ts/get-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ Live demo
The fastest way to use `@hey-api/openapi-ts` is via npx

```sh
npx @hey-api/openapi-ts -i path/to/openapi.json -o src/client -c @hey-api/client-fetch
npx @hey-api/openapi-ts \
-c @hey-api/client-fetch \
-i path/to/openapi.json \
-o src/client \
```

Congratulations on creating your first client! 🎉 You can learn more about the generated files on the [Output](/openapi-ts/output) page.
Expand Down
Loading
Loading