@kubb/swagger-ts 2.11.0 → 2.12.0

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.
@@ -2,10 +2,7 @@
2
2
  import type { OasTypes } from '@kubb/swagger/oas'
3
3
  import type { SplitByDelimiter, TupleToUnion } from '@kubb/types'
4
4
  import type { Pipe, Strings, Tuples } from 'hotscript'
5
- import type {
6
- FromSchema,
7
- JSONSchema,
8
- } from 'json-schema-to-ts'
5
+ import type { FromSchema, JSONSchema } from 'json-schema-to-ts'
9
6
  import type { MethodMap, ParamMap, PathMap } from './mappers.ts'
10
7
  import type { SecurityParamsBySecurityRef } from './security.ts'
11
8
 
@@ -35,12 +32,7 @@ type Checks = {
35
32
 
36
33
  type ExtractPathParamsWithPattern<TPath extends string> = Pipe<
37
34
  TPath,
38
- [
39
- Strings.Split<'/'>,
40
- Tuples.Filter<Strings.StartsWith<':'>>,
41
- Tuples.Map<Strings.Trim<':'>>,
42
- Tuples.ToUnion,
43
- ]
35
+ [Strings.Split<'/'>, Tuples.Filter<Strings.StartsWith<':'>>, Tuples.Map<Strings.Trim<':'>>, Tuples.ToUnion]
44
36
  >
45
37
 
46
38
  type IsPathParameter<T extends string> = T extends `{${infer U}}` ? U : never
@@ -55,114 +47,90 @@ type ExtractSubSegments<T extends any[]> = {
55
47
  [K in keyof T]: SplitByDelimiter<T[K], ';'>
56
48
  }
57
49
 
58
- type ExtractPathParamsWithBrackets<TPath extends string> = TupleToUnion<
59
- ExtractPathParameters<ExtractSubSegments<ExtractSegments<TPath>>[number]>
60
- >
50
+ type ExtractPathParamsWithBrackets<TPath extends string> = TupleToUnion<ExtractPathParameters<ExtractSubSegments<ExtractSegments<TPath>>[number]>>
61
51
 
62
- export type RequestParams<
63
- TOAS extends OasTypes.OASDocument,
64
- TPath extends keyof PathMap<TOAS>,
65
- TMethod extends keyof MethodMap<TOAS, TPath>,
66
- > =
67
- & (MethodMap<TOAS, TPath>[TMethod] extends Checks['RequestBodyJson'] ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required'] ? {
52
+ export type RequestParams<TOAS extends OasTypes.OASDocument, TPath extends keyof PathMap<TOAS>, TMethod extends keyof MethodMap<TOAS, TPath>> = (MethodMap<
53
+ TOAS,
54
+ TPath
55
+ >[TMethod] extends Checks['RequestBodyJson']
56
+ ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required']
57
+ ? {
68
58
  /**
69
59
  * The request body in JSON is required for this request.
70
60
  *
71
61
  * The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
72
62
  */
73
- json: FromSchema<
74
- MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']
75
- >
63
+ json: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>
76
64
  }
77
65
  : {
78
- /**
79
- * The request body in JSON is optional for this request.
80
- *
81
- * The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
82
- */
83
- json?: FromSchema<
84
- MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']
85
- >
86
- }
87
- : MethodMap<TOAS, TPath>[TMethod] extends Checks['RequestBodyFormData'] ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required'] ? {
66
+ /**
67
+ * The request body in JSON is optional for this request.
68
+ *
69
+ * The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
70
+ */
71
+ json?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']>
72
+ }
73
+ : MethodMap<TOAS, TPath>[TMethod] extends Checks['RequestBodyFormData']
74
+ ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required']
75
+ ? {
88
76
  /**
89
77
  * The request body in multipart/form-data is required for this request.
90
78
  *
91
79
  * The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
92
80
  */
93
- formData: FromSchema<
94
- MethodMap<
95
- TOAS,
96
- TPath
97
- >[TMethod]['requestBody']['content']['multipart/form-data']['schema']
98
- >
81
+ formData: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>
99
82
  }
100
83
  : {
101
- /**
102
- * The request body in multipart/form-data is optional for this request.
103
- *
104
- * The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
105
- */
106
- formData?: FromSchema<
107
- MethodMap<
108
- TOAS,
109
- TPath
110
- >[TMethod]['requestBody']['content']['multipart/form-data']['schema']
111
- >
112
- }
113
- : MethodMap<TOAS, TPath>[TMethod] extends Checks['RequestBodyFormEncoded'] ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required'] ? {
114
84
  /**
115
- * The request body in application/x-www-form-urlencoded is required for this request.
85
+ * The request body in multipart/form-data is optional for this request.
116
86
  *
117
- * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
87
+ * The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
118
88
  */
119
- formUrlEncoded: FromSchema<
120
- MethodMap<
121
- TOAS,
122
- TPath
123
- >[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
124
- >
89
+ formData?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['multipart/form-data']['schema']>
125
90
  }
126
- : {
91
+ : MethodMap<TOAS, TPath>[TMethod] extends Checks['RequestBodyFormEncoded']
92
+ ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks['Required']
93
+ ? {
94
+ /**
95
+ * The request body in application/x-www-form-urlencoded is required for this request.
96
+ *
97
+ * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
98
+ */
99
+ formUrlEncoded: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>
100
+ }
101
+ : {
102
+ /**
103
+ * The request body in application/x-www-form-urlencoded is optional for this request.
104
+ *
105
+ * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
106
+ */
107
+ formUrlEncoded?: FromSchema<MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']>
108
+ }
109
+ : {}) &
110
+ (MethodMap<TOAS, TPath>[TMethod] extends Checks['Parameters'] ? ParamMap<MethodMap<TOAS, TPath>[TMethod]['parameters']> : {}) & // If there is any parameters defined in path but not in the parameters array, we should add them to the params
111
+ (TPath extends Checks['PathBrackets']
112
+ ? {
127
113
  /**
128
- * The request body in application/x-www-form-urlencoded is optional for this request.
114
+ * Parameters defined in the path are required for this request.
115
+ *
116
+ * The value of `params` will be used to replace the path parameters.
129
117
  *
130
- * The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
118
+ * For example if path is `/todos/{id}` and `params` is `{ id: '1' }`, the path will be `/todos/1`
131
119
  */
132
- formUrlEncoded?: FromSchema<
133
- MethodMap<
134
- TOAS,
135
- TPath
136
- >[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
137
- >
120
+ params: Record<ExtractPathParamsWithBrackets<TPath>, string | number | bigint | boolean>
138
121
  }
139
- : {})
140
- & (MethodMap<TOAS, TPath>[TMethod] extends Checks['Parameters'] ? ParamMap<MethodMap<TOAS, TPath>[TMethod]['parameters']>
141
- : {})
142
- & // If there is any parameters defined in path but not in the parameters array, we should add them to the params
143
- (TPath extends Checks['PathBrackets'] ? {
144
- /**
145
- * Parameters defined in the path are required for this request.
146
- *
147
- * The value of `params` will be used to replace the path parameters.
148
- *
149
- * For example if path is `/todos/{id}` and `params` is `{ id: '1' }`, the path will be `/todos/1`
150
- */
151
- params: Record<ExtractPathParamsWithBrackets<TPath>, string | number | bigint | boolean>
152
- }
153
- : {})
154
- & (TPath extends Checks['PathPattern'] ? {
155
- /**
156
- * Parameters defined in the path are required for this request.
157
- *
158
- * The value of `params` will be used to replace the path parameters.
159
- *
160
- * For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
161
- */
162
- params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>
163
- }
164
- : {})
165
- & // Respect security definitions in path object
166
- SecurityParamsBySecurityRef<TOAS, MethodMap<TOAS, TPath>[TMethod]>
167
- & // Respect global security definitions
122
+ : {}) &
123
+ (TPath extends Checks['PathPattern']
124
+ ? {
125
+ /**
126
+ * Parameters defined in the path are required for this request.
127
+ *
128
+ * The value of `params` will be used to replace the path parameters.
129
+ *
130
+ * For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
131
+ */
132
+ params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>
133
+ }
134
+ : {}) & // Respect security definitions in path object
135
+ SecurityParamsBySecurityRef<TOAS, MethodMap<TOAS, TPath>[TMethod]> & // Respect global security definitions
168
136
  SecurityParamsBySecurityRef<TOAS, TOAS>
@@ -1,7 +1,5 @@
1
1
  import type { OasTypes } from '@kubb/swagger/oas'
2
- import type {
3
- FromSchema,
4
- } from 'json-schema-to-ts'
2
+ import type { FromSchema } from 'json-schema-to-ts'
5
3
  import type { MethodMap, PathMap, StatusMap } from './mappers.ts'
6
4
 
7
5
  type Checks = {
@@ -20,14 +18,8 @@ type JSONResponseSchema<
20
18
  TPath extends keyof PathMap<TOAS>,
21
19
  TMethod extends keyof MethodMap<TOAS, TPath>,
22
20
  TStatus extends keyof StatusMap<TOAS, TPath, TMethod>,
23
- > = StatusMap<TOAS, TPath, TMethod>[TStatus] extends Checks['Content'] ? ResponseSchemas<TOAS, TPath, TMethod, TStatus>[
24
- keyof ResponseSchemas<
25
- TOAS,
26
- TPath,
27
- TMethod,
28
- TStatus
29
- >
30
- ]['schema']
21
+ > = StatusMap<TOAS, TPath, TMethod>[TStatus] extends Checks['Content']
22
+ ? ResponseSchemas<TOAS, TPath, TMethod, TStatus>[keyof ResponseSchemas<TOAS, TPath, TMethod, TStatus>]['schema']
31
23
  : StatusMap<TOAS, TPath, TMethod>[TStatus]['schema']
32
24
 
33
25
  export type Response<
@@ -9,15 +9,15 @@ type Checks = {
9
9
  AuthParams: {
10
10
  Basic:
11
11
  | {
12
- type: 'http'
13
- scheme: 'basic'
14
- }
12
+ type: 'http'
13
+ scheme: 'basic'
14
+ }
15
15
  | { type: 'basic' }
16
16
  Bearer:
17
17
  | {
18
- type: 'http'
19
- scheme: 'bearer'
20
- }
18
+ type: 'http'
19
+ scheme: 'bearer'
20
+ }
21
21
  | { type: 'bearer' }
22
22
 
23
23
  OAuth2: {
@@ -36,123 +36,125 @@ type Checks = {
36
36
  }
37
37
  }
38
38
 
39
- type SecuritySchemeName<T extends Checks['Security']> = Call<
40
- Tuples.Map<Objects.Keys>,
41
- T['security']
42
- >[number]
39
+ type SecuritySchemeName<T extends Checks['Security']> = Call<Tuples.Map<Objects.Keys>, T['security']>[number]
43
40
 
44
41
  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}`
42
+ export type Basic<TSecurityScheme> = TSecurityScheme extends Checks['AuthParams']['Basic']
43
+ ? {
44
+ headers: {
45
+ /**
46
+ * `Authorization` header is required for basic authentication
47
+ * @see https://en.wikipedia.org/wiki/Basic_access_authentication
48
+ *
49
+ * It contains the word `Basic` followed by a space and a base64-encoded string `username:password`
50
+ *
51
+ * @example
52
+ * ```
53
+ * Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
54
+ * ```
55
+ */
56
+ Authorization: `Basic ${string}`
57
+ }
59
58
  }
60
- }
61
59
  : {}
62
60
 
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: {
61
+ export type Bearer<TSecurityScheme> = TSecurityScheme extends Checks['AuthParams']['Bearer']
62
+ ? {
69
63
  /**
70
- * It contains the word `Bearer` followed by a space and the token
71
- *
72
- * @example
73
- * ```
74
- * Authorization: Bearer {token}
75
- * ```
64
+ * `Authorization` header is required for bearer authentication
65
+ * @see https://swagger.io/docs/specification/authentication/bearer-authentication/
76
66
  */
77
- Authorization: `Bearer ${string}`
67
+ headers: {
68
+ /**
69
+ * It contains the word `Bearer` followed by a space and the token
70
+ *
71
+ * @example
72
+ * ```
73
+ * Authorization: Bearer {token}
74
+ * ```
75
+ */
76
+ Authorization: `Bearer ${string}`
77
+ }
78
78
  }
79
- }
80
79
  : {}
81
80
 
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: {
81
+ export type ApiKey<TSecurityScheme> = TSecurityScheme extends Checks['AuthParams']['ApiKey'] & {
82
+ name: infer TApiKeyHeaderName
83
+ }
84
+ ? {
85
+ headers: {
96
86
  /**
97
- * Query parameter required for API key authentication
87
+ * Header required for API key authentication
98
88
  */
99
- [TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string
89
+ [THeaderName in TApiKeyHeaderName extends string ? TApiKeyHeaderName : never]: string
100
90
  }
101
91
  }
102
- : {}
92
+ : TSecurityScheme extends {
93
+ type: 'apiKey'
94
+ in: 'query'
95
+ name: infer TApiKeyQueryName
96
+ }
97
+ ? {
98
+ query: {
99
+ /**
100
+ * Query parameter required for API key authentication
101
+ */
102
+ [TQueryName in TApiKeyQueryName extends string ? TApiKeyQueryName : never]: string
103
+ }
104
+ }
105
+ : {}
103
106
 
104
- export type OAuth2<TSecurityScheme> = TSecurityScheme extends Checks['AuthParams']['OAuth2'] ? {
105
- /**
106
- * `Authorization` header is required for OAuth2.
107
- */
108
- headers: {
107
+ export type OAuth2<TSecurityScheme> = TSecurityScheme extends Checks['AuthParams']['OAuth2']
108
+ ? {
109
109
  /**
110
- * The access token string as issued by the authorization server.
111
- * @example `Authorization: Bearer <access_token>`
110
+ * `Authorization` header is required for OAuth2.
112
111
  */
113
- Authorization: `Bearer ${string}`
112
+ headers: {
113
+ /**
114
+ * The access token string as issued by the authorization server.
115
+ * @example `Authorization: Bearer <access_token>`
116
+ */
117
+ Authorization: `Bearer ${string}`
118
+ }
114
119
  }
115
- }
116
120
  : {}
117
121
  }
118
122
 
119
- type OASSecurityParams<TSecurityScheme> =
120
- & AuthParams.Basic<TSecurityScheme>
121
- & AuthParams.Bearer<TSecurityScheme>
122
- & AuthParams.ApiKey<TSecurityScheme>
123
- & AuthParams.OAuth2<TSecurityScheme>
123
+ type OASSecurityParams<TSecurityScheme> = AuthParams.Basic<TSecurityScheme> &
124
+ AuthParams.Bearer<TSecurityScheme> &
125
+ AuthParams.ApiKey<TSecurityScheme> &
126
+ AuthParams.OAuth2<TSecurityScheme>
124
127
 
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
128
+ export type SecurityParamsBySecurityRef<TOAS, TSecurityObj> = TSecurityObj extends Checks['Security']
129
+ ? TOAS extends
130
+ | {
131
+ components: {
132
+ securitySchemes: {
133
+ [TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme
134
+ }
135
+ }
133
136
  }
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
- : {}
137
+ | {
138
+ securityDefinitions: {
139
+ [TSecuritySchemeNameKey in SecuritySchemeName<TSecurityObj> extends string ? SecuritySchemeName<TSecurityObj> : never]: infer TSecurityScheme
140
+ }
141
+ }
142
+ ? OASSecurityParams<TSecurityScheme>
143
+ : // OAS may have a bad reference to a security scheme
144
+ // So we can assume it
145
+ SecuritySchemeName<TSecurityObj> extends Checks['AuthName']['Basic']
146
+ ? AuthParams.Basic<{
147
+ type: 'http'
148
+ scheme: 'basic'
149
+ }>
150
+ : SecuritySchemeName<TSecurityObj> extends Checks['AuthName']['Bearer']
151
+ ? AuthParams.Bearer<{
152
+ type: 'http'
153
+ scheme: 'bearer'
154
+ }>
155
+ : SecuritySchemeName<TSecurityObj> extends Checks['AuthName']['OAuth2']
156
+ ? AuthParams.OAuth2<{
157
+ type: 'oauth2'
158
+ }>
159
+ : {}
158
160
  : {}
package/src/plugin.ts CHANGED
@@ -88,35 +88,29 @@ export const definePlugin = createPlugin<PluginOptions>((options) => {
88
88
  const root = path.resolve(this.config.root, this.config.output.path)
89
89
  const mode = FileManager.getMode(path.resolve(root, output.path))
90
90
 
91
- const schemaGenerator = new SchemaGenerator(
92
- this.plugin.options,
93
- {
94
- oas,
95
- pluginManager: this.pluginManager,
96
- plugin: this.plugin,
97
- contentType: swaggerPlugin.api.contentType,
98
- include: undefined,
99
- mode,
100
- output: output.path,
101
- },
102
- )
91
+ const schemaGenerator = new SchemaGenerator(this.plugin.options, {
92
+ oas,
93
+ pluginManager: this.pluginManager,
94
+ plugin: this.plugin,
95
+ contentType: swaggerPlugin.api.contentType,
96
+ include: undefined,
97
+ mode,
98
+ output: output.path,
99
+ })
103
100
 
104
101
  const schemaFiles = await schemaGenerator.build()
105
102
  await this.addFile(...schemaFiles)
106
103
 
107
- const operationGenerator = new OperationGenerator(
108
- this.plugin.options,
109
- {
110
- oas,
111
- pluginManager: this.pluginManager,
112
- plugin: this.plugin,
113
- contentType: swaggerPlugin.api.contentType,
114
- exclude,
115
- include,
116
- override,
117
- mode,
118
- },
119
- )
104
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
105
+ oas,
106
+ pluginManager: this.pluginManager,
107
+ plugin: this.plugin,
108
+ contentType: swaggerPlugin.api.contentType,
109
+ exclude,
110
+ include,
111
+ override,
112
+ mode,
113
+ })
120
114
 
121
115
  const operationFiles = await operationGenerator.build()
122
116
  await this.addFile(...operationFiles)