@kubb/swagger-ts 2.0.0-alpha.3 → 2.0.0-alpha.4

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/src/plugin.ts ADDED
@@ -0,0 +1,223 @@
1
+ import path from 'node:path'
2
+
3
+ import { createPlugin, FileManager, PluginManager } from '@kubb/core'
4
+ import { getRelativePath, renderTemplate } from '@kubb/core/utils'
5
+ import { pluginName as swaggerPluginName } from '@kubb/swagger'
6
+
7
+ import { camelCase, camelCaseTransformMerge, pascalCase, pascalCaseTransformMerge } from 'change-case'
8
+
9
+ import { TypeBuilder } from './builders/index.ts'
10
+ import { OperationGenerator } from './generators/index.ts'
11
+
12
+ import type { KubbFile, KubbPlugin } from '@kubb/core'
13
+ import type { OpenAPIV3, PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
14
+ import type { PluginOptions } from './types.ts'
15
+
16
+ export const pluginName = 'swagger-ts' satisfies PluginOptions['name']
17
+ export const pluginKey: PluginOptions['key'] = ['schema', pluginName] satisfies PluginOptions['key']
18
+
19
+ export const definePlugin = createPlugin<PluginOptions>((options) => {
20
+ const {
21
+ output = 'types',
22
+ groupBy,
23
+ skipBy = [],
24
+ overrideBy = [],
25
+ enumType = 'asConst',
26
+ dateType = 'string',
27
+ optionalType = 'questionToken',
28
+ transformers = {},
29
+ exportAs,
30
+ } = options
31
+ const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`
32
+ let pluginsOptions: [KubbPlugin<SwaggerPluginOptions>]
33
+
34
+ return {
35
+ name: pluginName,
36
+ options,
37
+ kind: 'schema',
38
+ validate(plugins) {
39
+ pluginsOptions = PluginManager.getDependedPlugins<SwaggerPluginOptions>(plugins, [swaggerPluginName])
40
+
41
+ return true
42
+ },
43
+ resolvePath(baseName, directory, options) {
44
+ const root = path.resolve(this.config.root, this.config.output.path)
45
+ const mode = FileManager.getMode(path.resolve(root, output))
46
+
47
+ if (mode === 'file') {
48
+ /**
49
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
50
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
51
+ */
52
+ return path.resolve(root, output)
53
+ }
54
+
55
+ if (options?.tag && groupBy?.type === 'tag') {
56
+ const tag = camelCase(options.tag, { delimiter: '', transform: camelCaseTransformMerge })
57
+
58
+ return path.resolve(root, renderTemplate(template, { tag }), baseName)
59
+ }
60
+
61
+ return path.resolve(root, output, baseName)
62
+ },
63
+ resolveName(name) {
64
+ const resolvedName = pascalCase(name, { delimiter: '', stripRegexp: /[^A-Z0-9$]/gi, transform: pascalCaseTransformMerge })
65
+
66
+ return transformers?.name?.(resolvedName) || resolvedName
67
+ },
68
+ async writeFile(source, writePath) {
69
+ if (!writePath.endsWith('.ts') || !source) {
70
+ return
71
+ }
72
+
73
+ return this.fileManager.write(source, writePath)
74
+ },
75
+ async buildStart() {
76
+ const [swaggerPlugin] = pluginsOptions
77
+
78
+ const oas = await swaggerPlugin.api.getOas()
79
+
80
+ const schemas = await swaggerPlugin.api.getSchemas()
81
+ const root = path.resolve(this.config.root, this.config.output.path)
82
+ const mode = FileManager.getMode(path.resolve(root, output))
83
+ // keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
84
+ const usedEnumNames = {}
85
+
86
+ if (mode === 'directory') {
87
+ const builder = await new TypeBuilder({
88
+ usedEnumNames,
89
+ resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
90
+ fileResolver: (name) => {
91
+ const resolvedTypeId = this.resolvePath({
92
+ baseName: `${name}.ts`,
93
+ pluginKey: this.plugin.key,
94
+ })
95
+
96
+ const root = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key })
97
+
98
+ return getRelativePath(root, resolvedTypeId)
99
+ },
100
+ withJSDocs: true,
101
+ enumType,
102
+ dateType,
103
+ optionalType,
104
+ }).configure()
105
+ Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
106
+ // generate and pass through new code back to the core so it can be write to that file
107
+ return builder.add({
108
+ schema,
109
+ name,
110
+ })
111
+ })
112
+
113
+ const mapFolderSchema = async ([name]: [string, OpenAPIV3.SchemaObject]) => {
114
+ const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key })
115
+
116
+ if (!resolvedPath) {
117
+ return null
118
+ }
119
+
120
+ return this.addFile({
121
+ path: resolvedPath,
122
+ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
123
+ source: builder.print(name),
124
+ meta: {
125
+ pluginKey: this.plugin.key,
126
+ },
127
+ })
128
+ }
129
+
130
+ const promises = Object.entries(schemas).map(mapFolderSchema)
131
+
132
+ await Promise.all(promises)
133
+ }
134
+
135
+ if (mode === 'file') {
136
+ // outside the loop because we need to add files to just one instance to have the correct sorting, see refsSorter
137
+ const builder = new TypeBuilder({
138
+ usedEnumNames,
139
+ resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
140
+ withJSDocs: true,
141
+ enumType,
142
+ dateType,
143
+ optionalType,
144
+ }).configure()
145
+ Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
146
+ // generate and pass through new code back to the core so it can be write to that file
147
+ return builder.add({
148
+ schema,
149
+ name,
150
+ })
151
+ })
152
+
153
+ const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
154
+ if (!resolvedPath) {
155
+ return
156
+ }
157
+
158
+ await this.addFile({
159
+ path: resolvedPath,
160
+ baseName: output as KubbFile.BaseName,
161
+ source: builder.print(),
162
+ meta: {
163
+ pluginKey: this.plugin.key,
164
+ },
165
+ validate: false,
166
+ })
167
+ }
168
+
169
+ const operationGenerator = new OperationGenerator(
170
+ {
171
+ mode,
172
+ enumType,
173
+ dateType,
174
+ optionalType,
175
+ usedEnumNames,
176
+ },
177
+ {
178
+ oas,
179
+ pluginManager: this.pluginManager,
180
+ plugin: this.plugin,
181
+ contentType: swaggerPlugin.api.contentType,
182
+ skipBy,
183
+ overrideBy,
184
+ },
185
+ )
186
+
187
+ const files = await operationGenerator.build()
188
+ await this.addFile(...files)
189
+ },
190
+ async buildEnd() {
191
+ if (this.config.output.write === false) {
192
+ return
193
+ }
194
+
195
+ const root = path.resolve(this.config.root, this.config.output.path)
196
+
197
+ await this.fileManager.addIndexes({
198
+ root,
199
+ extName: '.ts',
200
+ meta: { pluginKey: this.plugin.key },
201
+ options: {
202
+ map: (file) => {
203
+ return {
204
+ ...file,
205
+ exports: file.exports?.map((item) => {
206
+ if (exportAs) {
207
+ return {
208
+ ...item,
209
+ name: exportAs,
210
+ asAlias: !!exportAs,
211
+ }
212
+ }
213
+ return item
214
+ }),
215
+ }
216
+ },
217
+ output,
218
+ isTypeOnly: true,
219
+ },
220
+ })
221
+ },
222
+ }
223
+ })
package/src/resolve.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { resolve as swaggerResolve } from '@kubb/swagger'
2
+
3
+ import { pluginKey } from './plugin.ts'
4
+
5
+ import type { ResolveProps, Resolver } from '@kubb/swagger'
6
+
7
+ export function resolve(props: ResolveProps): Resolver {
8
+ return swaggerResolve({ pluginKey, ...props })
9
+ }
package/src/types.ts ADDED
@@ -0,0 +1,77 @@
1
+ import type { KubbPlugin, PluginFactoryOptions } from '@kubb/core'
2
+ import type { OverrideBy, ResolvePathOptions, SkipBy } from '@kubb/swagger'
3
+
4
+ export type Options = {
5
+ /**
6
+ * Relative path to save the TypeScript types.
7
+ * When output is a file it will save all models inside that file else it will create a file per schema item.
8
+ * @default 'types'
9
+ */
10
+ output?: string
11
+ /**
12
+ * Group the TypeScript types based on the provided name.
13
+ */
14
+ groupBy?: {
15
+ /**
16
+ * Tag will group based on the operation tag inside the Swagger file.
17
+ */
18
+ type: 'tag'
19
+ /**
20
+ * Relative path to save the grouped TypeScript Types.
21
+ *
22
+ * `{{tag}}` will be replaced by the current tagName.
23
+ * @example `${output}/{{tag}}Controller` => `models/PetController`
24
+ * @default `${output}/{{tag}}Controller`
25
+ */
26
+ output?: string
27
+ }
28
+ /**
29
+ * Name to be used for the `export * as {{exportAs}} from './`
30
+ */
31
+ exportAs?: string
32
+ /**
33
+ * Array containing skipBy paramaters to exclude/skip tags/operations/methods/paths.
34
+ */
35
+ skipBy?: Array<SkipBy>
36
+ /**
37
+ * Array containing overrideBy paramaters to override `options` based on tags/operations/methods/paths.
38
+ */
39
+ overrideBy?: Array<OverrideBy<Options>>
40
+ /**
41
+ * Choose to use `enum` or `as const` for enums
42
+ * @default 'asConst'
43
+ */
44
+ enumType?: 'enum' | 'asConst' | 'asPascalConst'
45
+ /**
46
+ * Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
47
+ * @default 'string'
48
+ */
49
+ dateType?: 'string' | 'date'
50
+ /**
51
+ * Choose what to use as mode for an optional value.
52
+ * @examples 'questionToken': type?: string
53
+ * @examples 'undefined': type: string | undefined
54
+ * @examples 'questionTokenAndUndefined': type?: string | undefined
55
+ * @default 'questionToken'
56
+ */
57
+ optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
58
+ transformers?: {
59
+ /**
60
+ * Override the name of the TypeScript type that is getting generated, this will also override the name of the file.
61
+ */
62
+ name?: (name: string) => string
63
+ }
64
+ }
65
+
66
+ export type FileMeta = {
67
+ pluginKey?: KubbPlugin['key']
68
+ tag?: string
69
+ }
70
+
71
+ export type PluginOptions = PluginFactoryOptions<'swagger-ts', 'schema', Options, false, never, ResolvePathOptions>
72
+
73
+ declare module '@kubb/core' {
74
+ export interface _Register {
75
+ ['@kubb/swagger-ts']: PluginOptions
76
+ }
77
+ }