@kubb/plugin-fetch 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/fetch.d.ts +25 -56
- package/docs/options.md +1 -1
- package/package.json +5 -5
- package/templates/fetch.ts +97 -190
- package/templates/serializers.ts +13 -5
|
@@ -5,27 +5,21 @@ import { ParseError, StandardSchemaValidator } from "./standardSchema.js";
|
|
|
5
5
|
* HTTP status codes treated as a success, everything else is an error.
|
|
6
6
|
*/
|
|
7
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
8
|
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
9
|
export type ErrorOf<TResponses> = TResponses[Exclude<keyof TResponses, SuccessStatusCode>];
|
|
16
10
|
/**
|
|
17
11
|
* Converts a response record's string status key to its numeric literal, leaving non-numeric keys like `default` as `number`.
|
|
18
12
|
*/
|
|
19
13
|
export type ToStatusNumber<TStatus> = TStatus extends `${infer TNumber extends number}` ? TNumber : number;
|
|
20
14
|
/**
|
|
21
|
-
*
|
|
15
|
+
* Unwraps content-type variants to keep the bare body on `error`.
|
|
22
16
|
*/
|
|
23
17
|
export type DataOf<T> = T extends {
|
|
24
18
|
contentType: string;
|
|
25
19
|
data: infer TData;
|
|
26
20
|
} ? TData : T;
|
|
27
21
|
/**
|
|
28
|
-
*
|
|
22
|
+
* Keeps `contentType` beside `data` so switching on it narrows the body.
|
|
29
23
|
*/
|
|
30
24
|
export type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extends {
|
|
31
25
|
contentType: string;
|
|
@@ -49,7 +43,7 @@ export type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extend
|
|
|
49
43
|
response: TResponse;
|
|
50
44
|
};
|
|
51
45
|
/**
|
|
52
|
-
*
|
|
46
|
+
* Narrows `data` or `error` when switching on the numeric status.
|
|
53
47
|
*/
|
|
54
48
|
export type ResultByStatus<TResponses, TStatus extends keyof TResponses, TRequest, TResponse> = TStatus extends SuccessStatusCode ? SuccessVariant<TStatus, TResponses[TStatus], TRequest, TResponse> : {
|
|
55
49
|
status: ToStatusNumber<TStatus>;
|
|
@@ -59,16 +53,13 @@ export type ResultByStatus<TResponses, TStatus extends keyof TResponses, TReques
|
|
|
59
53
|
request: TRequest;
|
|
60
54
|
response: TResponse;
|
|
61
55
|
};
|
|
62
|
-
/**
|
|
63
|
-
* The union of every documented status' result variant.
|
|
64
|
-
*/
|
|
65
56
|
export type ResultUnion<TResponses, TRequest, TResponse> = { [TStatus in keyof TResponses]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[keyof TResponses];
|
|
66
57
|
/**
|
|
67
|
-
*
|
|
58
|
+
* Selects success statuses without letting untyped error payloads widen `data`.
|
|
68
59
|
*/
|
|
69
60
|
export type SuccessResultUnion<TResponses, TRequest, TResponse> = { [TStatus in Extract<keyof TResponses, SuccessStatusCode>]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[Extract<keyof TResponses, SuccessStatusCode>];
|
|
70
61
|
/**
|
|
71
|
-
*
|
|
62
|
+
* Narrows `data` and `error` by status. With `throwOnError`, includes only success variants.
|
|
72
63
|
*/
|
|
73
64
|
export type RequestResult<TResponses, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = ThrowOnError extends true ? [SuccessResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
|
|
74
65
|
status: number;
|
|
@@ -99,11 +90,15 @@ export type Unwrappable<T extends {
|
|
|
99
90
|
/**
|
|
100
91
|
* Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one.
|
|
101
92
|
*
|
|
102
|
-
* @example
|
|
103
|
-
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* const { data, error } = await getPetById({ path: { petId: 1 } })
|
|
96
|
+
* ```
|
|
104
97
|
*
|
|
105
|
-
* @example
|
|
106
|
-
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const pet = await getPetById({ path: { petId: 1 } }).unwrap()
|
|
101
|
+
* ```
|
|
107
102
|
*/
|
|
108
103
|
export declare function withUnwrap<T extends {
|
|
109
104
|
data: unknown;
|
|
@@ -116,15 +111,11 @@ export declare function withUnwrap<T extends {
|
|
|
116
111
|
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
112
|
/**
|
|
118
113
|
* Returns the success body when `throwOnError` is true, or the full result when it is false.
|
|
119
|
-
* Backs generated operations with `returnType: 'data'`.
|
|
120
114
|
*/
|
|
121
115
|
export declare function unwrapResult<T extends {
|
|
122
116
|
data: unknown;
|
|
123
117
|
error: unknown;
|
|
124
118
|
}>(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
119
|
export type DataShape = {
|
|
129
120
|
body?: unknown;
|
|
130
121
|
cookies?: unknown;
|
|
@@ -135,41 +126,33 @@ export type DataShape = {
|
|
|
135
126
|
export type RequestCredentials = 'omit' | 'same-origin' | 'include';
|
|
136
127
|
export type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream';
|
|
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) => RequestBody | undefined;
|
|
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
|
/**
|
|
@@ -213,9 +194,6 @@ export type AuthResolver = AuthToken | ((auth: Auth) => AuthToken | Promise<Auth
|
|
|
213
194
|
export type FetchOptions = RequestInit & {
|
|
214
195
|
next?: Record<string, unknown>;
|
|
215
196
|
};
|
|
216
|
-
/**
|
|
217
|
-
* The request a generated function hands to the runtime, with `body` / `headers` / `path` / `query` from the grouped options.
|
|
218
|
-
*/
|
|
219
197
|
export type RequestConfig<TBody = unknown, TRequest = Request, TResponse = Response> = {
|
|
220
198
|
baseURL?: string;
|
|
221
199
|
url?: string;
|
|
@@ -250,8 +228,7 @@ export type RequestConfig<TBody = unknown, TRequest = Request, TResponse = Respo
|
|
|
250
228
|
auth?: AuthResolver;
|
|
251
229
|
};
|
|
252
230
|
/**
|
|
253
|
-
*
|
|
254
|
-
* data-shaped keys and the literal `url`, plus the per-operation `<Name>Request`.
|
|
231
|
+
* Combines client options with the operation's typed request params and body.
|
|
255
232
|
*/
|
|
256
233
|
export type Options<TData extends DataShape, ThrowOnError extends boolean = true, TRequest = Request, TResponse = Response> = Omit<RequestConfig<unknown, TRequest, TResponse>, keyof DataShape | 'url'> & TData & {
|
|
257
234
|
client?: ClientInstance<TRequest, TResponse>;
|
|
@@ -301,9 +278,6 @@ export type TransportResult<TData = unknown, TRequest = Request, TResponse = Res
|
|
|
301
278
|
* The per-plugin send, supplied to `createClientCore` as `defaultTransport`.
|
|
302
279
|
*/
|
|
303
280
|
export type Transport<TRequest = Request, TResponse = Response> = (request: ResolvedRequest) => Promise<TransportResult<unknown, TRequest, TResponse>>;
|
|
304
|
-
/**
|
|
305
|
-
* The result a resolved call produces before it is cast to `RequestResult` by the generated wrapper.
|
|
306
|
-
*/
|
|
307
281
|
export type CallResult<TRequest = Request, TResponse = Response> = {
|
|
308
282
|
status: number;
|
|
309
283
|
data: unknown;
|
|
@@ -322,9 +296,6 @@ export type InterceptorStack<T, TContext = never> = {
|
|
|
322
296
|
update: (id: number, fn: InterceptorFn<T, TContext>) => void;
|
|
323
297
|
run: (value: T, context?: TContext) => Promise<T>;
|
|
324
298
|
};
|
|
325
|
-
/**
|
|
326
|
-
* The three interceptor channels every client instance exposes.
|
|
327
|
-
*/
|
|
328
299
|
export type Interceptors<TRequest = Request, TResponse = Response> = {
|
|
329
300
|
request: InterceptorStack<ResolvedRequest, RequestConfig<unknown, TRequest, TResponse>>;
|
|
330
301
|
response: InterceptorStack<TransportResult<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
|
|
@@ -383,7 +354,7 @@ export declare function resolveAuth(params: {
|
|
|
383
354
|
query: Record<string, unknown>;
|
|
384
355
|
}): Promise<void>;
|
|
385
356
|
/**
|
|
386
|
-
*
|
|
357
|
+
* Creates an isolated client using the supplied default transport.
|
|
387
358
|
*/
|
|
388
359
|
export declare function createClientCore<TRequest = Request, TResponse = Response>(options: {
|
|
389
360
|
defaultTransport: Transport<TRequest, TResponse>;
|
|
@@ -406,16 +377,14 @@ export type ServerSentEvent<TData = unknown> = {
|
|
|
406
377
|
*/
|
|
407
378
|
export declare function parseEventStream<TData = unknown>(stream: ReadableStream<Uint8Array> | AsyncIterable<Uint8Array>): AsyncGenerator<ServerSentEvent<TData>>;
|
|
408
379
|
/**
|
|
409
|
-
*
|
|
410
|
-
* `stream` plus the native `response`.
|
|
380
|
+
* Returns parsed events alongside the native response.
|
|
411
381
|
*/
|
|
412
382
|
export type EventStreamResult<TData = unknown, TResponse = Response> = {
|
|
413
383
|
stream: AsyncGenerator<ServerSentEvent<TData>>;
|
|
414
384
|
response: TResponse;
|
|
415
385
|
};
|
|
416
386
|
/**
|
|
417
|
-
*
|
|
418
|
-
* the parsed events as a typed async iterator. Generated SSE operations call this.
|
|
387
|
+
* Exposes a streaming response body as typed Server-Sent Events.
|
|
419
388
|
*/
|
|
420
389
|
export declare function toEventStream<TData = unknown>(result: Promise<{
|
|
421
390
|
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-fetch",
|
|
3
|
-
"version": "5.9.
|
|
3
|
+
"version": "5.9.2",
|
|
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",
|
|
@@ -80,17 +80,17 @@
|
|
|
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
|
-
"kubb": "^5.
|
|
89
|
+
"kubb": "^5.14.0",
|
|
90
90
|
"typescript": "^7.0.2"
|
|
91
91
|
},
|
|
92
92
|
"peerDependencies": {
|
|
93
|
-
"kubb": "^5.
|
|
93
|
+
"kubb": "^5.14.0"
|
|
94
94
|
},
|
|
95
95
|
"engines": {
|
|
96
96
|
"node": ">=22.22"
|