@kubb/plugin-axios 5.9.0 → 5.9.2
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 +35 -17
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +35 -17
- package/dist/index.js.map +1 -1
- package/dist/templates/axios.d.ts +27 -55
- package/docs/options.md +1 -1
- package/package.json +5 -5
- package/templates/axios.ts +170 -266
- package/templates/serializers.ts +13 -5
|
@@ -6,27 +6,21 @@ import { AxiosError, AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalA
|
|
|
6
6
|
* HTTP status codes treated as a success, everything else is an error.
|
|
7
7
|
*/
|
|
8
8
|
export type SuccessStatusCode = '200' | '201' | '202' | '203' | '204' | '205' | '206' | '207' | '208' | '226';
|
|
9
|
-
/**
|
|
10
|
-
* The success members of a per-status responses record.
|
|
11
|
-
*/
|
|
12
9
|
export type SuccessOf<TResponses> = TResponses[Extract<keyof TResponses, SuccessStatusCode>];
|
|
13
|
-
/**
|
|
14
|
-
* The error members of a per-status responses record, every documented status that is not a 2xx.
|
|
15
|
-
*/
|
|
16
10
|
export type ErrorOf<TResponses> = TResponses[Exclude<keyof TResponses, SuccessStatusCode>];
|
|
17
11
|
/**
|
|
18
12
|
* Converts a response record's string status key to its numeric literal, leaving non-numeric keys like `default` as `number`.
|
|
19
13
|
*/
|
|
20
14
|
export type ToStatusNumber<TStatus> = TStatus extends `${infer TNumber extends number}` ? TNumber : number;
|
|
21
15
|
/**
|
|
22
|
-
*
|
|
16
|
+
* Unwraps content-type variants to keep the bare body on `error`.
|
|
23
17
|
*/
|
|
24
18
|
export type DataOf<T> = T extends {
|
|
25
19
|
contentType: string;
|
|
26
20
|
data: infer TData;
|
|
27
21
|
} ? TData : T;
|
|
28
22
|
/**
|
|
29
|
-
*
|
|
23
|
+
* Keeps `contentType` beside `data` so switching on it narrows the body.
|
|
30
24
|
*/
|
|
31
25
|
export type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extends {
|
|
32
26
|
contentType: string;
|
|
@@ -50,7 +44,7 @@ export type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extend
|
|
|
50
44
|
response: TResponse;
|
|
51
45
|
};
|
|
52
46
|
/**
|
|
53
|
-
*
|
|
47
|
+
* Narrows `data` or `error` when switching on the numeric status.
|
|
54
48
|
*/
|
|
55
49
|
export type ResultByStatus<TResponses, TStatus extends keyof TResponses, TRequest, TResponse> = TStatus extends SuccessStatusCode ? SuccessVariant<TStatus, TResponses[TStatus], TRequest, TResponse> : {
|
|
56
50
|
status: ToStatusNumber<TStatus>;
|
|
@@ -60,16 +54,13 @@ export type ResultByStatus<TResponses, TStatus extends keyof TResponses, TReques
|
|
|
60
54
|
request: TRequest;
|
|
61
55
|
response: TResponse;
|
|
62
56
|
};
|
|
63
|
-
/**
|
|
64
|
-
* The union of every documented status' result variant.
|
|
65
|
-
*/
|
|
66
57
|
export type ResultUnion<TResponses, TRequest, TResponse> = { [TStatus in keyof TResponses]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[keyof TResponses];
|
|
67
58
|
/**
|
|
68
|
-
*
|
|
59
|
+
* Selects success statuses without letting untyped error payloads widen `data`.
|
|
69
60
|
*/
|
|
70
61
|
export type SuccessResultUnion<TResponses, TRequest, TResponse> = { [TStatus in Extract<keyof TResponses, SuccessStatusCode>]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[Extract<keyof TResponses, SuccessStatusCode>];
|
|
71
62
|
/**
|
|
72
|
-
*
|
|
63
|
+
* Narrows `data` and `error` by status. With `throwOnError`, includes only success variants.
|
|
73
64
|
*/
|
|
74
65
|
export type RequestResult<TResponses, ThrowOnError extends boolean = true, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = ThrowOnError extends true ? [SuccessResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
|
|
75
66
|
status: number;
|
|
@@ -100,11 +91,15 @@ export type Unwrappable<T extends {
|
|
|
100
91
|
/**
|
|
101
92
|
* Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one.
|
|
102
93
|
*
|
|
103
|
-
* @example
|
|
104
|
-
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const { data, error } = await getPetById({ path: { petId: 1 } })
|
|
97
|
+
* ```
|
|
105
98
|
*
|
|
106
|
-
* @example
|
|
107
|
-
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```ts
|
|
101
|
+
* const pet = await getPetById({ path: { petId: 1 } }).unwrap()
|
|
102
|
+
* ```
|
|
108
103
|
*/
|
|
109
104
|
export declare function withUnwrap<T extends {
|
|
110
105
|
data: unknown;
|
|
@@ -117,15 +112,11 @@ export declare function withUnwrap<T extends {
|
|
|
117
112
|
export type UnwrappedResult<TResponses, ThrowOnError extends boolean = true, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = ThrowOnError extends true ? RequestResult<TResponses, true, TRequest, TResponse>['data'] : RequestResult<TResponses, ThrowOnError, TRequest, TResponse>;
|
|
118
113
|
/**
|
|
119
114
|
* Returns the success body when `throwOnError` is true, or the full result when it is false.
|
|
120
|
-
* Backs generated operations with `returnType: 'data'`.
|
|
121
115
|
*/
|
|
122
116
|
export declare function unwrapResult<T extends {
|
|
123
117
|
data: unknown;
|
|
124
118
|
error: unknown;
|
|
125
119
|
}>(promise: Promise<T>, throwOnError: boolean | undefined): Promise<T | T['data']>;
|
|
126
|
-
/**
|
|
127
|
-
* The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation.
|
|
128
|
-
*/
|
|
129
120
|
export type DataShape = {
|
|
130
121
|
body?: unknown;
|
|
131
122
|
cookies?: unknown;
|
|
@@ -135,41 +126,33 @@ export type DataShape = {
|
|
|
135
126
|
};
|
|
136
127
|
export type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream' | 'formdata';
|
|
137
128
|
/**
|
|
138
|
-
*
|
|
129
|
+
* Decodes a response body for a codec's media type.
|
|
139
130
|
*/
|
|
140
131
|
export type Deserializer<T = unknown> = (raw: unknown, contentType: string) => T | Promise<T>;
|
|
141
132
|
/**
|
|
142
|
-
*
|
|
133
|
+
* Encodes a request body for a codec's media type.
|
|
143
134
|
*/
|
|
144
135
|
export type ContentBodySerializer = (body: unknown, contentType?: string) => unknown;
|
|
145
136
|
/**
|
|
146
|
-
*
|
|
147
|
-
* request body for that media type and `deserialize` decodes the response body. Either half is
|
|
148
|
-
* optional, so a codec can handle one direction.
|
|
137
|
+
* Encodes requests or decodes responses for a media type. Either direction can be omitted.
|
|
149
138
|
*/
|
|
150
139
|
export type Codec = {
|
|
151
140
|
serialize?: ContentBodySerializer;
|
|
152
141
|
deserialize?: Deserializer;
|
|
153
142
|
};
|
|
154
143
|
/**
|
|
155
|
-
*
|
|
144
|
+
* A string sets the request content type. The object form can also set `Accept`.
|
|
156
145
|
*/
|
|
157
146
|
export type ContentType = string | {
|
|
158
147
|
request?: string;
|
|
159
148
|
response?: string;
|
|
160
149
|
};
|
|
161
150
|
/**
|
|
162
|
-
*
|
|
163
|
-
* it is received. `runValidator` runs it through `validateStandardSchema`. Wired through the per-call
|
|
164
|
-
* `validator.request` / `validator.response` / `validator.error` hooks (`error` runs on the error body when a
|
|
165
|
-
* non-2xx call does not throw), and `validator.path` / `validator.query` / `validator.headers` for the
|
|
166
|
-
* request params.
|
|
151
|
+
* Validates request bodies and params, success bodies, or non-throwing error bodies with Standard Schema.
|
|
167
152
|
*/
|
|
168
153
|
export type Validator<T = unknown> = StandardSchemaValidator<T>;
|
|
169
154
|
/**
|
|
170
|
-
*
|
|
171
|
-
* `direction` says which side rejected it: the request, the success body, or the error body. `source`
|
|
172
|
-
* says which part of it: the body, or one of the request param groups.
|
|
155
|
+
* Identifies the value and request that failed validation in `onValidationError`.
|
|
173
156
|
*/
|
|
174
157
|
export type ValidationErrorContext = {
|
|
175
158
|
value: unknown;
|
|
@@ -180,9 +163,7 @@ export type ValidationErrorContext = {
|
|
|
180
163
|
status?: number;
|
|
181
164
|
};
|
|
182
165
|
/**
|
|
183
|
-
*
|
|
184
|
-
* `{ value }` resolves the call with that value instead, so a drifted body can be reported and still
|
|
185
|
-
* delivered. The box keeps an explicit `{ value: undefined }` substitution distinct from declining.
|
|
166
|
+
* Return `{ value }` to substitute a failed value, including `undefined`. Returning nothing rethrows the `ParseError`.
|
|
186
167
|
*/
|
|
187
168
|
export type ValidationErrorHandler = (error: ParseError, context: ValidationErrorContext) => {
|
|
188
169
|
value: unknown;
|
|
@@ -200,7 +181,7 @@ export type Auth = {
|
|
|
200
181
|
scopes?: Array<string>;
|
|
201
182
|
};
|
|
202
183
|
/**
|
|
203
|
-
*
|
|
184
|
+
* Return `user:password` for basic auth, or `undefined` to skip a scheme.
|
|
204
185
|
*/
|
|
205
186
|
export type AuthToken = string | undefined;
|
|
206
187
|
/**
|
|
@@ -211,9 +192,6 @@ export type AuthResolver = AuthToken | ((auth: Auth) => AuthToken | Promise<Auth
|
|
|
211
192
|
* Extra axios config the runtime spreads onto every request, an escape hatch for per-call fields it does not set itself such as `timeout`, `proxy`, and the progress callbacks.
|
|
212
193
|
*/
|
|
213
194
|
export type AxiosOptions = AxiosRequestConfig;
|
|
214
|
-
/**
|
|
215
|
-
* The request a generated function hands to the runtime, with `body` / `headers` / `path` / `query` from the grouped options.
|
|
216
|
-
*/
|
|
217
195
|
export type RequestConfig<TBody = unknown, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = {
|
|
218
196
|
baseURL?: string;
|
|
219
197
|
url?: string;
|
|
@@ -248,8 +226,7 @@ export type RequestConfig<TBody = unknown, TRequest = AxiosRequestConfig, TRespo
|
|
|
248
226
|
auth?: AuthResolver;
|
|
249
227
|
};
|
|
250
228
|
/**
|
|
251
|
-
*
|
|
252
|
-
* data-shaped keys and the literal `url`, plus the per-operation `<Name>Request`.
|
|
229
|
+
* Combines client options with the operation's typed request params and body.
|
|
253
230
|
*/
|
|
254
231
|
export type Options<TData extends DataShape, ThrowOnError extends boolean = true, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = Omit<RequestConfig<unknown, TRequest, TResponse>, keyof DataShape | 'url'> & TData & {
|
|
255
232
|
client?: ClientInstance<TRequest, TResponse>;
|
|
@@ -270,9 +247,6 @@ export type ClientConfig = {
|
|
|
270
247
|
onValidationError?: ValidationErrorHandler;
|
|
271
248
|
auth?: AuthResolver;
|
|
272
249
|
};
|
|
273
|
-
/**
|
|
274
|
-
* The result a resolved call produces before it is cast to `RequestResult` by the generated wrapper.
|
|
275
|
-
*/
|
|
276
250
|
export type CallResult<TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = {
|
|
277
251
|
status: number;
|
|
278
252
|
data: unknown;
|
|
@@ -283,7 +257,7 @@ export type CallResult<TRequest = AxiosRequestConfig, TResponse = AxiosResponse>
|
|
|
283
257
|
};
|
|
284
258
|
export type InterceptorFn<T, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = (value: T, requestConfig?: RequestConfig<unknown, TRequest, TResponse>) => T | Promise<T>;
|
|
285
259
|
/**
|
|
286
|
-
*
|
|
260
|
+
* Registers, removes, or replaces handlers on axios's native interceptor managers.
|
|
287
261
|
*/
|
|
288
262
|
export type InterceptorChannel<T, TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = {
|
|
289
263
|
use: (fn: InterceptorFn<T, TRequest, TResponse>) => number;
|
|
@@ -291,7 +265,7 @@ export type InterceptorChannel<T, TRequest = AxiosRequestConfig, TResponse = Axi
|
|
|
291
265
|
update: (id: number, fn: InterceptorFn<T, TRequest, TResponse>) => void;
|
|
292
266
|
};
|
|
293
267
|
/**
|
|
294
|
-
*
|
|
268
|
+
* Maps error interceptors to axios's response rejection handler.
|
|
295
269
|
*/
|
|
296
270
|
export type Interceptors<TRequest = AxiosRequestConfig, TResponse = AxiosResponse> = {
|
|
297
271
|
request: InterceptorChannel<InternalAxiosRequestConfig, TRequest, TResponse>;
|
|
@@ -347,7 +321,7 @@ export declare function resolveAuth(params: {
|
|
|
347
321
|
query: Record<string, unknown>;
|
|
348
322
|
}): Promise<void>;
|
|
349
323
|
/**
|
|
350
|
-
*
|
|
324
|
+
* Uses the configured axios instance or creates one. `throwOnError` sets axios's default `validateStatus`.
|
|
351
325
|
*/
|
|
352
326
|
export declare function createClientCore<TRequest = AxiosRequestConfig, TResponse = AxiosResponse>(options?: {
|
|
353
327
|
defaultTransport?: AxiosInstance;
|
|
@@ -370,16 +344,14 @@ export type ServerSentEvent<TData = unknown> = {
|
|
|
370
344
|
*/
|
|
371
345
|
export declare function parseEventStream<TData = unknown>(stream: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>): AsyncGenerator<ServerSentEvent<TData>>;
|
|
372
346
|
/**
|
|
373
|
-
*
|
|
374
|
-
* `stream` plus the native `response`.
|
|
347
|
+
* Returns parsed events alongside the native response.
|
|
375
348
|
*/
|
|
376
349
|
export type EventStreamResult<TData = unknown, TResponse = AxiosResponse> = {
|
|
377
350
|
stream: AsyncGenerator<ServerSentEvent<TData>>;
|
|
378
351
|
response: TResponse;
|
|
379
352
|
};
|
|
380
353
|
/**
|
|
381
|
-
*
|
|
382
|
-
* the parsed events as a typed async iterator. Generated SSE operations call this.
|
|
354
|
+
* Exposes a streaming response body as typed Server-Sent Events.
|
|
383
355
|
*/
|
|
384
356
|
export declare function toEventStream<TData = unknown>(result: Promise<{
|
|
385
357
|
data: unknown;
|
package/docs/options.md
CHANGED
|
@@ -93,7 +93,7 @@ export default defineConfig({
|
|
|
93
93
|
|
|
94
94
|
Only the params your OpenAPI spec declares are sent. Path and query keys the spec does not declare are stripped, so a key such as an undocumented filter or a cache-buster never reaches the URL. Declare it in the spec, or leave `params` off. Headers are the exception: client-level `headers` (such as `Authorization` or tracing headers) are merged with the call's headers before validation, so headers the spec does not declare pass through unchanged, and declared header params match in any casing.
|
|
95
95
|
|
|
96
|
-
Pair `request` and `params` with `pluginZod({ inferred: '
|
|
96
|
+
Pair `request` and `params` with `pluginZod({ inferred: 'codec' })` when the schemas use codecs, so the request types accept the domain values (such as a `Date`) that the validators encode. See [`inferred`](https://kubb.dev/extensions/kubb-labs/plugin-zod/options#inferred).
|
|
97
97
|
|
|
98
98
|
### sdk
|
|
99
99
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-axios",
|
|
3
|
-
"version": "5.9.
|
|
3
|
+
"version": "5.9.2",
|
|
4
4
|
"description": "Generate a slim, type-safe HTTP client with Kubb based on axios.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
@@ -80,19 +80,19 @@
|
|
|
80
80
|
"registry": "https://registry.npmjs.org/"
|
|
81
81
|
},
|
|
82
82
|
"dependencies": {
|
|
83
|
-
"@kubb/plugin-ts": "^5.
|
|
84
|
-
"@kubb/plugin-zod": "^5.7.
|
|
83
|
+
"@kubb/plugin-ts": "^5.2.0",
|
|
84
|
+
"@kubb/plugin-zod": "^5.7.2"
|
|
85
85
|
},
|
|
86
86
|
"devDependencies": {
|
|
87
87
|
"@internals/client": "0.0.0",
|
|
88
88
|
"@internals/shared": "0.0.0",
|
|
89
89
|
"axios": "^1.20.0",
|
|
90
|
-
"kubb": "^5.
|
|
90
|
+
"kubb": "^5.14.0",
|
|
91
91
|
"typescript": "^7.0.2"
|
|
92
92
|
},
|
|
93
93
|
"peerDependencies": {
|
|
94
94
|
"axios": "^1.20.0",
|
|
95
|
-
"kubb": "^5.
|
|
95
|
+
"kubb": "^5.14.0"
|
|
96
96
|
},
|
|
97
97
|
"peerDependenciesMeta": {
|
|
98
98
|
"axios": {
|