@kubb/plugin-client 5.0.0-beta.3 → 5.0.0-beta.30

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.
Files changed (42) hide show
  1. package/README.md +24 -4
  2. package/dist/clients/axios.cjs +25 -3
  3. package/dist/clients/axios.cjs.map +1 -1
  4. package/dist/clients/axios.d.ts +9 -2
  5. package/dist/clients/axios.js +25 -3
  6. package/dist/clients/axios.js.map +1 -1
  7. package/dist/clients/fetch.cjs +20 -2
  8. package/dist/clients/fetch.cjs.map +1 -1
  9. package/dist/clients/fetch.d.ts +9 -2
  10. package/dist/clients/fetch.js +20 -2
  11. package/dist/clients/fetch.js.map +1 -1
  12. package/dist/index.cjs +524 -301
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.ts +150 -84
  15. package/dist/index.js +525 -302
  16. package/dist/index.js.map +1 -1
  17. package/dist/templates/clients/axios.source.cjs +1 -1
  18. package/dist/templates/clients/axios.source.js +1 -1
  19. package/dist/templates/clients/fetch.source.cjs +1 -1
  20. package/dist/templates/clients/fetch.source.js +1 -1
  21. package/extension.yaml +1293 -0
  22. package/package.json +11 -17
  23. package/src/clients/axios.ts +41 -7
  24. package/src/clients/fetch.ts +30 -3
  25. package/src/components/ClassClient.tsx +17 -19
  26. package/src/components/Client.tsx +68 -51
  27. package/src/components/StaticClassClient.tsx +17 -19
  28. package/src/components/Url.tsx +7 -9
  29. package/src/components/WrapperClient.tsx +9 -5
  30. package/src/functionParams.ts +8 -8
  31. package/src/generators/classClientGenerator.tsx +40 -38
  32. package/src/generators/clientGenerator.tsx +32 -35
  33. package/src/generators/groupedClientGenerator.tsx +14 -8
  34. package/src/generators/operationsGenerator.tsx +12 -6
  35. package/src/generators/staticClassClientGenerator.tsx +34 -32
  36. package/src/plugin.ts +24 -11
  37. package/src/resolvers/resolverClient.ts +31 -8
  38. package/src/types.ts +90 -53
  39. package/src/utils.ts +30 -53
  40. package/templates/clients/axios.ts +0 -73
  41. package/templates/clients/fetch.ts +0 -96
  42. package/templates/config.ts +0 -43
package/dist/index.d.ts CHANGED
@@ -13,10 +13,34 @@ import { KubbReactNode } from "@kubb/renderer-jsx/types";
13
13
  type ResolverClient = Resolver & {
14
14
  /**
15
15
  * Resolves the function name for a given raw operation name.
16
+ *
16
17
  * @example Resolving operation names
17
18
  * `resolver.resolveName('show pet by id') // -> 'showPetById'`
18
19
  */
19
20
  resolveName(this: ResolverClient, name: string): string;
21
+ /**
22
+ * Resolves the output file name for a client module.
23
+ */
24
+ resolvePathName(this: ResolverClient, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
25
+ /**
26
+ * Resolves the generated class name for class-based clients.
27
+ */
28
+ resolveClassName(this: ResolverClient, name: string): string;
29
+ /**
30
+ * Resolves the generated class name for tag-based client groups.
31
+ */
32
+ resolveGroupName(this: ResolverClient, name: string): string;
33
+ /**
34
+ * Resolves the generated SDK facade property name for a client class.
35
+ */
36
+ resolveClientPropertyName(this: ResolverClient, name: string): string;
37
+ /**
38
+ * Resolves the URL helper function name for an operation.
39
+ *
40
+ * @example Resolving URL helper names
41
+ * `resolver.resolveUrlName(node) // -> 'getShowPetByIdUrl'`
42
+ */
43
+ resolveUrlName(this: ResolverClient, node: ast.OperationNode): string;
20
44
  };
21
45
  /**
22
46
  * Use either a preset `client` type OR a custom `importPath`, not both.
@@ -26,9 +50,10 @@ type ResolverClient = Resolver & {
26
50
  */
27
51
  type ClientImportPath = {
28
52
  /**
29
- * Which client should be used to do the HTTP calls.
30
- * - 'axios' uses axios client for HTTP requests.
31
- * - 'fetch' uses native fetch API for HTTP requests.
53
+ * HTTP client used by the generated code.
54
+ * - `'axios'` imports from `@kubb/plugin-client/clients/axios`. Requires `axios` at runtime.
55
+ * - `'fetch'` imports from `@kubb/plugin-client/clients/fetch`. Uses the global `fetch`.
56
+ *
32
57
  * @default 'axios'
33
58
  */
34
59
  client?: 'axios' | 'fetch';
@@ -36,9 +61,12 @@ type ClientImportPath = {
36
61
  } | {
37
62
  client?: never;
38
63
  /**
39
- * Client import path for API calls.
40
- * Used as `import client from '${importPath}'`.
41
- * Accepts relative and absolute paths; path changes are not performed.
64
+ * Path to a custom client module. Generated files import their HTTP runtime from here
65
+ * instead of `@kubb/plugin-client/clients/{client}`. Accepts both relative paths and
66
+ * bare module specifiers; the value is used as-is.
67
+ *
68
+ * @note When combined with a query plugin, the module must export `Client`,
69
+ * `RequestConfig`, and `ResponseErrorConfig` types.
42
70
  */
43
71
  importPath: string;
44
72
  /**
@@ -58,115 +86,123 @@ type ClientImportPath = {
58
86
  */
59
87
  type ParamsTypeOptions = {
60
88
  /**
61
- * All parameters path, query, headers, and body are merged into a single
89
+ * Every operation parameter (path, query, headers, body) is wrapped in a single
62
90
  * destructured object argument.
63
- * - 'object' returns the params and pathParams as an object.
64
- * @default 'inline'
65
91
  */
66
92
  paramsType: 'object';
67
93
  /**
68
94
  * `pathParamsType` has no effect when `paramsType` is `'object'`.
69
- * Path params are already inside the single destructured object.
95
+ * Path params already live inside the single destructured object.
70
96
  */
71
97
  pathParamsType?: never;
72
98
  } | {
73
99
  /**
74
- * Each parameter group is emitted as a separate function argument.
75
- * - 'inline' returns the params as comma separated params.
100
+ * Each parameter group is emitted as a separate positional function argument.
101
+ *
76
102
  * @default 'inline'
77
103
  */
78
104
  paramsType?: 'inline';
79
105
  /**
80
- * Controls how path parameters are arranged within the inline argument list.
81
- * - 'object' groups path params into a destructured object: `{ petId }: PathParams`.
82
- * - 'inline' emits each path param as its own argument: `petId: string`.
106
+ * How URL path parameters are arranged inside the inline argument list.
107
+ * - `'object'` groups them into one destructured object: `{ petId }: PathParams`.
108
+ * - `'inline'` emits each path param as its own argument: `petId: string`.
109
+ *
83
110
  * @default 'inline'
84
111
  */
85
112
  pathParamsType?: 'object' | 'inline';
86
113
  };
87
114
  type Options = {
88
115
  /**
89
- * Specify the export location for the files and define the behavior of the output.
90
- * @default { path: 'clients', barrelType: 'named' }
116
+ * Where the generated client files are written and how they are exported.
117
+ *
118
+ * @default { path: 'clients', barrel: { type: 'named' } }
91
119
  */
92
120
  output?: Output;
93
121
  /**
94
- * Group the clients based on the provided name.
122
+ * Split generated files into subfolders based on the operation's tag.
95
123
  */
96
124
  group?: Group;
97
125
  /**
98
- * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
126
+ * Skip operations matching at least one entry in the list.
99
127
  */
100
128
  exclude?: Array<Exclude>;
101
129
  /**
102
- * Array containing include parameters to include tags/operations/methods/paths.
130
+ * Restrict generation to operations matching at least one entry in the list.
103
131
  */
104
132
  include?: Array<Include>;
105
133
  /**
106
- * Array containing override parameters to override `options` based on tags/operations/methods/paths.
134
+ * Apply a different options object to operations matching a pattern.
107
135
  */
108
136
  override?: Array<Override<ResolvedOptions>>;
109
137
  /**
110
- * Create `operations.ts` file with all operations grouped by methods.
138
+ * Emit an `operations.ts` file that re-exports every generated function grouped by HTTP method.
139
+ *
111
140
  * @default false
112
141
  */
113
142
  operations?: boolean;
114
143
  /**
115
- * Export urls that are used by operation x.
116
- * - 'export' makes them part of your barrel file.
117
- * - false does not make them exportable.
144
+ * Whether to also export the URL builder helpers (`get<Operation>Url`).
145
+ * - `'export'` exposes them via the barrel.
146
+ * - `false` keeps them private.
147
+ *
118
148
  * @default false
119
- * @example getGetPetByIdUrl
120
149
  */
121
150
  urlType?: 'export' | false;
122
151
  /**
123
- * Allows you to set a custom base url for all generated calls.
152
+ * Base URL prepended to every request. When omitted, falls back to the adapter's
153
+ * server URL (typically `servers[0].url`).
124
154
  */
125
155
  baseURL?: string;
126
156
  /**
127
- * ReturnType that is used when calling the client.
128
- * - 'data' returns ResponseConfig[data].
129
- * - 'full' returns ResponseConfig.
157
+ * Shape of the value returned by each generated client function.
158
+ * - `'data'` only the response body.
159
+ * - `'full'` the full response config (body, status, headers, request).
160
+ *
130
161
  * @default 'data'
131
162
  */
132
163
  dataReturnType?: 'data' | 'full';
133
164
  /**
134
- * How to style your params, by default no casing is applied.
135
- * - 'camelcase' uses camelCase for pathParams, queryParams and headerParams names
136
- * @note response types (data/body) are not affected by this option
165
+ * Rename parameter properties in the generated client (path, query, headers).
166
+ * The HTTP request still uses the original spec names; Kubb writes the mapping for you.
167
+ *
168
+ * @note Use the same value on `@kubb/plugin-ts` so types stay compatible.
137
169
  */
138
170
  paramsCasing?: 'camelcase';
139
171
  /**
140
- * Which parser can be used before returning the data.
141
- * - 'client' returns the data as-is from the client.
142
- * - 'zod' uses @kubb/plugin-zod to parse the data.
172
+ * Validator applied to response bodies before they are returned to the caller.
173
+ * - `'client'` no validation. Trusts the API.
174
+ * - `'zod'` pipes responses through schemas from `@kubb/plugin-zod`.
175
+ *
143
176
  * @default 'client'
144
177
  */
145
178
  parser?: 'client' | 'zod';
146
179
  /**
147
- * How to generate the client code.
148
- * - 'function' generates standalone functions for each operation.
149
- * - 'class' generates a class with methods for each operation.
150
- * - 'staticClass' generates a class with static methods for each operation.
180
+ * Shape of the generated client.
181
+ * - `'function'` one standalone async function per operation.
182
+ * - `'class'` one class per tag with instance methods.
183
+ * - `'staticClass'` one class per tag with static methods.
184
+ *
151
185
  * @default 'function'
186
+ * @note Only `'function'` is compatible with query plugins.
152
187
  */
153
188
  clientType?: 'function' | 'class' | 'staticClass';
154
189
  /**
155
- * Bundle the selected client into the generated `.kubb` directory.
156
- * When disabled the generated clients will import the shared runtime from `@kubb/plugin-client/clients/*`.
190
+ * Copy the HTTP client runtime into the generated output so consumers do not need
191
+ * `@kubb/plugin-client` at runtime. When `false`, generated files import from
192
+ * `@kubb/plugin-client/clients/{client}`.
193
+ *
157
194
  * @default false
158
- * In version 5 of Kubb this is by default true
159
195
  */
160
196
  bundle?: boolean;
161
197
  /**
162
- * Generate an SDK facade class that composes all tag-based client classes into a single entry point.
163
- * Setting this option automatically enables `clientType: 'class'`.
198
+ * Generate a single SDK class composing every tag-based client into one entry point.
199
+ * Automatically enables `clientType: 'class'`.
200
+ *
164
201
  * @example
165
202
  * ```ts
166
203
  * pluginClient({
167
204
  * sdk: { className: 'PetStoreSDK' },
168
205
  * })
169
- * // Generates a class with a shared constructor config and one property per tag:
170
206
  * // class PetStoreSDK {
171
207
  * // readonly petController: petController
172
208
  * // readonly storeController: storeController
@@ -176,22 +212,23 @@ type Options = {
176
212
  */
177
213
  sdk?: {
178
214
  /**
179
- * Name of the generated SDK facade class.
215
+ * Name of the generated SDK facade class. Also the file name.
180
216
  */
181
217
  className: string;
182
218
  };
183
219
  /**
184
- * Override individual resolver methods. Any method you omit falls back to the
185
- * preset resolver's implementation. Use `this.default(...)` to call it.
220
+ * Override how names and file paths are built for the generated client.
221
+ * Methods you omit fall back to the default resolver. `this` is bound to the
222
+ * full resolver, so `this.default(name)` delegates to the built-in implementation.
186
223
  */
187
224
  resolver?: Partial<ResolverClient> & ThisType<ResolverClient>;
188
225
  /**
189
- * Single AST visitor applied to each node before printing.
190
- * Return `null` or `undefined` from a method to leave the node unchanged.
226
+ * AST visitor applied to each operation node before code is printed.
227
+ * Return `null` or `undefined` to leave the node unchanged.
191
228
  */
192
229
  transformer?: ast.Visitor;
193
230
  /**
194
- * Define some generators next to the client generators.
231
+ * Custom generators that run alongside the built-in client generators.
195
232
  */
196
233
  generators?: Array<Generator<PluginClient>>;
197
234
  } & ClientImportPath & ParamsTypeOptions;
@@ -200,7 +237,7 @@ type ResolvedOptions = {
200
237
  exclude: Array<Exclude>;
201
238
  include: Array<Include> | undefined;
202
239
  override: Array<Override<ResolvedOptions>>;
203
- group: Group | undefined;
240
+ group: Group | null;
204
241
  client: Options['client'];
205
242
  clientType: NonNullable<Options['clientType']>;
206
243
  bundle: NonNullable<Options['bundle']>;
@@ -232,7 +269,7 @@ type Props = {
232
269
  isIndexable?: boolean;
233
270
  isConfigurable?: boolean;
234
271
  returnType?: string;
235
- baseURL: string | undefined;
272
+ baseURL: string | null | undefined;
236
273
  dataReturnType: PluginClient['resolvedOptions']['dataReturnType'];
237
274
  paramsCasing: PluginClient['resolvedOptions']['paramsCasing'];
238
275
  paramsType: PluginClient['resolvedOptions']['pathParamsType'];
@@ -240,17 +277,9 @@ type Props = {
240
277
  parser: PluginClient['resolvedOptions']['parser'] | undefined;
241
278
  node: ast.OperationNode;
242
279
  tsResolver: ResolverTs;
243
- zodResolver?: ResolverZod;
280
+ zodResolver?: ResolverZod | null;
244
281
  children?: KubbReactNode;
245
282
  };
246
- type GetParamsProps = {
247
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing'];
248
- paramsType: PluginClient['resolvedOptions']['paramsType'];
249
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType'];
250
- node: ast.OperationNode;
251
- tsResolver: ResolverTs;
252
- isConfigurable: boolean;
253
- };
254
283
  declare function Client({
255
284
  name,
256
285
  isExportable,
@@ -269,47 +298,78 @@ declare function Client({
269
298
  children,
270
299
  isConfigurable
271
300
  }: Props): KubbReactNode;
272
- declare namespace Client {
273
- var getParams: ({
274
- paramsType,
275
- paramsCasing,
276
- pathParamsType,
277
- node,
278
- tsResolver,
279
- isConfigurable
280
- }: GetParamsProps) => ast.FunctionParametersNode;
281
- }
282
301
  //#endregion
283
302
  //#region src/generators/classClientGenerator.d.ts
303
+ /**
304
+ * Built-in `operations` generator for `@kubb/plugin-client` when
305
+ * `clientType: 'class'`. Emits one class per tag, with one instance method
306
+ * per operation and a shared constructor for request configuration.
307
+ */
284
308
  declare const classClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
285
309
  //#endregion
286
310
  //#region src/generators/clientGenerator.d.ts
311
+ /**
312
+ * Built-in operation generator for `@kubb/plugin-client`. Emits one async
313
+ * function per OpenAPI operation, plus the matching URL helper. Used when
314
+ * `clientType: 'function'` (the default).
315
+ */
287
316
  declare const clientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
288
317
  //#endregion
289
318
  //#region src/generators/groupedClientGenerator.d.ts
319
+ /**
320
+ * Emits one aggregate file per tag/group when `group` is configured. Each
321
+ * file re-exports every client function for that group, so callers can
322
+ * `import { petController } from './gen/clients'` instead of importing
323
+ * each operation individually.
324
+ */
290
325
  declare const groupedClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
291
326
  //#endregion
292
327
  //#region src/generators/operationsGenerator.d.ts
328
+ /**
329
+ * Generates an `operations.ts` file that re-exports every operation grouped
330
+ * by HTTP method. Enabled when `pluginClient({ operations: true })`. Useful
331
+ * for building meta-tooling on top of the generated client (route
332
+ * registries, API explorers).
333
+ */
293
334
  declare const operationsGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
294
335
  //#endregion
295
336
  //#region src/generators/staticClassClientGenerator.d.ts
337
+ /**
338
+ * Built-in `operations` generator for `@kubb/plugin-client` when
339
+ * `clientType: 'staticClass'`. Emits one class per tag, with a static method
340
+ * per operation so callers can use `Pet.getPetById(...)` without
341
+ * instantiating the class.
342
+ */
296
343
  declare const staticClassClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
297
344
  //#endregion
298
345
  //#region src/plugin.d.ts
299
346
  /**
300
- * Canonical plugin name for `@kubb/plugin-client`, used in driver lookups and warnings.
347
+ * Canonical plugin name for `@kubb/plugin-client`. Used for driver lookups and
348
+ * cross-plugin dependency references.
301
349
  */
302
350
  declare const pluginClientName = "plugin-client";
303
351
  /**
304
- * Generates type-safe HTTP client functions or classes from an OpenAPI specification.
305
- * Creates client APIs by walking operations and delegating to generators.
306
- * Writes barrel files based on the configured `barrelType`.
352
+ * Generates one HTTP client function per OpenAPI operation. Each function has
353
+ * typed path params, query params, body, and response, so callers use the API
354
+ * like any other typed function. Ships with `axios` and `fetch` runtimes; bring
355
+ * your own by setting `importPath`.
307
356
  *
308
- * @example Client generator
357
+ * @example
309
358
  * ```ts
310
- * import pluginClient from '@kubb/plugin-client'
359
+ * import { defineConfig } from 'kubb'
360
+ * import { pluginTs } from '@kubb/plugin-ts'
361
+ * import { pluginClient } from '@kubb/plugin-client'
362
+ *
311
363
  * export default defineConfig({
312
- * plugins: [pluginClient({ output: { path: 'clients' } })]
364
+ * input: { path: './petStore.yaml' },
365
+ * output: { path: './src/gen' },
366
+ * plugins: [
367
+ * pluginTs(),
368
+ * pluginClient({
369
+ * output: { path: './clients' },
370
+ * client: 'fetch',
371
+ * }),
372
+ * ],
313
373
  * })
314
374
  * ```
315
375
  */
@@ -317,12 +377,18 @@ declare const pluginClient: (options?: Options | undefined) => _$_kubb_core0.Plu
317
377
  //#endregion
318
378
  //#region src/resolvers/resolverClient.d.ts
319
379
  /**
320
- * Naming convention resolver for client plugin.
380
+ * Default resolver used by `@kubb/plugin-client`. Decides the names and file
381
+ * paths for every generated client function or class. Functions and files use
382
+ * camelCase; classes and tag groups use PascalCase.
321
383
  *
322
- * Provides default naming helpers using camelCase for functions and file paths.
384
+ * @example Resolve client function and class names
385
+ * ```ts
386
+ * import { resolverClient } from '@kubb/plugin-client'
323
387
  *
324
- * @example
325
- * `resolverClient.default('list pets', 'function') // 'listPets'`
388
+ * resolverClient.default('list pets', 'function') // 'listPets'
389
+ * resolverClient.resolveClassName('pet') // 'Pet'
390
+ * resolverClient.resolveUrlName(operationNode) // 'getShowPetByIdUrl'
391
+ * ```
326
392
  */
327
393
  declare const resolverClient: ResolverClient;
328
394
  //#endregion