@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.
@@ -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 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 { getRelativePath, renderTemplate } from '@kubb/core/utils'
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 { camelCase, camelCaseTransformMerge, pascalCase, pascalCaseTransformMerge } from 'change-case'
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
- import type { OpenAPIV3, PluginOptions as SwaggerPluginOptions } from '@kubb/swagger'
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'] = ['schema', pluginName] satisfies 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
- exportAs,
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
- kind: 'schema',
39
- validate(plugins) {
40
- pluginsOptions = PluginManager.getDependedPlugins<SwaggerPluginOptions>(plugins, [swaggerPluginName])
41
-
42
- return true
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, { delimiter: '', transform: camelCaseTransformMerge })
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, { delimiter: '', stripRegexp: /[^A-Z0-9$]/gi, transform: pascalCaseTransformMerge })
65
+ const resolvedName = pascalCase(name)
66
66
 
67
67
  if (type) {
68
68
  return transformers?.name?.(resolvedName, type) || resolvedName
@@ -78,45 +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] = pluginsOptions
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
- // keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
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
- })
100
-
101
- const root = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key })
87
+ const mode = FileManager.getMode(path.resolve(root, output.path))
88
+ const builder = new TypeBuilder(this.plugin.options, { oas, pluginManager: this.pluginManager })
102
89
 
103
- return getRelativePath(root, resolvedTypeId)
104
- },
105
- withJSDocs: true,
106
- enumType,
107
- dateType,
108
- optionalType,
109
- }).configure()
110
- Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
111
- // generate and pass through new code back to the core so it can be write to that file
112
- return builder.add({
113
- schema,
114
- name,
115
- })
116
- })
90
+ builder.add(
91
+ Object.entries(schemas).map(([name, schema]: [string, OasTypes.SchemaObject]) => ({ name, schema })),
92
+ )
117
93
 
118
- const mapFolderSchema = async ([name]: [string, OpenAPIV3.SchemaObject]) => {
119
- const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key })
94
+ if (mode === 'directory') {
95
+ const mapFolderSchema = async ([name]: [string, OasTypes.SchemaObject]) => {
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)
120
99
 
121
100
  if (!resolvedPath) {
122
101
  return null
@@ -124,8 +103,9 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
124
103
 
125
104
  return this.addFile({
126
105
  path: resolvedPath,
127
- baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
128
- source: builder.print(name),
106
+ baseName,
107
+ source,
108
+ imports: imports.map(item => ({ ...item, root: resolvedPath })),
129
109
  meta: {
130
110
  pluginKey: this.plugin.key,
131
111
  },
@@ -138,47 +118,26 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
138
118
  }
139
119
 
140
120
  if (mode === 'file') {
141
- // outside the loop because we need to add files to just one instance to have the correct sorting, see refsSorter
142
- const builder = new TypeBuilder({
143
- usedEnumNames,
144
- resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
145
- withJSDocs: true,
146
- enumType,
147
- dateType,
148
- optionalType,
149
- }).configure()
150
- Object.entries(schemas).forEach(([name, schema]: [string, OpenAPIV3.SchemaObject]) => {
151
- // generate and pass through new code back to the core so it can be write to that file
152
- return builder.add({
153
- schema,
154
- name,
155
- })
156
- })
157
-
158
121
  const resolvedPath = this.resolvePath({ baseName: '', pluginKey: this.plugin.key })
122
+ const { source } = builder.build()
123
+
159
124
  if (!resolvedPath) {
160
125
  return
161
126
  }
162
127
 
163
128
  await this.addFile({
164
129
  path: resolvedPath,
165
- baseName: output as KubbFile.BaseName,
166
- source: builder.print(),
130
+ baseName: output.path as KubbFile.BaseName,
131
+ source,
132
+ imports: [],
167
133
  meta: {
168
134
  pluginKey: this.plugin.key,
169
135
  },
170
- validate: false,
171
136
  })
172
137
  }
173
138
 
174
139
  const operationGenerator = new OperationGenerator(
175
- {
176
- mode,
177
- enumType,
178
- dateType,
179
- optionalType,
180
- usedEnumNames,
181
- },
140
+ this.plugin.options,
182
141
  {
183
142
  oas,
184
143
  pluginManager: this.pluginManager,
@@ -187,6 +146,7 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
187
146
  exclude,
188
147
  include,
189
148
  override,
149
+ mode,
190
150
  },
191
151
  )
192
152
 
@@ -199,31 +159,18 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
199
159
  }
200
160
 
201
161
  const root = path.resolve(this.config.root, this.config.output.path)
162
+ const { exportType = 'barrel' } = output
202
163
 
203
- await this.fileManager.addIndexes({
204
- root,
205
- extName: '.ts',
206
- meta: { pluginKey: this.plugin.key },
207
- options: {
208
- map: (file) => {
209
- return {
210
- ...file,
211
- exports: file.exports?.map((item) => {
212
- if (exportAs) {
213
- return {
214
- ...item,
215
- name: exportAs,
216
- asAlias: !!exportAs,
217
- }
218
- }
219
- return item
220
- }),
221
- }
222
- },
164
+ if (exportType === 'barrel') {
165
+ await this.fileManager.addIndexes({
166
+ root,
223
167
  output,
224
- isTypeOnly: true,
225
- },
226
- })
168
+ meta: { pluginKey: this.plugin.key },
169
+ options: {
170
+ isTypeOnly: true,
171
+ },
172
+ })
173
+ }
227
174
  },
228
175
  }
229
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
- * 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
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
- export type PluginOptions = PluginFactoryOptions<'swagger-ts', 'schema', Options, Options, never, ResolvePathOptions>
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,93 +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 } from '@kubb/swagger'
12
-
13
- type Options = {
14
- usedEnumNames: Record<string, number>
15
-
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
- })
53
- const sources = generator.build({
54
- schema: operationSchema.schema,
55
- baseName: operationSchema.name,
56
- description: operationSchema.description,
57
- keysToOmit: operationSchema.keysToOmit,
58
- })
59
-
60
- return {
61
- import: {
62
- refs: generator.refs,
63
- name: operationSchema.name,
64
- },
65
- sources,
66
- }
67
- })
68
- .sort(refsSorter)
69
-
70
- generated.forEach((item) => {
71
- codes.push(print(item.sources))
72
- })
73
-
74
- if (this.options.withImports) {
75
- const importsGenerator = new ImportsGenerator({ fileResolver: this.options.fileResolver })
76
- const importMeta = importsGenerator.build(generated.map((item) => item.import))
77
-
78
- if (importMeta) {
79
- const nodes = importMeta.map((item) => {
80
- return factory.createImportDeclaration({
81
- name: [{ propertyName: item.ref.propertyName }],
82
- path: item.path,
83
- isTypeOnly: true,
84
- })
85
- })
86
-
87
- codes.unshift(print(nodes))
88
- }
89
- }
90
-
91
- return transformers.combineCodes(codes)
92
- }
93
- }
@@ -1 +0,0 @@
1
- export * from './TypeBuilder.ts'