@kubb/plugin-axios 5.12.0-canary.20261105T092614 → 5.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/templates/axios.d.ts +13 -0
- package/docs/guides/error-handling.md +18 -0
- package/package.json +5 -5
- package/templates/axios.ts +27 -7
package/dist/index.cjs
CHANGED
|
@@ -1511,7 +1511,7 @@ var compatibility = {
|
|
|
1511
1511
|
"node": ">=22.22"
|
|
1512
1512
|
};
|
|
1513
1513
|
// package.json
|
|
1514
|
-
var version = "5.12.0
|
|
1514
|
+
var version = "5.12.0";
|
|
1515
1515
|
// src/templates.ts
|
|
1516
1516
|
/** Absolute path to the axios client template, copied into `.kubb/client.ts`. */
|
|
1517
1517
|
const axiosClientTemplatePath = (0, node_url.fileURLToPath)(new URL("../templates/axios.ts", require("url").pathToFileURL(__filename).href));
|
package/dist/index.js
CHANGED
|
@@ -1485,7 +1485,7 @@ var compatibility = {
|
|
|
1485
1485
|
"node": ">=22.22"
|
|
1486
1486
|
};
|
|
1487
1487
|
// package.json
|
|
1488
|
-
var version = "5.12.0
|
|
1488
|
+
var version = "5.12.0";
|
|
1489
1489
|
// src/templates.ts
|
|
1490
1490
|
/** Absolute path to the axios client template, copied into `.kubb/client.ts`. */
|
|
1491
1491
|
const axiosClientTemplatePath = fileURLToPath(new URL("../templates/axios.ts", import.meta.url));
|
|
@@ -169,6 +169,17 @@ export type ValidationErrorHandler = (error: ParseError, context: ValidationErro
|
|
|
169
169
|
} | void | Promise<{
|
|
170
170
|
value: unknown;
|
|
171
171
|
} | void>;
|
|
172
|
+
/**
|
|
173
|
+
* Turns generated validation on or off. `false` skips every validator, an object skips only the directions set to `false`:
|
|
174
|
+
* `request` is the body, `params` are path, query, and header params, `response` and `error` are the response bodies.
|
|
175
|
+
* @default true
|
|
176
|
+
*/
|
|
177
|
+
export type ValidateOption = boolean | {
|
|
178
|
+
request?: boolean;
|
|
179
|
+
params?: boolean;
|
|
180
|
+
response?: boolean;
|
|
181
|
+
error?: boolean;
|
|
182
|
+
};
|
|
172
183
|
/**
|
|
173
184
|
* A resolved security scheme carried on each generated call's `security` array and passed to the `auth` resolver.
|
|
174
185
|
*/
|
|
@@ -221,6 +232,7 @@ export type RequestConfig<TBody = unknown, TRequest = AxiosRequestConfig, TRespo
|
|
|
221
232
|
error?: Validator;
|
|
222
233
|
};
|
|
223
234
|
onValidationError?: ValidationErrorHandler;
|
|
235
|
+
validate?: ValidateOption;
|
|
224
236
|
security?: Array<Auth>;
|
|
225
237
|
auth?: AuthResolver;
|
|
226
238
|
};
|
|
@@ -244,6 +256,7 @@ export type ClientConfig = {
|
|
|
244
256
|
serializer?: Serializers;
|
|
245
257
|
codecs?: Record<string, Codec>;
|
|
246
258
|
onValidationError?: ValidationErrorHandler;
|
|
259
|
+
validate?: ValidateOption;
|
|
247
260
|
auth?: AuthResolver;
|
|
248
261
|
};
|
|
249
262
|
export type CallResult<TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = {
|
|
@@ -116,3 +116,21 @@ A `ParseError` reports schema validation issues. A `ResponseError` reports a non
|
|
|
116
116
|
|
|
117
117
|
Calling `.unwrap()` on a `throwOnError: false` call turns that same `error` into a rejection, so a
|
|
118
118
|
`try`/`catch` works there too.
|
|
119
|
+
|
|
120
|
+
## Turn validation off at runtime
|
|
121
|
+
|
|
122
|
+
The `validator` option decides at generation time which schemas each call carries. Set `validate` on the client to skip them at runtime, for example in production or in tests with partial fixtures:
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
import { client } from './gen/.kubb/client'
|
|
126
|
+
|
|
127
|
+
client.setConfig({ validate: import.meta.env.DEV })
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
`validate: false` skips every schema. An object skips only the directions set to `false`: `request` (the body), `params` (path, query, and header params), `response`, and `error`:
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
client.setConfig({ validate: { response: false } })
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
A per-call `validate` overrides the client setting, such as `getPetById({ path: { petId: 1 }, validate: true })`. The default is `true`.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-axios",
|
|
3
|
-
"version": "5.12.0
|
|
3
|
+
"version": "5.12.0",
|
|
4
4
|
"description": "Generate a slim, type-safe HTTP client with Kubb based on axios.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
@@ -81,13 +81,13 @@
|
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
83
|
"@kubb/kit": "^5.15.4",
|
|
84
|
-
"@kubb/plugin-ts": "^5.
|
|
85
|
-
"@kubb/plugin-zod": "^5.
|
|
84
|
+
"@kubb/plugin-ts": "^5.4.0",
|
|
85
|
+
"@kubb/plugin-zod": "^5.8.1",
|
|
86
86
|
"@kubb/renderer-jsx": "^5.15.4"
|
|
87
87
|
},
|
|
88
88
|
"devDependencies": {
|
|
89
|
-
"@internals/client": "0.
|
|
90
|
-
"@internals/shared": "0.
|
|
89
|
+
"@internals/client": "0.0.0",
|
|
90
|
+
"@internals/shared": "0.0.0",
|
|
91
91
|
"axios": "^1.21.0",
|
|
92
92
|
"typescript": "^7.0.2"
|
|
93
93
|
},
|
package/templates/axios.ts
CHANGED
|
@@ -172,6 +172,13 @@ export type ValidationErrorContext = {
|
|
|
172
172
|
*/
|
|
173
173
|
export type ValidationErrorHandler = (error: ParseError, context: ValidationErrorContext) => { value: unknown } | void | Promise<{ value: unknown } | void>
|
|
174
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Turns generated validation on or off. `false` skips every validator, an object skips only the directions set to `false`:
|
|
177
|
+
* `request` is the body, `params` are path, query, and header params, `response` and `error` are the response bodies.
|
|
178
|
+
* @default true
|
|
179
|
+
*/
|
|
180
|
+
export type ValidateOption = boolean | { request?: boolean; params?: boolean; response?: boolean; error?: boolean }
|
|
181
|
+
|
|
175
182
|
/**
|
|
176
183
|
* A resolved security scheme carried on each generated call's `security` array and passed to the `auth` resolver.
|
|
177
184
|
*/
|
|
@@ -221,6 +228,7 @@ export type RequestConfig<TBody = unknown, TRequest = AxiosRequestConfig, TRespo
|
|
|
221
228
|
codecs?: Record<string, Codec>
|
|
222
229
|
validator?: { request?: Validator; path?: Validator; query?: Validator; headers?: Validator; response?: Validator; error?: Validator }
|
|
223
230
|
onValidationError?: ValidationErrorHandler
|
|
231
|
+
validate?: ValidateOption
|
|
224
232
|
security?: Array<Auth>
|
|
225
233
|
auth?: AuthResolver
|
|
226
234
|
}
|
|
@@ -250,6 +258,7 @@ export type ClientConfig = {
|
|
|
250
258
|
serializer?: Serializers
|
|
251
259
|
codecs?: Record<string, Codec>
|
|
252
260
|
onValidationError?: ValidationErrorHandler
|
|
261
|
+
validate?: ValidateOption
|
|
253
262
|
auth?: AuthResolver
|
|
254
263
|
}
|
|
255
264
|
|
|
@@ -488,6 +497,10 @@ export async function resolveAuth(params: {
|
|
|
488
497
|
}
|
|
489
498
|
}
|
|
490
499
|
|
|
500
|
+
function isValidationEnabled(validate: ValidateOption | undefined, direction: 'request' | 'params' | 'response' | 'error'): boolean {
|
|
501
|
+
return typeof validate === 'object' ? validate[direction] !== false : validate !== false
|
|
502
|
+
}
|
|
503
|
+
|
|
491
504
|
async function runValidator<T>({
|
|
492
505
|
validator,
|
|
493
506
|
value,
|
|
@@ -535,10 +548,11 @@ async function resolveRequest<TBody, TRequest, TResponse>({
|
|
|
535
548
|
}: {
|
|
536
549
|
config: ClientConfig
|
|
537
550
|
requestConfig: RequestConfig<TBody, TRequest, TResponse>
|
|
538
|
-
}): Promise<{ axiosConfig: AxiosRequestConfig; codecs: Record<string, Codec>; throwOnError: boolean }> {
|
|
551
|
+
}): Promise<{ axiosConfig: AxiosRequestConfig; codecs: Record<string, Codec>; throwOnError: boolean; validate: ValidateOption | undefined }> {
|
|
539
552
|
const { querySerializer, bodySerializer, pathSerializer } = resolveSerializers({ config, requestConfig })
|
|
540
553
|
const codecs = { ...config.codecs, ...requestConfig.codecs }
|
|
541
554
|
const onValidationError = requestConfig.onValidationError ?? config.onValidationError
|
|
555
|
+
const validate = requestConfig.validate ?? config.validate
|
|
542
556
|
const validationContext = { direction: 'request', method: requestConfig.method, url: requestConfig.url } as const
|
|
543
557
|
|
|
544
558
|
// Params are encoded before anything reads them: headers before styling, query before auth adds its values.
|
|
@@ -549,7 +563,12 @@ async function resolveRequest<TBody, TRequest, TResponse>({
|
|
|
549
563
|
}
|
|
550
564
|
const [validatedPath, validatedQuery, validatedHeaders] = await Promise.all(
|
|
551
565
|
(['path', 'query', 'headers'] as const).map((source) =>
|
|
552
|
-
runValidator({
|
|
566
|
+
runValidator({
|
|
567
|
+
validator: isValidationEnabled(validate, 'params') ? requestConfig.validator?.[source] : undefined,
|
|
568
|
+
value: paramValues[source],
|
|
569
|
+
context: { ...validationContext, source },
|
|
570
|
+
onValidationError,
|
|
571
|
+
}),
|
|
553
572
|
),
|
|
554
573
|
)
|
|
555
574
|
|
|
@@ -576,7 +595,7 @@ async function resolveRequest<TBody, TRequest, TResponse>({
|
|
|
576
595
|
}
|
|
577
596
|
|
|
578
597
|
const validatedBody = await runValidator({
|
|
579
|
-
validator: requestConfig.validator?.request,
|
|
598
|
+
validator: isValidationEnabled(validate, 'request') ? requestConfig.validator?.request : undefined,
|
|
580
599
|
value: requestConfig.body,
|
|
581
600
|
context: { ...validationContext, source: 'body' },
|
|
582
601
|
onValidationError,
|
|
@@ -634,7 +653,7 @@ async function resolveRequest<TBody, TRequest, TResponse>({
|
|
|
634
653
|
axiosConfig.adapter = 'fetch'
|
|
635
654
|
}
|
|
636
655
|
|
|
637
|
-
return { axiosConfig, codecs, throwOnError }
|
|
656
|
+
return { axiosConfig, codecs, throwOnError, validate }
|
|
638
657
|
}
|
|
639
658
|
|
|
640
659
|
/**
|
|
@@ -712,7 +731,7 @@ export function createClientCore<TRequest = AxiosRequestConfig, TResponse = Axio
|
|
|
712
731
|
|
|
713
732
|
const client = (async <TBody = unknown>(requestConfig: RequestConfig<TBody, TRequest, TResponse>): Promise<CallResult<TRequest, TResponse>> => {
|
|
714
733
|
const activeInstance = requestConfig.transport ?? instance
|
|
715
|
-
const { axiosConfig, codecs, throwOnError } = await resolveRequest({ config, requestConfig })
|
|
734
|
+
const { axiosConfig, codecs, throwOnError, validate } = await resolveRequest({ config, requestConfig })
|
|
716
735
|
const contextId = ++requestContextId
|
|
717
736
|
requestContexts.set(contextId, requestConfig)
|
|
718
737
|
;(axiosConfig as AxiosRequestConfig & { __kubbRequestContext: number }).__kubbRequestContext = contextId
|
|
@@ -722,14 +741,15 @@ export function createClientCore<TRequest = AxiosRequestConfig, TResponse = Axio
|
|
|
722
741
|
const validator = requestConfig.validator
|
|
723
742
|
const onValidationError = requestConfig.onValidationError ?? config.onValidationError
|
|
724
743
|
const isSuccess = response.status >= 200 && response.status < 300
|
|
744
|
+
const direction = isSuccess ? 'response' : 'error'
|
|
725
745
|
const contentType = getResponseContentType(response.headers as Record<string, unknown>)
|
|
726
746
|
const codec = contentType ? codecs[contentType] : undefined
|
|
727
747
|
const decoded: unknown = contentType && codec?.deserialize ? await codec.deserialize(response.data, contentType) : response.data
|
|
728
748
|
const data = await runValidator({
|
|
729
|
-
validator:
|
|
749
|
+
validator: isValidationEnabled(validate, direction) ? validator?.[direction] : undefined,
|
|
730
750
|
value: decoded,
|
|
731
751
|
context: {
|
|
732
|
-
direction
|
|
752
|
+
direction,
|
|
733
753
|
source: 'body',
|
|
734
754
|
method: response.config?.method?.toUpperCase(),
|
|
735
755
|
url: response.config?.url,
|