@kubb/swagger-ts 2.0.0-beta.1 → 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/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 +3561 -289
- 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 +3563 -292
- 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 +21 -7
- package/src/OperationGenerator.tsx +62 -0
- package/src/TypeBuilder.ts +58 -0
- package/src/{generators/TypeGenerator.ts → TypeGenerator.ts} +78 -68
- 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 +52 -107
- package/src/types.ts +41 -13
- 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
package/src/oas/index.ts
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
// based on https://github.com/ardatan/feTS/tree/master
|
2
|
+
|
3
|
+
export type { Infer } from './infer.ts'
|
4
|
+
export type { MethodMap, PathMap, StatusMap } from './mappers.ts'
|
5
|
+
export type { Model } from './model.ts'
|
6
|
+
export type { RequestParams } from './requestParams.ts'
|
7
|
+
export type { Response } from './response.ts'
|
package/src/oas/infer.ts
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
+
import type { Booleans, Call, Objects, Strings, Tuples } from 'hotscript'
|
3
|
+
import type { Object } from 'ts-toolbelt'
|
4
|
+
|
5
|
+
namespace Checks {
|
6
|
+
export type AllOFf = { allOf: any[] }
|
7
|
+
export type Object = {
|
8
|
+
type: 'object'
|
9
|
+
properties: any
|
10
|
+
}
|
11
|
+
export type Properties = { properties: any }
|
12
|
+
export type PropertiesRequired = {
|
13
|
+
properties: Record<string, any>
|
14
|
+
required: string[]
|
15
|
+
}
|
16
|
+
}
|
17
|
+
|
18
|
+
type FixAdditionalPropertiesForAllOf<T> = T extends Checks.AllOFf ? Omit<T, 'allOf'> & {
|
19
|
+
allOf: Call<Tuples.Map<Objects.Omit<'additionalProperties'>>, T['allOf']>
|
20
|
+
}
|
21
|
+
: T
|
22
|
+
|
23
|
+
type FixMissingAdditionalProperties<T> = T extends Checks.Object ? Omit<T, 'additionalProperties'> & { additionalProperties: false }
|
24
|
+
: T
|
25
|
+
type FixMissingTypeObject<T> = T extends Checks.Properties ? T & { type: 'object' } : T
|
26
|
+
|
27
|
+
type FixExtraRequiredFields<T> = T extends Checks.PropertiesRequired ? Omit<T, 'required'> & {
|
28
|
+
required: Call<Tuples.Filter<Booleans.Extends<keyof T['properties']>>, T['required']>
|
29
|
+
}
|
30
|
+
: T
|
31
|
+
|
32
|
+
// Later suggest using json-machete
|
33
|
+
type FixJSONSchema<T> = FixAdditionalPropertiesForAllOf<
|
34
|
+
FixMissingAdditionalProperties<FixMissingTypeObject<FixExtraRequiredFields<T>>>
|
35
|
+
>
|
36
|
+
|
37
|
+
type Mutable<Type> = FixJSONSchema<
|
38
|
+
{
|
39
|
+
-readonly [Key in keyof Type]: Mutable<Type[Key]>
|
40
|
+
}
|
41
|
+
>
|
42
|
+
|
43
|
+
type RefToPath<T extends string> = T extends `#/${infer Ref}` ? Call<Strings.Split<'/'>, Ref>
|
44
|
+
: never
|
45
|
+
|
46
|
+
type ResolveRef<TObj, TRef extends string> = {
|
47
|
+
$id: TRef
|
48
|
+
} & Object.Path<TObj, RefToPath<TRef>>
|
49
|
+
|
50
|
+
type ResolveRefInObj<T, TBase> = T extends { $ref: infer Ref } ? Ref extends string ? ResolveRef<TBase, Ref>
|
51
|
+
: T
|
52
|
+
: T
|
53
|
+
|
54
|
+
type ResolveRefsInObj<T, TBase = T> = {
|
55
|
+
[K in keyof T]: ResolveRefsInObj<ResolveRefInObj<T[K], TBase>, TBase>
|
56
|
+
}
|
57
|
+
|
58
|
+
export type Infer<TOAS> = Mutable<ResolveRefsInObj<TOAS>>
|
@@ -0,0 +1,93 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
3
|
+
import type { OasTypes } from '@kubb/swagger'
|
4
|
+
import type { Fn, Pipe, Tuples } from 'hotscript'
|
5
|
+
import type {
|
6
|
+
FromSchema,
|
7
|
+
JSONSchema,
|
8
|
+
} from 'json-schema-to-ts'
|
9
|
+
|
10
|
+
namespace Checks {
|
11
|
+
export type Required = { required: true }
|
12
|
+
|
13
|
+
export type Schemas = {
|
14
|
+
schema: JSONSchema
|
15
|
+
}
|
16
|
+
export type Enum = { type: JSONSchemaTypeName; enum?: any[] }
|
17
|
+
export type Parameters = { in: string; required?: boolean }[]
|
18
|
+
export type SingleParameter<TParamType> = [{ in: TParamType; required?: true }]
|
19
|
+
export type Responses = { responses: any }
|
20
|
+
}
|
21
|
+
|
22
|
+
export type PathMap<TOAS extends OasTypes.OASDocument> = TOAS['paths']
|
23
|
+
|
24
|
+
interface ParamPropMap {
|
25
|
+
query: 'query'
|
26
|
+
path: 'params'
|
27
|
+
header: 'headers'
|
28
|
+
}
|
29
|
+
|
30
|
+
type JSONSchemaTypeName =
|
31
|
+
| 'string'
|
32
|
+
| 'number'
|
33
|
+
| 'integer'
|
34
|
+
| 'boolean'
|
35
|
+
| 'object'
|
36
|
+
| 'array'
|
37
|
+
| 'null'
|
38
|
+
|
39
|
+
type ParamObj<
|
40
|
+
TParameter extends {
|
41
|
+
name: string
|
42
|
+
},
|
43
|
+
> = TParameter extends Checks.Required ? {
|
44
|
+
[TName in TParameter['name']]: TParameter extends Checks.Schemas ? FromSchema<TParameter['schema']>
|
45
|
+
: TParameter extends Checks.Enum ? FromSchema<{
|
46
|
+
type: TParameter['type']
|
47
|
+
enum: TParameter['enum']
|
48
|
+
}>
|
49
|
+
: unknown
|
50
|
+
}
|
51
|
+
: {
|
52
|
+
[TName in TParameter['name']]?: TParameter extends Checks.Schemas ? FromSchema<TParameter['schema']>
|
53
|
+
: TParameter extends Checks.Enum ? FromSchema<{
|
54
|
+
type: TParameter['type']
|
55
|
+
enum: TParameter['enum']
|
56
|
+
}>
|
57
|
+
: unknown
|
58
|
+
}
|
59
|
+
|
60
|
+
interface ParamToRequestParam<TParameters extends Checks.Parameters> extends Fn {
|
61
|
+
return: this['arg0'] extends { name: string; in: infer TParamType }
|
62
|
+
// If there is any required parameter for this parameter type, make that parameter type required
|
63
|
+
? TParameters extends Checks.SingleParameter<TParamType> ? {
|
64
|
+
[
|
65
|
+
TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType]
|
66
|
+
: never
|
67
|
+
]: ParamObj<this['arg0']>
|
68
|
+
}
|
69
|
+
: {
|
70
|
+
[
|
71
|
+
TKey in TParamType extends keyof ParamPropMap ? ParamPropMap[TParamType]
|
72
|
+
: never
|
73
|
+
]?: ParamObj<this['arg0']>
|
74
|
+
}
|
75
|
+
: {}
|
76
|
+
}
|
77
|
+
|
78
|
+
export type ParamMap<TParameters extends Checks.Parameters> = Pipe<
|
79
|
+
TParameters,
|
80
|
+
[Tuples.Map<ParamToRequestParam<TParameters>>, Tuples.ToIntersection]
|
81
|
+
>
|
82
|
+
|
83
|
+
export type MethodMap<
|
84
|
+
TOAS extends OasTypes.OASDocument,
|
85
|
+
TPath extends keyof PathMap<TOAS>,
|
86
|
+
> = PathMap<TOAS>[TPath]
|
87
|
+
|
88
|
+
export type StatusMap<
|
89
|
+
TOAS extends OasTypes.OASDocument,
|
90
|
+
TPath extends keyof PathMap<TOAS>,
|
91
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
92
|
+
> = MethodMap<TOAS, TPath>[TMethod] extends Checks.Responses ? MethodMap<TOAS, TPath>[TMethod]['responses']
|
93
|
+
: never
|
package/src/oas/model.ts
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
+
import type { OasTypes } from '@kubb/swagger'
|
3
|
+
import type {
|
4
|
+
FromSchema,
|
5
|
+
JSONSchema,
|
6
|
+
} from 'json-schema-to-ts'
|
7
|
+
|
8
|
+
namespace Checks {
|
9
|
+
export type ModelWithSchemas = {
|
10
|
+
components: {
|
11
|
+
schemas: Record<string, JSONSchema>
|
12
|
+
}
|
13
|
+
}
|
14
|
+
export type ModelWithSchemasNamed<TName extends string | number | symbol> = {
|
15
|
+
components: {
|
16
|
+
schemas: {
|
17
|
+
[TModelName in TName]: JSONSchema
|
18
|
+
}
|
19
|
+
}
|
20
|
+
}
|
21
|
+
export type ModelWithDefinitions = {
|
22
|
+
definitions: Record<string, JSONSchema>
|
23
|
+
}
|
24
|
+
export type ModelWithDefinitionsNamed<TName extends string | number | symbol = never> = {
|
25
|
+
definitions: {
|
26
|
+
[TModelName in TName]: JSONSchema
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
|
31
|
+
export type Model<
|
32
|
+
TOAS extends OasTypes.OASDocument,
|
33
|
+
TName extends TOAS extends Checks.ModelWithSchemas ? keyof TOAS['components']['schemas']
|
34
|
+
: TOAS extends Checks.ModelWithDefinitions ? keyof TOAS['definitions']
|
35
|
+
: never,
|
36
|
+
> = TOAS extends Checks.ModelWithSchemasNamed<TName> ? FromSchema<TOAS['components']['schemas'][TName]>
|
37
|
+
: TOAS extends Checks.ModelWithDefinitionsNamed<TName> ? FromSchema<TOAS['definitions'][TName]>
|
38
|
+
: never
|
@@ -0,0 +1,170 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
3
|
+
|
4
|
+
import type { OasTypes } from '@kubb/swagger'
|
5
|
+
import type { SplitByDelimiter, TupleToUnion } from '@kubb/types'
|
6
|
+
import type { Pipe, Strings, Tuples } from 'hotscript'
|
7
|
+
import type {
|
8
|
+
FromSchema,
|
9
|
+
JSONSchema,
|
10
|
+
} from 'json-schema-to-ts'
|
11
|
+
import type { MethodMap, ParamMap, PathMap } from './mappers.ts'
|
12
|
+
import type { SecurityParamsBySecurityRef } from './security.ts'
|
13
|
+
|
14
|
+
namespace Checks {
|
15
|
+
export type RequestBodyJson = {
|
16
|
+
requestBody: { content: { 'application/json': { schema: JSONSchema } } }
|
17
|
+
}
|
18
|
+
export type RequestBodyFormData = {
|
19
|
+
requestBody: {
|
20
|
+
content: { 'multipart/form-data': { schema: JSONSchema } }
|
21
|
+
}
|
22
|
+
}
|
23
|
+
export type RequestBodyFormEncoded = {
|
24
|
+
requestBody: {
|
25
|
+
content: {
|
26
|
+
'application/x-www-form-urlencoded': { schema: JSONSchema }
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
export type Parameters = {
|
31
|
+
parameters: { name: string; in: string }[]
|
32
|
+
}
|
33
|
+
export type PathBrackets = `${string}{${string}}${string}`
|
34
|
+
export type PathPattern = `${string}:${string}${string}`
|
35
|
+
export type Required = { required: true }
|
36
|
+
}
|
37
|
+
|
38
|
+
type ExtractPathParamsWithPattern<TPath extends string> = Pipe<
|
39
|
+
TPath,
|
40
|
+
[
|
41
|
+
Strings.Split<'/'>,
|
42
|
+
Tuples.Filter<Strings.StartsWith<':'>>,
|
43
|
+
Tuples.Map<Strings.Trim<':'>>,
|
44
|
+
Tuples.ToUnion,
|
45
|
+
]
|
46
|
+
>
|
47
|
+
|
48
|
+
type IsPathParameter<T extends string> = T extends `{${infer U}}` ? U : never
|
49
|
+
|
50
|
+
type ExtractPathParameters<T extends any[]> = {
|
51
|
+
[K in keyof T]: IsPathParameter<T[K]>
|
52
|
+
}
|
53
|
+
|
54
|
+
type ExtractSegments<TPath extends string> = SplitByDelimiter<TPath, '/'>
|
55
|
+
|
56
|
+
type ExtractSubSegments<T extends any[]> = {
|
57
|
+
[K in keyof T]: SplitByDelimiter<T[K], ';'>
|
58
|
+
}
|
59
|
+
|
60
|
+
type ExtractPathParamsWithBrackets<TPath extends string> = TupleToUnion<
|
61
|
+
ExtractPathParameters<ExtractSubSegments<ExtractSegments<TPath>>[number]>
|
62
|
+
>
|
63
|
+
|
64
|
+
export type RequestParams<
|
65
|
+
TOAS extends OasTypes.OASDocument,
|
66
|
+
TPath extends keyof PathMap<TOAS>,
|
67
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
68
|
+
> =
|
69
|
+
& (MethodMap<TOAS, TPath>[TMethod] extends Checks.RequestBodyJson ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks.Required ? {
|
70
|
+
/**
|
71
|
+
* The request body in JSON is required for this request.
|
72
|
+
*
|
73
|
+
* The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
|
74
|
+
*/
|
75
|
+
json: FromSchema<
|
76
|
+
MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']
|
77
|
+
>
|
78
|
+
}
|
79
|
+
: {
|
80
|
+
/**
|
81
|
+
* The request body in JSON is optional for this request.
|
82
|
+
*
|
83
|
+
* The value of `json` will be stringified and sent as the request body with `Content-Type: application/json`.
|
84
|
+
*/
|
85
|
+
json?: FromSchema<
|
86
|
+
MethodMap<TOAS, TPath>[TMethod]['requestBody']['content']['application/json']['schema']
|
87
|
+
>
|
88
|
+
}
|
89
|
+
: MethodMap<TOAS, TPath>[TMethod] extends Checks.RequestBodyFormData ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks.Required ? {
|
90
|
+
/**
|
91
|
+
* The request body in multipart/form-data is required for this request.
|
92
|
+
*
|
93
|
+
* The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
|
94
|
+
*/
|
95
|
+
formData: FromSchema<
|
96
|
+
MethodMap<
|
97
|
+
TOAS,
|
98
|
+
TPath
|
99
|
+
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
|
100
|
+
>
|
101
|
+
}
|
102
|
+
: {
|
103
|
+
/**
|
104
|
+
* The request body in multipart/form-data is optional for this request.
|
105
|
+
*
|
106
|
+
* The value of `formData` will be sent as the request body with `Content-Type: multipart/form-data`.
|
107
|
+
*/
|
108
|
+
formData?: FromSchema<
|
109
|
+
MethodMap<
|
110
|
+
TOAS,
|
111
|
+
TPath
|
112
|
+
>[TMethod]['requestBody']['content']['multipart/form-data']['schema']
|
113
|
+
>
|
114
|
+
}
|
115
|
+
: MethodMap<TOAS, TPath>[TMethod] extends Checks.RequestBodyFormEncoded ? MethodMap<TOAS, TPath>[TMethod]['requestBody'] extends Checks.Required ? {
|
116
|
+
/**
|
117
|
+
* The request body in application/x-www-form-urlencoded is required for this request.
|
118
|
+
*
|
119
|
+
* The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
|
120
|
+
*/
|
121
|
+
formUrlEncoded: FromSchema<
|
122
|
+
MethodMap<
|
123
|
+
TOAS,
|
124
|
+
TPath
|
125
|
+
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
|
126
|
+
>
|
127
|
+
}
|
128
|
+
: {
|
129
|
+
/**
|
130
|
+
* The request body in application/x-www-form-urlencoded is optional for this request.
|
131
|
+
*
|
132
|
+
* The value of `formUrlEncoded` will be sent as the request body with `Content-Type: application/x-www-form-urlencoded`.
|
133
|
+
*/
|
134
|
+
formUrlEncoded?: FromSchema<
|
135
|
+
MethodMap<
|
136
|
+
TOAS,
|
137
|
+
TPath
|
138
|
+
>[TMethod]['requestBody']['content']['application/x-www-form-urlencoded']['schema']
|
139
|
+
>
|
140
|
+
}
|
141
|
+
: {})
|
142
|
+
& (MethodMap<TOAS, TPath>[TMethod] extends Checks.Parameters ? ParamMap<MethodMap<TOAS, TPath>[TMethod]['parameters']>
|
143
|
+
: {})
|
144
|
+
& // If there is any parameters defined in path but not in the parameters array, we should add them to the params
|
145
|
+
(TPath extends Checks.PathBrackets ? {
|
146
|
+
/**
|
147
|
+
* Parameters defined in the path are required for this request.
|
148
|
+
*
|
149
|
+
* The value of `params` will be used to replace the path parameters.
|
150
|
+
*
|
151
|
+
* For example if path is `/todos/{id}` and `params` is `{ id: '1' }`, the path will be `/todos/1`
|
152
|
+
*/
|
153
|
+
params: Record<ExtractPathParamsWithBrackets<TPath>, string | number | bigint | boolean>
|
154
|
+
}
|
155
|
+
: {})
|
156
|
+
& (TPath extends Checks.PathPattern ? {
|
157
|
+
/**
|
158
|
+
* Parameters defined in the path are required for this request.
|
159
|
+
*
|
160
|
+
* The value of `params` will be used to replace the path parameters.
|
161
|
+
*
|
162
|
+
* For example if path is `/todos/:id` and `params` is `{ id: '1' }`, the path will be `/todos/1`.
|
163
|
+
*/
|
164
|
+
params: Record<ExtractPathParamsWithPattern<TPath>, string | number | bigint | boolean>
|
165
|
+
}
|
166
|
+
: {})
|
167
|
+
& // Respect security definitions in path object
|
168
|
+
SecurityParamsBySecurityRef<TOAS, MethodMap<TOAS, TPath>[TMethod]>
|
169
|
+
& // Respect global security definitions
|
170
|
+
SecurityParamsBySecurityRef<TOAS, TOAS>
|
@@ -0,0 +1,39 @@
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
2
|
+
import type { OasTypes } from '@kubb/swagger'
|
3
|
+
import type {
|
4
|
+
FromSchema,
|
5
|
+
} from 'json-schema-to-ts'
|
6
|
+
import type { MethodMap, PathMap, StatusMap } from './mappers.ts'
|
7
|
+
|
8
|
+
namespace Checks {
|
9
|
+
export type Content = { content: any }
|
10
|
+
}
|
11
|
+
|
12
|
+
type ResponseSchemas<
|
13
|
+
TOAS extends OasTypes.OASDocument,
|
14
|
+
TPath extends keyof PathMap<TOAS>,
|
15
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
16
|
+
TStatus extends keyof StatusMap<TOAS, TPath, TMethod>,
|
17
|
+
> = StatusMap<TOAS, TPath, TMethod>[TStatus]['content']
|
18
|
+
|
19
|
+
type JSONResponseSchema<
|
20
|
+
TOAS extends OasTypes.OASDocument,
|
21
|
+
TPath extends keyof PathMap<TOAS>,
|
22
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
23
|
+
TStatus extends keyof StatusMap<TOAS, TPath, TMethod>,
|
24
|
+
> = StatusMap<TOAS, TPath, TMethod>[TStatus] extends Checks.Content ? ResponseSchemas<TOAS, TPath, TMethod, TStatus>[
|
25
|
+
keyof ResponseSchemas<
|
26
|
+
TOAS,
|
27
|
+
TPath,
|
28
|
+
TMethod,
|
29
|
+
TStatus
|
30
|
+
>
|
31
|
+
]['schema']
|
32
|
+
: StatusMap<TOAS, TPath, TMethod>[TStatus]['schema']
|
33
|
+
|
34
|
+
export type Response<
|
35
|
+
TOAS extends OasTypes.OASDocument,
|
36
|
+
TPath extends keyof PathMap<TOAS>,
|
37
|
+
TMethod extends keyof MethodMap<TOAS, TPath>,
|
38
|
+
TStatusCode extends keyof StatusMap<TOAS, TPath, TMethod> = 200,
|
39
|
+
> = FromSchema<JSONResponseSchema<TOAS, TPath, TMethod, TStatusCode>>
|
@@ -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
|
+
: {}
|