@kubb/plugin-client 5.12.0 → 6.0.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/README.md +1 -1
- package/dist/client-Dq0z61hu.d.ts +554 -0
- package/dist/index.cjs +1378 -784
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +114 -74
- package/dist/index.d.ts +114 -74
- package/dist/index.js +1376 -786
- package/dist/index.js.map +1 -1
- package/dist/serializers-BRgfPGr1.d.ts +119 -0
- package/dist/{standardSchema-BShJWz2i.d.ts → standardSchema-CGuiSK7F.d.ts} +15 -37
- package/dist/templates/client.d.ts +3 -2
- package/dist/templates/core.d.ts +3 -3
- package/dist/templates/mediaTypes/formData.d.ts +5 -0
- package/dist/templates/mediaTypes/urlencoded.d.ts +5 -0
- package/dist/templates/mediaTypes.d.ts +2 -0
- package/dist/templates/serializers/cookies.d.ts +5 -0
- package/dist/templates/serializers/headers.d.ts +5 -0
- package/dist/templates/serializers/path.d.ts +5 -0
- package/dist/templates/serializers/pathStyles.d.ts +5 -0
- package/dist/templates/serializers/query.d.ts +5 -0
- package/dist/templates/serializers/queryStyles.d.ts +14 -0
- package/dist/templates/serializers.d.ts +2 -2
- package/dist/templates/sse.d.ts +20 -113
- package/dist/templates/standardSchema.d.ts +1 -1
- package/docs/guides/authentication.md +9 -1
- package/docs/guides/migrate-to-v6.md +139 -0
- package/docs/guides/validate-requests-and-responses.md +8 -3
- package/docs/guides/write-your-client.md +117 -11
- package/docs/options.md +2 -2
- package/docs/overview.md +1 -1
- package/kubb-extension.json +1 -1
- package/package.json +6 -5
- package/src/builders/callConfig.ts +80 -0
- package/src/builders/contentType.ts +35 -0
- package/src/builders/generics.ts +29 -0
- package/src/builders/operationBody.ts +43 -0
- package/src/builders/operationData.ts +64 -0
- package/src/builders/returnStatement.ts +103 -0
- package/src/builders/security.ts +125 -0
- package/src/builders/signature.ts +71 -0
- package/src/builders/styles.ts +94 -0
- package/src/builders/validator.ts +84 -0
- package/src/builders/validatorOptions.ts +53 -0
- package/src/clientTypes.ts +195 -0
- package/src/components/ClientRuntime.tsx +97 -0
- package/src/components/Operation.tsx +72 -0
- package/src/components/SdkClient.tsx +100 -0
- package/src/components/SdkFacade.tsx +35 -0
- package/src/generators/clientGenerator.tsx +91 -6
- package/src/generators/sdkGenerator.tsx +209 -0
- package/src/index.ts +3 -1
- package/src/macros.ts +9 -0
- package/src/plugin.ts +7 -8
- package/src/resolver.ts +28 -0
- package/src/setup.ts +90 -0
- package/src/types.ts +4 -2
- package/templates/client.ts +2 -1
- package/templates/core.ts +735 -286
- package/templates/mediaTypes/formData.ts +59 -0
- package/templates/mediaTypes/urlencoded.ts +33 -0
- package/templates/mediaTypes.ts +147 -0
- package/templates/serializers/cookies.ts +58 -0
- package/templates/serializers/headers.ts +35 -0
- package/templates/serializers/path.ts +19 -0
- package/templates/serializers/pathStyles.ts +27 -0
- package/templates/serializers/query.ts +21 -0
- package/templates/serializers/queryStyles.ts +107 -0
- package/templates/serializers.ts +48 -364
- package/templates/sse.ts +87 -178
- package/templates/standardSchema.ts +16 -37
- package/dist/client-BxO6Dygu.d.ts +0 -355
- package/dist/serializers-Ds8zZwOO.d.ts +0 -190
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ pluginClient({ importPath: '../api/client' })
|
|
|
10
10
|
|
|
11
11
|
`importPath` is written into each generated import as-is, so it must resolve from the generated operation file (for example `../../../client`, or a package or alias specifier). The module must export `client`, plus the `Options` and `RequestResult` types. Generated operations call `client` with a config containing `method`, `url`, and the operation options, and return its promise typed as `RequestResult`. There are no `withUnwrap` or `unwrapResult` helpers to write, and `returnType` is not an option.
|
|
12
12
|
|
|
13
|
-
If your spec has server-sent event operations, also
|
|
13
|
+
If your spec has server-sent event or JSON Lines operations, `client` also needs a `stream` method, and the module exports the `StreamResult`, `SuccessOf`, `TypedEvent`, and `ServerSentEvent` types. A generated function calls `client.stream<TResponses, ThrowOnError, TItem>(config)` and returns its `StreamResult`. `TItem` is `TypedEvent<…>` when the document names each event, and `ServerSentEvent<…>` when it types only the payload. The call keeps the response validator on `config.validator.response`, so your module decides how to validate items.
|
|
14
14
|
|
|
15
15
|
Set the base URL, auth, retries, and any other behavior inside your `client`. `sdk`, `returnType`, and `baseURL` are not options of this plugin. `validator: 'zod'` needs `pluginZod()` in the plugins, and generation stops with an error if it is missing. Your `client` receives the schemas on `config.validator` (`{ request?, response?, error? }`), so add that field to your `RequestConfig` and run them yourself.
|
|
16
16
|
|
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
import { c as HttpMethod, g as RequestBody, s as HeadersInit, v as Serializers, y as Styles } from "./serializers-BRgfPGr1.js";
|
|
2
|
+
import { a as ValidateOption, i as StandardSchemaValidator, s as ValidationErrorHandler } from "./standardSchema-CGuiSK7F.js";
|
|
3
|
+
/**
|
|
4
|
+
* What a media type handler gets next to the body.
|
|
5
|
+
*/
|
|
6
|
+
type MediaTypeContext = {
|
|
7
|
+
contentType: string;
|
|
8
|
+
/**
|
|
9
|
+
* The response status, or `undefined` while a request body is encoded.
|
|
10
|
+
*/
|
|
11
|
+
status?: number | undefined;
|
|
12
|
+
signal?: AbortSignal | undefined;
|
|
13
|
+
config: Omit<RequestConfig, 'client' | 'transport'> & EventStreamOptions;
|
|
14
|
+
/**
|
|
15
|
+
* Where a stream records the last event ID it read, for `StreamResult.lastEventId`.
|
|
16
|
+
*/
|
|
17
|
+
cursor?: {
|
|
18
|
+
lastEventId: string;
|
|
19
|
+
} | undefined;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Encodes and decodes bodies of one media type. Every method is optional.
|
|
23
|
+
*/
|
|
24
|
+
type MediaType = {
|
|
25
|
+
encode?: (body: unknown, context: MediaTypeContext) => RequestBody | undefined;
|
|
26
|
+
decode?: (response: Response, context: MediaTypeContext) => unknown;
|
|
27
|
+
/**
|
|
28
|
+
* Reads a 2xx body item by item. Each item is validated with `validator.response`.
|
|
29
|
+
*/
|
|
30
|
+
stream?: (body: ReadableStream<Uint8Array>, context: MediaTypeContext) => AsyncIterable<unknown>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Handlers keyed by media type. A key may be a pattern such as `application/*+json` or `text/*`.
|
|
34
|
+
*/
|
|
35
|
+
type MediaTypes = Record<string, MediaType>;
|
|
36
|
+
/**
|
|
37
|
+
* Picks the handler for a `Content-Type`: the exact type, then its `+suffix`, then `type/*`, then the catch-all. With `use`, a
|
|
38
|
+
* handler without that method is skipped.
|
|
39
|
+
*/
|
|
40
|
+
declare function resolveMediaType({ mediaTypes, contentType, use }: {
|
|
41
|
+
mediaTypes: MediaTypes;
|
|
42
|
+
contentType: string | null | undefined;
|
|
43
|
+
use?: keyof MediaType;
|
|
44
|
+
}): MediaType | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* Whether a body is already encoded, so every built-in media type sends it as is.
|
|
47
|
+
*/
|
|
48
|
+
declare function isEncodedBody(body: unknown): body is RequestBody;
|
|
49
|
+
/**
|
|
50
|
+
* Parses JSON text, throwing a `ParseError` that names the content type when it is not valid JSON.
|
|
51
|
+
*/
|
|
52
|
+
declare function parseJson(text: string, contentType: string | undefined): unknown;
|
|
53
|
+
/**
|
|
54
|
+
* The media types every client knows: JSON, text, binary, and a catch-all that sends JSON and reads JSON or text.
|
|
55
|
+
*/
|
|
56
|
+
declare const defaultMediaTypes: MediaTypes;
|
|
57
|
+
/**
|
|
58
|
+
* The status keys that count as success. `2xx` covers every 2xx status.
|
|
59
|
+
*/
|
|
60
|
+
type SuccessStatusCode = '200' | '201' | '202' | '203' | '204' | '205' | '206' | '207' | '208' | '226' | '2xx';
|
|
61
|
+
type SuccessOf<TResponses> = TResponses[Extract<keyof TResponses, SuccessStatusCode>];
|
|
62
|
+
type ErrorOf<TResponses> = TResponses[Exclude<keyof TResponses, SuccessStatusCode>];
|
|
63
|
+
type Digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
|
|
64
|
+
type ToNumber<T> = T extends `${infer TNumber extends number}` ? TNumber : never;
|
|
65
|
+
/**
|
|
66
|
+
* Maps a status key such as `404` or `4xx` to its numeric statuses. `default` becomes `number`.
|
|
67
|
+
*/
|
|
68
|
+
type ToStatusNumber<TStatus, TKeys = never> = TStatus extends `${infer TNumber extends number}` ? TNumber : TStatus extends `${infer TFirst extends '1' | '2' | '3' | '4' | '5'}xx` ? Exclude<ToNumber<`${TFirst}${Digit}${Digit}`>, ToNumber<TKeys>> : number;
|
|
69
|
+
/**
|
|
70
|
+
* The `error` of a failed call without a body, so `if (error)` still sees the failure.
|
|
71
|
+
*/
|
|
72
|
+
type EmptyErrorBody = {
|
|
73
|
+
status: number;
|
|
74
|
+
statusText: string;
|
|
75
|
+
};
|
|
76
|
+
type DataOf<T> = T extends {
|
|
77
|
+
contentType: string;
|
|
78
|
+
data: infer TData;
|
|
79
|
+
} ? TData : T;
|
|
80
|
+
/**
|
|
81
|
+
* A response's `Headers`, whose `get` suggests the header names the spec declares. Values stay strings, as sent.
|
|
82
|
+
*/
|
|
83
|
+
type ResponseHeaders<THeaders = unknown> = Omit<Headers, 'get'> & {
|
|
84
|
+
get: (name: Extract<keyof THeaders, string> | (string & {})) => string | null;
|
|
85
|
+
};
|
|
86
|
+
type StreamContentType = `text/event-stream${string}` | `application/jsonl${string}` | `application/x-ndjson${string}` | `application/json-seq${string}` | `application/${string}+json-seq${string}`;
|
|
87
|
+
type HeadersFor<THeaders, TStatus> = ResponseHeaders<TStatus extends keyof THeaders ? THeaders[TStatus] : unknown>;
|
|
88
|
+
type SuccessVariant<TStatus, TEntry, TRequest, TResponse, TKeys = never, THeaders = unknown> = TEntry extends {
|
|
89
|
+
contentType: string;
|
|
90
|
+
data: unknown;
|
|
91
|
+
} ? TEntry extends {
|
|
92
|
+
contentType: infer TContentType;
|
|
93
|
+
data: infer TData;
|
|
94
|
+
} ? {
|
|
95
|
+
status: ToStatusNumber<TStatus, TKeys>;
|
|
96
|
+
data: TContentType extends StreamContentType ? ReadableStream<Uint8Array> : TData;
|
|
97
|
+
error: undefined;
|
|
98
|
+
contentType: TContentType;
|
|
99
|
+
headers: HeadersFor<THeaders, TStatus>;
|
|
100
|
+
request: TRequest;
|
|
101
|
+
response: TResponse;
|
|
102
|
+
} : never : {
|
|
103
|
+
status: ToStatusNumber<TStatus, TKeys>;
|
|
104
|
+
data: TEntry;
|
|
105
|
+
error: undefined;
|
|
106
|
+
contentType: string | undefined;
|
|
107
|
+
headers: HeadersFor<THeaders, TStatus>;
|
|
108
|
+
request: TRequest;
|
|
109
|
+
response: TResponse;
|
|
110
|
+
};
|
|
111
|
+
type ResultByStatus<TResponses, TStatus extends keyof TResponses, TRequest, TResponse, THeaders = unknown> = TStatus extends SuccessStatusCode ? SuccessVariant<TStatus, TResponses[TStatus], TRequest, TResponse, keyof TResponses, THeaders> : {
|
|
112
|
+
status: ToStatusNumber<TStatus, keyof TResponses>;
|
|
113
|
+
data: undefined;
|
|
114
|
+
error: DataOf<TResponses[TStatus]> | EmptyErrorBody;
|
|
115
|
+
contentType: string | undefined;
|
|
116
|
+
headers: HeadersFor<THeaders, TStatus>;
|
|
117
|
+
request: TRequest;
|
|
118
|
+
response: TResponse;
|
|
119
|
+
};
|
|
120
|
+
type ResultUnion<TResponses, TRequest, TResponse, THeaders = unknown> = { [TStatus in keyof TResponses]: ResultByStatus<TResponses, TStatus, TRequest, TResponse, THeaders>; }[keyof TResponses];
|
|
121
|
+
type SuccessResultUnion<TResponses, TRequest, TResponse, THeaders = unknown> = { [TStatus in Extract<keyof TResponses, SuccessStatusCode>]: ResultByStatus<TResponses, TStatus, TRequest, TResponse, THeaders>; }[Extract<keyof TResponses, SuccessStatusCode>];
|
|
122
|
+
/**
|
|
123
|
+
* The result of a call, narrowed by `status`. With `throwOnError`, it holds only the success variants. `THeaders` keys the
|
|
124
|
+
* declared response headers by status like the responses record, so `headers.get` suggests them for the narrowed status.
|
|
125
|
+
*/
|
|
126
|
+
type RequestResult<TResponses, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown> = ThrowOnError extends true ? [SuccessResultUnion<TResponses, TRequest, TResponse, THeaders>] extends [never] ? {
|
|
127
|
+
status: number;
|
|
128
|
+
data: SuccessOf<TResponses>;
|
|
129
|
+
error: undefined;
|
|
130
|
+
contentType: string | undefined;
|
|
131
|
+
headers: ResponseHeaders;
|
|
132
|
+
request: TRequest;
|
|
133
|
+
response: TResponse;
|
|
134
|
+
} : SuccessResultUnion<TResponses, TRequest, TResponse, THeaders> : [ResultUnion<TResponses, TRequest, TResponse, THeaders>] extends [never] ? {
|
|
135
|
+
status: number;
|
|
136
|
+
data: undefined;
|
|
137
|
+
error: undefined;
|
|
138
|
+
contentType: string | undefined;
|
|
139
|
+
headers: ResponseHeaders;
|
|
140
|
+
request: TRequest;
|
|
141
|
+
response: TResponse;
|
|
142
|
+
} : ResultUnion<TResponses, TRequest, TResponse, THeaders>;
|
|
143
|
+
/**
|
|
144
|
+
* `RequestResult` with the declared response headers by status: `RequestResultWithHeaders<ListPetsResponses, { '200': ListPetsStatus200Headers }>`.
|
|
145
|
+
*/
|
|
146
|
+
type RequestResultWithHeaders<TResponses, THeaders, ThrowOnError extends boolean = true> = RequestResult<TResponses, ThrowOnError, NativeRequest, NativeResponse, THeaders>;
|
|
147
|
+
/**
|
|
148
|
+
* A result promise whose `unwrap()` resolves to the success body.
|
|
149
|
+
*/
|
|
150
|
+
type Unwrappable<T extends {
|
|
151
|
+
data: unknown;
|
|
152
|
+
error: unknown;
|
|
153
|
+
}> = Promise<T> & {
|
|
154
|
+
unwrap: () => Promise<Extract<T, {
|
|
155
|
+
error: undefined;
|
|
156
|
+
}>['data']>;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Adds `unwrap()` to a result promise. It rejects with `error` when the result has one.
|
|
160
|
+
*/
|
|
161
|
+
declare function withUnwrap<T extends {
|
|
162
|
+
data: unknown;
|
|
163
|
+
error: unknown;
|
|
164
|
+
}>(promise: Promise<T>): Unwrappable<T>;
|
|
165
|
+
type Unwrapped<T, ThrowOnError extends boolean> = ThrowOnError extends true ? (T extends {
|
|
166
|
+
data: infer TData;
|
|
167
|
+
} ? TData : never) : T;
|
|
168
|
+
/**
|
|
169
|
+
* What an operation returns with `returnType: 'data'`: the success body, or the full result when `throwOnError` is false.
|
|
170
|
+
*/
|
|
171
|
+
type UnwrappedResult<TResponses, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown> = Unwrapped<RequestResult<TResponses, ThrowOnError, TRequest, TResponse, THeaders>, ThrowOnError>;
|
|
172
|
+
/**
|
|
173
|
+
* `UnwrappedResult` with the declared response headers by status, for the full result a `throwOnError: false` call resolves to.
|
|
174
|
+
*/
|
|
175
|
+
type UnwrappedResultWithHeaders<TResponses, THeaders, ThrowOnError extends boolean = true> = UnwrappedResult<TResponses, ThrowOnError, NativeRequest, NativeResponse, THeaders>;
|
|
176
|
+
/**
|
|
177
|
+
* Resolves to the success body when `throwOnError` is true, or to the full result when it is false.
|
|
178
|
+
*/
|
|
179
|
+
declare function unwrapResult<T extends {
|
|
180
|
+
data: unknown;
|
|
181
|
+
error: unknown;
|
|
182
|
+
}, ThrowOnError extends boolean>(promise: Promise<T>, throwOnError: ThrowOnError): Promise<Unwrapped<T, ThrowOnError>>;
|
|
183
|
+
/**
|
|
184
|
+
* The call's `throwOnError`, or the operation's generated default when the call leaves it unset.
|
|
185
|
+
*/
|
|
186
|
+
declare function resolveThrowOnError<ThrowOnError extends boolean>(throwOnError: ThrowOnError | undefined, fallback: boolean): ThrowOnError;
|
|
187
|
+
type DataShape = {
|
|
188
|
+
body?: unknown;
|
|
189
|
+
cookies?: unknown;
|
|
190
|
+
headers?: unknown;
|
|
191
|
+
path?: unknown;
|
|
192
|
+
query?: unknown;
|
|
193
|
+
};
|
|
194
|
+
type RequestCredentials = 'omit' | 'same-origin' | 'include';
|
|
195
|
+
/**
|
|
196
|
+
* Reads a 2xx body as this format instead of by its media type. An error body still uses its media type.
|
|
197
|
+
*/
|
|
198
|
+
type ParseAs = 'json' | 'text' | 'blob' | 'arrayBuffer' | 'stream';
|
|
199
|
+
/**
|
|
200
|
+
* The request content type, or `{ request, response }` to also set `Accept`.
|
|
201
|
+
*/
|
|
202
|
+
type ContentType = string | {
|
|
203
|
+
request?: string;
|
|
204
|
+
response?: string;
|
|
205
|
+
};
|
|
206
|
+
type Validator<T = unknown> = StandardSchemaValidator<T>;
|
|
207
|
+
/**
|
|
208
|
+
* A security scheme of a generated call, passed to the `auth` resolver. OAuth2 `flows` and `metadataUrl` (RFC 8414) and
|
|
209
|
+
* the OpenID Connect `url` tell the resolver where to get a token.
|
|
210
|
+
*/
|
|
211
|
+
type Auth = {
|
|
212
|
+
type: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect';
|
|
213
|
+
scheme?: 'bearer' | 'basic';
|
|
214
|
+
name?: string;
|
|
215
|
+
in?: 'header' | 'query' | 'cookie';
|
|
216
|
+
scopes?: Array<string>;
|
|
217
|
+
flows?: Record<string, {
|
|
218
|
+
authorizationUrl?: string;
|
|
219
|
+
deviceAuthorizationUrl?: string;
|
|
220
|
+
tokenUrl?: string;
|
|
221
|
+
refreshUrl?: string;
|
|
222
|
+
}>;
|
|
223
|
+
metadataUrl?: string;
|
|
224
|
+
url?: string;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* One scheme, or the schemes one requirement sends together.
|
|
228
|
+
*/
|
|
229
|
+
type SecurityRequirement = Auth | Array<Auth>;
|
|
230
|
+
/**
|
|
231
|
+
* `user:password` for basic auth, or `undefined` to skip the scheme.
|
|
232
|
+
*/
|
|
233
|
+
type AuthToken = string | undefined;
|
|
234
|
+
/**
|
|
235
|
+
* A static token, or a callback per scheme. The first requirement whose schemes all get a token is sent.
|
|
236
|
+
*/
|
|
237
|
+
type AuthResolver = AuthToken | ((auth: Auth) => AuthToken | Promise<AuthToken>);
|
|
238
|
+
/**
|
|
239
|
+
* One Server-Sent Event. `data` is parsed JSON when it parses, otherwise the raw string.
|
|
240
|
+
*/
|
|
241
|
+
type ServerSentEvent<TData = unknown> = {
|
|
242
|
+
data: TData;
|
|
243
|
+
event: string;
|
|
244
|
+
id?: string;
|
|
245
|
+
retry?: number;
|
|
246
|
+
};
|
|
247
|
+
/**
|
|
248
|
+
* An event the document types by name, plus the `id` and `retry` any event may carry.
|
|
249
|
+
*/
|
|
250
|
+
type TypedEvent<TEvent> = TEvent & Pick<ServerSentEvent, 'id' | 'retry'>;
|
|
251
|
+
/**
|
|
252
|
+
* Per-call options of a stream operation.
|
|
253
|
+
*/
|
|
254
|
+
type EventStreamOptions = {
|
|
255
|
+
/**
|
|
256
|
+
* The most characters one event or JSON line may hold. A larger one throws a `RangeError`, and `Infinity` turns the limit off.
|
|
257
|
+
* @default 16777216
|
|
258
|
+
*/
|
|
259
|
+
maxEventSize?: number | undefined;
|
|
260
|
+
/**
|
|
261
|
+
* Ends the stream when an event's raw `data` or a JSON line equals this value, such as `[DONE]`.
|
|
262
|
+
*/
|
|
263
|
+
terminator?: string | undefined;
|
|
264
|
+
/**
|
|
265
|
+
* Maps each Server-Sent Event before validation.
|
|
266
|
+
*/
|
|
267
|
+
transform?: (event: ServerSentEvent) => ServerSentEvent;
|
|
268
|
+
/**
|
|
269
|
+
* What `validator.response` checks on each event: the payload (`data`) or the whole event (`event`).
|
|
270
|
+
*/
|
|
271
|
+
eventShape?: 'event' | 'data';
|
|
272
|
+
/**
|
|
273
|
+
* Keeps each Server-Sent Event's `data` as the raw text instead of JSON-parsing it. The generated client sets it when the
|
|
274
|
+
* document types `data` as a string. A list of event names keeps it for those events only.
|
|
275
|
+
*/
|
|
276
|
+
rawData?: boolean | Array<string> | undefined;
|
|
277
|
+
};
|
|
278
|
+
type RequestConfig<TBody = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
279
|
+
baseURL?: string;
|
|
280
|
+
url?: string;
|
|
281
|
+
method?: HttpMethod;
|
|
282
|
+
path?: unknown;
|
|
283
|
+
query?: unknown;
|
|
284
|
+
/**
|
|
285
|
+
* Alias of `query`.
|
|
286
|
+
*/
|
|
287
|
+
params?: unknown;
|
|
288
|
+
cookies?: unknown;
|
|
289
|
+
body?: TBody;
|
|
290
|
+
headers?: unknown;
|
|
291
|
+
styles?: Styles | undefined;
|
|
292
|
+
signal?: AbortSignal | undefined;
|
|
293
|
+
credentials?: RequestCredentials | undefined;
|
|
294
|
+
options?: NativeOptions | undefined;
|
|
295
|
+
contentType?: ContentType;
|
|
296
|
+
parseAs?: ParseAs;
|
|
297
|
+
throwOnError?: boolean;
|
|
298
|
+
client?: ClientInstance<TRequest, TResponse>;
|
|
299
|
+
transport?: Transport<TRequest, TResponse>;
|
|
300
|
+
serializer?: Serializers;
|
|
301
|
+
/**
|
|
302
|
+
* Merged per key over the client's media types.
|
|
303
|
+
*/
|
|
304
|
+
mediaTypes?: MediaTypes;
|
|
305
|
+
/**
|
|
306
|
+
* `error` takes one validator, or a map by status such as `{ 404: schema, '5xx': schema, default: schema }`.
|
|
307
|
+
*/
|
|
308
|
+
validator?: {
|
|
309
|
+
request?: Validator;
|
|
310
|
+
path?: Validator;
|
|
311
|
+
query?: Validator;
|
|
312
|
+
headers?: Validator;
|
|
313
|
+
cookies?: Validator;
|
|
314
|
+
response?: Validator;
|
|
315
|
+
error?: Validator | Record<string, Validator>;
|
|
316
|
+
};
|
|
317
|
+
onValidationError?: ValidationErrorHandler;
|
|
318
|
+
validate?: ValidateOption;
|
|
319
|
+
security?: Array<SecurityRequirement>;
|
|
320
|
+
auth?: AuthResolver;
|
|
321
|
+
};
|
|
322
|
+
/**
|
|
323
|
+
* The options of a generated operation: its typed request groups plus the call config.
|
|
324
|
+
*/
|
|
325
|
+
type Options<TData extends DataShape, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = Omit<RequestConfig<unknown, TRequest, TResponse>, keyof DataShape | 'url'> & TData & {
|
|
326
|
+
client?: ClientInstance<TRequest, TResponse>;
|
|
327
|
+
throwOnError?: ThrowOnError;
|
|
328
|
+
};
|
|
329
|
+
type StreamOptions<TData extends DataShape, ThrowOnError extends boolean = true> = Options<TData, ThrowOnError> & EventStreamOptions;
|
|
330
|
+
/**
|
|
331
|
+
* Config shared by every call of a client. A call's own config overrides it.
|
|
332
|
+
*/
|
|
333
|
+
type ClientConfig<TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
334
|
+
baseURL?: string;
|
|
335
|
+
headers?: HeadersInit;
|
|
336
|
+
credentials?: RequestCredentials;
|
|
337
|
+
options?: NativeOptions;
|
|
338
|
+
throwOnError?: boolean;
|
|
339
|
+
transport?: Transport<TRequest, TResponse>;
|
|
340
|
+
serializer?: Serializers;
|
|
341
|
+
/**
|
|
342
|
+
* Merged per key over `defaultMediaTypes`.
|
|
343
|
+
*/
|
|
344
|
+
mediaTypes?: MediaTypes;
|
|
345
|
+
onValidationError?: ValidationErrorHandler;
|
|
346
|
+
validate?: ValidateOption;
|
|
347
|
+
auth?: AuthResolver;
|
|
348
|
+
};
|
|
349
|
+
/**
|
|
350
|
+
* The request a transport sends, with its URL, headers, body, and auth already resolved.
|
|
351
|
+
*/
|
|
352
|
+
type ResolvedRequest = {
|
|
353
|
+
url: string;
|
|
354
|
+
method: string;
|
|
355
|
+
headers: Record<string, string>;
|
|
356
|
+
body?: RequestBody | undefined;
|
|
357
|
+
signal?: AbortSignal | undefined;
|
|
358
|
+
credentials?: RequestCredentials | undefined;
|
|
359
|
+
options?: NativeOptions | undefined;
|
|
360
|
+
/**
|
|
361
|
+
* Read the response as a stream. A transport that buffers bodies, such as axios, must stream it.
|
|
362
|
+
*/
|
|
363
|
+
stream?: boolean;
|
|
364
|
+
};
|
|
365
|
+
/**
|
|
366
|
+
* A transport's `Response` with the native request and response that results and errors carry.
|
|
367
|
+
*/
|
|
368
|
+
type TransportResult<TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
369
|
+
webResponse: Response;
|
|
370
|
+
request: TRequest;
|
|
371
|
+
response: TResponse;
|
|
372
|
+
};
|
|
373
|
+
/**
|
|
374
|
+
* Sends a resolved request and returns its `Response` with the body unread. The client decodes it.
|
|
375
|
+
*/
|
|
376
|
+
type Transport<TRequest = NativeRequest, TResponse = NativeResponse> = (request: ResolvedRequest) => Promise<Response | TransportResult<TRequest, TResponse>>;
|
|
377
|
+
/**
|
|
378
|
+
* What a response interceptor gets: the decoded body with the status, headers, and native objects.
|
|
379
|
+
*/
|
|
380
|
+
type DecodedResponse<TData = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
381
|
+
data: TData;
|
|
382
|
+
status: number;
|
|
383
|
+
statusText: string;
|
|
384
|
+
headers: Headers;
|
|
385
|
+
contentType?: string | undefined;
|
|
386
|
+
request: TRequest;
|
|
387
|
+
response: TResponse;
|
|
388
|
+
};
|
|
389
|
+
type InterceptorFn<T, TContext = never, TResult = T> = (value: T, context?: TContext) => TResult | Promise<TResult>;
|
|
390
|
+
type InterceptorStack<T, TContext = never, TResult = T> = {
|
|
391
|
+
use: (fn: InterceptorFn<T, TContext, TResult>) => number;
|
|
392
|
+
eject: (id: number) => void;
|
|
393
|
+
update: (id: number, fn: InterceptorFn<T, TContext, TResult>) => void;
|
|
394
|
+
run: (value: T, context?: TContext) => Promise<T>;
|
|
395
|
+
};
|
|
396
|
+
type Interceptors<TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
397
|
+
request: InterceptorStack<ResolvedRequest, RequestConfig<unknown, TRequest, TResponse>>;
|
|
398
|
+
response: InterceptorStack<DecodedResponse<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
|
|
399
|
+
/**
|
|
400
|
+
* Sees every failure: a `ResponseError`, a network error, an abort, or a validation error. Return another error to throw it instead.
|
|
401
|
+
*/
|
|
402
|
+
error: InterceptorStack<ResponseError<unknown, TRequest, TResponse> | Error, RequestConfig<unknown, TRequest, TResponse>, Error | void>;
|
|
403
|
+
};
|
|
404
|
+
type UrlConfig = Pick<RequestConfig, 'baseURL' | 'url' | 'path' | 'query' | 'params' | 'serializer' | 'styles'>;
|
|
405
|
+
type StreamParts<TEvent> = AsyncIterable<TEvent> & {
|
|
406
|
+
stream: AsyncGenerator<TEvent>;
|
|
407
|
+
/**
|
|
408
|
+
* The last event ID the server sent, for a `Last-Event-ID` header on a new call.
|
|
409
|
+
*/
|
|
410
|
+
readonly lastEventId: string;
|
|
411
|
+
};
|
|
412
|
+
type WithEvents<TResult, TEvent> = TResult extends {
|
|
413
|
+
error: undefined;
|
|
414
|
+
} ? Omit<TResult, 'data'> & {
|
|
415
|
+
data: AsyncGenerator<TEvent>;
|
|
416
|
+
} : TResult;
|
|
417
|
+
/**
|
|
418
|
+
* The result of a stream call. Iterate it, or its `data`, for the items. A failed call yields none.
|
|
419
|
+
*/
|
|
420
|
+
type StreamResult<TResponses, ThrowOnError extends boolean = true, TEvent = ServerSentEvent<SuccessOf<TResponses>>, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown> = WithEvents<RequestResult<TResponses, ThrowOnError, TRequest, TResponse, THeaders>, TEvent> & StreamParts<TEvent>;
|
|
421
|
+
/**
|
|
422
|
+
* `StreamResult` with the declared response headers by status: `StreamResultWithHeaders<StreamEventsResponses, { '200': StreamEventsStatus200Headers }>`.
|
|
423
|
+
*/
|
|
424
|
+
type StreamResultWithHeaders<TResponses, THeaders, ThrowOnError extends boolean = true, TEvent = ServerSentEvent<SuccessOf<TResponses>>> = StreamResult<TResponses, ThrowOnError, TEvent, NativeRequest, NativeResponse, THeaders>;
|
|
425
|
+
/**
|
|
426
|
+
* What a stream operation returns with `returnType: 'data'`.
|
|
427
|
+
*/
|
|
428
|
+
type UnwrappedStreamResult<TResponses, ThrowOnError extends boolean = true, TEvent = ServerSentEvent<SuccessOf<TResponses>>, THeaders = unknown> = Unwrapped<StreamResult<TResponses, ThrowOnError, TEvent, NativeRequest, NativeResponse, THeaders>, ThrowOnError>;
|
|
429
|
+
/**
|
|
430
|
+
* `UnwrappedStreamResult` with the declared response headers by status, for the full result a `throwOnError: false` call resolves to.
|
|
431
|
+
*/
|
|
432
|
+
type UnwrappedStreamResultWithHeaders<TResponses, THeaders, ThrowOnError extends boolean = true, TEvent = ServerSentEvent<SuccessOf<TResponses>>> = UnwrappedStreamResult<TResponses, ThrowOnError, TEvent, THeaders>;
|
|
433
|
+
/**
|
|
434
|
+
* A client. Call it to send a request.
|
|
435
|
+
*/
|
|
436
|
+
type ClientInstance<TRequest = NativeRequest, TResponse = NativeResponse> = {
|
|
437
|
+
<TResponses = unknown, ThrowOnError extends boolean = true, THeaders = unknown>(config: RequestConfig<unknown, TRequest, TResponse> & {
|
|
438
|
+
throwOnError?: ThrowOnError;
|
|
439
|
+
}): Promise<RequestResult<TResponses, ThrowOnError, TRequest, TResponse, THeaders>>;
|
|
440
|
+
/**
|
|
441
|
+
* Sends the request and reads a 2xx body item by item. A body whose media type cannot stream throws a `TypeError`.
|
|
442
|
+
*/
|
|
443
|
+
stream: <TResponses = unknown, ThrowOnError extends boolean = true, TItem = ServerSentEvent<SuccessOf<TResponses>>, THeaders = unknown>(config: RequestConfig<unknown, TRequest, TResponse> & EventStreamOptions & {
|
|
444
|
+
throwOnError?: ThrowOnError;
|
|
445
|
+
}) => Promise<StreamResult<TResponses, ThrowOnError, TItem, TRequest, TResponse, THeaders>>;
|
|
446
|
+
getConfig: () => ClientConfig<TRequest, TResponse>;
|
|
447
|
+
setConfig: (config: ClientConfig<TRequest, TResponse>) => ClientConfig<TRequest, TResponse>;
|
|
448
|
+
getUrl: (config: UrlConfig) => string;
|
|
449
|
+
interceptors: Interceptors<TRequest, TResponse>;
|
|
450
|
+
/**
|
|
451
|
+
* A client with this config and no interceptors.
|
|
452
|
+
*/
|
|
453
|
+
createClient: (config?: ClientConfig<TRequest, TResponse>) => ClientInstance<TRequest, TResponse>;
|
|
454
|
+
/**
|
|
455
|
+
* A client with this config that runs this client's interceptors before its own.
|
|
456
|
+
*/
|
|
457
|
+
extend: (config?: ClientConfig<TRequest, TResponse>) => ClientInstance<TRequest, TResponse>;
|
|
458
|
+
};
|
|
459
|
+
/**
|
|
460
|
+
* Thrown for a non-2xx response when `throwOnError` is true.
|
|
461
|
+
*/
|
|
462
|
+
declare class ResponseError<TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse> extends Error {
|
|
463
|
+
data: TError;
|
|
464
|
+
status: number;
|
|
465
|
+
statusText: string;
|
|
466
|
+
contentType: string | undefined;
|
|
467
|
+
/**
|
|
468
|
+
* The response's `Headers`, the same on every transport.
|
|
469
|
+
*/
|
|
470
|
+
headers: ResponseHeaders;
|
|
471
|
+
request: TRequest;
|
|
472
|
+
response: TResponse;
|
|
473
|
+
constructor(config: {
|
|
474
|
+
data: TError;
|
|
475
|
+
status: number;
|
|
476
|
+
statusText: string;
|
|
477
|
+
contentType?: string | undefined;
|
|
478
|
+
headers?: Headers | undefined;
|
|
479
|
+
request: TRequest;
|
|
480
|
+
response: TResponse;
|
|
481
|
+
method?: string | undefined;
|
|
482
|
+
url?: string | undefined;
|
|
483
|
+
});
|
|
484
|
+
/**
|
|
485
|
+
* Matches on `name`, since every generated client has its own `ResponseError` class.
|
|
486
|
+
*/
|
|
487
|
+
static is(error: unknown): error is ResponseError<unknown, unknown, unknown>;
|
|
488
|
+
}
|
|
489
|
+
type ResponseErrorConfig<TError = unknown> = ResponseError<TError>;
|
|
490
|
+
type ErrorStatus<TResponses> = Exclude<keyof TResponses, SuccessStatusCode>;
|
|
491
|
+
/**
|
|
492
|
+
* The `ResponseError` an operation throws. Checking `status` narrows `data` to that status's body, and `headers.get` to
|
|
493
|
+
* the header names in `THeaders`, the same record `RequestResult` takes.
|
|
494
|
+
*/
|
|
495
|
+
type ResponseErrorOf<TResponses, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown> = [ErrorStatus<TResponses>] extends [never] ? ResponseError<unknown, TRequest, TResponse> : { [TStatus in ErrorStatus<TResponses>]: ResponseError<DataOf<TResponses[TStatus]>, TRequest, TResponse> & {
|
|
496
|
+
status: ToStatusNumber<TStatus, keyof TResponses>;
|
|
497
|
+
headers: HeadersFor<THeaders, TStatus>;
|
|
498
|
+
}; }[ErrorStatus<TResponses>];
|
|
499
|
+
/**
|
|
500
|
+
* Narrows a caught error to the `ResponseError` of the operation whose responses record you pass.
|
|
501
|
+
*
|
|
502
|
+
* @example
|
|
503
|
+
* ```ts
|
|
504
|
+
* try {
|
|
505
|
+
* await getPetById({ path: { petId: 7 } })
|
|
506
|
+
* } catch (error) {
|
|
507
|
+
* if (isResponseError<GetPetByIdResponses>(error) && error.status === 404) error.data // GetPetById404
|
|
508
|
+
* }
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
declare function isResponseError<TResponses = unknown, TRequest = NativeRequest, TResponse = NativeResponse, THeaders = unknown>(error: unknown): error is ResponseErrorOf<TResponses, TRequest, TResponse, THeaders>;
|
|
512
|
+
/**
|
|
513
|
+
* The value of a header, matched by name in any letter case.
|
|
514
|
+
*/
|
|
515
|
+
declare function getHeader(headers: Record<string, string>, name: string): string | undefined;
|
|
516
|
+
declare function createInterceptorStack<T, TContext = never, TResult = T>(): InterceptorStack<T, TContext, TResult>;
|
|
517
|
+
/**
|
|
518
|
+
* Adds the tokens of the first security requirement whose schemes all get one to `headers` and `query`.
|
|
519
|
+
*/
|
|
520
|
+
declare function resolveAuth(params: {
|
|
521
|
+
security: Array<SecurityRequirement> | undefined;
|
|
522
|
+
auth: AuthResolver | undefined;
|
|
523
|
+
headers: Record<string, string>;
|
|
524
|
+
query: Record<string, unknown>;
|
|
525
|
+
}): Promise<void>;
|
|
526
|
+
/**
|
|
527
|
+
* The `RequestInit` of a resolved request, for `fetch` or a library that takes the same init, such as ky.
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```ts
|
|
531
|
+
* client.setConfig({ transport: (request) => ky(request.url, { ...toRequestInit(request), throwHttpErrors: false, retry: 0, timeout: false }) })
|
|
532
|
+
* ```
|
|
533
|
+
*/
|
|
534
|
+
declare function toRequestInit(request: ResolvedRequest): RequestInit;
|
|
535
|
+
type InterceptorRuns<TRequest, TResponse> = {
|
|
536
|
+
request: (value: ResolvedRequest, context?: RequestConfig<unknown, TRequest, TResponse>) => Promise<ResolvedRequest>;
|
|
537
|
+
response: (value: DecodedResponse<unknown, TRequest, TResponse>, context?: RequestConfig<unknown, TRequest, TResponse>) => Promise<DecodedResponse<unknown, TRequest, TResponse>>;
|
|
538
|
+
error: (value: Error, context?: RequestConfig<unknown, TRequest, TResponse>) => Promise<Error>;
|
|
539
|
+
};
|
|
540
|
+
/**
|
|
541
|
+
* Creates a client that sends through `defaultTransport`.
|
|
542
|
+
*/
|
|
543
|
+
declare function createClientCore<TRequest = NativeRequest, TResponse = NativeResponse>(options: {
|
|
544
|
+
defaultTransport: Transport<TRequest, TResponse>;
|
|
545
|
+
parent?: InterceptorRuns<TRequest, TResponse>;
|
|
546
|
+
} & ClientConfig<TRequest, TResponse>): ClientInstance<TRequest, TResponse>;
|
|
547
|
+
/**
|
|
548
|
+
* A client created on first use, so importing an operation runs no setup. Config set before that is passed to `create`.
|
|
549
|
+
*/
|
|
550
|
+
declare function createLazyClient<TRequest = NativeRequest, TResponse = NativeResponse>(create: (config: ClientConfig<TRequest, TResponse>) => ClientInstance<TRequest, TResponse>): ClientInstance<TRequest, TResponse>;
|
|
551
|
+
type NativeOptions = Record<string, unknown>;
|
|
552
|
+
type NativeRequest = unknown;
|
|
553
|
+
type NativeResponse = unknown;
|
|
554
|
+
export { createClientCore as $, ResultByStatus as A, SuccessVariant as B, RequestResult as C, ResponseErrorConfig as D, ResponseError as E, StreamResult as F, Unwrappable as G, Transport as H, StreamResultWithHeaders as I, UnwrappedResultWithHeaders as J, Unwrapped as K, SuccessOf as L, SecurityRequirement as M, ServerSentEvent as N, ResponseErrorOf as O, StreamOptions as P, Validator as Q, SuccessResultUnion as R, RequestCredentials as S, ResolvedRequest as T, TransportResult as U, ToStatusNumber as V, TypedEvent as W, UnwrappedStreamResultWithHeaders as X, UnwrappedStreamResult as Y, UrlConfig as Z, InterceptorStack as _, AuthResolver as a, resolveThrowOnError as at, ParseAs as b, ClientInstance as c, withUnwrap as ct, DataShape as d, MediaTypes as dt, createInterceptorStack as et, DecodedResponse as f, defaultMediaTypes as ft, InterceptorFn as g, EventStreamOptions as h, resolveMediaType as ht, Auth as i, resolveAuth as it, ResultUnion as j, ResponseHeaders as k, ContentType as l, MediaType as lt, ErrorOf as m, parseJson as mt, NativeRequest as n, getHeader as nt, AuthToken as o, toRequestInit as ot, EmptyErrorBody as p, isEncodedBody as pt, UnwrappedResult as q, NativeResponse as r, isResponseError as rt, ClientConfig as s, unwrapResult as st, NativeOptions as t, createLazyClient as tt, DataOf as u, MediaTypeContext as ut, Interceptors as v, RequestResultWithHeaders as w, RequestConfig as x, Options as y, SuccessStatusCode as z };
|