@kubb/swagger-ts 2.0.0-beta.2 → 2.0.0-beta.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/dist/components.cjs +3945 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +111 -0
- package/dist/components.d.ts +111 -0
- package/dist/components.js +3918 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +3470 -248
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -2
- package/dist/index.d.ts +10 -2
- package/dist/index.js +3471 -250
- package/dist/index.js.map +1 -1
- package/package.json +16 -8
- package/src/OperationGenerator.tsx +50 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/{generators/TypeGenerator.ts → TypeGenerator.ts} +53 -63
- package/src/components/Mutation.tsx +137 -0
- package/src/components/Query.tsx +137 -0
- package/src/components/index.ts +2 -0
- package/src/plugin.ts +31 -68
- package/src/types.ts +12 -2
- 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
@@ -0,0 +1,137 @@
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
2
|
+
import { print } from '@kubb/parser'
|
3
|
+
import * as factory from '@kubb/parser/factory'
|
4
|
+
import { File, usePlugin, usePluginManager } from '@kubb/react'
|
5
|
+
import { useOas, useOperation, useOperationFile, useOperationName, useSchemas } from '@kubb/swagger/hooks'
|
6
|
+
|
7
|
+
import { TypeBuilder } from '../TypeBuilder.ts'
|
8
|
+
|
9
|
+
import type { ts } from '@kubb/parser'
|
10
|
+
import type { Operation, OperationSchemas } from '@kubb/swagger'
|
11
|
+
import type { ReactNode } from 'react'
|
12
|
+
import type { FileMeta, PluginOptions } from '../types.ts'
|
13
|
+
import type { KubbFile } from '@kubb/core'
|
14
|
+
|
15
|
+
type Props = {
|
16
|
+
builder: TypeBuilder
|
17
|
+
}
|
18
|
+
|
19
|
+
function printCombinedSchema(name: string, operation: Operation, schemas: OperationSchemas): string {
|
20
|
+
const properties: Record<string, ts.TypeNode> = {
|
21
|
+
'response': factory.createTypeReferenceNode(
|
22
|
+
factory.createIdentifier(schemas.response.name),
|
23
|
+
undefined,
|
24
|
+
),
|
25
|
+
}
|
26
|
+
|
27
|
+
if (schemas.request) {
|
28
|
+
properties['request'] = factory.createTypeReferenceNode(
|
29
|
+
factory.createIdentifier(schemas.request.name),
|
30
|
+
undefined,
|
31
|
+
)
|
32
|
+
}
|
33
|
+
|
34
|
+
if (schemas.pathParams) {
|
35
|
+
properties['pathParams'] = factory.createTypeReferenceNode(
|
36
|
+
factory.createIdentifier(schemas.pathParams.name),
|
37
|
+
undefined,
|
38
|
+
)
|
39
|
+
}
|
40
|
+
|
41
|
+
if (schemas.queryParams) {
|
42
|
+
properties['queryParams'] = factory.createTypeReferenceNode(
|
43
|
+
factory.createIdentifier(schemas.queryParams.name),
|
44
|
+
undefined,
|
45
|
+
)
|
46
|
+
}
|
47
|
+
|
48
|
+
if (schemas.headerParams) {
|
49
|
+
properties['headerParams'] = factory.createTypeReferenceNode(
|
50
|
+
factory.createIdentifier(schemas.headerParams.name),
|
51
|
+
undefined,
|
52
|
+
)
|
53
|
+
}
|
54
|
+
|
55
|
+
if (schemas.errors) {
|
56
|
+
properties['errors'] = factory.createUnionDeclaration({
|
57
|
+
nodes: schemas.errors.map(error => {
|
58
|
+
return factory.createTypeReferenceNode(
|
59
|
+
factory.createIdentifier(error.name),
|
60
|
+
undefined,
|
61
|
+
)
|
62
|
+
}),
|
63
|
+
})!
|
64
|
+
}
|
65
|
+
|
66
|
+
const namespaceNode = factory.createNamespaceDeclaration({
|
67
|
+
name: operation.method === 'get' ? `${name}Query` : `${name}Mutation`,
|
68
|
+
statements: Object.keys(properties).map(key => {
|
69
|
+
const type = properties[key]
|
70
|
+
if (!type) {
|
71
|
+
return undefined
|
72
|
+
}
|
73
|
+
return factory.createTypeAliasDeclaration({
|
74
|
+
modifiers: [factory.modifiers.export],
|
75
|
+
name: transformers.pascalCase(key),
|
76
|
+
type,
|
77
|
+
})
|
78
|
+
}).filter(Boolean),
|
79
|
+
})
|
80
|
+
|
81
|
+
return print(namespaceNode)
|
82
|
+
}
|
83
|
+
|
84
|
+
export function Query({
|
85
|
+
builder,
|
86
|
+
}: Props): ReactNode {
|
87
|
+
const { source } = builder.build()
|
88
|
+
|
89
|
+
return (
|
90
|
+
<>
|
91
|
+
{source}
|
92
|
+
</>
|
93
|
+
)
|
94
|
+
}
|
95
|
+
|
96
|
+
// eslint-disable-next-line @typescript-eslint/ban-types
|
97
|
+
type FileProps = {
|
98
|
+
mode: KubbFile.Mode
|
99
|
+
}
|
100
|
+
|
101
|
+
Query.File = function({ mode }: FileProps): ReactNode {
|
102
|
+
const { options } = usePlugin<PluginOptions>()
|
103
|
+
|
104
|
+
const schemas = useSchemas()
|
105
|
+
const pluginManager = usePluginManager()
|
106
|
+
const oas = useOas()
|
107
|
+
const file = useOperationFile()
|
108
|
+
const factoryName = useOperationName({ type: 'type' })
|
109
|
+
const operation = useOperation()
|
110
|
+
|
111
|
+
const builder = new TypeBuilder(options, { oas, pluginManager })
|
112
|
+
.add(schemas.pathParams)
|
113
|
+
.add(schemas.queryParams)
|
114
|
+
.add(schemas.headerParams)
|
115
|
+
.add(schemas.response)
|
116
|
+
.add(schemas.errors)
|
117
|
+
|
118
|
+
const { source, imports } = builder.build()
|
119
|
+
|
120
|
+
return (
|
121
|
+
<>
|
122
|
+
<File<FileMeta>
|
123
|
+
baseName={file.baseName}
|
124
|
+
path={file.path}
|
125
|
+
meta={file.meta}
|
126
|
+
>
|
127
|
+
{mode === 'directory' && imports.map((item, index) => {
|
128
|
+
return <File.Import key={index} root={file.path} {...item} />
|
129
|
+
})}
|
130
|
+
<File.Source>
|
131
|
+
{source}
|
132
|
+
{printCombinedSchema(factoryName, operation, schemas)}
|
133
|
+
</File.Source>
|
134
|
+
</File>
|
135
|
+
</>
|
136
|
+
)
|
137
|
+
}
|
package/src/plugin.ts
CHANGED
@@ -1,18 +1,16 @@
|
|
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
15
|
export const pluginKey: PluginOptions['key'] = [pluginName] satisfies PluginOptions['key']
|
18
16
|
|
@@ -33,7 +31,14 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
33
31
|
|
34
32
|
return {
|
35
33
|
name: pluginName,
|
36
|
-
options
|
34
|
+
options: {
|
35
|
+
transformers,
|
36
|
+
dateType,
|
37
|
+
enumType,
|
38
|
+
optionalType,
|
39
|
+
// keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
|
40
|
+
usedEnumNames: {},
|
41
|
+
},
|
37
42
|
pre: [swaggerPluginName],
|
38
43
|
resolvePath(baseName, directory, options) {
|
39
44
|
const root = path.resolve(this.config.root, this.config.output.path)
|
@@ -48,7 +53,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
48
53
|
}
|
49
54
|
|
50
55
|
if (options?.tag && group?.type === 'tag') {
|
51
|
-
const tag = camelCase(options.tag
|
56
|
+
const tag = camelCase(options.tag)
|
52
57
|
|
53
58
|
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
54
59
|
}
|
@@ -56,7 +61,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
56
61
|
return path.resolve(root, output, baseName)
|
57
62
|
},
|
58
63
|
resolveName(name, type) {
|
59
|
-
const resolvedName = pascalCase(name
|
64
|
+
const resolvedName = pascalCase(name)
|
60
65
|
|
61
66
|
if (type) {
|
62
67
|
return transformers?.name?.(resolvedName, type) || resolvedName
|
@@ -79,39 +84,17 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
79
84
|
const schemas = await swaggerPlugin.api.getSchemas()
|
80
85
|
const root = path.resolve(this.config.root, this.config.output.path)
|
81
86
|
const mode = FileManager.getMode(path.resolve(root, output))
|
82
|
-
|
83
|
-
const usedEnumNames = {}
|
84
|
-
|
85
|
-
if (mode === 'directory') {
|
86
|
-
const builder = await new TypeBuilder({
|
87
|
-
usedEnumNames,
|
88
|
-
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
89
|
-
fileResolver: (name) => {
|
90
|
-
const resolvedTypeId = this.resolvePath({
|
91
|
-
baseName: `${name}.ts`,
|
92
|
-
pluginKey: this.plugin.key,
|
93
|
-
})
|
94
|
-
|
95
|
-
const root = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key })
|
87
|
+
const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager })
|
96
88
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
enumType,
|
101
|
-
dateType,
|
102
|
-
optionalType,
|
103
|
-
oas,
|
104
|
-
}).configure()
|
105
|
-
Object.entries(schemas).forEach(([name, schema]: [string, OasTypes.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
|
-
})
|
89
|
+
builder.add(
|
90
|
+
Object.entries(schemas).map(([name, schema]: [string, OasTypes.SchemaObject]) => ({ name, schema })),
|
91
|
+
)
|
112
92
|
|
93
|
+
if (mode === 'directory') {
|
113
94
|
const mapFolderSchema = async ([name]: [string, OasTypes.SchemaObject]) => {
|
114
|
-
const
|
95
|
+
const baseName = `${this.resolveName({ name, pluginKey: this.plugin.key, type: 'file' })}.ts` as const
|
96
|
+
const resolvedPath = this.resolvePath({ baseName, pluginKey: this.plugin.key })
|
97
|
+
const { source, imports } = builder.build(name)
|
115
98
|
|
116
99
|
if (!resolvedPath) {
|
117
100
|
return null
|
@@ -119,8 +102,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
119
102
|
|
120
103
|
return this.addFile({
|
121
104
|
path: resolvedPath,
|
122
|
-
baseName
|
123
|
-
source
|
105
|
+
baseName,
|
106
|
+
source,
|
107
|
+
imports: imports.map(item => ({ ...item, root: resolvedPath })),
|
124
108
|
meta: {
|
125
109
|
pluginKey: this.plugin.key,
|
126
110
|
},
|
@@ -133,25 +117,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
133
117
|
}
|
134
118
|
|
135
119
|
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
|
-
oas,
|
145
|
-
}).configure()
|
146
|
-
Object.entries(schemas).forEach(([name, schema]: [string, OasTypes.SchemaObject]) => {
|
147
|
-
// generate and pass through new code back to the core so it can be write to that file
|
148
|
-
return builder.add({
|
149
|
-
schema,
|
150
|
-
name,
|
151
|
-
})
|
152
|
-
})
|
153
|
-
|
154
120
|
const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
|
121
|
+
const { source } = builder.build()
|
122
|
+
|
155
123
|
if (!resolvedPath) {
|
156
124
|
return
|
157
125
|
}
|
@@ -159,22 +127,16 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
159
127
|
await this.addFile({
|
160
128
|
path: resolvedPath,
|
161
129
|
baseName: output as KubbFile.BaseName,
|
162
|
-
source
|
130
|
+
source,
|
131
|
+
imports: [],
|
163
132
|
meta: {
|
164
133
|
pluginKey: this.plugin.key,
|
165
134
|
},
|
166
|
-
validate: false,
|
167
135
|
})
|
168
136
|
}
|
169
137
|
|
170
138
|
const operationGenerator = new OperationGenerator(
|
171
|
-
|
172
|
-
mode,
|
173
|
-
enumType,
|
174
|
-
dateType,
|
175
|
-
optionalType,
|
176
|
-
usedEnumNames,
|
177
|
-
},
|
139
|
+
this.plugin.options,
|
178
140
|
{
|
179
141
|
oas,
|
180
142
|
pluginManager: this.pluginManager,
|
@@ -183,6 +145,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
|
|
183
145
|
exclude,
|
184
146
|
include,
|
185
147
|
override,
|
148
|
+
mode,
|
186
149
|
},
|
187
150
|
)
|
188
151
|
|
package/src/types.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { KubbPlugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
2
|
-
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
2
|
+
import type { AppMeta as SwaggerAppMeta, Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
3
3
|
|
4
4
|
export type Options = {
|
5
5
|
/**
|
@@ -67,13 +67,23 @@ export type Options = {
|
|
67
67
|
}
|
68
68
|
}
|
69
69
|
|
70
|
+
type ResolvedOptions = {
|
71
|
+
enumType: NonNullable<Options['enumType']>
|
72
|
+
dateType: NonNullable<Options['dateType']>
|
73
|
+
optionalType: NonNullable<Options['optionalType']>
|
74
|
+
transformers: NonNullable<Options['transformers']>
|
75
|
+
usedEnumNames: Record<string, number>
|
76
|
+
}
|
77
|
+
|
70
78
|
export type FileMeta = {
|
71
79
|
pluginKey?: KubbPlugin['key']
|
72
80
|
name?: string
|
73
81
|
tag?: string
|
74
82
|
}
|
75
83
|
|
76
|
-
|
84
|
+
type AppMeta = SwaggerAppMeta
|
85
|
+
|
86
|
+
export type PluginOptions = PluginFactoryOptions<'swagger-ts', Options, ResolvedOptions, never, ResolvePathOptions, AppMeta>
|
77
87
|
|
78
88
|
declare module '@kubb/core' {
|
79
89
|
export interface _Register {
|
@@ -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