Skip to content

Commit 4ea6f24

Browse files
committed
docs: update validators pages
1 parent 48c4d0c commit 4ea6f24

File tree

21 files changed

+235
-153
lines changed

21 files changed

+235
-153
lines changed

.changeset/clever-jeans-end.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'@hey-api/openapi-ts': minor
3+
---
4+
5+
refactor(plugin): add `DefinePlugin` utility types
6+
7+
### Updated Plugin API
8+
9+
Please refer to the [custom plugin](https://heyapi.dev/openapi-ts/plugins/custom) tutorial for the latest guide.

.changeset/quick-hotels-knock.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@hey-api/openapi-ts': minor
3+
---
4+
5+
feat(sdk): update `validator` option
6+
7+
### Updated `sdk.validator` option
8+
9+
Clients can now validate both request and response data. As a result, passing a boolean or string to `validator` will control both of these options. To preserve the previous behavior, set `validator.request` to `false` and `validator.response` to your previous configuration.
10+
11+
```js
12+
export default {
13+
input: 'https://get.heyapi.dev/hey-api/backend',
14+
output: 'src/client',
15+
plugins: [
16+
// ...other plugins
17+
{
18+
name: '@hey-api/sdk',
19+
validator: {
20+
request: false,
21+
response: true,
22+
},
23+
},
24+
],
25+
};
26+
```

.changeset/real-horses-lay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@hey-api/openapi-ts': patch
3+
---
4+
5+
fix(client): add requestValidator option

docs/openapi-ts/migrating.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,35 @@ This config option is deprecated and will be removed in favor of [clients](./cli
2727

2828
This config option is deprecated and will be removed.
2929

30+
## v0.77.0
31+
32+
### Updated `sdk.validator` option
33+
34+
Clients can now validate both request and response data. As a result, passing a boolean or string to `validator` will control both of these options. To preserve the previous behavior, set `validator.request` to `false` and `validator.response` to your previous configuration.
35+
36+
```js
37+
export default {
38+
input: 'https://get.heyapi.dev/hey-api/backend',
39+
output: 'src/client',
40+
plugins: [
41+
// ...other plugins
42+
{
43+
name: '@hey-api/sdk',
44+
validator: true, // [!code --]
45+
validator: {
46+
// [!code ++]
47+
request: false, // [!code ++]
48+
response: true, // [!code ++]
49+
}, // [!code ++]
50+
},
51+
],
52+
};
53+
```
54+
55+
### Updated Plugin API
56+
57+
Please refer to the [custom plugin](/openapi-ts/plugins/custom) tutorial for the latest guide.
58+
3059
## v0.76.0
3160

3261
### Single Valibot schema per request

docs/openapi-ts/output/sdk.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,5 +146,83 @@ client.post({
146146

147147
:::
148148

149+
## Validators
150+
151+
There are two ways to configure validators. If you only want to add validators to your SDKs, set `sdk.validator` to a validator plugin name. This will implicitly add the selected plugin with default values.
152+
153+
For a more granular approach, add a validator plugin and set `sdk.validator` to the plugin name or `true` to automatically select a plugin. Until you customize the validator plugin, both approaches will produce the same default output.
154+
155+
::: code-group
156+
157+
```js [sdk]
158+
export default {
159+
input: 'https://get.heyapi.dev/hey-api/backend',
160+
output: 'src/client',
161+
plugins: [
162+
{
163+
name: '@hey-api/sdk',
164+
validator: 'zod', // [!code ++]
165+
},
166+
],
167+
};
168+
```
169+
170+
```js [validator]
171+
export default {
172+
input: 'https://get.heyapi.dev/hey-api/backend',
173+
output: 'src/client',
174+
plugins: [
175+
{
176+
name: '@hey-api/sdk',
177+
validator: true, // or 'zod' // [!code ++]
178+
},
179+
{
180+
name: 'zod', // [!code ++]
181+
// other options
182+
},
183+
],
184+
};
185+
```
186+
187+
:::
188+
189+
You can choose to validate only requests or responses.
190+
191+
::: code-group
192+
193+
```js [requests]
194+
export default {
195+
input: 'https://get.heyapi.dev/hey-api/backend',
196+
output: 'src/client',
197+
plugins: [
198+
{
199+
name: '@hey-api/sdk',
200+
validator: {
201+
request: 'zod', // [!code ++]
202+
},
203+
},
204+
],
205+
};
206+
```
207+
208+
```js [responses]
209+
export default {
210+
input: 'https://get.heyapi.dev/hey-api/backend',
211+
output: 'src/client',
212+
plugins: [
213+
{
214+
name: '@hey-api/sdk',
215+
validator: {
216+
response: 'zod', // [!code ++]
217+
},
218+
},
219+
],
220+
};
221+
```
222+
223+
:::
224+
225+
Learn more about available validators on the [Validators](/openapi-ts/validators/) page.
226+
149227
<!--@include: ../../examples.md-->
150228
<!--@include: ../../sponsors.md-->

docs/openapi-ts/plugins/custom.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ We recommend following the design pattern of the native plugins. You can browse
1919

2020
```ts [index.ts]
2121
export { defaultConfig, defineConfig } from './config';
22-
export type { Config } from './types';
22+
export type { MyPlugin } from './types';
2323
```
2424

2525
:::
@@ -31,6 +31,8 @@ export type { Config } from './types';
3131
::: code-group
3232

3333
```ts [types.d.ts]
34+
import type { DefinePlugin } from '@hey-api/openapi-ts';
35+
3436
export type Config = {
3537
/**
3638
* Plugin name. Must be unique.
@@ -49,27 +51,25 @@ export type Config = {
4951
*/
5052
myOption?: boolean;
5153
};
54+
55+
export type MyPlugin = DefinePlugin<Config>;
5256
```
5357

5458
:::
5559

5660
## Configuration
5761

58-
::: tip
59-
Reserved fields are prefixed with an underscore and are not exposed to the user.
60-
:::
61-
62-
`config.ts` contains the runtime configuration for your plugin. It must implement the `Config` interface we created above and define `handler()` and `handlerLegacy()` functions from the `Plugin.Config` interface.
62+
`config.ts` contains the runtime configuration for your plugin. It must implement the `MyPlugin` interface we created above and define the `handler()` function from the `MyPlugin['Config']` interface.
6363

6464
::: code-group
6565

6666
```ts [config.ts]
67-
import type { Plugin } from '@hey-api/openapi-ts';
67+
import { definePluginConfig } from '@hey-api/openapi-ts';
6868

6969
import { handler } from './plugin';
70-
import type { Config } from './types';
70+
import type { MyPlugin } from './types';
7171

72-
export const defaultConfig: Plugin.Config<Config> = {
72+
export const defaultConfig: MyPlugin['Config'] = {
7373
config: {
7474
myOption: false, // implements default value from types
7575
},
@@ -82,10 +82,7 @@ export const defaultConfig: Plugin.Config<Config> = {
8282
/**
8383
* Type helper for `my-plugin` plugin, returns {@link Plugin.Config} object
8484
*/
85-
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
86-
...defaultConfig,
87-
...config,
88-
});
85+
export const defineConfig = definePluginConfig(defaultConfig);
8986
```
9087

9188
:::
@@ -105,11 +102,9 @@ Notice we defined `handler` in our `config.ts` file. This method is responsible
105102
::: code-group
106103

107104
```ts [plugin.ts]
108-
import type { Plugin } from '@hey-api/openapi-ts';
109-
110-
import type { Config } from './types';
105+
import type { MyPlugin } from './types';
111106

112-
export const handler: Plugin.Handler<Config> = ({ plugin }) => {
107+
export const handler: MyPlugin['Handler'] = ({ plugin }) => {
113108
// create an output file. it will not be
114109
// generated until it contains nodes
115110
const file = plugin.createFile({
@@ -148,9 +143,9 @@ export const handler: Plugin.Handler<Config> = ({ plugin }) => {
148143

149144
:::
150145

151-
### Legacy
146+
### Legacy Handler
152147

153-
Notice we defined `handlerLegacy` in our `config.ts` file. This method is responsible for generating the actual output when using the legacy parser. We do not recommend implementing this method unless you must use the legacy parser. You can use one of our [`plugin-legacy.ts`](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/%40hey-api/typescript/plugin-legacy.ts) files as an inspiration for potential implementation.
148+
You can also define an optional `handlerLegacy` function in `config.ts`. This method is responsible for generating the output when using the legacy parser. We do not recommend implementing this method unless you must use the legacy parser. You can use one of our [`plugin-legacy.ts`](https://github.com/hey-api/openapi-ts/blob/main/packages/openapi-ts/src/plugins/%40hey-api/typescript/plugin-legacy.ts) files as an inspiration for potential implementation.
154149

155150
## Usage
156151

docs/openapi-ts/plugins/valibot.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545

4646
### SDKs
4747

48-
To automatically validate response data in your SDKs, set `sdk.validator` to `true`.
48+
To add data validators to your SDKs, set `sdk.validator` to `true`.
4949

5050
```js
5151
export default {
@@ -62,6 +62,8 @@ export default {
6262
};
6363
```
6464

65+
Learn more about data validators in your SDKs on the [SDKs](/openapi-ts/output/sdk#validators) page.
66+
6567
## Output
6668

6769
The Valibot plugin will generate the following artifacts, depending on the input specification.
@@ -78,7 +80,6 @@ const vData = v.object({
7880
bar: v.optional(v.union([v.number(), v.null()])),
7981
}),
8082
),
81-
headers: v.optional(v.never()),
8283
path: v.object({
8384
baz: v.string(),
8485
}),

docs/openapi-ts/plugins/zod.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545

4646
### SDKs
4747

48-
To automatically validate response data in your SDKs, set `sdk.validator` to `true`.
48+
To add data validators to your SDKs, set `sdk.validator` to `true`.
4949

5050
```js
5151
export default {
@@ -62,6 +62,8 @@ export default {
6262
};
6363
```
6464

65+
Learn more about data validators in your SDKs on the [SDKs](/openapi-ts/output/sdk#validators) page.
66+
6567
## Output
6668

6769
The Zod plugin will generate the following artifacts, depending on the input specification.
@@ -78,7 +80,6 @@ const zData = z.object({
7880
bar: z.union([z.number(), z.null()]).optional(),
7981
})
8082
.optional(),
81-
headers: z.never().optional(),
8283
path: z.object({
8384
baz: z.string(),
8485
}),

docs/openapi-ts/validators.md

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ There are times when you cannot blindly trust the server to return the correct d
99

1010
Whatever your reason to use validators might be, you can rest assured that you're working with the correct data.
1111

12+
## Features
13+
14+
- seamless integration with `@hey-api/openapi-ts` ecosystem
15+
- schemas for requests, responses, and reusable definitions
16+
1217
## Options
1318

1419
Hey API natively supports the following validators.
@@ -23,43 +28,5 @@ Hey API natively supports the following validators.
2328

2429
Don't see your validator? Let us know your interest by [opening an issue](https://github.com/hey-api/openapi-ts/issues).
2530

26-
## Installation
27-
28-
There are two ways to generate validators. If you only need response validation in your SDKs, set `sdk.validator` to the desired value. For a more granular approach, add your validator to plugins and set `sdk.validator` to `true`.
29-
30-
::: code-group
31-
32-
```js [sdk]
33-
export default {
34-
input: 'https://get.heyapi.dev/hey-api/backend',
35-
output: 'src/client',
36-
plugins: [
37-
{
38-
name: '@hey-api/sdk',
39-
validator: 'zod', // [!code ++]
40-
},
41-
],
42-
};
43-
```
44-
45-
```js [validator]
46-
export default {
47-
input: 'https://get.heyapi.dev/hey-api/backend',
48-
output: 'src/client',
49-
plugins: [
50-
{
51-
name: '@hey-api/sdk',
52-
validator: true, // [!code ++]
53-
},
54-
{
55-
name: 'zod', // [!code ++]
56-
// other options
57-
},
58-
],
59-
};
60-
```
61-
62-
:::
63-
6431
<!--@include: ../examples.md-->
6532
<!--@include: ../sponsors.md-->

packages/openapi-ts-tests/test/__snapshots__/3.1.x/clients/my-client/base-url-false/client/plugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
clientDefaultConfig,
44
clientDefaultMeta,
55
clientPluginHandler,
6+
type DefinePlugin,
67
definePluginConfig,
7-
type Plugin,
88
} from '@hey-api/openapi-ts';
99

1010
type Config = Client.Config & {
@@ -14,12 +14,12 @@ type Config = Client.Config & {
1414
name: string;
1515
};
1616

17-
export type MyClientPlugin = Plugin.Types<Config>;
17+
export type MyClientPlugin = DefinePlugin<Config>;
1818

19-
export const defaultConfig: Plugin.Config<MyClientPlugin> = {
19+
export const defaultConfig: MyClientPlugin['Config'] = {
2020
...clientDefaultMeta,
2121
config: clientDefaultConfig,
22-
handler: clientPluginHandler as Plugin.Handler<MyClientPlugin>,
22+
handler: clientPluginHandler as MyClientPlugin['Handler'],
2323
name: __filename,
2424
};
2525

0 commit comments

Comments
 (0)