@kubb/swagger-ts 2.0.0-canary.20231030T125204 → 2.0.1
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/chunk-N6FDP2EJ.cjs +784 -0
- package/dist/chunk-N6FDP2EJ.cjs.map +1 -0
- package/dist/chunk-TCUL2H74.js +757 -0
- package/dist/chunk-TCUL2H74.js.map +1 -0
- package/dist/components.cjs +20 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +69 -0
- package/dist/components.d.ts +69 -0
- package/dist/components.js +6 -0
- package/dist/components.js.map +1 -0
- package/dist/index-sycg8owy.d.cts +392 -0
- package/dist/index-sycg8owy.d.ts +392 -0
- package/dist/index.cjs +14 -652
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -78
- package/dist/index.d.ts +11 -78
- package/dist/index.js +3 -625
- package/dist/index.js.map +1 -1
- package/dist/oas.cjs +4 -0
- package/dist/oas.cjs.map +1 -0
- package/dist/oas.d.cts +6 -0
- package/dist/oas.d.ts +6 -0
- package/dist/oas.js +5 -0
- package/dist/oas.js.map +1 -0
- package/dist/types-IAThMYCO.d.cts +105 -0
- package/dist/types-IAThMYCO.d.ts +105 -0
- package/package.json +27 -18
- package/src/OperationGenerator.tsx +63 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/TypeGenerator.ts +396 -0
- package/src/components/Mutation.tsx +138 -0
- package/src/components/Oas.tsx +84 -0
- package/src/components/Query.tsx +137 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +6 -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 +174 -0
- package/src/types.ts +110 -0
- package/dist/hooks.cjs +0 -656
- package/dist/hooks.cjs.map +0 -1
- package/dist/hooks.d.cts +0 -6
- package/dist/hooks.d.ts +0 -6
- package/dist/hooks.js +0 -634
- package/dist/hooks.js.map +0 -1
@@ -0,0 +1,158 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
3
|
+
|
4
|
+
import type { Call, Objects, Tuples } from 'hotscript'
|
5
|
+
|
6
|
+
namespace Checks {
|
7
|
+
export type Security = { security: { [key: string]: any }[] }
|
8
|
+
|
9
|
+
export namespace AuthParams {
|
10
|
+
export type Basic =
|
11
|
+
| {
|
12
|
+
type: 'http'
|
13
|
+
scheme: 'basic'
|
14
|
+
}
|
15
|
+
| { type: 'basic' }
|
16
|
+
export type Bearer =
|
17
|
+
| {
|
18
|
+
type: 'http'
|
19
|
+
scheme: 'bearer'
|
20
|
+
}
|
21
|
+
| { type: 'bearer' }
|
22
|
+
|
23
|
+
export type OAuth2 = {
|
24
|
+
type: 'oauth2'
|
25
|
+
}
|
26
|
+
export type ApiKey = {
|
27
|
+
type: 'apiKey'
|
28
|
+
in: 'header'
|
29
|
+
}
|
30
|
+
}
|
31
|
+
|
32
|
+
export namespace AuthName {
|
33
|
+
export type Basic = `basic${string}`
|
34
|
+
export type Bearer = `bearer${string}`
|
35
|
+
export type OAuth2 = `oauth${string}`
|
36
|
+
}
|
37
|
+
}
|
38
|
+
|
39
|
+
type SecuritySchemeName<T extends Checks.Security> = Call<
|
40
|
+
Tuples.Map<Objects.Keys>,
|
41
|
+
T['security']
|
42
|
+
>[number]
|
43
|
+
|
44
|
+
namespace AuthParams {
|
45
|
+
export type Basic<TSecurityScheme> = TSecurityScheme extends Checks.AuthParams.Basic ? {
|
46
|
+
headers: {
|
47
|
+
/**
|
48
|
+
* `Authorization` header is required for basic authentication
|
49
|
+
* @see https://en.wikipedia.org/wiki/Basic_access_authentication
|
50
|
+
*
|
51
|
+
* It contains the word `Basic` followed by a space and a base64-encoded string `username:password`
|
52
|
+
*
|
53
|
+
* @example
|
54
|
+
* ```
|
55
|
+
* Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
|
56
|
+
* ```
|
57
|
+
*/
|
58
|
+
Authorization: `Basic ${string}`
|
59
|
+
}
|
60
|
+
}
|
61
|
+
: {}
|
62
|
+
|
63
|
+
export type Bearer<TSecurityScheme> = TSecurityScheme extends Checks.AuthParams.Bearer ? {
|
64
|
+
/**
|
65
|
+
* `Authorization` header is required for bearer authentication
|
66
|
+
* @see https://swagger.io/docs/specification/authentication/bearer-authentication/
|
67
|
+
*/
|
68
|
+
headers: {
|
69
|
+
/**
|
70
|
+
* It contains the word `Bearer` followed by a space and the token
|
71
|
+
*
|
72
|
+
* @example
|
73
|
+
* ```
|
74
|
+
* Authorization: Bearer {token}
|
75
|
+
* ```
|
76
|
+
*/
|
77
|
+
Authorization: `Bearer ${string}`
|
78
|
+
}
|
79
|
+
}
|
80
|
+
: {}
|
81
|
+
|
82
|
+
export type ApiKey<TSecurityScheme> = TSecurityScheme extends Checks.AuthParams.ApiKey & { name: infer TApiKeyHeaderName } ? {
|
83
|
+
headers: {
|
84
|
+
/**
|
85
|
+
* Header required for API key authentication
|
86
|
+
*/
|
87
|
+
[THeaderName in TApiKeyHeaderName extends string ? TApiKeyHeaderName : never]: string
|
88
|
+
}
|
89
|
+
}
|
90
|
+
: TSecurityScheme extends {
|
91
|
+
type: 'apiKey'
|
92
|
+
in: 'query'
|
93
|
+
name: infer TApiKeyQueryName
|
94
|
+
} ? {
|
95
|
+
query: {
|
96
|
+
/**
|
97
|
+
* Query parameter required for API key authentication
|
98
|
+
*/
|
99
|
+
[TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string
|
100
|
+
}
|
101
|
+
}
|
102
|
+
: {}
|
103
|
+
|
104
|
+
export type OAuth2<TSecurityScheme> = TSecurityScheme extends Checks.AuthParams.OAuth2 ? {
|
105
|
+
/**
|
106
|
+
* `Authorization` header is required for OAuth2.
|
107
|
+
*/
|
108
|
+
headers: {
|
109
|
+
/**
|
110
|
+
* The access token string as issued by the authorization server.
|
111
|
+
* @example `Authorization: Bearer <access_token>`
|
112
|
+
*/
|
113
|
+
Authorization: `Bearer ${string}`
|
114
|
+
}
|
115
|
+
}
|
116
|
+
: {}
|
117
|
+
}
|
118
|
+
|
119
|
+
type OASSecurityParams<TSecurityScheme> =
|
120
|
+
& AuthParams.Basic<TSecurityScheme>
|
121
|
+
& AuthParams.Bearer<TSecurityScheme>
|
122
|
+
& AuthParams.ApiKey<TSecurityScheme>
|
123
|
+
& AuthParams.OAuth2<TSecurityScheme>
|
124
|
+
|
125
|
+
export type SecurityParamsBySecurityRef<TOAS, TSecurityObj> = TSecurityObj extends Checks.Security ? TOAS extends
|
126
|
+
| {
|
127
|
+
components: {
|
128
|
+
securitySchemes: {
|
129
|
+
[
|
130
|
+
TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj>
|
131
|
+
: never
|
132
|
+
]: infer TSecurityScheme
|
133
|
+
}
|
134
|
+
}
|
135
|
+
}
|
136
|
+
| {
|
137
|
+
securityDefinitions: {
|
138
|
+
[
|
139
|
+
TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj>
|
140
|
+
: never
|
141
|
+
]: infer TSecurityScheme
|
142
|
+
}
|
143
|
+
} ? OASSecurityParams<TSecurityScheme>
|
144
|
+
// OAS may have a bad reference to a security scheme
|
145
|
+
// So we can assume it
|
146
|
+
: SecuritySchemeName<TSecurityObj> extends Checks.AuthName.Basic ? AuthParams.Basic<{
|
147
|
+
type: 'http'
|
148
|
+
scheme: 'basic'
|
149
|
+
}>
|
150
|
+
: SecuritySchemeName<TSecurityObj> extends Checks.AuthName.Bearer ? AuthParams.Bearer<{
|
151
|
+
type: 'http'
|
152
|
+
scheme: 'bearer'
|
153
|
+
}>
|
154
|
+
: SecuritySchemeName<TSecurityObj> extends Checks.AuthName.OAuth2 ? AuthParams.OAuth2<{
|
155
|
+
type: 'oauth2'
|
156
|
+
}>
|
157
|
+
: {}
|
158
|
+
: {}
|
package/src/plugin.ts
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
import path from 'node:path'
|
2
|
+
|
3
|
+
import { createPlugin, FileManager, PluginManager } from '@kubb/core'
|
4
|
+
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
5
|
+
import { renderTemplate } from '@kubb/core/utils'
|
6
|
+
import { pluginName as swaggerPluginName } from '@kubb/swagger'
|
7
|
+
|
8
|
+
import { OperationGenerator } from './OperationGenerator.tsx'
|
9
|
+
import { TypeBuilder } from './TypeBuilder.ts'
|
10
|
+
|
11
|
+
import type { KubbFile, KubbPlugin } from '@kubb/core'
|
12
|
+
import type { PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
|
13
|
+
import type { OasTypes } from '@kubb/swagger/oas'
|
14
|
+
import type { PluginOptions } from './types.ts'
|
15
|
+
export const pluginName = 'swagger-ts' satisfies PluginOptions['name']
|
16
|
+
export const pluginKey: PluginOptions['key'] = [pluginName] satisfies PluginOptions['key']
|
17
|
+
|
18
|
+
export const definePlugin = createPlugin<PluginOptions>((options) => {
|
19
|
+
const {
|
20
|
+
output = { path: 'types' },
|
21
|
+
group,
|
22
|
+
exclude = [],
|
23
|
+
include,
|
24
|
+
override = [],
|
25
|
+
enumType = 'asConst',
|
26
|
+
dateType = 'string',
|
27
|
+
optionalType = 'questionToken',
|
28
|
+
transformers = {},
|
29
|
+
oasType = false,
|
30
|
+
} = options
|
31
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
32
|
+
|
33
|
+
return {
|
34
|
+
name: pluginName,
|
35
|
+
options: {
|
36
|
+
transformers,
|
37
|
+
dateType,
|
38
|
+
enumType,
|
39
|
+
optionalType,
|
40
|
+
oasType,
|
41
|
+
// keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
|
42
|
+
usedEnumNames: {},
|
43
|
+
},
|
44
|
+
pre: [swaggerPluginName],
|
45
|
+
resolvePath(baseName, directory, options) {
|
46
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
47
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
48
|
+
|
49
|
+
if (mode === 'file') {
|
50
|
+
/**
|
51
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
52
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
53
|
+
*/
|
54
|
+
return path.resolve(root, output.path)
|
55
|
+
}
|
56
|
+
|
57
|
+
if (options?.tag && group?.type === 'tag') {
|
58
|
+
const tag = camelCase(options.tag)
|
59
|
+
|
60
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
61
|
+
}
|
62
|
+
|
63
|
+
return path.resolve(root, output.path, baseName)
|
64
|
+
},
|
65
|
+
resolveName(name, type) {
|
66
|
+
const resolvedName = pascalCase(name)
|
67
|
+
|
68
|
+
if (type) {
|
69
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
70
|
+
}
|
71
|
+
|
72
|
+
return resolvedName
|
73
|
+
},
|
74
|
+
async writeFile(source, writePath) {
|
75
|
+
if (!writePath.endsWith('.ts') || !source) {
|
76
|
+
return
|
77
|
+
}
|
78
|
+
|
79
|
+
return this.fileManager.write(source, writePath, { sanity: false })
|
80
|
+
},
|
81
|
+
async buildStart() {
|
82
|
+
const [swaggerPlugin]: [KubbPlugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [swaggerPluginName])
|
83
|
+
|
84
|
+
const oas = await swaggerPlugin.api.getOas()
|
85
|
+
|
86
|
+
const schemas = await swaggerPlugin.api.getSchemas()
|
87
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
88
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
89
|
+
const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager })
|
90
|
+
|
91
|
+
builder.add(
|
92
|
+
Object.entries(schemas).map(([name, schema]: [string, OasTypes.SchemaObject]) => ({ name, schema })),
|
93
|
+
)
|
94
|
+
|
95
|
+
if (mode === 'directory') {
|
96
|
+
const mapFolderSchema = async ([name]: [string, OasTypes.SchemaObject]) => {
|
97
|
+
const baseName = `${this.resolveName({ name, pluginKey: this.plugin.key, type: 'file' })}.ts` as const
|
98
|
+
const resolvedPath = this.resolvePath({ baseName, pluginKey: this.plugin.key })
|
99
|
+
const { source, imports } = builder.build(name)
|
100
|
+
|
101
|
+
if (!resolvedPath) {
|
102
|
+
return null
|
103
|
+
}
|
104
|
+
|
105
|
+
return this.addFile({
|
106
|
+
path: resolvedPath,
|
107
|
+
baseName,
|
108
|
+
source,
|
109
|
+
imports: imports.map(item => ({ ...item, root: resolvedPath })),
|
110
|
+
meta: {
|
111
|
+
pluginKey: this.plugin.key,
|
112
|
+
},
|
113
|
+
})
|
114
|
+
}
|
115
|
+
|
116
|
+
const promises = Object.entries(schemas).map(mapFolderSchema)
|
117
|
+
|
118
|
+
await Promise.all(promises)
|
119
|
+
}
|
120
|
+
|
121
|
+
if (mode === 'file') {
|
122
|
+
const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
|
123
|
+
const { source } = builder.build()
|
124
|
+
|
125
|
+
if (!resolvedPath) {
|
126
|
+
return
|
127
|
+
}
|
128
|
+
|
129
|
+
await this.addFile({
|
130
|
+
path: resolvedPath,
|
131
|
+
baseName: output.path as KubbFile.BaseName,
|
132
|
+
source,
|
133
|
+
imports: [],
|
134
|
+
meta: {
|
135
|
+
pluginKey: this.plugin.key,
|
136
|
+
},
|
137
|
+
})
|
138
|
+
}
|
139
|
+
|
140
|
+
const operationGenerator = new OperationGenerator(
|
141
|
+
this.plugin.options,
|
142
|
+
{
|
143
|
+
oas,
|
144
|
+
pluginManager: this.pluginManager,
|
145
|
+
plugin: this.plugin,
|
146
|
+
contentType: swaggerPlugin.api.contentType,
|
147
|
+
exclude,
|
148
|
+
include,
|
149
|
+
override,
|
150
|
+
mode,
|
151
|
+
},
|
152
|
+
)
|
153
|
+
|
154
|
+
const files = await operationGenerator.build()
|
155
|
+
await this.addFile(...files)
|
156
|
+
},
|
157
|
+
async buildEnd() {
|
158
|
+
if (this.config.output.write === false) {
|
159
|
+
return
|
160
|
+
}
|
161
|
+
|
162
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
163
|
+
const { exportType = 'barrel' } = output
|
164
|
+
|
165
|
+
if (exportType === 'barrel') {
|
166
|
+
await this.fileManager.addIndexes({
|
167
|
+
root,
|
168
|
+
output,
|
169
|
+
meta: { pluginKey: this.plugin.key },
|
170
|
+
})
|
171
|
+
}
|
172
|
+
},
|
173
|
+
}
|
174
|
+
})
|
package/src/types.ts
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
import type { KubbFile, KubbPlugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
2
|
+
import type { AppMeta as SwaggerAppMeta, Exclude, Include, Override, ResolvePathOptions } from '@kubb/swagger'
|
3
|
+
|
4
|
+
export type Options = {
|
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
|
+
}
|
26
|
+
/**
|
27
|
+
* Group the TypeScript types based on the provided name.
|
28
|
+
*/
|
29
|
+
group?: {
|
30
|
+
/**
|
31
|
+
* Tag will group based on the operation tag inside the Swagger file.
|
32
|
+
*/
|
33
|
+
type: 'tag'
|
34
|
+
/**
|
35
|
+
* Relative path to save the grouped TypeScript Types.
|
36
|
+
*
|
37
|
+
* `{{tag}}` will be replaced by the current tagName.
|
38
|
+
* @example `${output}/{{tag}}Controller` => `models/PetController`
|
39
|
+
* @default `${output}/{{tag}}Controller`
|
40
|
+
*/
|
41
|
+
output?: string
|
42
|
+
}
|
43
|
+
/**
|
44
|
+
* Array containing exclude paramaters to exclude/skip tags/operations/methods/paths.
|
45
|
+
*/
|
46
|
+
exclude?: Array<Exclude>
|
47
|
+
/**
|
48
|
+
* Array containing include paramaters to include tags/operations/methods/paths.
|
49
|
+
*/
|
50
|
+
include?: Array<Include>
|
51
|
+
/**
|
52
|
+
* Array containing override paramaters to override `options` based on tags/operations/methods/paths.
|
53
|
+
*/
|
54
|
+
override?: Array<Override<Options>>
|
55
|
+
/**
|
56
|
+
* Choose to use `enum` or `as const` for enums
|
57
|
+
* @default 'asConst'
|
58
|
+
*/
|
59
|
+
enumType?: 'enum' | 'asConst' | 'asPascalConst'
|
60
|
+
/**
|
61
|
+
* Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
|
62
|
+
* @default 'string'
|
63
|
+
*/
|
64
|
+
dateType?: 'string' | 'date'
|
65
|
+
/**
|
66
|
+
* Choose what to use as mode for an optional value.
|
67
|
+
* @examples 'questionToken': type?: string
|
68
|
+
* @examples 'undefined': type: string | undefined
|
69
|
+
* @examples 'questionTokenAndUndefined': type?: string | undefined
|
70
|
+
* @default 'questionToken'
|
71
|
+
*/
|
72
|
+
optionalType?: 'questionToken' | 'undefined' | 'questionTokenAndUndefined'
|
73
|
+
transformers?: {
|
74
|
+
/**
|
75
|
+
* Customize the names based on the type that is provided by the plugin.
|
76
|
+
*/
|
77
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
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>
|
92
|
+
}
|
93
|
+
|
94
|
+
export type FileMeta = {
|
95
|
+
pluginKey?: KubbPlugin['key']
|
96
|
+
name?: string
|
97
|
+
tag?: string
|
98
|
+
}
|
99
|
+
|
100
|
+
type AppMeta = SwaggerAppMeta
|
101
|
+
|
102
|
+
export type PluginOptions = PluginFactoryOptions<'swagger-ts', Options, ResolvedOptions, never, ResolvePathOptions, AppMeta>
|
103
|
+
|
104
|
+
declare module '@kubb/core' {
|
105
|
+
export interface _Register {
|
106
|
+
['@kubb/swagger-ts']: PluginOptions
|
107
|
+
}
|
108
|
+
}
|
109
|
+
// external packages
|
110
|
+
export * as Oas from './oas/index.ts'
|