@kubb/plugin-fetch 5.6.0-canary.20261014T114046 → 5.6.1
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/fetch.d.ts +414 -0
- package/dist/templates/serializers.d.ts +177 -0
- package/dist/templates/standardSchema.d.ts +51 -0
- package/package.json +12 -5
- package/templates/fetch.ts +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1487,7 +1487,7 @@ var compatibility = {
|
|
|
1487
1487
|
};
|
|
1488
1488
|
//#endregion
|
|
1489
1489
|
//#region package.json
|
|
1490
|
-
var version = "5.6.
|
|
1490
|
+
var version = "5.6.1";
|
|
1491
1491
|
//#endregion
|
|
1492
1492
|
//#region src/templates.ts
|
|
1493
1493
|
/** Absolute path to the fetch client template, copied into `.kubb/client.ts`. */
|
package/dist/index.js
CHANGED
|
@@ -1461,7 +1461,7 @@ var compatibility = {
|
|
|
1461
1461
|
};
|
|
1462
1462
|
//#endregion
|
|
1463
1463
|
//#region package.json
|
|
1464
|
-
var version = "5.6.
|
|
1464
|
+
var version = "5.6.1";
|
|
1465
1465
|
//#endregion
|
|
1466
1466
|
//#region src/templates.ts
|
|
1467
1467
|
/** Absolute path to the fetch client template, copied into `.kubb/client.ts`. */
|
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
import { HeadersInit, RequestBody, Serializers, Styles } from "./serializers.js";
|
|
2
|
+
import { ParseError, StandardSchemaValidator } from "./standardSchema.js";
|
|
3
|
+
//#region templates/fetch.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* HTTP status codes treated as a success, everything else is an error.
|
|
6
|
+
*/
|
|
7
|
+
export type SuccessStatusCode = '200' | '201' | '202' | '203' | '204' | '205' | '206' | '207' | '208' | '226';
|
|
8
|
+
/**
|
|
9
|
+
* The success members of a per-status responses record.
|
|
10
|
+
*/
|
|
11
|
+
export type SuccessOf<TResponses> = TResponses[Extract<keyof TResponses, SuccessStatusCode>];
|
|
12
|
+
/**
|
|
13
|
+
* The error members of a per-status responses record, every documented status that is not a 2xx.
|
|
14
|
+
*/
|
|
15
|
+
export type ErrorOf<TResponses> = TResponses[Exclude<keyof TResponses, SuccessStatusCode>];
|
|
16
|
+
/**
|
|
17
|
+
* Converts a response record's string status key to its numeric literal, leaving non-numeric keys like `default` as `number`.
|
|
18
|
+
*/
|
|
19
|
+
export type ToStatusNumber<TStatus> = TStatus extends `${infer TNumber extends number}` ? TNumber : number;
|
|
20
|
+
/**
|
|
21
|
+
* The plain body of a per-status response, unwrapping the `{ contentType; data }` union so an error result keeps the bare body union on `error`.
|
|
22
|
+
*/
|
|
23
|
+
export type DataOf<T> = T extends {
|
|
24
|
+
contentType: string;
|
|
25
|
+
data: infer TData;
|
|
26
|
+
} ? TData : T;
|
|
27
|
+
/**
|
|
28
|
+
* The success variant for a single status, flattened so the negotiated `contentType` sits next to `data` and `switch (result.contentType)` narrows it.
|
|
29
|
+
*/
|
|
30
|
+
export type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extends {
|
|
31
|
+
contentType: string;
|
|
32
|
+
data: unknown;
|
|
33
|
+
} ? TEntry extends {
|
|
34
|
+
contentType: infer TContentType;
|
|
35
|
+
data: infer TData;
|
|
36
|
+
} ? {
|
|
37
|
+
status: ToStatusNumber<TStatus>;
|
|
38
|
+
data: TData;
|
|
39
|
+
error: undefined;
|
|
40
|
+
contentType: TContentType;
|
|
41
|
+
request: TRequest;
|
|
42
|
+
response: TResponse;
|
|
43
|
+
} : never : {
|
|
44
|
+
status: ToStatusNumber<TStatus>;
|
|
45
|
+
data: TEntry;
|
|
46
|
+
error: undefined;
|
|
47
|
+
contentType: string | undefined;
|
|
48
|
+
request: TRequest;
|
|
49
|
+
response: TResponse;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* One result variant for a single documented status, keyed by the numeric `status` so a `switch (result.status)` narrows `data` or `error`.
|
|
53
|
+
*/
|
|
54
|
+
export type ResultByStatus<TResponses, TStatus extends keyof TResponses, TRequest, TResponse> = TStatus extends SuccessStatusCode ? SuccessVariant<TStatus, TResponses[TStatus], TRequest, TResponse> : {
|
|
55
|
+
status: ToStatusNumber<TStatus>;
|
|
56
|
+
data: undefined;
|
|
57
|
+
error: DataOf<TResponses[TStatus]>;
|
|
58
|
+
contentType: string | undefined;
|
|
59
|
+
request: TRequest;
|
|
60
|
+
response: TResponse;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* The union of every documented status' result variant.
|
|
64
|
+
*/
|
|
65
|
+
export type ResultUnion<TResponses, TRequest, TResponse> = { [TStatus in keyof TResponses]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[keyof TResponses];
|
|
66
|
+
/**
|
|
67
|
+
* The union of just the success (2xx) status variants, selected by status code so an untyped error payload can never widen `data`.
|
|
68
|
+
*/
|
|
69
|
+
export type SuccessResultUnion<TResponses, TRequest, TResponse> = { [TStatus in Extract<keyof TResponses, SuccessStatusCode>]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[Extract<keyof TResponses, SuccessStatusCode>];
|
|
70
|
+
/**
|
|
71
|
+
* The shape every generated function returns, discriminated by the top-level `status`, narrowing to the 2xx variants under `throwOnError` and to every documented status without it.
|
|
72
|
+
*/
|
|
73
|
+
export type RequestResult<TResponses, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = ThrowOnError extends true ? [SuccessResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
|
|
74
|
+
status: number;
|
|
75
|
+
data: SuccessOf<TResponses>;
|
|
76
|
+
error: undefined;
|
|
77
|
+
contentType: string | undefined;
|
|
78
|
+
request: TRequest;
|
|
79
|
+
response: TResponse;
|
|
80
|
+
} : SuccessResultUnion<TResponses, TRequest, TResponse> : [ResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
|
|
81
|
+
status: number;
|
|
82
|
+
data: undefined;
|
|
83
|
+
error: undefined;
|
|
84
|
+
contentType: string | undefined;
|
|
85
|
+
request: TRequest;
|
|
86
|
+
response: TResponse;
|
|
87
|
+
} : ResultUnion<TResponses, TRequest, TResponse>;
|
|
88
|
+
/**
|
|
89
|
+
* A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body.
|
|
90
|
+
*/
|
|
91
|
+
export type Unwrappable<T extends {
|
|
92
|
+
data: unknown;
|
|
93
|
+
error: unknown;
|
|
94
|
+
}> = Promise<T> & {
|
|
95
|
+
unwrap: () => Promise<Extract<T, {
|
|
96
|
+
error: undefined;
|
|
97
|
+
}>['data']>;
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one.
|
|
101
|
+
*
|
|
102
|
+
* @example Full result
|
|
103
|
+
* `const { data, error } = await getPetById({ path: { petId: 1 } })`
|
|
104
|
+
*
|
|
105
|
+
* @example Success body only
|
|
106
|
+
* `const pet = await getPetById({ path: { petId: 1 } }).unwrap()`
|
|
107
|
+
*/
|
|
108
|
+
export declare function withUnwrap<T extends {
|
|
109
|
+
data: unknown;
|
|
110
|
+
error: unknown;
|
|
111
|
+
}>(promise: Promise<T>): Unwrappable<T>;
|
|
112
|
+
/**
|
|
113
|
+
* The shape a generated operation returns with `returnType: 'data'`: the success body when
|
|
114
|
+
* `throwOnError` is true, or the full `RequestResult` when it is false.
|
|
115
|
+
*/
|
|
116
|
+
export type UnwrappedResult<TResponses, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = ThrowOnError extends true ? RequestResult<TResponses, true, TRequest, TResponse>['data'] : RequestResult<TResponses, ThrowOnError, TRequest, TResponse>;
|
|
117
|
+
/**
|
|
118
|
+
* Returns the success body when `throwOnError` is true, or the full result when it is false.
|
|
119
|
+
* Backs generated operations with `returnType: 'data'`.
|
|
120
|
+
*/
|
|
121
|
+
export declare function unwrapResult<T extends {
|
|
122
|
+
data: unknown;
|
|
123
|
+
error: unknown;
|
|
124
|
+
}>(promise: Promise<T>, throwOnError: boolean | undefined): Promise<T | T['data']>;
|
|
125
|
+
/**
|
|
126
|
+
* The data-shaped keys of the grouped options object, which `Options` re-adds typed per operation.
|
|
127
|
+
*/
|
|
128
|
+
export type DataShape = {
|
|
129
|
+
body?: unknown;
|
|
130
|
+
cookies?: unknown;
|
|
131
|
+
headers?: unknown;
|
|
132
|
+
path?: unknown;
|
|
133
|
+
query?: unknown;
|
|
134
|
+
};
|
|
135
|
+
export type RequestCredentials = 'omit' | 'same-origin' | 'include';
|
|
136
|
+
export type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
|
|
137
|
+
/**
|
|
138
|
+
* Turns a raw response body into a parsed value, registered per media type as a codec's `deserialize` to handle formats the runtime does not decode itself.
|
|
139
|
+
*/
|
|
140
|
+
export type Deserializer<T = unknown> = (raw: unknown, contentType: string) => T | Promise<T>;
|
|
141
|
+
/**
|
|
142
|
+
* Serializes a request body for a single media type, registered per content type as a codec's `serialize` to encode formats the default serializer does not handle.
|
|
143
|
+
*/
|
|
144
|
+
export type ContentBodySerializer = (body: unknown, contentType?: string) => RequestBody | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* A per-content-type codec registered on `codecs`, keyed by content type. `serialize` encodes the
|
|
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.
|
|
149
|
+
*/
|
|
150
|
+
export type Codec = {
|
|
151
|
+
serialize?: ContentBodySerializer;
|
|
152
|
+
deserialize?: Deserializer;
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
155
|
+
* The per-call content type selection, where a bare string sets the request content type and the object form also sets the response format sent as `Accept`.
|
|
156
|
+
*/
|
|
157
|
+
export type ContentType = string | {
|
|
158
|
+
request?: string;
|
|
159
|
+
response?: string;
|
|
160
|
+
};
|
|
161
|
+
/**
|
|
162
|
+
* A Standard Schema validator (zod, valibot, arktype) that parses a value before it is sent or after
|
|
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).
|
|
166
|
+
*/
|
|
167
|
+
export type Validator<T = unknown> = StandardSchemaValidator<T>;
|
|
168
|
+
/**
|
|
169
|
+
* The failing body and the call it came from, handed to `onValidationError` alongside the `ParseError`.
|
|
170
|
+
* `direction` says which slot rejected it: the request body, the success body, or the error body.
|
|
171
|
+
*/
|
|
172
|
+
export type ValidationErrorContext = {
|
|
173
|
+
value: unknown;
|
|
174
|
+
direction: 'request' | 'response' | 'error';
|
|
175
|
+
method?: string;
|
|
176
|
+
url?: string;
|
|
177
|
+
status?: number;
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Decides what a failed validation does. Returning nothing rethrows the `ParseError`; returning
|
|
181
|
+
* `{ value }` resolves the call with that value instead, so a drifted body can be reported and still
|
|
182
|
+
* delivered. The box keeps an explicit `{ value: undefined }` substitution distinct from declining.
|
|
183
|
+
*/
|
|
184
|
+
export type ValidationErrorHandler = (error: ParseError, context: ValidationErrorContext) => {
|
|
185
|
+
value: unknown;
|
|
186
|
+
} | void | Promise<{
|
|
187
|
+
value: unknown;
|
|
188
|
+
} | void>;
|
|
189
|
+
/**
|
|
190
|
+
* A resolved security scheme carried on each generated call's `security` array and passed to the `auth` resolver.
|
|
191
|
+
*/
|
|
192
|
+
export type Auth = {
|
|
193
|
+
type: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect';
|
|
194
|
+
scheme?: 'bearer' | 'basic';
|
|
195
|
+
name?: string;
|
|
196
|
+
in?: 'header' | 'query' | 'cookie';
|
|
197
|
+
};
|
|
198
|
+
/**
|
|
199
|
+
* The raw token a consumer returns for a scheme (or `user:password` for basic), or `undefined` to skip it.
|
|
200
|
+
*/
|
|
201
|
+
export type AuthToken = string | undefined;
|
|
202
|
+
/**
|
|
203
|
+
* Resolves the token for a security scheme, either a static token or a callback called per scheme until one returns a token.
|
|
204
|
+
*/
|
|
205
|
+
export type AuthResolver = AuthToken | ((auth: Auth) => AuthToken | Promise<AuthToken>);
|
|
206
|
+
/**
|
|
207
|
+
* Extra `fetch` init the transport spreads onto every `Request`, an escape hatch for fields the runtime does not set itself such as `cache`, `redirect`, and Next.js's `next`.
|
|
208
|
+
*/
|
|
209
|
+
export type FetchOptions = RequestInit & {
|
|
210
|
+
next?: Record<string, unknown>;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* The request a generated function hands to the runtime, with `body` / `headers` / `path` / `query` from the grouped options.
|
|
214
|
+
*/
|
|
215
|
+
export type RequestConfig<TBody = unknown, TRequest = Request, TResponse = Response> = {
|
|
216
|
+
baseURL?: string;
|
|
217
|
+
url?: string;
|
|
218
|
+
method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'QUERY';
|
|
219
|
+
path?: unknown;
|
|
220
|
+
query?: unknown;
|
|
221
|
+
params?: unknown;
|
|
222
|
+
cookies?: unknown;
|
|
223
|
+
body?: TBody;
|
|
224
|
+
headers?: unknown;
|
|
225
|
+
styles?: Styles;
|
|
226
|
+
signal?: AbortSignal;
|
|
227
|
+
credentials?: RequestCredentials;
|
|
228
|
+
options?: FetchOptions;
|
|
229
|
+
contentType?: ContentType;
|
|
230
|
+
responseType?: ResponseType;
|
|
231
|
+
throwOnError?: boolean;
|
|
232
|
+
client?: ClientInstance<TRequest, TResponse>;
|
|
233
|
+
transport?: Transport<TRequest, TResponse>;
|
|
234
|
+
serializer?: Serializers;
|
|
235
|
+
codecs?: Record<string, Codec>;
|
|
236
|
+
validator?: {
|
|
237
|
+
request?: Validator;
|
|
238
|
+
response?: Validator;
|
|
239
|
+
error?: Validator;
|
|
240
|
+
};
|
|
241
|
+
onValidationError?: ValidationErrorHandler;
|
|
242
|
+
security?: Array<Auth>;
|
|
243
|
+
auth?: AuthResolver;
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* The grouped options object passed to every generated function: the request config minus the
|
|
247
|
+
* data-shaped keys and the literal `url`, plus the per-operation `<Name>Request`.
|
|
248
|
+
*/
|
|
249
|
+
export type Options<TData extends DataShape, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = Omit<RequestConfig<unknown, TRequest, TResponse>, keyof DataShape | 'url'> & TData & {
|
|
250
|
+
client?: ClientInstance<TRequest, TResponse>;
|
|
251
|
+
throwOnError?: ThrowOnError;
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Client-level configuration shared by every call an instance makes, overridden by the per-call `RequestConfig`.
|
|
255
|
+
*/
|
|
256
|
+
export type ClientConfig<TRequest = Request, TResponse = Response> = {
|
|
257
|
+
baseURL?: string;
|
|
258
|
+
headers?: HeadersInit;
|
|
259
|
+
credentials?: RequestCredentials;
|
|
260
|
+
options?: FetchOptions;
|
|
261
|
+
throwOnError?: boolean;
|
|
262
|
+
transport?: Transport<TRequest, TResponse>;
|
|
263
|
+
serializer?: Serializers;
|
|
264
|
+
codecs?: Record<string, Codec>;
|
|
265
|
+
onValidationError?: ValidationErrorHandler;
|
|
266
|
+
auth?: AuthResolver;
|
|
267
|
+
};
|
|
268
|
+
/**
|
|
269
|
+
* The normalized request the transport receives, with all serialization, auth, and header work already done.
|
|
270
|
+
*/
|
|
271
|
+
export type ResolvedRequest = {
|
|
272
|
+
url: string;
|
|
273
|
+
method: string;
|
|
274
|
+
headers: Record<string, string>;
|
|
275
|
+
body?: RequestBody;
|
|
276
|
+
signal?: AbortSignal;
|
|
277
|
+
credentials?: RequestCredentials;
|
|
278
|
+
options?: FetchOptions;
|
|
279
|
+
responseType?: ResponseType;
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* What a transport returns: the parsed body plus the native request and response objects.
|
|
283
|
+
*/
|
|
284
|
+
export type TransportResult<TData = unknown, TRequest = Request, TResponse = Response> = {
|
|
285
|
+
data: TData;
|
|
286
|
+
status: number;
|
|
287
|
+
statusText: string;
|
|
288
|
+
headers: Headers;
|
|
289
|
+
contentType?: string;
|
|
290
|
+
request: TRequest;
|
|
291
|
+
response: TResponse;
|
|
292
|
+
};
|
|
293
|
+
/**
|
|
294
|
+
* The per-plugin send, supplied to `createClientCore` as `defaultTransport`.
|
|
295
|
+
*/
|
|
296
|
+
export type Transport<TRequest = Request, TResponse = Response> = (request: ResolvedRequest) => Promise<TransportResult<unknown, TRequest, TResponse>>;
|
|
297
|
+
/**
|
|
298
|
+
* The result a resolved call produces before it is cast to `RequestResult` by the generated wrapper.
|
|
299
|
+
*/
|
|
300
|
+
export type CallResult<TRequest = Request, TResponse = Response> = {
|
|
301
|
+
status: number;
|
|
302
|
+
data: unknown;
|
|
303
|
+
error: unknown;
|
|
304
|
+
contentType: string | undefined;
|
|
305
|
+
request: TRequest;
|
|
306
|
+
response: TResponse;
|
|
307
|
+
};
|
|
308
|
+
export type InterceptorFn<T, TContext = never> = (value: T, context?: TContext) => T | Promise<T>;
|
|
309
|
+
/**
|
|
310
|
+
* A single interceptor channel with a transport-agnostic `use` / `eject` / `update` API.
|
|
311
|
+
*/
|
|
312
|
+
export type InterceptorStack<T, TContext = never> = {
|
|
313
|
+
use: (fn: InterceptorFn<T, TContext>) => number;
|
|
314
|
+
eject: (id: number) => void;
|
|
315
|
+
update: (id: number, fn: InterceptorFn<T, TContext>) => void;
|
|
316
|
+
run: (value: T, context?: TContext) => Promise<T>;
|
|
317
|
+
};
|
|
318
|
+
/**
|
|
319
|
+
* The three interceptor channels every client instance exposes.
|
|
320
|
+
*/
|
|
321
|
+
export type Interceptors<TRequest = Request, TResponse = Response> = {
|
|
322
|
+
request: InterceptorStack<ResolvedRequest, RequestConfig<unknown, TRequest, TResponse>>;
|
|
323
|
+
response: InterceptorStack<TransportResult<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
|
|
324
|
+
error: InterceptorStack<ResponseError<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
|
|
325
|
+
};
|
|
326
|
+
/**
|
|
327
|
+
* A client instance: the callable send plus configuration, interceptors, and an isolated
|
|
328
|
+
* `createClient` factory bound to the same transport.
|
|
329
|
+
*/
|
|
330
|
+
export type ClientInstance<TRequest = Request, TResponse = Response> = {
|
|
331
|
+
<TBody = unknown>(config: RequestConfig<TBody, TRequest, TResponse>): Promise<CallResult<TRequest, TResponse>>;
|
|
332
|
+
getConfig: () => ClientConfig<TRequest, TResponse>;
|
|
333
|
+
setConfig: (config: ClientConfig<TRequest, TResponse>) => ClientConfig<TRequest, TResponse>;
|
|
334
|
+
getUrl: <TBody = unknown>(config: RequestConfig<TBody, TRequest, TResponse>) => string;
|
|
335
|
+
interceptors: Interceptors<TRequest, TResponse>;
|
|
336
|
+
createClient: (config?: ClientConfig<TRequest, TResponse>) => ClientInstance<TRequest, TResponse>;
|
|
337
|
+
};
|
|
338
|
+
/**
|
|
339
|
+
* Thrown for a non-2xx response, so a resolved call always means success.
|
|
340
|
+
*/
|
|
341
|
+
export declare class ResponseError<TError = unknown, TRequest = Request, TResponse = Response> extends Error {
|
|
342
|
+
data: TError;
|
|
343
|
+
status: number;
|
|
344
|
+
statusText: string;
|
|
345
|
+
contentType: string | undefined;
|
|
346
|
+
request: TRequest;
|
|
347
|
+
response: TResponse;
|
|
348
|
+
constructor(config: {
|
|
349
|
+
data: TError;
|
|
350
|
+
status: number;
|
|
351
|
+
statusText: string;
|
|
352
|
+
contentType?: string;
|
|
353
|
+
request: TRequest;
|
|
354
|
+
response: TResponse;
|
|
355
|
+
method?: string;
|
|
356
|
+
url?: string;
|
|
357
|
+
});
|
|
358
|
+
/**
|
|
359
|
+
* Matches on `name`, not `instanceof`: every generated client bundles its own `ResponseError` class.
|
|
360
|
+
*/
|
|
361
|
+
static is(error: unknown): error is ResponseError<unknown, unknown, unknown>;
|
|
362
|
+
}
|
|
363
|
+
export type ResponseErrorConfig<TError = unknown> = ResponseError<TError>;
|
|
364
|
+
/**
|
|
365
|
+
* Creates a transport-agnostic interceptor channel that runs interceptors in registration order.
|
|
366
|
+
*/
|
|
367
|
+
export declare function createInterceptorStack<T, TContext = never>(): InterceptorStack<T, TContext>;
|
|
368
|
+
/**
|
|
369
|
+
* Walks the per-operation security in order and places the first resolved token on the request, mutating `headers` / `query` in place.
|
|
370
|
+
*/
|
|
371
|
+
export declare function resolveAuth(params: {
|
|
372
|
+
security: Array<Auth> | undefined;
|
|
373
|
+
auth: AuthResolver | undefined;
|
|
374
|
+
headers: Record<string, string>;
|
|
375
|
+
query: Record<string, unknown>;
|
|
376
|
+
}): Promise<void>;
|
|
377
|
+
/**
|
|
378
|
+
* Builds the shared client core bound to a transport, exported by each plugin as `client` plus a `createClient` factory.
|
|
379
|
+
*/
|
|
380
|
+
export declare function createClientCore<TRequest = Request, TResponse = Response>(options: {
|
|
381
|
+
defaultTransport: Transport<TRequest, TResponse>;
|
|
382
|
+
} & ClientConfig<TRequest, TResponse>): ClientInstance<TRequest, TResponse>;
|
|
383
|
+
/**
|
|
384
|
+
* One decoded Server-Sent Event, with `data` parsed as JSON when valid and kept as the raw string otherwise.
|
|
385
|
+
*/
|
|
386
|
+
export type ServerSentEvent<TData = unknown> = {
|
|
387
|
+
data: TData;
|
|
388
|
+
event?: string;
|
|
389
|
+
id?: string;
|
|
390
|
+
retry?: number;
|
|
391
|
+
};
|
|
392
|
+
/**
|
|
393
|
+
* Parses a `text/event-stream` body into typed Server-Sent Events, consumed with `for await` and stopped early by breaking the loop.
|
|
394
|
+
*/
|
|
395
|
+
export declare function parseEventStream<TData = unknown>(stream: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>): AsyncGenerator<ServerSentEvent<TData>>;
|
|
396
|
+
/**
|
|
397
|
+
* The resolved shape returned by a generated `text/event-stream` operation: the typed event
|
|
398
|
+
* `stream` plus the native `response`.
|
|
399
|
+
*/
|
|
400
|
+
export type EventStreamResult<TData = unknown, TResponse = Response> = {
|
|
401
|
+
stream: AsyncGenerator<ServerSentEvent<TData>>;
|
|
402
|
+
response: TResponse;
|
|
403
|
+
};
|
|
404
|
+
/**
|
|
405
|
+
* Wraps a transport result whose `data` is a streaming body into an `EventStreamResult`, exposing
|
|
406
|
+
* the parsed events as a typed async iterator. Generated SSE operations call this.
|
|
407
|
+
*/
|
|
408
|
+
export declare function toEventStream<TData = unknown>(result: Promise<{
|
|
409
|
+
data: unknown;
|
|
410
|
+
response: Response;
|
|
411
|
+
}>): Promise<EventStreamResult<TData>>;
|
|
412
|
+
export declare const client: ClientInstance<Request, Response>;
|
|
413
|
+
export declare const createClient: (config?: Parameters<typeof client.createClient>[0]) => ClientInstance<Request, Response>;
|
|
414
|
+
//#endregion
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
//#region templates/serializers.d.ts
|
|
2
|
+
export type HeaderValue = string | number | boolean | null | undefined | object;
|
|
3
|
+
export type HeadersInit = Array<[string, HeaderValue]> | Record<string, HeaderValue>;
|
|
4
|
+
/**
|
|
5
|
+
* The request body type `fetch` accepts, derived from `RequestInit` instead of the global `BodyInit`
|
|
6
|
+
* name, which a Node-only project (`@types/node` without the `dom` lib) does not declare.
|
|
7
|
+
*/
|
|
8
|
+
export type RequestBody = NonNullable<RequestInit['body']>;
|
|
9
|
+
/**
|
|
10
|
+
* The OpenAPI query-parameter serialization style. `form` is the default; `spaceDelimited` and
|
|
11
|
+
* `pipeDelimited` join arrays with a space or pipe, and `deepObject` renders objects as
|
|
12
|
+
* `key[prop]=value`.
|
|
13
|
+
*/
|
|
14
|
+
export type QueryStyle = 'form' | 'spaceDelimited' | 'pipeDelimited' | 'deepObject';
|
|
15
|
+
/**
|
|
16
|
+
* The serialization metadata shared by the styled parameter locations: the OpenAPI `style` (typed per
|
|
17
|
+
* location through `TStyle`), `explode`, and `allowReserved` (keeps RFC 3986 reserved characters
|
|
18
|
+
* unencoded, used by query and request bodies).
|
|
19
|
+
*/
|
|
20
|
+
export type SerializationStyle<TStyle = string> = {
|
|
21
|
+
style?: TStyle;
|
|
22
|
+
explode?: boolean;
|
|
23
|
+
allowReserved?: boolean;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* The per-parameter query serialization metadata carried by the generated request.
|
|
27
|
+
*/
|
|
28
|
+
export type QueryParamStyle = SerializationStyle<QueryStyle>;
|
|
29
|
+
/**
|
|
30
|
+
* Serializes the query object into a search string. The optional second argument carries the
|
|
31
|
+
* per-parameter OpenAPI `style` / `explode` / `allowReserved` metadata; without it arrays explode
|
|
32
|
+
* into repeated keys and nested objects use the `deepObject` style.
|
|
33
|
+
*/
|
|
34
|
+
export type QuerySerializer = (params: Record<string, unknown>, options?: Record<string, QueryParamStyle>) => string;
|
|
35
|
+
/**
|
|
36
|
+
* The per-parameter cookie serialization metadata carried by the generated request. Cookies use the
|
|
37
|
+
* OpenAPI `form` style, so only `explode` is configurable.
|
|
38
|
+
*/
|
|
39
|
+
export type CookieParamStyle = {
|
|
40
|
+
explode?: boolean;
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* The per-parameter header serialization metadata carried by the generated request. Headers use the
|
|
44
|
+
* OpenAPI `simple` style, so only `explode` is configurable.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // styles.header: { 'X-Ids': { explode: false } }, header [3, 4] -> 'X-Ids: 3,4'
|
|
49
|
+
* // styles.header: { 'X-Filter': { explode: true } }, header { role: 'admin' } -> 'X-Filter: role=admin'
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export type HeaderParamStyle = {
|
|
53
|
+
explode?: boolean;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* The per-property `encoding` metadata for an `application/x-www-form-urlencoded` or
|
|
57
|
+
* `multipart/form-data` request body. `contentType` overrides the part's media type; `style` /
|
|
58
|
+
* `explode` / `allowReserved` follow the OpenAPI query rules for urlencoded bodies.
|
|
59
|
+
*/
|
|
60
|
+
export type BodyEncoding = SerializationStyle<QueryStyle> & {
|
|
61
|
+
contentType?: string;
|
|
62
|
+
};
|
|
63
|
+
/**
|
|
64
|
+
* Serializes the request body. JSON by default; `FormData`, `URLSearchParams`, `Blob`,
|
|
65
|
+
* `ArrayBuffer`, and string bodies pass through untouched. The optional `encoding` argument carries
|
|
66
|
+
* the per-property OpenAPI `encoding` metadata for form bodies.
|
|
67
|
+
*/
|
|
68
|
+
export type BodySerializer = (args: {
|
|
69
|
+
body: unknown;
|
|
70
|
+
contentType?: string;
|
|
71
|
+
encoding?: Record<string, BodyEncoding>;
|
|
72
|
+
}) => RequestBody | undefined;
|
|
73
|
+
/**
|
|
74
|
+
* The OpenAPI path-parameter serialization style. `simple` is the default and emits the bare value;
|
|
75
|
+
* `label` prefixes a `.` and `matrix` prefixes a `;name=` segment.
|
|
76
|
+
*/
|
|
77
|
+
export type PathStyle = 'simple' | 'label' | 'matrix';
|
|
78
|
+
/**
|
|
79
|
+
* The per-parameter serialization metadata carried by the generated request. `style` selects the
|
|
80
|
+
* OpenAPI style and `explode` controls how arrays and objects expand.
|
|
81
|
+
*/
|
|
82
|
+
export type PathParamStyle = SerializationStyle<PathStyle>;
|
|
83
|
+
/**
|
|
84
|
+
* Serializes a single path parameter for interpolation into the URL, honoring the OpenAPI `style` /
|
|
85
|
+
* `explode` passed as `options`. Defaults to `simple` style with `explode: false`: primitives are
|
|
86
|
+
* URL-encoded, arrays join their members with commas, and objects flatten to `key,value` pairs.
|
|
87
|
+
*/
|
|
88
|
+
export type PathSerializer = (args: {
|
|
89
|
+
name: string;
|
|
90
|
+
value: unknown;
|
|
91
|
+
options?: PathParamStyle;
|
|
92
|
+
}) => string;
|
|
93
|
+
/**
|
|
94
|
+
* The per-concern serializers, grouped so they can be set in one place and overridden per client or
|
|
95
|
+
* per call. Each field falls back to the matching `default*Serializer` when omitted.
|
|
96
|
+
*/
|
|
97
|
+
export type Serializers = {
|
|
98
|
+
query?: QuerySerializer;
|
|
99
|
+
body?: BodySerializer;
|
|
100
|
+
path?: PathSerializer;
|
|
101
|
+
};
|
|
102
|
+
/**
|
|
103
|
+
* The per-parameter OpenAPI `style` / `explode` metadata a generated request carries, grouped by
|
|
104
|
+
* location and keyed by parameter name. Mirrors the `serializer` grouping and feeds the default
|
|
105
|
+
* serializers; `body` carries the form `encoding` for a urlencoded or multipart body.
|
|
106
|
+
*/
|
|
107
|
+
export type Styles = {
|
|
108
|
+
path?: Record<string, PathParamStyle>;
|
|
109
|
+
query?: Record<string, QueryParamStyle>;
|
|
110
|
+
header?: Record<string, HeaderParamStyle>;
|
|
111
|
+
cookie?: Record<string, CookieParamStyle>;
|
|
112
|
+
body?: Record<string, BodyEncoding>;
|
|
113
|
+
};
|
|
114
|
+
export declare function isDefaultJsonBody(body: unknown): boolean;
|
|
115
|
+
/**
|
|
116
|
+
* Default body serializer: passes binary/form bodies through and JSON-serializes everything else.
|
|
117
|
+
* For `multipart/form-data` plain objects become `FormData` and for
|
|
118
|
+
* `application/x-www-form-urlencoded` they become `URLSearchParams`. When `encoding` is supplied each
|
|
119
|
+
* urlencoded property follows its OpenAPI `style` / `explode` / `allowReserved`.
|
|
120
|
+
*
|
|
121
|
+
* @example
|
|
122
|
+
* ```ts
|
|
123
|
+
* defaultBodySerializer({ body: { name: 'odie' } }) // '{"name":"odie"}'
|
|
124
|
+
* defaultBodySerializer({ body: { field: 'x' }, contentType: 'multipart/form-data' }) // FormData
|
|
125
|
+
* defaultBodySerializer({ body: { tags: ['a', 'b'] }, contentType: 'application/x-www-form-urlencoded', encoding: { tags: { explode: false } } }) // 'tags=a,b'
|
|
126
|
+
* defaultBodySerializer({ body: { meta: { a: 1 } }, contentType: 'multipart/form-data', encoding: { meta: { contentType: 'application/json' } } }) // FormData with a typed Blob part
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
export declare const defaultBodySerializer: BodySerializer;
|
|
130
|
+
/**
|
|
131
|
+
* Serializes cookie parameters into a `Cookie` header value using the OpenAPI `form` style, joined
|
|
132
|
+
* with `; `. Values are URL-encoded and `explode` is honored per parameter.
|
|
133
|
+
*
|
|
134
|
+
* @example
|
|
135
|
+
* ```ts
|
|
136
|
+
* serializeCookies({ session: 'abc', ids: [1, 2] }) // 'session=abc; ids=1,2'
|
|
137
|
+
* serializeCookies({ ids: [1, 2] }, { ids: { explode: true } }) // 'ids=1; ids=2'
|
|
138
|
+
* ```
|
|
139
|
+
*/
|
|
140
|
+
export declare function serializeCookies(cookies: Record<string, unknown>, styles?: Record<string, CookieParamStyle>): string;
|
|
141
|
+
/**
|
|
142
|
+
* Default query serializer. Members with `options` metadata follow their OpenAPI `style` / `explode`
|
|
143
|
+
* / `allowReserved`. Members without it keep the defaults: arrays explode into repeated keys and
|
|
144
|
+
* nested objects use the `deepObject` style (`key[prop]=value`).
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* ```ts
|
|
148
|
+
* defaultQuerySerializer({ id: [3, 4, 5] }) // 'id=3&id=4&id=5'
|
|
149
|
+
* defaultQuerySerializer({ id: [3, 4, 5] }, { id: { style: 'form', explode: false } }) // 'id=3,4,5'
|
|
150
|
+
* defaultQuerySerializer({ id: [3, 4, 5] }, { id: { style: 'spaceDelimited', explode: false } }) // 'id=3%204%205'
|
|
151
|
+
* defaultQuerySerializer({ id: [3, 4, 5] }, { id: { style: 'pipeDelimited', explode: false } }) // 'id=3|4|5'
|
|
152
|
+
* defaultQuerySerializer({ a: { b: 1 } }, { a: { style: 'deepObject' } }) // 'a%5Bb%5D=1'
|
|
153
|
+
* defaultQuerySerializer({ a: { b: { c: 1 } } }, { a: { style: 'deepObject' } }) // 'a%5Bb%5D%5Bc%5D=1'
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare const defaultQuerySerializer: QuerySerializer;
|
|
157
|
+
/**
|
|
158
|
+
* Default path serializer honoring the OpenAPI `style` / `explode` metadata. Without metadata it
|
|
159
|
+
* falls back to `simple` style with `explode: false`. Replaces the previous `String(value)`
|
|
160
|
+
* interpolation, which emitted `[object Object]` for object path params.
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* defaultPathSerializer({ name: 'id', value: [3, 4, 5] }) // '3,4,5'
|
|
165
|
+
* defaultPathSerializer({ name: 'id', value: [3, 4, 5], options: { style: 'label', explode: true } }) // '.3.4.5'
|
|
166
|
+
* defaultPathSerializer({ name: 'id', value: [3, 4, 5], options: { style: 'matrix', explode: true } }) // ';id=3;id=4;id=5'
|
|
167
|
+
* defaultPathSerializer({ name: 'pt', value: { x: 1, y: 2 } }) // 'x,1,y,2'
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
export declare const defaultPathSerializer: PathSerializer;
|
|
171
|
+
/**
|
|
172
|
+
* Serializes array and object header parameters with the OpenAPI `simple` style before they are
|
|
173
|
+
* merged. Header values are not URL-encoded. Primitive values and headers without metadata pass
|
|
174
|
+
* through untouched.
|
|
175
|
+
*/
|
|
176
|
+
export declare function applyHeaderStyles(headers: HeadersInit | undefined, styles: Record<string, HeaderParamStyle> | undefined): HeadersInit | undefined;
|
|
177
|
+
//#endregion
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
//#region templates/standardSchema.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* A Standard Schema-compatible validator: a minimal duck-type covering Zod v3/v4, valibot, and
|
|
4
|
+
* arktype schemas. Only the `~standard.validate` method is required at runtime.
|
|
5
|
+
*/
|
|
6
|
+
export type StandardSchemaValidator<TOutput = unknown> = {
|
|
7
|
+
readonly '~standard': {
|
|
8
|
+
validate(value: unknown): StandardSchemaResult<TOutput> | Promise<StandardSchemaResult<TOutput>>;
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* The two possible outcomes of a Standard Schema `validate` call. A successful result carries
|
|
13
|
+
* `value`; a failed result carries `issues`.
|
|
14
|
+
*/
|
|
15
|
+
export type StandardSchemaResult<TOutput> = {
|
|
16
|
+
readonly value: TOutput;
|
|
17
|
+
readonly issues?: undefined;
|
|
18
|
+
} | {
|
|
19
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* One validation issue from a Standard Schema `validate` call.
|
|
23
|
+
*/
|
|
24
|
+
export type StandardSchemaIssue = {
|
|
25
|
+
readonly message?: string;
|
|
26
|
+
readonly path?: ReadonlyArray<PropertyKey | {
|
|
27
|
+
readonly key: PropertyKey;
|
|
28
|
+
}>;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Thrown by `validateStandardSchema` when validation fails. Carries the raw `issues` array from
|
|
32
|
+
* the schema's `validate` result so callers receive a uniform error shape regardless of which
|
|
33
|
+
* schema library is in use.
|
|
34
|
+
*/
|
|
35
|
+
export declare class ParseError extends Error {
|
|
36
|
+
readonly issues: ReadonlyArray<StandardSchemaIssue>;
|
|
37
|
+
constructor({ issues, message }: {
|
|
38
|
+
issues: ReadonlyArray<StandardSchemaIssue>;
|
|
39
|
+
message?: string;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Validates `value` against a Standard Schema-compatible `schema`. Returns the parsed output on
|
|
44
|
+
* success; throws `ParseError` with the schema's `issues` on failure. Handles both sync and async
|
|
45
|
+
* `validate` implementations transparently.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* const pet = await validateStandardSchema(PetSchema, rawData)
|
|
49
|
+
*/
|
|
50
|
+
export declare function validateStandardSchema<TOutput>(schema: StandardSchemaValidator<TOutput>, value: unknown): Promise<TOutput>;
|
|
51
|
+
//#endregion
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/plugin-fetch",
|
|
3
|
-
"version": "5.6.
|
|
3
|
+
"version": "5.6.1",
|
|
4
4
|
"description": "Generate a slim, type-safe HTTP client with Kubb based on the native Fetch api.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"api-client",
|
|
@@ -61,6 +61,13 @@
|
|
|
61
61
|
},
|
|
62
62
|
"./package.json": "./package.json",
|
|
63
63
|
"./kubb-extension.json": "./kubb-extension.json",
|
|
64
|
+
"./templates/*.ts": {
|
|
65
|
+
"types": "./dist/templates/*.d.ts",
|
|
66
|
+
"default": "./templates/*.ts"
|
|
67
|
+
},
|
|
68
|
+
"./templates/*.js": {
|
|
69
|
+
"types": "./dist/templates/*.d.ts"
|
|
70
|
+
},
|
|
64
71
|
"./templates/*": "./templates/*"
|
|
65
72
|
},
|
|
66
73
|
"publishConfig": {
|
|
@@ -68,12 +75,12 @@
|
|
|
68
75
|
"registry": "https://registry.npmjs.org/"
|
|
69
76
|
},
|
|
70
77
|
"dependencies": {
|
|
71
|
-
"@kubb/plugin-ts": "^5.
|
|
72
|
-
"@kubb/plugin-zod": "^5.
|
|
78
|
+
"@kubb/plugin-ts": "^5.0.11",
|
|
79
|
+
"@kubb/plugin-zod": "^5.4.9"
|
|
73
80
|
},
|
|
74
81
|
"devDependencies": {
|
|
75
|
-
"@internals/client": "0.
|
|
76
|
-
"@internals/shared": "0.
|
|
82
|
+
"@internals/client": "0.0.0",
|
|
83
|
+
"@internals/shared": "0.0.0",
|
|
77
84
|
"kubb": "^5.9.2",
|
|
78
85
|
"typescript": "^6.0.3"
|
|
79
86
|
},
|
package/templates/fetch.ts
CHANGED
|
@@ -215,7 +215,7 @@ export type FetchOptions = RequestInit & { next?: Record<string, unknown> }
|
|
|
215
215
|
export type RequestConfig<TBody = unknown, TRequest = Request, TResponse = Response> = {
|
|
216
216
|
baseURL?: string
|
|
217
217
|
url?: string
|
|
218
|
-
method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD'
|
|
218
|
+
method?: 'GET' | 'PUT' | 'PATCH' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'QUERY'
|
|
219
219
|
path?: unknown
|
|
220
220
|
query?: unknown
|
|
221
221
|
params?: unknown
|