Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/hey-api/openapi-ts into fea…
Browse files Browse the repository at this point in the history
…t/openapi-2.0.0-parser
  • Loading branch information
mrlubos committed Jan 6, 2025
2 parents 7b7313e + ff29d7b commit 3b01857
Show file tree
Hide file tree
Showing 53 changed files with 829 additions and 570 deletions.
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
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
2 changes: 1 addition & 1 deletion docs/openapi-ts/clients/legacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,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
12 changes: 10 additions & 2 deletions docs/openapi-ts/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
20 changes: 20 additions & 0 deletions docs/openapi-ts/migrating.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,30 @@ This config option is deprecated and will be removed.

## v0.61.0

### 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.

### Added `watch` option

While this is a new feature, supporting it involved replacing the `@apidevtools/json-schema-ref-parser` dependency with our own implementation. Since this was a big change, we're applying caution and marking this as a breaking change.

### Changed `parseAs: 'auto'` behavior

The Fetch API client will return raw response body as `ReadableStream` when `Content-Type` response header is undefined and `parseAs` is `auto`.

## v0.60.0

### Added `sdk.transformer` option
Expand Down
Loading

0 comments on commit 3b01857

Please sign in to comment.