@kubb/swagger-ts 2.0.0-beta.1 → 2.0.0-beta.10
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/dist/components.cjs +3993 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +163 -0
- package/dist/components.d.ts +163 -0
- package/dist/components.js +3969 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +3561 -289
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +426 -13
- package/dist/index.d.ts +426 -13
- package/dist/index.js +3563 -292
- package/dist/index.js.map +1 -1
- package/dist/oas.d.ts +381 -0
- package/dist/oas.js +5 -0
- package/dist/oas.js.map +1 -0
- package/package.json +21 -7
- package/src/OperationGenerator.tsx +62 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/{generators/TypeGenerator.ts → TypeGenerator.ts} +78 -68
- package/src/components/Mutation.tsx +137 -0
- package/src/components/Oas.tsx +84 -0
- package/src/components/Query.tsx +136 -0
- package/src/components/index.ts +3 -0
- package/src/oas/index.ts +7 -0
- package/src/oas/infer.ts +58 -0
- package/src/oas/mappers.ts +93 -0
- package/src/oas/model.ts +38 -0
- package/src/oas/requestParams.ts +170 -0
- package/src/oas/response.ts +39 -0
- package/src/oas/security.ts +158 -0
- package/src/plugin.ts +52 -107
- package/src/types.ts +41 -13
- package/src/builders/TypeBuilder.ts +0 -94
- package/src/builders/index.ts +0 -1
- package/src/generators/OperationGenerator.ts +0 -213
- package/src/generators/index.ts +0 -2
package/src/plugin.ts
CHANGED
@@ -1,24 +1,22 @@
|
|
1
1
|
import path from 'node:path'
|
2
2
|
|
3
3
|
import { createPlugin, FileManager, PluginManager } from '@kubb/core'
|
4
|
-
import {
|
4
|
+
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
5
|
+
import { renderTemplate } from '@kubb/core/utils'
|
5
6
|
import { pluginName as swaggerPluginName } from '@kubb/swagger'
|
6
7
|
|
7
|
-
import {
|
8
|
-
|
9
|
-
import { TypeBuilder } from './builders/index.ts'
|
10
|
-
import { OperationGenerator } from './generators/index.ts'
|
8
|
+
import { OperationGenerator } from './OperationGenerator.tsx'
|
9
|
+
import { TypeBuilder } from './TypeBuilder.ts'
|
11
10
|
|
12
11
|
import type { KubbFile, KubbPlugin } from '@kubb/core'
|
13
12
|
import type { OasTypes, PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
|
14
13
|
import type { PluginOptions } from './types.ts'
|
15
|
-
|
16
14
|
export const pluginName = 'swagger-ts' satisfies PluginOptions['name']
|
17
|
-
export const pluginKey: PluginOptions['key'] = [
|
15
|
+
export const pluginKey: PluginOptions['key'] = [pluginName] satisfies PluginOptions['key']
|
18
16
|
|
19
17
|
export const definePlugin = createPlugin<PluginOptions>((options) => {
|
20
18
|
const {
|
21
|
-
output = 'types',
|
19
|
+
output = { path: 'types' },
|
22
20
|
group,
|
23
21
|
exclude = [],
|
24
22
|
include,
|
@@ -27,42 +25,44 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
27
25
|
dateType = 'string',
|
28
26
|
optionalType = 'questionToken',
|
29
27
|
transformers = {},
|
30
|
-
|
28
|
+
oasType = false,
|
31
29
|
} = options
|
32
|
-
const template = group?.output ? group.output : `${output}/{{tag}}Controller`
|
33
|
-
let pluginsOptions: [KubbPlugin<SwaggerPluginOptions>]
|
30
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
34
31
|
|
35
32
|
return {
|
36
33
|
name: pluginName,
|
37
|
-
options
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
34
|
+
options: {
|
35
|
+
transformers,
|
36
|
+
dateType,
|
37
|
+
enumType,
|
38
|
+
optionalType,
|
39
|
+
oasType,
|
40
|
+
// keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
|
41
|
+
usedEnumNames: {},
|
43
42
|
},
|
43
|
+
pre: [swaggerPluginName],
|
44
44
|
resolvePath(baseName, directory, options) {
|
45
45
|
const root = path.resolve(this.config.root, this.config.output.path)
|
46
|
-
const mode = FileManager.getMode(path.resolve(root, output))
|
46
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
47
47
|
|
48
48
|
if (mode === 'file') {
|
49
49
|
/**
|
50
50
|
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
51
51
|
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
52
52
|
*/
|
53
|
-
return path.resolve(root, output)
|
53
|
+
return path.resolve(root, output.path)
|
54
54
|
}
|
55
55
|
|
56
56
|
if (options?.tag && group?.type === 'tag') {
|
57
|
-
const tag = camelCase(options.tag
|
57
|
+
const tag = camelCase(options.tag)
|
58
58
|
|
59
59
|
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
60
60
|
}
|
61
61
|
|
62
|
-
return path.resolve(root, output, baseName)
|
62
|
+
return path.resolve(root, output.path, baseName)
|
63
63
|
},
|
64
64
|
resolveName(name, type) {
|
65
|
-
const resolvedName = pascalCase(name
|
65
|
+
const resolvedName = pascalCase(name)
|
66
66
|
|
67
67
|
if (type) {
|
68
68
|
return transformers?.name?.(resolvedName, type) || resolvedName
|
@@ -78,46 +78,24 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
78
78
|
return this.fileManager.write(source, writePath)
|
79
79
|
},
|
80
80
|
async buildStart() {
|
81
|
-
const [swaggerPlugin] =
|
81
|
+
const [swaggerPlugin]: [KubbPlugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [swaggerPluginName])
|
82
82
|
|
83
83
|
const oas = await swaggerPlugin.api.getOas()
|
84
84
|
|
85
85
|
const schemas = await swaggerPlugin.api.getSchemas()
|
86
86
|
const root = path.resolve(this.config.root, this.config.output.path)
|
87
|
-
const mode = FileManager.getMode(path.resolve(root, output))
|
88
|
-
|
89
|
-
const usedEnumNames = {}
|
90
|
-
|
91
|
-
if (mode === 'directory') {
|
92
|
-
const builder = await new TypeBuilder({
|
93
|
-
usedEnumNames,
|
94
|
-
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
95
|
-
fileResolver: (name) => {
|
96
|
-
const resolvedTypeId = this.resolvePath({
|
97
|
-
baseName: `${name}.ts`,
|
98
|
-
pluginKey: this.plugin.key,
|
99
|
-
})
|
87
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
88
|
+
const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager })
|
100
89
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
},
|
105
|
-
withJSDocs: true,
|
106
|
-
enumType,
|
107
|
-
dateType,
|
108
|
-
optionalType,
|
109
|
-
oas,
|
110
|
-
}).configure()
|
111
|
-
Object.entries(schemas).forEach(([name, schema]: [string, OasTypes.SchemaObject]) => {
|
112
|
-
// generate and pass through new code back to the core so it can be write to that file
|
113
|
-
return builder.add({
|
114
|
-
schema,
|
115
|
-
name,
|
116
|
-
})
|
117
|
-
})
|
90
|
+
builder.add(
|
91
|
+
Object.entries(schemas).map(([name, schema]: [string, OasTypes.SchemaObject]) => ({ name, schema })),
|
92
|
+
)
|
118
93
|
|
94
|
+
if (mode === 'directory') {
|
119
95
|
const mapFolderSchema = async ([name]: [string, OasTypes.SchemaObject]) => {
|
120
|
-
const
|
96
|
+
const baseName = `${this.resolveName({ name, pluginKey: this.plugin.key, type: 'file' })}.ts` as const
|
97
|
+
const resolvedPath = this.resolvePath({ baseName, pluginKey: this.plugin.key })
|
98
|
+
const { source, imports } = builder.build(name)
|
121
99
|
|
122
100
|
if (!resolvedPath) {
|
123
101
|
return null
|
@@ -125,8 +103,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
125
103
|
|
126
104
|
return this.addFile({
|
127
105
|
path: resolvedPath,
|
128
|
-
baseName
|
129
|
-
source
|
106
|
+
baseName,
|
107
|
+
source,
|
108
|
+
imports: imports.map(item => ({ ...item, root: resolvedPath })),
|
130
109
|
meta: {
|
131
110
|
pluginKey: this.plugin.key,
|
132
111
|
},
|
@@ -139,48 +118,26 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
139
118
|
}
|
140
119
|
|
141
120
|
if (mode === 'file') {
|
142
|
-
// outside the loop because we need to add files to just one instance to have the correct sorting, see refsSorter
|
143
|
-
const builder = new TypeBuilder({
|
144
|
-
usedEnumNames,
|
145
|
-
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
146
|
-
withJSDocs: true,
|
147
|
-
enumType,
|
148
|
-
dateType,
|
149
|
-
optionalType,
|
150
|
-
oas,
|
151
|
-
}).configure()
|
152
|
-
Object.entries(schemas).forEach(([name, schema]: [string, OasTypes.SchemaObject]) => {
|
153
|
-
// generate and pass through new code back to the core so it can be write to that file
|
154
|
-
return builder.add({
|
155
|
-
schema,
|
156
|
-
name,
|
157
|
-
})
|
158
|
-
})
|
159
|
-
|
160
121
|
const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
|
122
|
+
const { source } = builder.build()
|
123
|
+
|
161
124
|
if (!resolvedPath) {
|
162
125
|
return
|
163
126
|
}
|
164
127
|
|
165
128
|
await this.addFile({
|
166
129
|
path: resolvedPath,
|
167
|
-
baseName: output as KubbFile.BaseName,
|
168
|
-
source
|
130
|
+
baseName: output.path as KubbFile.BaseName,
|
131
|
+
source,
|
132
|
+
imports: [],
|
169
133
|
meta: {
|
170
134
|
pluginKey: this.plugin.key,
|
171
135
|
},
|
172
|
-
validate: false,
|
173
136
|
})
|
174
137
|
}
|
175
138
|
|
176
139
|
const operationGenerator = new OperationGenerator(
|
177
|
-
|
178
|
-
mode,
|
179
|
-
enumType,
|
180
|
-
dateType,
|
181
|
-
optionalType,
|
182
|
-
usedEnumNames,
|
183
|
-
},
|
140
|
+
this.plugin.options,
|
184
141
|
{
|
185
142
|
oas,
|
186
143
|
pluginManager: this.pluginManager,
|
@@ -189,6 +146,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
189
146
|
exclude,
|
190
147
|
include,
|
191
148
|
override,
|
149
|
+
mode,
|
192
150
|
},
|
193
151
|
)
|
194
152
|
|
@@ -201,31 +159,18 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
201
159
|
}
|
202
160
|
|
203
161
|
const root = path.resolve(this.config.root, this.config.output.path)
|
162
|
+
const { exportType = 'barrel' } = output
|
204
163
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
meta: { pluginKey: this.plugin.key },
|
209
|
-
options: {
|
210
|
-
map: (file) => {
|
211
|
-
return {
|
212
|
-
...file,
|
213
|
-
exports: file.exports?.map((item) => {
|
214
|
-
if (exportAs) {
|
215
|
-
return {
|
216
|
-
...item,
|
217
|
-
name: exportAs,
|
218
|
-
asAlias: !!exportAs,
|
219
|
-
}
|
220
|
-
}
|
221
|
-
return item
|
222
|
-
}),
|
223
|
-
}
|
224
|
-
},
|
164
|
+
if (exportType === 'barrel') {
|
165
|
+
await this.fileManager.addIndexes({
|
166
|
+
root,
|
225
167
|
output,
|
226
|
-
|
227
|
-
|
228
|
-
|
168
|
+
meta: { pluginKey: this.plugin.key },
|
169
|
+
options: {
|
170
|
+
isTypeOnly: true,
|
171
|
+
},
|
172
|
+
})
|
173
|
+
}
|
229
174
|
},
|
230
175
|
}
|
231
176
|
})
|
package/src/types.ts
CHANGED
@@ -1,13 +1,28 @@
|
|
1
|
-
import type { KubbPlugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
2
|
-
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
1
|
+
import type { KubbFile, KubbPlugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
2
|
+
import type { AppMeta as SwaggerAppMeta, Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
3
3
|
|
4
4
|
export type Options = {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
output?: {
|
6
|
+
/**
|
7
|
+
* Relative path to save the TypeScript types.
|
8
|
+
* When output is a file it will save all models inside that file else it will create a file per schema item.
|
9
|
+
* @default 'types'
|
10
|
+
*/
|
11
|
+
path: string
|
12
|
+
/**
|
13
|
+
* Name to be used for the `export * as {{exportAs}} from './'`
|
14
|
+
*/
|
15
|
+
exportAs?: string
|
16
|
+
/**
|
17
|
+
* Add an extension to the generated imports and exports, default it will not use an extension
|
18
|
+
*/
|
19
|
+
extName?: KubbFile.Extname
|
20
|
+
/**
|
21
|
+
* Define what needs to exported, here you can also disable the export of barrel files
|
22
|
+
* @default `'barrel'`
|
23
|
+
*/
|
24
|
+
exportType?: 'barrel' | false
|
25
|
+
}
|
11
26
|
/**
|
12
27
|
* Group the TypeScript types based on the provided name.
|
13
28
|
*/
|
@@ -25,10 +40,6 @@ export type Options = {
|
|
25
40
|
*/
|
26
41
|
output?: string
|
27
42
|
}
|
28
|
-
/**
|
29
|
-
* Name to be used for the `export * as {{exportAs}} from './`
|
30
|
-
*/
|
31
|
-
exportAs?: string
|
32
43
|
/**
|
33
44
|
* Array containing exclude paramaters to exclude/skip tags/operations/methods/paths.
|
34
45
|
*/
|
@@ -65,6 +76,19 @@ export type Options = {
|
|
65
76
|
*/
|
66
77
|
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
67
78
|
}
|
79
|
+
/**
|
80
|
+
* Export an Oas object as Oas type with `import type { Infer } from '@kubb/swagger-ts/oas'`
|
81
|
+
*/
|
82
|
+
oasType?: 'infer' | false
|
83
|
+
}
|
84
|
+
|
85
|
+
type ResolvedOptions = {
|
86
|
+
enumType: NonNullable<Options['enumType']>
|
87
|
+
dateType: NonNullable<Options['dateType']>
|
88
|
+
optionalType: NonNullable<Options['optionalType']>
|
89
|
+
transformers: NonNullable<Options['transformers']>
|
90
|
+
oasType: NonNullable<Options['oasType']>
|
91
|
+
usedEnumNames: Record<string, number>
|
68
92
|
}
|
69
93
|
|
70
94
|
export type FileMeta = {
|
@@ -73,10 +97,14 @@ export type FileMeta = {
|
|
73
97
|
tag?: string
|
74
98
|
}
|
75
99
|
|
76
|
-
|
100
|
+
type AppMeta = SwaggerAppMeta
|
101
|
+
|
102
|
+
export type PluginOptions = PluginFactoryOptions<'swagger-ts', Options, ResolvedOptions, never, ResolvePathOptions, AppMeta>
|
77
103
|
|
78
104
|
declare module '@kubb/core' {
|
79
105
|
export interface _Register {
|
80
106
|
['@kubb/swagger-ts']: PluginOptions
|
81
107
|
}
|
82
108
|
}
|
109
|
+
// external packages
|
110
|
+
export * as Oas from './oas/index.ts'
|
@@ -1,94 +0,0 @@
|
|
1
|
-
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
2
|
-
import { transformers } from '@kubb/core/utils'
|
3
|
-
import { print } from '@kubb/parser'
|
4
|
-
import * as factory from '@kubb/parser/factory'
|
5
|
-
import { ImportsGenerator, OasBuilder } from '@kubb/swagger'
|
6
|
-
import { refsSorter } from '@kubb/swagger/utils'
|
7
|
-
|
8
|
-
import { TypeGenerator } from '../generators/TypeGenerator.ts'
|
9
|
-
|
10
|
-
import type { PluginContext } from '@kubb/core'
|
11
|
-
import type { FileResolver, Oas } from '@kubb/swagger'
|
12
|
-
|
13
|
-
type Options = {
|
14
|
-
oas: Oas
|
15
|
-
usedEnumNames: Record<string, number>
|
16
|
-
resolveName: PluginContext['resolveName']
|
17
|
-
fileResolver?: FileResolver
|
18
|
-
withJSDocs?: boolean
|
19
|
-
withImports?: boolean
|
20
|
-
enumType: 'enum' | 'asConst' | 'asPascalConst'
|
21
|
-
dateType: 'string' | 'date'
|
22
|
-
optionalType: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
23
|
-
}
|
24
|
-
|
25
|
-
export class TypeBuilder extends OasBuilder<Options, never> {
|
26
|
-
configure(options?: Options) {
|
27
|
-
if (options) {
|
28
|
-
this.options = options
|
29
|
-
}
|
30
|
-
|
31
|
-
if (this.options.fileResolver) {
|
32
|
-
this.options.withImports = true
|
33
|
-
}
|
34
|
-
|
35
|
-
return this
|
36
|
-
}
|
37
|
-
|
38
|
-
print(name?: string): string {
|
39
|
-
const codes: string[] = []
|
40
|
-
|
41
|
-
const generated = this.items
|
42
|
-
.filter((operationSchema) => (name ? operationSchema.name === name : true))
|
43
|
-
.sort(transformers.nameSorter)
|
44
|
-
.map((operationSchema) => {
|
45
|
-
const generator = new TypeGenerator({
|
46
|
-
usedEnumNames: this.options.usedEnumNames,
|
47
|
-
withJSDocs: this.options.withJSDocs,
|
48
|
-
resolveName: this.options.resolveName,
|
49
|
-
enumType: this.options.enumType,
|
50
|
-
dateType: this.options.dateType,
|
51
|
-
optionalType: this.options.optionalType,
|
52
|
-
oas: this.options.oas,
|
53
|
-
})
|
54
|
-
const sources = generator.build({
|
55
|
-
schema: operationSchema.schema,
|
56
|
-
baseName: operationSchema.name,
|
57
|
-
description: operationSchema.description,
|
58
|
-
keysToOmit: operationSchema.keysToOmit,
|
59
|
-
})
|
60
|
-
|
61
|
-
return {
|
62
|
-
import: {
|
63
|
-
refs: generator.refs,
|
64
|
-
name: operationSchema.name,
|
65
|
-
},
|
66
|
-
sources,
|
67
|
-
}
|
68
|
-
})
|
69
|
-
.sort(refsSorter)
|
70
|
-
|
71
|
-
generated.forEach((item) => {
|
72
|
-
codes.push(print(item.sources))
|
73
|
-
})
|
74
|
-
|
75
|
-
if (this.options.withImports) {
|
76
|
-
const importsGenerator = new ImportsGenerator({ fileResolver: this.options.fileResolver })
|
77
|
-
const importMeta = importsGenerator.build(generated.map((item) => item.import))
|
78
|
-
|
79
|
-
if (importMeta) {
|
80
|
-
const nodes = importMeta.map((item) => {
|
81
|
-
return factory.createImportDeclaration({
|
82
|
-
name: [{ propertyName: item.ref.propertyName }],
|
83
|
-
path: item.path,
|
84
|
-
isTypeOnly: true,
|
85
|
-
})
|
86
|
-
})
|
87
|
-
|
88
|
-
codes.unshift(print(nodes))
|
89
|
-
}
|
90
|
-
}
|
91
|
-
|
92
|
-
return transformers.combineCodes(codes)
|
93
|
-
}
|
94
|
-
}
|
package/src/builders/index.ts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export * from './TypeBuilder.ts'
|
@@ -1,213 +0,0 @@
|
|
1
|
-
import { getRelativePath } from '@kubb/core/utils'
|
2
|
-
import { print } from '@kubb/parser'
|
3
|
-
import * as factory from '@kubb/parser/factory'
|
4
|
-
import { OperationGenerator as Generator, resolve } from '@kubb/swagger'
|
5
|
-
|
6
|
-
import { pascalCase } from 'change-case'
|
7
|
-
|
8
|
-
import { TypeBuilder } from '../builders/index.ts'
|
9
|
-
|
10
|
-
import type { KubbFile } from '@kubb/core'
|
11
|
-
import type { FileResolver, Operation, OperationSchemas, Resolver } from '@kubb/swagger'
|
12
|
-
import type ts from 'typescript'
|
13
|
-
import type { FileMeta, PluginOptions } from '../types.ts'
|
14
|
-
|
15
|
-
type Options = {
|
16
|
-
usedEnumNames: Record<string, number>
|
17
|
-
|
18
|
-
mode: KubbFile.Mode
|
19
|
-
enumType: NonNullable<PluginOptions['options']['enumType']>
|
20
|
-
dateType: NonNullable<PluginOptions['options']['dateType']>
|
21
|
-
optionalType: NonNullable<PluginOptions['options']['optionalType']>
|
22
|
-
}
|
23
|
-
|
24
|
-
export class OperationGenerator extends Generator<Options, PluginOptions> {
|
25
|
-
resolve(operation: Operation): Resolver {
|
26
|
-
const { pluginManager, plugin } = this.context
|
27
|
-
|
28
|
-
return resolve({
|
29
|
-
operation,
|
30
|
-
resolveName: pluginManager.resolveName,
|
31
|
-
resolvePath: pluginManager.resolvePath,
|
32
|
-
pluginKey: plugin?.key,
|
33
|
-
})
|
34
|
-
}
|
35
|
-
|
36
|
-
async all(): Promise<KubbFile.File | null> {
|
37
|
-
return null
|
38
|
-
}
|
39
|
-
|
40
|
-
#printCombinedSchema(name: string, operation: Operation, schemas: OperationSchemas): string {
|
41
|
-
const properties: Record<string, ts.TypeNode> = {
|
42
|
-
'response': factory.createTypeReferenceNode(
|
43
|
-
factory.createIdentifier(schemas.response.name),
|
44
|
-
undefined,
|
45
|
-
),
|
46
|
-
}
|
47
|
-
|
48
|
-
if (schemas.request) {
|
49
|
-
properties['request'] = factory.createTypeReferenceNode(
|
50
|
-
factory.createIdentifier(schemas.request.name),
|
51
|
-
undefined,
|
52
|
-
)
|
53
|
-
}
|
54
|
-
|
55
|
-
if (schemas.pathParams) {
|
56
|
-
properties['pathParams'] = factory.createTypeReferenceNode(
|
57
|
-
factory.createIdentifier(schemas.pathParams.name),
|
58
|
-
undefined,
|
59
|
-
)
|
60
|
-
}
|
61
|
-
|
62
|
-
if (schemas.queryParams) {
|
63
|
-
properties['queryParams'] = factory.createTypeReferenceNode(
|
64
|
-
factory.createIdentifier(schemas.queryParams.name),
|
65
|
-
undefined,
|
66
|
-
)
|
67
|
-
}
|
68
|
-
|
69
|
-
if (schemas.headerParams) {
|
70
|
-
properties['headerParams'] = factory.createTypeReferenceNode(
|
71
|
-
factory.createIdentifier(schemas.headerParams.name),
|
72
|
-
undefined,
|
73
|
-
)
|
74
|
-
}
|
75
|
-
|
76
|
-
if (schemas.errors) {
|
77
|
-
properties['errors'] = factory.createUnionDeclaration({
|
78
|
-
nodes: schemas.errors.map(error => {
|
79
|
-
return factory.createTypeReferenceNode(
|
80
|
-
factory.createIdentifier(error.name),
|
81
|
-
undefined,
|
82
|
-
)
|
83
|
-
}),
|
84
|
-
})!
|
85
|
-
}
|
86
|
-
|
87
|
-
const namespaceNode = factory.createNamespaceDeclaration({
|
88
|
-
name: operation.method === 'get' ? `${name}Query` : `${name}Mutation`,
|
89
|
-
statements: Object.keys(properties).map(key => {
|
90
|
-
const type = properties[key]
|
91
|
-
if (!type) {
|
92
|
-
return undefined
|
93
|
-
}
|
94
|
-
return factory.createTypeAliasDeclaration({
|
95
|
-
modifiers: [factory.modifiers.export],
|
96
|
-
name: pascalCase(key),
|
97
|
-
type,
|
98
|
-
})
|
99
|
-
}).filter(Boolean),
|
100
|
-
})
|
101
|
-
|
102
|
-
return print(namespaceNode)
|
103
|
-
}
|
104
|
-
|
105
|
-
async get(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
106
|
-
const { mode, enumType, dateType, optionalType, usedEnumNames } = options
|
107
|
-
const { pluginManager, plugin, oas } = this.context
|
108
|
-
|
109
|
-
const type = this.resolve(operation)
|
110
|
-
|
111
|
-
const fileResolver: FileResolver = (name) => {
|
112
|
-
// Used when a react-query type(request, response, params) has an import of a global type
|
113
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } })
|
114
|
-
// refs import, will always been created with the SwaggerTS plugin, our global type
|
115
|
-
const resolvedTypeId = pluginManager.resolvePath({
|
116
|
-
baseName: `${name}.ts`,
|
117
|
-
pluginKey: plugin?.key,
|
118
|
-
})
|
119
|
-
|
120
|
-
return getRelativePath(root, resolvedTypeId)
|
121
|
-
}
|
122
|
-
|
123
|
-
const source = new TypeBuilder({
|
124
|
-
usedEnumNames,
|
125
|
-
fileResolver: mode === 'file' ? undefined : fileResolver,
|
126
|
-
withJSDocs: true,
|
127
|
-
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
128
|
-
enumType,
|
129
|
-
optionalType,
|
130
|
-
dateType,
|
131
|
-
oas,
|
132
|
-
})
|
133
|
-
.add(schemas.pathParams)
|
134
|
-
.add(schemas.queryParams)
|
135
|
-
.add(schemas.headerParams)
|
136
|
-
.add(schemas.response)
|
137
|
-
.add(schemas.errors)
|
138
|
-
.configure()
|
139
|
-
.print()
|
140
|
-
|
141
|
-
const combinedSchemaSource = this.#printCombinedSchema(type.name, operation, schemas)
|
142
|
-
|
143
|
-
return {
|
144
|
-
path: type.path,
|
145
|
-
baseName: type.baseName,
|
146
|
-
source: [source, combinedSchemaSource].join('\n'),
|
147
|
-
meta: {
|
148
|
-
pluginKey: plugin.key,
|
149
|
-
tag: operation.getTags()[0]?.name,
|
150
|
-
},
|
151
|
-
}
|
152
|
-
}
|
153
|
-
|
154
|
-
async post(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
155
|
-
const { mode, enumType, dateType, optionalType, usedEnumNames } = options
|
156
|
-
const { pluginManager, plugin, oas } = this.context
|
157
|
-
|
158
|
-
const type = this.resolve(operation)
|
159
|
-
|
160
|
-
const fileResolver: FileResolver = (name) => {
|
161
|
-
// Used when a react-query type(request, response, params) has an import of a global type
|
162
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } })
|
163
|
-
// refs import, will always been created with the SwaggerTS plugin, our global type
|
164
|
-
const resolvedTypeId = pluginManager.resolvePath({
|
165
|
-
baseName: `${name}.ts`,
|
166
|
-
pluginKey: plugin?.key,
|
167
|
-
})
|
168
|
-
|
169
|
-
return getRelativePath(root, resolvedTypeId)
|
170
|
-
}
|
171
|
-
|
172
|
-
const source = new TypeBuilder({
|
173
|
-
usedEnumNames,
|
174
|
-
fileResolver: mode === 'file' ? undefined : fileResolver,
|
175
|
-
withJSDocs: true,
|
176
|
-
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
177
|
-
enumType,
|
178
|
-
optionalType,
|
179
|
-
dateType,
|
180
|
-
oas,
|
181
|
-
})
|
182
|
-
.add(schemas.pathParams)
|
183
|
-
.add(schemas.queryParams)
|
184
|
-
.add(schemas.headerParams)
|
185
|
-
.add(schemas.request)
|
186
|
-
.add(schemas.response)
|
187
|
-
.add(schemas.errors)
|
188
|
-
.configure()
|
189
|
-
.print()
|
190
|
-
|
191
|
-
const combinedSchemaSource = this.#printCombinedSchema(type.name, operation, schemas)
|
192
|
-
|
193
|
-
return {
|
194
|
-
path: type.path,
|
195
|
-
baseName: type.baseName,
|
196
|
-
source: [source, combinedSchemaSource].join('\n'),
|
197
|
-
meta: {
|
198
|
-
pluginKey: plugin.key,
|
199
|
-
tag: operation.getTags()[0]?.name,
|
200
|
-
},
|
201
|
-
}
|
202
|
-
}
|
203
|
-
|
204
|
-
async put(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
205
|
-
return this.post(operation, schemas, options)
|
206
|
-
}
|
207
|
-
async patch(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
208
|
-
return this.post(operation, schemas, options)
|
209
|
-
}
|
210
|
-
async delete(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
211
|
-
return this.post(operation, schemas, options)
|
212
|
-
}
|
213
|
-
}
|
package/src/generators/index.ts
DELETED