@kubb/plugin-client 5.10.0-canary.20261105T164114 → 5.10.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 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 export `toEventStream` and the `EventStreamResult` and `SuccessOf` types. Implement the behavior your application needs in that module.
13
+ If your spec has server-sent event operations, also export `toEventStream` and the `EventStreamResult`, `SuccessOf`, and `TypedEvent` types. A generated function calls `toEventStream<TData>(promise)` with the promise your `client` returns and nothing else, and leaves the response validator on `config.validator.response`, so your module decides how to validate events.
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,345 @@
1
+ import { _ as Serializers, h as RequestBody, o as HeadersInit, s as HttpMethod, v as Styles } from "./serializers-Ds8zZwOO.js";
2
+ import { a as ValidateOption, i as StandardSchemaValidator, s as ValidationErrorHandler } from "./standardSchema-BShJWz2i.js";
3
+ /**
4
+ * HTTP status codes treated as a success, everything else is an error.
5
+ */
6
+ type SuccessStatusCode = '200' | '201' | '202' | '203' | '204' | '205' | '206' | '207' | '208' | '226';
7
+ type SuccessOf<TResponses> = TResponses[Extract<keyof TResponses, SuccessStatusCode>];
8
+ type ErrorOf<TResponses> = TResponses[Exclude<keyof TResponses, SuccessStatusCode>];
9
+ /**
10
+ * Converts a response record's string status key to its numeric literal, leaving non-numeric keys like `default` as `number`.
11
+ */
12
+ type ToStatusNumber<TStatus> = TStatus extends `${infer TNumber extends number}` ? TNumber : number;
13
+ /**
14
+ * Unwraps content-type variants to keep the bare body on `error`.
15
+ */
16
+ type DataOf<T> = T extends {
17
+ contentType: string;
18
+ data: infer TData;
19
+ } ? TData : T;
20
+ /**
21
+ * Keeps `contentType` beside `data` so switching on it narrows the body.
22
+ */
23
+ type SuccessVariant<TStatus, TEntry, TRequest, TResponse> = TEntry extends {
24
+ contentType: string;
25
+ data: unknown;
26
+ } ? TEntry extends {
27
+ contentType: infer TContentType;
28
+ data: infer TData;
29
+ } ? {
30
+ status: ToStatusNumber<TStatus>;
31
+ data: TData;
32
+ error: undefined;
33
+ contentType: TContentType;
34
+ request: TRequest;
35
+ response: TResponse;
36
+ } : never : {
37
+ status: ToStatusNumber<TStatus>;
38
+ data: TEntry;
39
+ error: undefined;
40
+ contentType: string | undefined;
41
+ request: TRequest;
42
+ response: TResponse;
43
+ };
44
+ /**
45
+ * Narrows `data` or `error` when switching on the numeric status.
46
+ */
47
+ type ResultByStatus<TResponses, TStatus extends keyof TResponses, TRequest, TResponse> = TStatus extends SuccessStatusCode ? SuccessVariant<TStatus, TResponses[TStatus], TRequest, TResponse> : {
48
+ status: ToStatusNumber<TStatus>;
49
+ data: undefined;
50
+ error: DataOf<TResponses[TStatus]>;
51
+ contentType: string | undefined;
52
+ request: TRequest;
53
+ response: TResponse;
54
+ };
55
+ type ResultUnion<TResponses, TRequest, TResponse> = { [TStatus in keyof TResponses]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[keyof TResponses];
56
+ /**
57
+ * Selects success statuses without letting untyped error payloads widen `data`.
58
+ */
59
+ type SuccessResultUnion<TResponses, TRequest, TResponse> = { [TStatus in Extract<keyof TResponses, SuccessStatusCode>]: ResultByStatus<TResponses, TStatus, TRequest, TResponse>; }[Extract<keyof TResponses, SuccessStatusCode>];
60
+ /**
61
+ * Narrows `data` and `error` by status. With `throwOnError`, includes only success variants.
62
+ */
63
+ type RequestResult<TResponses, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = ThrowOnError extends true ? [SuccessResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
64
+ status: number;
65
+ data: SuccessOf<TResponses>;
66
+ error: undefined;
67
+ contentType: string | undefined;
68
+ request: TRequest;
69
+ response: TResponse;
70
+ } : SuccessResultUnion<TResponses, TRequest, TResponse> : [ResultUnion<TResponses, TRequest, TResponse>] extends [never] ? {
71
+ status: number;
72
+ data: undefined;
73
+ error: undefined;
74
+ contentType: string | undefined;
75
+ request: TRequest;
76
+ response: TResponse;
77
+ } : ResultUnion<TResponses, TRequest, TResponse>;
78
+ /**
79
+ * A `RequestResult` promise with an extra `unwrap()` method that resolves to the success body.
80
+ */
81
+ type Unwrappable<T extends {
82
+ data: unknown;
83
+ error: unknown;
84
+ }> = Promise<T> & {
85
+ unwrap: () => Promise<Extract<T, {
86
+ error: undefined;
87
+ }>['data']>;
88
+ };
89
+ /**
90
+ * Attaches `unwrap()` to a result promise, which rejects with `error` when the result carried one.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * const { data, error } = await getPetById({ path: { petId: 1 } })
95
+ * ```
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const pet = await getPetById({ path: { petId: 1 } }).unwrap()
100
+ * ```
101
+ */
102
+ declare function withUnwrap<T extends {
103
+ data: unknown;
104
+ error: unknown;
105
+ }>(promise: Promise<T>): Unwrappable<T>;
106
+ /**
107
+ * The shape a generated operation returns with `returnType: 'data'`: the success body when
108
+ * `throwOnError` is true, or the full `RequestResult` when it is false.
109
+ */
110
+ type UnwrappedResult<TResponses, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = ThrowOnError extends true ? RequestResult<TResponses, true, TRequest, TResponse>['data'] : RequestResult<TResponses, ThrowOnError, TRequest, TResponse>;
111
+ /**
112
+ * Returns the success body when `throwOnError` is true, or the full result when it is false.
113
+ */
114
+ declare function unwrapResult<T extends {
115
+ data: unknown;
116
+ error: unknown;
117
+ }>(promise: Promise<T>, throwOnError: boolean | undefined): Promise<T | T['data']>;
118
+ type DataShape = {
119
+ body?: unknown;
120
+ cookies?: unknown;
121
+ headers?: unknown;
122
+ path?: unknown;
123
+ query?: unknown;
124
+ };
125
+ type RequestCredentials = 'omit' | 'same-origin' | 'include';
126
+ type ResponseType = 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream' | 'formdata';
127
+ /**
128
+ * Decodes a response body for a codec's media type.
129
+ */
130
+ type Deserializer<T = unknown> = (raw: unknown, contentType: string) => T | Promise<T>;
131
+ /**
132
+ * Encodes a request body for a codec's media type.
133
+ */
134
+ type ContentBodySerializer = (body: unknown, contentType?: string) => RequestBody | undefined;
135
+ /**
136
+ * Encodes requests or decodes responses for a media type. Either direction can be omitted.
137
+ */
138
+ type Codec = {
139
+ serialize?: ContentBodySerializer;
140
+ deserialize?: Deserializer;
141
+ };
142
+ /**
143
+ * A string sets the request content type. The object form can also set `Accept`.
144
+ */
145
+ type ContentType = string | {
146
+ request?: string;
147
+ response?: string;
148
+ };
149
+ /**
150
+ * Validates request bodies and params, success bodies, or non-throwing error bodies with Standard Schema.
151
+ */
152
+ type Validator<T = unknown> = StandardSchemaValidator<T>;
153
+ /**
154
+ * A resolved security scheme carried on each generated call's `security` array and passed to the `auth` resolver.
155
+ */
156
+ type Auth = {
157
+ type: 'http' | 'apiKey' | 'oauth2' | 'openIdConnect';
158
+ scheme?: 'bearer' | 'basic';
159
+ name?: string;
160
+ in?: 'header' | 'query' | 'cookie';
161
+ scopes?: Array<string>;
162
+ };
163
+ /**
164
+ * Return `user:password` for basic auth, or `undefined` to skip a scheme.
165
+ */
166
+ type AuthToken = string | undefined;
167
+ /**
168
+ * Resolves the token for a security scheme, either a static token or a callback called per scheme until one returns a token.
169
+ */
170
+ type AuthResolver = AuthToken | ((auth: Auth) => AuthToken | Promise<AuthToken>);
171
+ type RequestConfig<TBody = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = {
172
+ baseURL?: string;
173
+ url?: string;
174
+ method?: HttpMethod;
175
+ path?: unknown;
176
+ query?: unknown;
177
+ /** Alias of `query`, kept for callers that pass the query under this name. */
178
+ params?: unknown;
179
+ cookies?: unknown;
180
+ body?: TBody;
181
+ headers?: unknown;
182
+ styles?: Styles;
183
+ signal?: AbortSignal;
184
+ credentials?: RequestCredentials;
185
+ options?: Record<string, unknown>;
186
+ contentType?: ContentType;
187
+ responseType?: ResponseType;
188
+ throwOnError?: boolean;
189
+ client?: ClientInstance<TRequest, TResponse>;
190
+ transport?: Transport<TRequest, TResponse>;
191
+ serializer?: Serializers;
192
+ codecs?: Record<string, Codec>;
193
+ validator?: {
194
+ request?: Validator;
195
+ path?: Validator;
196
+ query?: Validator;
197
+ headers?: Validator;
198
+ response?: Validator;
199
+ error?: Validator;
200
+ };
201
+ onValidationError?: ValidationErrorHandler;
202
+ validate?: ValidateOption;
203
+ security?: Array<Auth>;
204
+ auth?: AuthResolver;
205
+ };
206
+ /**
207
+ * Combines client options with the operation's typed request params and body.
208
+ */
209
+ type Options<TData extends DataShape, ThrowOnError extends boolean = true, TRequest = NativeRequest, TResponse = NativeResponse> = Omit<RequestConfig<unknown, TRequest, TResponse>, keyof DataShape | 'url'> & TData & {
210
+ client?: ClientInstance<TRequest, TResponse>;
211
+ throwOnError?: ThrowOnError;
212
+ };
213
+ /**
214
+ * Client-level configuration shared by every call an instance makes, overridden by the per-call `RequestConfig`.
215
+ */
216
+ type ClientConfig<TRequest = NativeRequest, TResponse = NativeResponse> = {
217
+ baseURL?: string;
218
+ headers?: HeadersInit;
219
+ credentials?: RequestCredentials;
220
+ options?: Record<string, unknown>;
221
+ throwOnError?: boolean;
222
+ transport?: Transport<TRequest, TResponse>;
223
+ serializer?: Serializers;
224
+ codecs?: Record<string, Codec>;
225
+ onValidationError?: ValidationErrorHandler;
226
+ validate?: ValidateOption;
227
+ auth?: AuthResolver;
228
+ };
229
+ /**
230
+ * The normalized request the transport receives, with all serialization, auth, and header work already done.
231
+ */
232
+ type ResolvedRequest = {
233
+ url: string;
234
+ method: string;
235
+ headers: Record<string, string>;
236
+ body?: RequestBody;
237
+ signal?: AbortSignal;
238
+ credentials?: RequestCredentials;
239
+ options?: Record<string, unknown>;
240
+ responseType?: ResponseType;
241
+ };
242
+ /**
243
+ * What a transport returns: the parsed body plus the native request and response objects.
244
+ */
245
+ type TransportResult<TData = unknown, TRequest = NativeRequest, TResponse = NativeResponse> = {
246
+ data: TData;
247
+ status: number;
248
+ statusText: string;
249
+ headers: Headers;
250
+ contentType?: string;
251
+ request: TRequest;
252
+ response: TResponse;
253
+ };
254
+ /**
255
+ * The per-plugin send, supplied to `createClientCore` as `defaultTransport`.
256
+ */
257
+ type Transport<TRequest = NativeRequest, TResponse = NativeResponse> = (request: ResolvedRequest) => Promise<TransportResult<unknown, TRequest, TResponse>>;
258
+ type CallResult<TRequest = NativeRequest, TResponse = NativeResponse> = {
259
+ status: number;
260
+ data: unknown;
261
+ error: unknown;
262
+ contentType: string | undefined;
263
+ request: TRequest;
264
+ response: TResponse;
265
+ };
266
+ type InterceptorFn<T, TContext = never> = (value: T, context?: TContext) => T | Promise<T>;
267
+ /**
268
+ * A single interceptor channel with a transport-agnostic `use` / `eject` / `update` API.
269
+ */
270
+ type InterceptorStack<T, TContext = never> = {
271
+ use: (fn: InterceptorFn<T, TContext>) => number;
272
+ eject: (id: number) => void;
273
+ update: (id: number, fn: InterceptorFn<T, TContext>) => void;
274
+ run: (value: T, context?: TContext) => Promise<T>;
275
+ };
276
+ type Interceptors<TRequest = NativeRequest, TResponse = NativeResponse> = {
277
+ request: InterceptorStack<ResolvedRequest, RequestConfig<unknown, TRequest, TResponse>>;
278
+ response: InterceptorStack<TransportResult<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
279
+ error: InterceptorStack<ResponseError<unknown, TRequest, TResponse>, RequestConfig<unknown, TRequest, TResponse>>;
280
+ };
281
+ type UrlConfig = Pick<RequestConfig, 'baseURL' | 'url' | 'path' | 'query' | 'params' | 'serializer' | 'styles'>;
282
+ /**
283
+ * A client instance: the callable send plus configuration, interceptors, and an isolated
284
+ * `createClient` factory bound to the same transport.
285
+ */
286
+ type ClientInstance<TRequest = NativeRequest, TResponse = NativeResponse> = {
287
+ <TBody = unknown>(config: RequestConfig<TBody, TRequest, TResponse>): Promise<CallResult<TRequest, TResponse>>;
288
+ getConfig: () => ClientConfig<TRequest, TResponse>;
289
+ setConfig: (config: ClientConfig<TRequest, TResponse>) => ClientConfig<TRequest, TResponse>;
290
+ getUrl: (config: UrlConfig) => string;
291
+ interceptors: Interceptors<TRequest, TResponse>;
292
+ createClient: (config?: ClientConfig<TRequest, TResponse>) => ClientInstance<TRequest, TResponse>;
293
+ };
294
+ /**
295
+ * Thrown for a non-2xx response, so a resolved call always means success.
296
+ */
297
+ declare class ResponseError<TError = unknown, TRequest = NativeRequest, TResponse = NativeResponse> extends Error {
298
+ data: TError;
299
+ status: number;
300
+ statusText: string;
301
+ contentType: string | undefined;
302
+ request: TRequest;
303
+ response: TResponse;
304
+ constructor(config: {
305
+ data: TError;
306
+ status: number;
307
+ statusText: string;
308
+ contentType?: string;
309
+ request: TRequest;
310
+ response: TResponse;
311
+ method?: string;
312
+ url?: string;
313
+ });
314
+ /**
315
+ * Matches on `name`, not `instanceof`: every generated client bundles its own `ResponseError` class.
316
+ */
317
+ static is(error: unknown): error is ResponseError<unknown, unknown, unknown>;
318
+ }
319
+ type ResponseErrorConfig<TError = unknown> = ResponseError<TError>;
320
+ /**
321
+ * Creates a transport-agnostic interceptor channel that runs interceptors in registration order.
322
+ */
323
+ declare function createInterceptorStack<T, TContext = never>(): InterceptorStack<T, TContext>;
324
+ /**
325
+ * Walks the per-operation security in order and places the first resolved token on the request, mutating `headers` / `query` in place.
326
+ */
327
+ declare function resolveAuth(params: {
328
+ security: Array<Auth> | undefined;
329
+ auth: AuthResolver | undefined;
330
+ headers: Record<string, string>;
331
+ query: Record<string, unknown>;
332
+ }): Promise<void>;
333
+ /** A body a transport left as a stream: a web `ReadableStream` or an async iterable such as a Node readable. */
334
+ declare function isStreamed(data: unknown): data is ReadableStream<Uint8Array> | AsyncIterable<Uint8Array | string>;
335
+ /** The media type of the `Content-Type` response header, without its parameters. */
336
+ declare function getResponseContentType(headers: Headers | Record<string, string> | undefined): string | undefined;
337
+ /**
338
+ * Creates an isolated client using the supplied default transport.
339
+ */
340
+ declare function createClientCore<TRequest = NativeRequest, TResponse = NativeResponse>(options: {
341
+ defaultTransport: Transport<TRequest, TResponse>;
342
+ } & ClientConfig<TRequest, TResponse>): ClientInstance<TRequest, TResponse>;
343
+ type NativeRequest = unknown;
344
+ type NativeResponse = unknown;
345
+ export { SuccessResultUnion as A, createClientCore as B, ResolvedRequest as C, ResultByStatus as D, ResponseType as E, TransportResult as F, unwrapResult as G, getResponseContentType as H, Unwrappable as I, withUnwrap as K, UnwrappedResult as L, SuccessVariant as M, ToStatusNumber as N, ResultUnion as O, Transport as P, UrlConfig as R, RequestResult as S, ResponseErrorConfig as T, isStreamed as U, createInterceptorStack as V, resolveAuth as W, InterceptorStack as _, AuthToken as a, RequestConfig as b, ClientInstance as c, ContentType as d, DataOf as f, InterceptorFn as g, ErrorOf as h, AuthResolver as i, SuccessStatusCode as j, SuccessOf as k, Codec as l, Deserializer as m, NativeResponse as n, CallResult as o, DataShape as p, Auth as r, ClientConfig as s, NativeRequest as t, ContentBodySerializer as u, Interceptors as v, ResponseError as w, RequestCredentials as x, Options as y, Validator as z };