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

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 (43) 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 +76 -8
  8. package/dist/clients/fetch.cjs.map +1 -1
  9. package/dist/clients/fetch.d.ts +9 -2
  10. package/dist/clients/fetch.js +76 -8
  11. package/dist/clients/fetch.js.map +1 -1
  12. package/dist/index.cjs +627 -353
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.ts +153 -86
  15. package/dist/index.js +628 -354
  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 +106 -6
  25. package/src/components/ClassClient.tsx +19 -20
  26. package/src/components/Client.tsx +74 -53
  27. package/src/components/Operations.tsx +2 -1
  28. package/src/components/StaticClassClient.tsx +19 -20
  29. package/src/components/Url.tsx +8 -9
  30. package/src/components/WrapperClient.tsx +9 -5
  31. package/src/functionParams.ts +8 -8
  32. package/src/generators/classClientGenerator.tsx +51 -47
  33. package/src/generators/clientGenerator.tsx +37 -48
  34. package/src/generators/groupedClientGenerator.tsx +14 -8
  35. package/src/generators/operationsGenerator.tsx +14 -8
  36. package/src/generators/staticClassClientGenerator.tsx +45 -41
  37. package/src/plugin.ts +27 -26
  38. package/src/resolvers/resolverClient.ts +31 -8
  39. package/src/types.ts +93 -55
  40. package/src/utils.ts +35 -56
  41. package/templates/clients/axios.ts +0 -73
  42. package/templates/clients/fetch.ts +0 -96
  43. 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,124 @@ 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.
143
- * @default 'client'
172
+ * Validator applied to response bodies before they are returned to the caller.
173
+ * - `false` (default) — no validation. The client has no runtime parser; the response is
174
+ * returned as-is, cast to the generated TypeScript type.
175
+ * - `'zod'` — pipes responses through schemas from `@kubb/plugin-zod`.
176
+ *
177
+ * @default false
144
178
  */
145
- parser?: 'client' | 'zod';
179
+ parser?: false | 'zod';
146
180
  /**
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.
181
+ * Shape of the generated client.
182
+ * - `'function'` one standalone async function per operation.
183
+ * - `'class'` one class per tag with instance methods.
184
+ * - `'staticClass'` one class per tag with static methods.
185
+ *
151
186
  * @default 'function'
187
+ * @note Only `'function'` is compatible with query plugins.
152
188
  */
153
189
  clientType?: 'function' | 'class' | 'staticClass';
154
190
  /**
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/*`.
191
+ * Copy the HTTP client runtime into the generated output so consumers do not need
192
+ * `@kubb/plugin-client` at runtime. When `false`, generated files import from
193
+ * `@kubb/plugin-client/clients/{client}`.
194
+ *
157
195
  * @default false
158
- * In version 5 of Kubb this is by default true
159
196
  */
160
197
  bundle?: boolean;
161
198
  /**
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'`.
199
+ * Generate a single SDK class composing every tag-based client into one entry point.
200
+ * Automatically enables `clientType: 'class'`.
201
+ *
164
202
  * @example
165
203
  * ```ts
166
204
  * pluginClient({
167
205
  * sdk: { className: 'PetStoreSDK' },
168
206
  * })
169
- * // Generates a class with a shared constructor config and one property per tag:
170
207
  * // class PetStoreSDK {
171
208
  * // readonly petController: petController
172
209
  * // readonly storeController: storeController
@@ -176,22 +213,23 @@ type Options = {
176
213
  */
177
214
  sdk?: {
178
215
  /**
179
- * Name of the generated SDK facade class.
216
+ * Name of the generated SDK facade class. Also the file name.
180
217
  */
181
218
  className: string;
182
219
  };
183
220
  /**
184
- * Override individual resolver methods. Any method you omit falls back to the
185
- * preset resolver's implementation. Use `this.default(...)` to call it.
221
+ * Override how names and file paths are built for the generated client.
222
+ * Methods you omit fall back to the default resolver. `this` is bound to the
223
+ * full resolver, so `this.default(name)` delegates to the built-in implementation.
186
224
  */
187
225
  resolver?: Partial<ResolverClient> & ThisType<ResolverClient>;
188
226
  /**
189
- * Single AST visitor applied to each node before printing.
190
- * Return `null` or `undefined` from a method to leave the node unchanged.
227
+ * AST visitor applied to each operation node before code is printed.
228
+ * Return `null` or `undefined` to leave the node unchanged.
191
229
  */
192
230
  transformer?: ast.Visitor;
193
231
  /**
194
- * Define some generators next to the client generators.
232
+ * Custom generators that run alongside the built-in client generators.
195
233
  */
196
234
  generators?: Array<Generator<PluginClient>>;
197
235
  } & ClientImportPath & ParamsTypeOptions;
@@ -200,7 +238,7 @@ type ResolvedOptions = {
200
238
  exclude: Array<Exclude>;
201
239
  include: Array<Include> | undefined;
202
240
  override: Array<Override<ResolvedOptions>>;
203
- group: Group | undefined;
241
+ group: Group | null;
204
242
  client: Options['client'];
205
243
  clientType: NonNullable<Options['clientType']>;
206
244
  bundle: NonNullable<Options['bundle']>;
@@ -232,7 +270,7 @@ type Props = {
232
270
  isIndexable?: boolean;
233
271
  isConfigurable?: boolean;
234
272
  returnType?: string;
235
- baseURL: string | undefined;
273
+ baseURL: string | null | undefined;
236
274
  dataReturnType: PluginClient['resolvedOptions']['dataReturnType'];
237
275
  paramsCasing: PluginClient['resolvedOptions']['paramsCasing'];
238
276
  paramsType: PluginClient['resolvedOptions']['pathParamsType'];
@@ -240,17 +278,9 @@ type Props = {
240
278
  parser: PluginClient['resolvedOptions']['parser'] | undefined;
241
279
  node: ast.OperationNode;
242
280
  tsResolver: ResolverTs;
243
- zodResolver?: ResolverZod;
281
+ zodResolver?: ResolverZod | null;
244
282
  children?: KubbReactNode;
245
283
  };
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
284
  declare function Client({
255
285
  name,
256
286
  isExportable,
@@ -269,47 +299,78 @@ declare function Client({
269
299
  children,
270
300
  isConfigurable
271
301
  }: 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
302
  //#endregion
283
303
  //#region src/generators/classClientGenerator.d.ts
304
+ /**
305
+ * Built-in `operations` generator for `@kubb/plugin-client` when
306
+ * `clientType: 'class'`. Emits one class per tag, with one instance method
307
+ * per operation and a shared constructor for request configuration.
308
+ */
284
309
  declare const classClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
285
310
  //#endregion
286
311
  //#region src/generators/clientGenerator.d.ts
312
+ /**
313
+ * Built-in operation generator for `@kubb/plugin-client`. Emits one async
314
+ * function per OpenAPI operation, plus the matching URL helper. Used when
315
+ * `clientType: 'function'` (the default).
316
+ */
287
317
  declare const clientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
288
318
  //#endregion
289
319
  //#region src/generators/groupedClientGenerator.d.ts
320
+ /**
321
+ * Emits one aggregate file per tag/group when `group` is configured. Each
322
+ * file re-exports every client function for that group, so callers can
323
+ * `import { petController } from './gen/clients'` instead of importing
324
+ * each operation individually.
325
+ */
290
326
  declare const groupedClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
291
327
  //#endregion
292
328
  //#region src/generators/operationsGenerator.d.ts
329
+ /**
330
+ * Generates an `operations.ts` file that re-exports every operation grouped
331
+ * by HTTP method. Enabled when `pluginClient({ operations: true })`. Useful
332
+ * for building meta-tooling on top of the generated client (route
333
+ * registries, API explorers).
334
+ */
293
335
  declare const operationsGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
294
336
  //#endregion
295
337
  //#region src/generators/staticClassClientGenerator.d.ts
338
+ /**
339
+ * Built-in `operations` generator for `@kubb/plugin-client` when
340
+ * `clientType: 'staticClass'`. Emits one class per tag, with a static method
341
+ * per operation so callers can use `Pet.getPetById(...)` without
342
+ * instantiating the class.
343
+ */
296
344
  declare const staticClassClientGenerator: _$_kubb_core0.Generator<PluginClient, unknown>;
297
345
  //#endregion
298
346
  //#region src/plugin.d.ts
299
347
  /**
300
- * Canonical plugin name for `@kubb/plugin-client`, used in driver lookups and warnings.
348
+ * Canonical plugin name for `@kubb/plugin-client`. Used for driver lookups and
349
+ * cross-plugin dependency references.
301
350
  */
302
351
  declare const pluginClientName = "plugin-client";
303
352
  /**
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`.
353
+ * Generates one HTTP client function per OpenAPI operation. Each function has
354
+ * typed path params, query params, body, and response, so callers use the API
355
+ * like any other typed function. Ships with `axios` and `fetch` runtimes; bring
356
+ * your own by setting `importPath`.
307
357
  *
308
- * @example Client generator
358
+ * @example
309
359
  * ```ts
310
- * import pluginClient from '@kubb/plugin-client'
360
+ * import { defineConfig } from 'kubb'
361
+ * import { pluginTs } from '@kubb/plugin-ts'
362
+ * import { pluginClient } from '@kubb/plugin-client'
363
+ *
311
364
  * export default defineConfig({
312
- * plugins: [pluginClient({ output: { path: 'clients' } })]
365
+ * input: { path: './petStore.yaml' },
366
+ * output: { path: './src/gen' },
367
+ * plugins: [
368
+ * pluginTs(),
369
+ * pluginClient({
370
+ * output: { path: './clients' },
371
+ * client: 'fetch',
372
+ * }),
373
+ * ],
313
374
  * })
314
375
  * ```
315
376
  */
@@ -317,12 +378,18 @@ declare const pluginClient: (options?: Options | undefined) => _$_kubb_core0.Plu
317
378
  //#endregion
318
379
  //#region src/resolvers/resolverClient.d.ts
319
380
  /**
320
- * Naming convention resolver for client plugin.
381
+ * Default resolver used by `@kubb/plugin-client`. Decides the names and file
382
+ * paths for every generated client function or class. Functions and files use
383
+ * camelCase; classes and tag groups use PascalCase.
321
384
  *
322
- * Provides default naming helpers using camelCase for functions and file paths.
385
+ * @example Resolve client function and class names
386
+ * ```ts
387
+ * import { resolverClient } from '@kubb/plugin-client'
323
388
  *
324
- * @example
325
- * `resolverClient.default('list pets', 'function') // 'listPets'`
389
+ * resolverClient.default('list pets', 'function') // 'listPets'
390
+ * resolverClient.resolveClassName('pet') // 'Pet'
391
+ * resolverClient.resolveUrlName(operationNode) // 'getShowPetByIdUrl'
392
+ * ```
326
393
  */
327
394
  declare const resolverClient: ResolverClient;
328
395
  //#endregion