@kubb/plugin-client 0.0.0-canary-20241104172400

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 (58) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/client.ts +65 -0
  4. package/dist/chunk-I2NEG5CE.js +222 -0
  5. package/dist/chunk-I2NEG5CE.js.map +1 -0
  6. package/dist/chunk-IIY26RTZ.cjs +232 -0
  7. package/dist/chunk-IIY26RTZ.cjs.map +1 -0
  8. package/dist/chunk-PHUXTZZT.js +159 -0
  9. package/dist/chunk-PHUXTZZT.js.map +1 -0
  10. package/dist/chunk-UBMTYBLG.cjs +162 -0
  11. package/dist/chunk-UBMTYBLG.cjs.map +1 -0
  12. package/dist/client.cjs +45 -0
  13. package/dist/client.cjs.map +1 -0
  14. package/dist/client.d.cts +35 -0
  15. package/dist/client.d.ts +35 -0
  16. package/dist/client.js +33 -0
  17. package/dist/client.js.map +1 -0
  18. package/dist/components.cjs +16 -0
  19. package/dist/components.cjs.map +1 -0
  20. package/dist/components.d.cts +40 -0
  21. package/dist/components.d.ts +40 -0
  22. package/dist/components.js +3 -0
  23. package/dist/components.js.map +1 -0
  24. package/dist/generators.cjs +21 -0
  25. package/dist/generators.cjs.map +1 -0
  26. package/dist/generators.d.cts +11 -0
  27. package/dist/generators.d.ts +11 -0
  28. package/dist/generators.js +4 -0
  29. package/dist/generators.js.map +1 -0
  30. package/dist/index.cjs +17 -0
  31. package/dist/index.cjs.map +1 -0
  32. package/dist/index.d.cts +8 -0
  33. package/dist/index.d.ts +8 -0
  34. package/dist/index.js +4 -0
  35. package/dist/index.js.map +1 -0
  36. package/dist/types-DXab6SYO.d.cts +92 -0
  37. package/dist/types-DXab6SYO.d.ts +92 -0
  38. package/package.json +117 -0
  39. package/src/components/Client.tsx +199 -0
  40. package/src/components/Operations.tsx +28 -0
  41. package/src/components/index.ts +2 -0
  42. package/src/generators/__snapshots__/deletePet.ts +13 -0
  43. package/src/generators/__snapshots__/deletePetObject.ts +13 -0
  44. package/src/generators/__snapshots__/findByTags.ts +13 -0
  45. package/src/generators/__snapshots__/findByTagsFull.ts +13 -0
  46. package/src/generators/__snapshots__/findByTagsObject.ts +15 -0
  47. package/src/generators/__snapshots__/findByTagsWithZod.ts +13 -0
  48. package/src/generators/__snapshots__/findByTagsWithZodFull.ts +13 -0
  49. package/src/generators/__snapshots__/importPath.ts +13 -0
  50. package/src/generators/__snapshots__/operations.ts +82 -0
  51. package/src/generators/__snapshots__/updatePetById.ts +12 -0
  52. package/src/generators/clientGenerator.tsx +67 -0
  53. package/src/generators/groupedClientGenerator.tsx +70 -0
  54. package/src/generators/index.ts +3 -0
  55. package/src/generators/operationsGenerator.tsx +26 -0
  56. package/src/index.ts +2 -0
  57. package/src/plugin.ts +122 -0
  58. package/src/types.ts +93 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,122 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase } from '@kubb/core/transformers'
5
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
6
+
7
+ import type { Plugin } from '@kubb/core'
8
+ import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
9
+ import { pluginZodName } from '@kubb/plugin-zod'
10
+ import { operationsGenerator } from './generators'
11
+ import { clientGenerator } from './generators/clientGenerator.tsx'
12
+ import { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'
13
+ import type { PluginClient } from './types.ts'
14
+
15
+ export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
16
+
17
+ export const pluginClient = createPlugin<PluginClient>((options) => {
18
+ const {
19
+ output = { path: 'clients', barrelType: 'named' },
20
+ group,
21
+ exclude = [],
22
+ include,
23
+ override = [],
24
+ transformers = {},
25
+ dataReturnType = 'data',
26
+ pathParamsType = 'inline',
27
+ paramsType = 'inline',
28
+ operations = false,
29
+ baseURL,
30
+ generators = [clientGenerator, group ? groupedClientGenerator : undefined, operations ? operationsGenerator : undefined].filter(Boolean),
31
+ importPath = '@kubb/plugin-client/client',
32
+ parser = 'client',
33
+ } = options
34
+
35
+ return {
36
+ name: pluginClientName,
37
+ options: {
38
+ output,
39
+ group,
40
+ parser,
41
+ dataReturnType,
42
+ importPath,
43
+ paramsType,
44
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType,
45
+ baseURL,
46
+ },
47
+ pre: [pluginOasName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
48
+ resolvePath(baseName, pathMode, options) {
49
+ const root = path.resolve(this.config.root, this.config.output.path)
50
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
51
+
52
+ if (options?.tag && group?.type === 'tag') {
53
+ const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
54
+
55
+ return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
56
+ }
57
+
58
+ if (mode === 'single') {
59
+ /**
60
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
61
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
62
+ */
63
+ return path.resolve(root, output.path)
64
+ }
65
+
66
+ return path.resolve(root, output.path, baseName)
67
+ },
68
+ resolveName(name, type) {
69
+ const resolvedName = camelCase(name, { isFile: type === 'file' })
70
+
71
+ if (type) {
72
+ return transformers?.name?.(resolvedName, type) || resolvedName
73
+ }
74
+
75
+ return resolvedName
76
+ },
77
+ async buildStart() {
78
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
79
+
80
+ const oas = await swaggerPlugin.context.getOas()
81
+ const root = path.resolve(this.config.root, this.config.output.path)
82
+ const mode = FileManager.getMode(path.resolve(root, output.path))
83
+ const baseURL = await swaggerPlugin.context.getBaseURL()
84
+
85
+ const operationGenerator = new OperationGenerator(
86
+ baseURL
87
+ ? {
88
+ ...this.plugin.options,
89
+ baseURL,
90
+ }
91
+ : this.plugin.options,
92
+ {
93
+ oas,
94
+ pluginManager: this.pluginManager,
95
+ plugin: this.plugin,
96
+ contentType: swaggerPlugin.context.contentType,
97
+ exclude,
98
+ include,
99
+ override,
100
+ mode,
101
+ },
102
+ )
103
+
104
+ const files = await operationGenerator.build(...generators)
105
+
106
+ await this.addFile(...files)
107
+
108
+ const barrelFiles = await this.fileManager.getBarrelFiles({
109
+ type: output.barrelType ?? 'named',
110
+ root,
111
+ output,
112
+ files: this.fileManager.files,
113
+ meta: {
114
+ pluginKey: this.plugin.key,
115
+ },
116
+ logger: this.logger,
117
+ })
118
+
119
+ await this.addFile(...barrelFiles)
120
+ },
121
+ }
122
+ })
package/src/types.ts ADDED
@@ -0,0 +1,93 @@
1
+ import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+
3
+ import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
4
+
5
+ export type Options = {
6
+ /**
7
+ * Specify the export location for the files and define the behavior of the output
8
+ * @default { path: 'clients', barrelType: 'named' }
9
+ */
10
+ output?: Output
11
+ /**
12
+ * Group the clients based on the provided name.
13
+ */
14
+ group?: Group
15
+ /**
16
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
17
+ */
18
+ exclude?: Array<Exclude>
19
+ /**
20
+ * Array containing include parameters to include tags/operations/methods/paths.
21
+ */
22
+ include?: Array<Include>
23
+ /**
24
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
25
+ */
26
+ override?: Array<Override<ResolvedOptions>>
27
+ /**
28
+ * Create `operations.ts` file with all operations grouped by methods.
29
+ * @default false
30
+ */
31
+ operations?: boolean
32
+ /**
33
+ * Path to the client import path that will be used to do the API calls.
34
+ * It will be used as `import client from '${client.importPath}'`.
35
+ * It allows both relative and absolute path but be aware that we will not change the path.
36
+ * @default '@kubb/plugin-client/client'
37
+ */
38
+ importPath?: string
39
+ /**
40
+ * Allows you to set a custom base url for all generated calls.
41
+ */
42
+ baseURL?: string
43
+ /**
44
+ * ReturnType that will be used when calling the client.
45
+ * - 'data' will return ResponseConfig[data].
46
+ * - 'full' will return ResponseConfig.
47
+ * @default 'data'
48
+ */
49
+ dataReturnType?: 'data' | 'full'
50
+ /**
51
+ * How to pass your params
52
+ * - 'object' will return the params and pathParams as an object.
53
+ * - 'inline' will return the params as comma separated params.
54
+ * @default 'inline'
55
+ */
56
+ paramsType?: 'object' | 'inline'
57
+ /**
58
+ * How to pass your pathParams.
59
+ * - 'object' will return the pathParams as an object.
60
+ * - 'inline' will return the pathParams as comma separated params.
61
+ * @default 'inline'
62
+ */
63
+ pathParamsType?: 'object' | 'inline'
64
+ /**
65
+ * Which parser can be used before returning the data
66
+ * - 'zod' will use `@kubb/plugin-zod` to parse the data.
67
+ * @default 'client'
68
+ */
69
+ parser?: 'client' | 'zod'
70
+ transformers?: {
71
+ /**
72
+ * Customize the names based on the type that is provided by the plugin.
73
+ */
74
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
75
+ }
76
+ /**
77
+ * Define some generators next to the client generators
78
+ */
79
+ generators?: Array<Generator<PluginClient>>
80
+ }
81
+
82
+ type ResolvedOptions = {
83
+ output: Output
84
+ group?: Options['group']
85
+ baseURL: string | undefined
86
+ parser: NonNullable<Options['parser']>
87
+ importPath: NonNullable<Options['importPath']>
88
+ dataReturnType: NonNullable<Options['dataReturnType']>
89
+ pathParamsType: NonNullable<Options['pathParamsType']>
90
+ paramsType: NonNullable<Options['paramsType']>
91
+ }
92
+
93
+ export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>