Skip to content

Commit d387959

Browse files
authored
Merge pull request #2227 from hey-api/feat/sdk-validator-request
feat(sdk): add request validators
2 parents acac297 + 82795b3 commit d387959

File tree

511 files changed

+4451
-1941
lines changed

Some content is hidden

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

511 files changed

+4451
-1941
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: 16 additions & 22 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,7 +31,9 @@ export type { Config } from './types';
3131
::: code-group
3232

3333
```ts [types.d.ts]
34-
export interface Config {
34+
import type { DefinePlugin } from '@hey-api/openapi-ts';
35+
36+
export type Config = {
3537
/**
3638
* Plugin name. Must be unique.
3739
*/
@@ -48,45 +50,39 @@ export interface Config {
4850
* @default false
4951
*/
5052
myOption?: boolean;
51-
}
53+
};
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
},
7676
dependencies: ['@hey-api/typescript'],
7777
handler,
78-
handlerLegacy: () => {},
7978
name: 'my-plugin',
8079
output: 'my-plugin',
8180
};
8281

8382
/**
8483
* Type helper for `my-plugin` plugin, returns {@link Plugin.Config} object
8584
*/
86-
export const defineConfig: Plugin.DefineConfig<Config> = (config) => ({
87-
...defaultConfig,
88-
...config,
89-
});
85+
export const defineConfig = definePluginConfig(defaultConfig);
9086
```
9187

9288
:::
@@ -106,11 +102,9 @@ Notice we defined `handler` in our `config.ts` file. This method is responsible
106102
::: code-group
107103

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

113-
export const handler: Plugin.Handler<Config> = ({ plugin }) => {
107+
export const handler: MyPlugin['Handler'] = ({ plugin }) => {
114108
// create an output file. it will not be
115109
// generated until it contains nodes
116110
const file = plugin.createFile({
@@ -149,9 +143,9 @@ export const handler: Plugin.Handler<Config> = ({ plugin }) => {
149143

150144
:::
151145

152-
### Legacy
146+
### Legacy Handler
153147

154-
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.
155149

156150
## Usage
157151

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

0 commit comments

Comments
 (0)