@bagelink/sdk 1.4.26 → 1.4.28
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/index.cjs +49 -16
- package/dist/index.d.cts +353 -2
- package/dist/index.d.mts +353 -2
- package/dist/index.d.ts +353 -2
- package/dist/index.mjs +46 -17
- package/package.json +5 -20
- package/src/index.ts +3 -5
- package/src/openAPITools/functionGenerator.ts +45 -33
- package/src/openAPITools/index.ts +4 -2
- package/src/openAPITools/typeGenerator.ts +13 -12
- package/src/openAPITools/types/index.ts +360 -0
- package/src/openAPITools/types/utils.ts +36 -0
- package/src/openAPITools/utils.ts +16 -7
- package/src/openAPITools/openApiTypes.ts +0 -96
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
OperationObject,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
2
|
+
OperationObject as OpenAPIOperation,
|
|
3
|
+
PathItemObject as OpenAPIPath,
|
|
4
|
+
PathsObject as OpenAPIPaths,
|
|
5
|
+
ResponseObject as OpenAPIResponse,
|
|
6
|
+
ResponsesObject as OpenAPIResponses,
|
|
7
|
+
OpenAPIObject as OpenAPISchema,
|
|
8
|
+
ReferenceObject,
|
|
9
9
|
SchemaObject,
|
|
10
|
-
} from './
|
|
10
|
+
} from './types'
|
|
11
|
+
import { dereference, isReferenceObject } from './types/utils'
|
|
11
12
|
|
|
12
13
|
import {
|
|
13
14
|
cleanPath,
|
|
@@ -82,7 +83,7 @@ function collectTypeForImportStatement(typeName: string): void {
|
|
|
82
83
|
* @param schema - The schema to convert to a type
|
|
83
84
|
* @returns The TypeScript type string
|
|
84
85
|
*/
|
|
85
|
-
function schemaToTypeWithCollection(schema?: SchemaObject): string {
|
|
86
|
+
function schemaToTypeWithCollection(schema?: OpenAPISchema | SchemaObject): string {
|
|
86
87
|
const type = schemaToType(schema)
|
|
87
88
|
collectTypeForImportStatement(type)
|
|
88
89
|
return type
|
|
@@ -93,7 +94,10 @@ function schemaToTypeWithCollection(schema?: SchemaObject): string {
|
|
|
93
94
|
* @param response - The OpenAPI response object
|
|
94
95
|
* @returns The TypeScript type string or undefined
|
|
95
96
|
*/
|
|
96
|
-
function getResponseType(response:
|
|
97
|
+
function getResponseType(response: OpenAPIResponse | ReferenceObject): string | undefined {
|
|
98
|
+
if (isReferenceObject(response)) {
|
|
99
|
+
return undefined // TODO: handle references properly
|
|
100
|
+
}
|
|
97
101
|
const mediaTypeObject = response.content?.['application/json']
|
|
98
102
|
if (!mediaTypeObject?.schema) return undefined
|
|
99
103
|
return schemaToTypeWithCollection(mediaTypeObject.schema)
|
|
@@ -104,7 +108,7 @@ function getResponseType(response: ResponseObject): string | undefined {
|
|
|
104
108
|
* @param responses - The OpenAPI responses object
|
|
105
109
|
* @returns The TypeScript response type string
|
|
106
110
|
*/
|
|
107
|
-
function generateResponseType(responses?:
|
|
111
|
+
function generateResponseType(responses?: OpenAPIResponses): string {
|
|
108
112
|
if (!responses) return ''
|
|
109
113
|
|
|
110
114
|
const types: string[] = []
|
|
@@ -147,21 +151,24 @@ function formatPathWithParams(path: string): string {
|
|
|
147
151
|
* @param requestBody - The OpenAPI request body object
|
|
148
152
|
* @returns Object containing request body parameter and payload information
|
|
149
153
|
*/
|
|
150
|
-
function generateRequestBody(requestBody?:
|
|
154
|
+
function generateRequestBody(requestBody?: OpenAPIOperation['requestBody']): {
|
|
151
155
|
requestBodyParam: string
|
|
152
156
|
requestBodyPayload: string
|
|
153
157
|
} {
|
|
154
|
-
if (!requestBody?.content)
|
|
155
|
-
|
|
158
|
+
// if (!requestBody || !requestBody?.content)
|
|
159
|
+
// return { requestBodyParam: '', requestBodyPayload: '' }
|
|
156
160
|
|
|
157
|
-
const
|
|
161
|
+
const content = dereference(requestBody)?.content
|
|
162
|
+
if (!content || Object.keys(content).length === 0) {
|
|
163
|
+
return { requestBodyParam: '', requestBodyPayload: '' }
|
|
164
|
+
}
|
|
158
165
|
|
|
159
166
|
// Handle multipart/form-data requests (file uploads)
|
|
160
167
|
if (content['multipart/form-data']) {
|
|
161
168
|
const multipartFormData = content['multipart/form-data']
|
|
162
169
|
|
|
163
170
|
// Special case for file uploads
|
|
164
|
-
if (multipartFormData.schema?.$ref?.includes('Body_upload_files')) {
|
|
171
|
+
if (isReferenceObject(multipartFormData.schema) && multipartFormData.schema?.$ref?.includes('Body_upload_files')) {
|
|
165
172
|
return {
|
|
166
173
|
requestBodyParam:
|
|
167
174
|
'file: File, options?: UploadOptions & { dirPath?: string, tags?: string[] }',
|
|
@@ -182,7 +189,7 @@ function generateRequestBody(requestBody?: RequestBodyObject): {
|
|
|
182
189
|
return { requestBodyParam: '', requestBodyPayload: '' }
|
|
183
190
|
}
|
|
184
191
|
|
|
185
|
-
const bodySchema = jsonContent.schema
|
|
192
|
+
const bodySchema = dereference(jsonContent.schema)
|
|
186
193
|
const requestBodyType = schemaToType(bodySchema)
|
|
187
194
|
collectTypeForImportStatement(requestBodyType)
|
|
188
195
|
|
|
@@ -206,7 +213,7 @@ function generateRequestBody(requestBody?: RequestBodyObject): {
|
|
|
206
213
|
* @returns Object containing parameter information
|
|
207
214
|
*/
|
|
208
215
|
function generateFunctionParameters(
|
|
209
|
-
params?:
|
|
216
|
+
params?: OpenAPIOperation['parameters'],
|
|
210
217
|
isFileUpload = false
|
|
211
218
|
): { params?: string, config?: { params?: string } } {
|
|
212
219
|
if (!params?.length) return {}
|
|
@@ -224,8 +231,11 @@ function generateFunctionParameters(
|
|
|
224
231
|
const functionParams: string[] = []
|
|
225
232
|
const queryParams: string[] = []
|
|
226
233
|
|
|
227
|
-
for (const
|
|
228
|
-
const
|
|
234
|
+
for (const p of params) {
|
|
235
|
+
const param = dereference(p)
|
|
236
|
+
const pSchema = dereference(param.schema)
|
|
237
|
+
|
|
238
|
+
const paramType = schemaToType(pSchema)
|
|
229
239
|
collectTypeForImportStatement(paramType)
|
|
230
240
|
const paramName = param.name
|
|
231
241
|
const varName = toCamelCase(param.name)
|
|
@@ -234,9 +244,9 @@ function generateFunctionParameters(
|
|
|
234
244
|
functionParams.push(
|
|
235
245
|
formatVarType({
|
|
236
246
|
varName,
|
|
237
|
-
schema:
|
|
247
|
+
schema: pSchema,
|
|
238
248
|
required: param.required,
|
|
239
|
-
defaultValue:
|
|
249
|
+
defaultValue: pSchema?.default,
|
|
240
250
|
})
|
|
241
251
|
)
|
|
242
252
|
}
|
|
@@ -346,7 +356,7 @@ function generateAxiosFunction(
|
|
|
346
356
|
* @param path - The API path
|
|
347
357
|
* @returns The JSDoc comment as a string
|
|
348
358
|
*/
|
|
349
|
-
function buildJSDocComment(operation:
|
|
359
|
+
function buildJSDocComment(operation: OpenAPIOperation, method: string, path: string) {
|
|
350
360
|
let functionComment = '/**\n'
|
|
351
361
|
|
|
352
362
|
// Add summary
|
|
@@ -383,14 +393,16 @@ function buildJSDocComment(operation: OperationObject, method: string, path: str
|
|
|
383
393
|
function generateFunctionForOperation(
|
|
384
394
|
method: string,
|
|
385
395
|
path: string,
|
|
386
|
-
operation:
|
|
396
|
+
operation: OpenAPIOperation
|
|
387
397
|
): string {
|
|
388
398
|
if (!operation) return ''
|
|
389
399
|
|
|
390
400
|
// Check if this is a file upload
|
|
391
|
-
const
|
|
401
|
+
const multiPartSchema = dereference(operation.requestBody)?.content?.[
|
|
392
402
|
'multipart/form-data'
|
|
393
|
-
]?.schema
|
|
403
|
+
]?.schema
|
|
404
|
+
|
|
405
|
+
const isFileUpload = isReferenceObject(multiPartSchema) && multiPartSchema?.$ref?.includes('Body_upload_files')
|
|
394
406
|
|
|
395
407
|
const parameters = generateFunctionParameters(
|
|
396
408
|
operation.parameters,
|
|
@@ -461,7 +473,7 @@ function generateRandomString(): string {
|
|
|
461
473
|
function createFunctionPlaceholder(
|
|
462
474
|
path: string,
|
|
463
475
|
method: string,
|
|
464
|
-
operation:
|
|
476
|
+
operation: OpenAPIOperation | undefined
|
|
465
477
|
): string {
|
|
466
478
|
const funcID = generateRandomString()
|
|
467
479
|
if (operation) {
|
|
@@ -485,10 +497,10 @@ function createFunctionPlaceholder(
|
|
|
485
497
|
*/
|
|
486
498
|
function handlePathSegment(
|
|
487
499
|
path: string,
|
|
488
|
-
operation:
|
|
500
|
+
operation: OpenAPIPath,
|
|
489
501
|
existingObj: Record<string, any> = {}
|
|
490
502
|
): Record<string, any> {
|
|
491
|
-
const methods = Object.keys(operation) as Array<keyof
|
|
503
|
+
const methods = Object.keys(operation) as Array<keyof OpenAPIPath>
|
|
492
504
|
const obj: Record<string, any> = {}
|
|
493
505
|
|
|
494
506
|
for (const method of methods) {
|
|
@@ -502,7 +514,7 @@ function handlePathSegment(
|
|
|
502
514
|
obj[functionName] = createFunctionPlaceholder(
|
|
503
515
|
path,
|
|
504
516
|
method,
|
|
505
|
-
operation[method]
|
|
517
|
+
operation[method] as OpenAPIOperation
|
|
506
518
|
)
|
|
507
519
|
}
|
|
508
520
|
|
|
@@ -515,7 +527,7 @@ function handlePathSegment(
|
|
|
515
527
|
* @param baseUrl - The base URL for the API
|
|
516
528
|
* @returns The generated TypeScript code
|
|
517
529
|
*/
|
|
518
|
-
export function generateFunctions(paths:
|
|
530
|
+
export function generateFunctions(paths: OpenAPIPaths, baseUrl: string): string {
|
|
519
531
|
const body: Record<string, any> = {}
|
|
520
532
|
const allPathsClean = Object.keys(paths).map(cleanPath)
|
|
521
533
|
|
|
@@ -527,7 +539,7 @@ export function generateFunctions(paths: PathsObject, baseUrl: string): string {
|
|
|
527
539
|
const objFuncKey = toCamelCase(key)
|
|
528
540
|
if (!objFuncKey) return acc
|
|
529
541
|
|
|
530
|
-
const methods = Object.keys(operation) as Array<keyof
|
|
542
|
+
const methods = Object.keys(operation) as Array<keyof OpenAPIPath>
|
|
531
543
|
|
|
532
544
|
if (
|
|
533
545
|
index === array.length - 1
|
|
@@ -536,7 +548,7 @@ export function generateFunctions(paths: PathsObject, baseUrl: string): string {
|
|
|
536
548
|
) {
|
|
537
549
|
// Single method endpoint with unique path
|
|
538
550
|
const method = methods[0]
|
|
539
|
-
const opp = operation[method]
|
|
551
|
+
const opp = operation[method] as OpenAPIOperation
|
|
540
552
|
acc[objFuncKey] = createFunctionPlaceholder(path, method, opp)
|
|
541
553
|
} else if (index === array.length - 1) {
|
|
542
554
|
// Multiple methods endpoint or path with conflicts
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OpenAPIObject } from './types'
|
|
2
2
|
import axios from 'axios'
|
|
3
3
|
import { generateFunctions } from './functionGenerator'
|
|
4
4
|
import { generateTypes } from './typeGenerator'
|
|
@@ -11,7 +11,7 @@ const basicAuthHeader = { Authorization: 'Basic YmFnZWxfdXNlcm5hbWU6Tm90U2VjdXJl
|
|
|
11
11
|
|
|
12
12
|
export default async (openApiUrl: string, baseUrl: string): Promise<OpenAPIResponse> => {
|
|
13
13
|
try {
|
|
14
|
-
const { data: openApi } = await axios.get<
|
|
14
|
+
const { data: openApi } = await axios.get<OpenAPIObject>(openApiUrl, { headers: basicAuthHeader })
|
|
15
15
|
const schemas = openApi.components?.schemas
|
|
16
16
|
if (!schemas) throw new Error('No schemas found in OpenAPI document')
|
|
17
17
|
const types = generateTypes(schemas)
|
|
@@ -24,3 +24,5 @@ export default async (openApiUrl: string, baseUrl: string): Promise<OpenAPIRespo
|
|
|
24
24
|
throw new Error(error)
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
export * from './types'
|
|
@@ -1,23 +1,24 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ComponentsObject } from './types'
|
|
2
|
+
import { isSchemaObject } from './types/utils'
|
|
2
3
|
import { formatType, formatVarType, schemaToType } from './utils'
|
|
3
4
|
|
|
4
|
-
export function generateTypes(schemas:
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
typeName = formatType(typeName)
|
|
5
|
+
export function generateTypes(schemas: ComponentsObject['schemas']): string {
|
|
6
|
+
if (!schemas) return ''
|
|
7
|
+
const schemaEntries = Object.entries(schemas)
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
return schemaEntries
|
|
10
|
+
.map(([_typeName, schema]) => {
|
|
11
|
+
const typeName = formatType(_typeName)
|
|
12
|
+
|
|
13
|
+
if (!isSchemaObject(schema)) return ''
|
|
14
|
+
|
|
15
|
+
if (schema?.enum) {
|
|
10
16
|
return `export type ${typeName} = ${schema.enum.map((item: string) => `'${item}'`).join(' | ')};\n`
|
|
11
17
|
}
|
|
12
18
|
|
|
13
|
-
if (schema
|
|
19
|
+
if (schema?.type === 'array' && schema.items) {
|
|
14
20
|
return `export type ${typeName} = (${schemaToType(schema.items)})[];\n`
|
|
15
21
|
}
|
|
16
|
-
|
|
17
|
-
// if (typeName === 'LocalizedJsonInt') {
|
|
18
|
-
// console.log(JSON.stringify({ typeName, schema }, null, 2))
|
|
19
|
-
// }
|
|
20
|
-
|
|
21
22
|
if (!schema.properties) {
|
|
22
23
|
// Handle case where schema has additionalProperties: true
|
|
23
24
|
if (schema.additionalProperties === true) {
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// Typed interfaces for OpenAPI 3.1.0
|
|
2
|
+
// see https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md
|
|
3
|
+
|
|
4
|
+
export interface ServerObject {
|
|
5
|
+
url: string
|
|
6
|
+
description?: string
|
|
7
|
+
variables?: { [v: string]: ServerVariableObject }
|
|
8
|
+
}
|
|
9
|
+
export interface ServerVariableObject {
|
|
10
|
+
enum?: string[] | boolean[] | number[]
|
|
11
|
+
default: string | boolean | number
|
|
12
|
+
description?: string
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface OpenAPIObject {
|
|
16
|
+
openapi: string
|
|
17
|
+
info: InfoObject
|
|
18
|
+
servers?: ServerObject[]
|
|
19
|
+
paths?: PathsObject
|
|
20
|
+
components?: ComponentsObject
|
|
21
|
+
security?: SecurityRequirementObject[]
|
|
22
|
+
tags?: TagObject[]
|
|
23
|
+
externalDocs?: ExternalDocumentationObject
|
|
24
|
+
/** Webhooks added in v. 3.1.0 */
|
|
25
|
+
webhooks?: PathsObject
|
|
26
|
+
}
|
|
27
|
+
export interface InfoObject {
|
|
28
|
+
title: string
|
|
29
|
+
description?: string
|
|
30
|
+
termsOfService?: string
|
|
31
|
+
contact?: ContactObject
|
|
32
|
+
license?: LicenseObject
|
|
33
|
+
version: string
|
|
34
|
+
}
|
|
35
|
+
export interface ContactObject {
|
|
36
|
+
name?: string
|
|
37
|
+
url?: string
|
|
38
|
+
email?: string
|
|
39
|
+
}
|
|
40
|
+
export interface LicenseObject {
|
|
41
|
+
name: string
|
|
42
|
+
identifier?: string
|
|
43
|
+
url?: string
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface ComponentsObject {
|
|
47
|
+
schemas?: { [schema: string]: SchemaObject | ReferenceObject }
|
|
48
|
+
responses?: { [response: string]: ResponseObject | ReferenceObject }
|
|
49
|
+
parameters?: { [parameter: string]: ParameterObject | ReferenceObject }
|
|
50
|
+
examples?: { [example: string]: ExampleObject | ReferenceObject }
|
|
51
|
+
requestBodies?: { [request: string]: RequestBodyObject | ReferenceObject }
|
|
52
|
+
headers?: { [header: string]: HeaderObject | ReferenceObject }
|
|
53
|
+
securitySchemes?: { [securityScheme: string]: SecuritySchemeObject | ReferenceObject }
|
|
54
|
+
links?: { [link: string]: LinkObject | ReferenceObject }
|
|
55
|
+
callbacks?: { [callback: string]: CallbackObject | ReferenceObject }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Rename it to Paths Object to be consistent with the spec
|
|
60
|
+
* See https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#pathsObject
|
|
61
|
+
*/
|
|
62
|
+
export interface PathsObject {
|
|
63
|
+
[path: string]: PathItemObject
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface PathItemObject {
|
|
67
|
+
$ref?: string
|
|
68
|
+
summary?: string
|
|
69
|
+
description?: string
|
|
70
|
+
get?: OperationObject
|
|
71
|
+
put?: OperationObject
|
|
72
|
+
post?: OperationObject
|
|
73
|
+
delete?: OperationObject
|
|
74
|
+
options?: OperationObject
|
|
75
|
+
head?: OperationObject
|
|
76
|
+
patch?: OperationObject
|
|
77
|
+
trace?: OperationObject
|
|
78
|
+
servers?: ServerObject[]
|
|
79
|
+
parameters?: (ParameterObject | ReferenceObject)[]
|
|
80
|
+
}
|
|
81
|
+
export interface OperationObject {
|
|
82
|
+
tags?: string[]
|
|
83
|
+
summary?: string
|
|
84
|
+
description?: string
|
|
85
|
+
externalDocs?: ExternalDocumentationObject
|
|
86
|
+
operationId?: string
|
|
87
|
+
parameters?: (ParameterObject | ReferenceObject)[]
|
|
88
|
+
requestBody?: RequestBodyObject | ReferenceObject
|
|
89
|
+
responses?: ResponsesObject
|
|
90
|
+
callbacks?: CallbacksObject
|
|
91
|
+
deprecated?: boolean
|
|
92
|
+
security?: SecurityRequirementObject[]
|
|
93
|
+
servers?: ServerObject[]
|
|
94
|
+
}
|
|
95
|
+
export interface ExternalDocumentationObject {
|
|
96
|
+
description?: string
|
|
97
|
+
url: string
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* The location of a parameter.
|
|
102
|
+
* Possible values are "query", "header", "path" or "cookie".
|
|
103
|
+
* Specification:
|
|
104
|
+
* https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameter-locations
|
|
105
|
+
*/
|
|
106
|
+
export type ParameterLocation = 'query' | 'header' | 'path' | 'cookie'
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The style of a parameter.
|
|
110
|
+
* Describes how the parameter value will be serialized.
|
|
111
|
+
* (serialization is not implemented yet)
|
|
112
|
+
* Specification:
|
|
113
|
+
* https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#style-values
|
|
114
|
+
*/
|
|
115
|
+
export type ParameterStyle =
|
|
116
|
+
| 'matrix'
|
|
117
|
+
| 'label'
|
|
118
|
+
| 'form'
|
|
119
|
+
| 'simple'
|
|
120
|
+
| 'spaceDelimited'
|
|
121
|
+
| 'pipeDelimited'
|
|
122
|
+
| 'deepObject'
|
|
123
|
+
|
|
124
|
+
export interface BaseParameterObject {
|
|
125
|
+
description?: string
|
|
126
|
+
required?: boolean
|
|
127
|
+
deprecated?: boolean
|
|
128
|
+
allowEmptyValue?: boolean
|
|
129
|
+
|
|
130
|
+
style?: ParameterStyle // "matrix" | "label" | "form" | "simple" | "spaceDelimited" | "pipeDelimited" | "deepObject";
|
|
131
|
+
explode?: boolean
|
|
132
|
+
allowReserved?: boolean
|
|
133
|
+
schema?: SchemaObject | ReferenceObject
|
|
134
|
+
examples?: { [param: string]: ExampleObject | ReferenceObject }
|
|
135
|
+
example?: any
|
|
136
|
+
content?: ContentObject
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export interface ParameterObject extends BaseParameterObject {
|
|
140
|
+
name: string
|
|
141
|
+
in: ParameterLocation // "query" | "header" | "path" | "cookie";
|
|
142
|
+
}
|
|
143
|
+
export interface RequestBodyObject {
|
|
144
|
+
description?: string
|
|
145
|
+
content: ContentObject
|
|
146
|
+
required?: boolean
|
|
147
|
+
}
|
|
148
|
+
export interface ContentObject {
|
|
149
|
+
[mediatype: string]: MediaTypeObject
|
|
150
|
+
}
|
|
151
|
+
export interface MediaTypeObject {
|
|
152
|
+
schema?: SchemaObject | ReferenceObject
|
|
153
|
+
examples?: ExamplesObject
|
|
154
|
+
example?: any
|
|
155
|
+
encoding?: EncodingObject
|
|
156
|
+
}
|
|
157
|
+
export interface EncodingObject {
|
|
158
|
+
[property: string]: EncodingPropertyObject
|
|
159
|
+
}
|
|
160
|
+
export interface EncodingPropertyObject {
|
|
161
|
+
contentType?: string
|
|
162
|
+
headers?: { [key: string]: HeaderObject | ReferenceObject }
|
|
163
|
+
style?: string
|
|
164
|
+
explode?: boolean
|
|
165
|
+
allowReserved?: boolean
|
|
166
|
+
|
|
167
|
+
}
|
|
168
|
+
export interface ResponsesObject {
|
|
169
|
+
[statuscode: string]: ResponseObject | ReferenceObject
|
|
170
|
+
}
|
|
171
|
+
export interface ResponseObject {
|
|
172
|
+
description: string
|
|
173
|
+
headers?: HeadersObject
|
|
174
|
+
content?: ContentObject
|
|
175
|
+
links?: LinksObject
|
|
176
|
+
}
|
|
177
|
+
export interface CallbacksObject {
|
|
178
|
+
[name: string]: CallbackObject | ReferenceObject
|
|
179
|
+
}
|
|
180
|
+
export interface CallbackObject {
|
|
181
|
+
[name: string]: PathItemObject
|
|
182
|
+
}
|
|
183
|
+
export interface HeadersObject {
|
|
184
|
+
[name: string]: HeaderObject | ReferenceObject
|
|
185
|
+
}
|
|
186
|
+
export interface ExampleObject {
|
|
187
|
+
summary?: string
|
|
188
|
+
description?: string
|
|
189
|
+
value?: any
|
|
190
|
+
externalValue?: string
|
|
191
|
+
}
|
|
192
|
+
export interface LinksObject {
|
|
193
|
+
[name: string]: LinkObject | ReferenceObject
|
|
194
|
+
}
|
|
195
|
+
export interface LinkObject {
|
|
196
|
+
operationRef?: string
|
|
197
|
+
operationId?: string
|
|
198
|
+
parameters?: LinkParametersObject
|
|
199
|
+
requestBody?: any | string
|
|
200
|
+
description?: string
|
|
201
|
+
server?: ServerObject
|
|
202
|
+
}
|
|
203
|
+
export interface LinkParametersObject {
|
|
204
|
+
[name: string]: any | string
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface HeaderObject extends BaseParameterObject {
|
|
208
|
+
$ref?: string
|
|
209
|
+
}
|
|
210
|
+
export interface TagObject {
|
|
211
|
+
name: string
|
|
212
|
+
description?: string
|
|
213
|
+
externalDocs?: ExternalDocumentationObject
|
|
214
|
+
}
|
|
215
|
+
export interface ExamplesObject {
|
|
216
|
+
[name: string]: ExampleObject | ReferenceObject
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export interface ReferenceObject {
|
|
220
|
+
$ref: string
|
|
221
|
+
summary?: string
|
|
222
|
+
description?: string
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export type SchemaObjectType =
|
|
226
|
+
| 'integer'
|
|
227
|
+
| 'number'
|
|
228
|
+
| 'string'
|
|
229
|
+
| 'boolean'
|
|
230
|
+
| 'object'
|
|
231
|
+
| 'null'
|
|
232
|
+
| 'array'
|
|
233
|
+
| 'undefined'
|
|
234
|
+
|
|
235
|
+
const TYPE_FORMATS = [
|
|
236
|
+
'uuid',
|
|
237
|
+
'uri',
|
|
238
|
+
'date-time',
|
|
239
|
+
'date',
|
|
240
|
+
'email',
|
|
241
|
+
'binary',
|
|
242
|
+
'password',
|
|
243
|
+
'phone',
|
|
244
|
+
'uuid4',
|
|
245
|
+
'int32',
|
|
246
|
+
'int64',
|
|
247
|
+
'float',
|
|
248
|
+
'double',
|
|
249
|
+
'byte'
|
|
250
|
+
] as const
|
|
251
|
+
|
|
252
|
+
export type TypeFormatT = (typeof TYPE_FORMATS)[number] | (string & {})
|
|
253
|
+
|
|
254
|
+
export interface SchemaObject {
|
|
255
|
+
discriminator?: DiscriminatorObject
|
|
256
|
+
readOnly?: boolean
|
|
257
|
+
writeOnly?: boolean
|
|
258
|
+
xml?: XmlObject
|
|
259
|
+
externalDocs?: ExternalDocumentationObject
|
|
260
|
+
/** @deprecated use examples instead */
|
|
261
|
+
example?: any
|
|
262
|
+
examples?: any[]
|
|
263
|
+
deprecated?: boolean
|
|
264
|
+
|
|
265
|
+
type?: SchemaObjectType | SchemaObjectType[]
|
|
266
|
+
format?: TypeFormatT
|
|
267
|
+
allOf?: (SchemaObject | ReferenceObject)[]
|
|
268
|
+
oneOf?: (SchemaObject | ReferenceObject)[]
|
|
269
|
+
anyOf?: (SchemaObject | ReferenceObject)[]
|
|
270
|
+
not?: SchemaObject | ReferenceObject
|
|
271
|
+
items?: SchemaObject | ReferenceObject
|
|
272
|
+
properties?: { [propertyName: string]: SchemaObject | ReferenceObject }
|
|
273
|
+
additionalProperties?: SchemaObject | ReferenceObject | boolean
|
|
274
|
+
propertyNames?: SchemaObject | ReferenceObject
|
|
275
|
+
description?: string
|
|
276
|
+
default?: any
|
|
277
|
+
|
|
278
|
+
title?: string
|
|
279
|
+
multipleOf?: number
|
|
280
|
+
maximum?: number
|
|
281
|
+
const?: any
|
|
282
|
+
/** @desc In OpenAPI 3.1: number */
|
|
283
|
+
exclusiveMaximum?: number
|
|
284
|
+
minimum?: number
|
|
285
|
+
/** @desc In OpenAPI 3.1: number */
|
|
286
|
+
exclusiveMinimum?: number
|
|
287
|
+
maxLength?: number
|
|
288
|
+
minLength?: number
|
|
289
|
+
pattern?: string
|
|
290
|
+
maxItems?: number
|
|
291
|
+
minItems?: number
|
|
292
|
+
uniqueItems?: boolean
|
|
293
|
+
maxProperties?: number
|
|
294
|
+
minProperties?: number
|
|
295
|
+
required?: string[]
|
|
296
|
+
enum?: any[]
|
|
297
|
+
prefixItems?: (SchemaObject | ReferenceObject)[]
|
|
298
|
+
/**
|
|
299
|
+
* @desc JSON Schema compliant Content-Type, optional when specified as a key of ContentObject
|
|
300
|
+
* @example image/png
|
|
301
|
+
*/
|
|
302
|
+
contentMediaType?: string
|
|
303
|
+
/**
|
|
304
|
+
* @desc Specifies the Content-Encoding for the schema, supports all encodings from RFC4648, and "quoted-printable" from RFC2045
|
|
305
|
+
* @override
|
|
306
|
+
* @see https://datatracker.ietf.org/doc/html/rfc4648
|
|
307
|
+
* @see https://datatracker.ietf.org/doc/html/rfc2045#section-6.7
|
|
308
|
+
* @example base64
|
|
309
|
+
*/
|
|
310
|
+
contentEncoding?: string
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
export interface SchemasObject {
|
|
314
|
+
[schema: string]: SchemaObject
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export interface DiscriminatorObject {
|
|
318
|
+
propertyName: string
|
|
319
|
+
mapping?: { [key: string]: string }
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export interface XmlObject {
|
|
323
|
+
name?: string
|
|
324
|
+
namespace?: string
|
|
325
|
+
prefix?: string
|
|
326
|
+
attribute?: boolean
|
|
327
|
+
wrapped?: boolean
|
|
328
|
+
}
|
|
329
|
+
export type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect'
|
|
330
|
+
|
|
331
|
+
export interface SecuritySchemeObject {
|
|
332
|
+
type: SecuritySchemeType
|
|
333
|
+
description?: string
|
|
334
|
+
name?: string // required only for apiKey
|
|
335
|
+
in?: string // required only for apiKey
|
|
336
|
+
scheme?: string // required only for http
|
|
337
|
+
bearerFormat?: string
|
|
338
|
+
flows?: OAuthFlowsObject // required only for oauth2
|
|
339
|
+
openIdConnectUrl?: string // required only for openIdConnect
|
|
340
|
+
}
|
|
341
|
+
export interface OAuthFlowsObject {
|
|
342
|
+
implicit?: OAuthFlowObject
|
|
343
|
+
password?: OAuthFlowObject
|
|
344
|
+
clientCredentials?: OAuthFlowObject
|
|
345
|
+
authorizationCode?: OAuthFlowObject
|
|
346
|
+
}
|
|
347
|
+
export interface OAuthFlowObject {
|
|
348
|
+
authorizationUrl?: string
|
|
349
|
+
tokenUrl?: string
|
|
350
|
+
refreshUrl?: string
|
|
351
|
+
scopes: ScopesObject
|
|
352
|
+
}
|
|
353
|
+
export interface ScopesObject {
|
|
354
|
+
[scope: string]: any
|
|
355
|
+
}
|
|
356
|
+
export interface SecurityRequirementObject {
|
|
357
|
+
[name: string]: string[]
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
export * from './utils'
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { PathItemObject, PathsObject, ReferenceObject, SchemaObject } from '.'
|
|
2
|
+
|
|
3
|
+
export function getPath(
|
|
4
|
+
pathsObject: PathsObject | undefined,
|
|
5
|
+
path: string
|
|
6
|
+
): PathItemObject | undefined {
|
|
7
|
+
return pathsObject ? (pathsObject[path] as PathItemObject) : undefined
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* A type guard to check if the given value is a `ReferenceObject`.
|
|
12
|
+
* See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
|
|
13
|
+
*
|
|
14
|
+
* @param obj The value to check.
|
|
15
|
+
*/
|
|
16
|
+
export function isReferenceObject(obj: any): obj is ReferenceObject {
|
|
17
|
+
if (!obj) return false
|
|
18
|
+
return Object.prototype.hasOwnProperty.call(obj, '$ref')
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A type guard to check if the given object is a `SchemaObject`.
|
|
23
|
+
* Useful to distinguish from `ReferenceObject` values that can be used
|
|
24
|
+
* in most places where `SchemaObject` is allowed.
|
|
25
|
+
*
|
|
26
|
+
* See https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-differentiating-types
|
|
27
|
+
*
|
|
28
|
+
* @param schema The value to check.
|
|
29
|
+
*/
|
|
30
|
+
export function isSchemaObject(schema: SchemaObject | ReferenceObject): schema is SchemaObject {
|
|
31
|
+
return !Object.prototype.hasOwnProperty.call(schema, '$ref')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function dereference<T>(property: ReferenceObject | T) {
|
|
35
|
+
return property as Exclude<T, ReferenceObject>
|
|
36
|
+
}
|