@kubb/swagger-ts 2.0.0-alpha.9 → 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/README.md +1 -1
- 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 +3569 -292
- 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 +3572 -296
- 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 +24 -11
- package/src/OperationGenerator.tsx +62 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/{generators/TypeGenerator.ts → TypeGenerator.ts} +106 -89
- 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 +54 -107
- package/src/types.ts +41 -13
- package/src/builders/TypeBuilder.ts +0 -93
- package/src/builders/index.ts +0 -1
- package/src/generators/OperationGenerator.ts +0 -211
- package/src/generators/index.ts +0 -2
@@ -1,211 +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 } = 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
|
-
})
|
132
|
-
.add(schemas.pathParams)
|
133
|
-
.add(schemas.queryParams)
|
134
|
-
.add(schemas.headerParams)
|
135
|
-
.add(schemas.response)
|
136
|
-
.add(schemas.errors)
|
137
|
-
.configure()
|
138
|
-
.print()
|
139
|
-
|
140
|
-
const combinedSchemaSource = this.#printCombinedSchema(type.name, operation, schemas)
|
141
|
-
|
142
|
-
return {
|
143
|
-
path: type.path,
|
144
|
-
baseName: type.baseName,
|
145
|
-
source: [source, combinedSchemaSource].join('\n'),
|
146
|
-
meta: {
|
147
|
-
pluginKey: plugin.key,
|
148
|
-
tag: operation.getTags()[0]?.name,
|
149
|
-
},
|
150
|
-
}
|
151
|
-
}
|
152
|
-
|
153
|
-
async post(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
154
|
-
const { mode, enumType, dateType, optionalType, usedEnumNames } = options
|
155
|
-
const { pluginManager, plugin } = this.context
|
156
|
-
|
157
|
-
const type = this.resolve(operation)
|
158
|
-
|
159
|
-
const fileResolver: FileResolver = (name) => {
|
160
|
-
// Used when a react-query type(request, response, params) has an import of a global type
|
161
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } })
|
162
|
-
// refs import, will always been created with the SwaggerTS plugin, our global type
|
163
|
-
const resolvedTypeId = pluginManager.resolvePath({
|
164
|
-
baseName: `${name}.ts`,
|
165
|
-
pluginKey: plugin?.key,
|
166
|
-
})
|
167
|
-
|
168
|
-
return getRelativePath(root, resolvedTypeId)
|
169
|
-
}
|
170
|
-
|
171
|
-
const source = new TypeBuilder({
|
172
|
-
usedEnumNames,
|
173
|
-
fileResolver: mode === 'file' ? undefined : fileResolver,
|
174
|
-
withJSDocs: true,
|
175
|
-
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
176
|
-
enumType,
|
177
|
-
optionalType,
|
178
|
-
dateType,
|
179
|
-
})
|
180
|
-
.add(schemas.pathParams)
|
181
|
-
.add(schemas.queryParams)
|
182
|
-
.add(schemas.headerParams)
|
183
|
-
.add(schemas.request)
|
184
|
-
.add(schemas.response)
|
185
|
-
.add(schemas.errors)
|
186
|
-
.configure()
|
187
|
-
.print()
|
188
|
-
|
189
|
-
const combinedSchemaSource = this.#printCombinedSchema(type.name, operation, schemas)
|
190
|
-
|
191
|
-
return {
|
192
|
-
path: type.path,
|
193
|
-
baseName: type.baseName,
|
194
|
-
source: [source, combinedSchemaSource].join('\n'),
|
195
|
-
meta: {
|
196
|
-
pluginKey: plugin.key,
|
197
|
-
tag: operation.getTags()[0]?.name,
|
198
|
-
},
|
199
|
-
}
|
200
|
-
}
|
201
|
-
|
202
|
-
async put(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
203
|
-
return this.post(operation, schemas, options)
|
204
|
-
}
|
205
|
-
async patch(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
206
|
-
return this.post(operation, schemas, options)
|
207
|
-
}
|
208
|
-
async delete(operation: Operation, schemas: OperationSchemas, options: Options): Promise<KubbFile.File<FileMeta> | null> {
|
209
|
-
return this.post(operation, schemas, options)
|
210
|
-
}
|
211
|
-
}
|
package/src/generators/index.ts
DELETED