-
Notifications
You must be signed in to change notification settings - Fork 568
/
index.ts
446 lines (403 loc) · 11.3 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/**
* inspired by https://github.com/metadevpro/openapi3-ts
* @see https://github.com/OAI/OpenAPI-Specification/blob/3.0.0-rc0/versions/3.0.md
*/
import { RouterOption } from '@midwayjs/core';
import { SwaggerExplorer } from '../swaggerExplorer';
export interface OpenAPIObject {
openapi: string;
info: InfoObject;
servers?: ServerObject[];
paths: PathsObject;
components?: ComponentsObject;
security?: SecurityRequirementObject[];
tags?: TagObject[];
externalDocs?: ExternalDocumentationObject;
}
export interface InfoObject {
title: string;
description?: string;
termsOfService?: string;
contact?: ContactObject;
license?: LicenseObject;
version: string;
}
export interface ContactObject {
name?: string;
url?: string;
email?: string;
}
export interface LicenseObject {
name: string;
url?: string;
}
export interface ServerObject {
url: string;
description?: string;
variables?: Record<string, ServerVariableObject>;
}
export interface ServerVariableObject {
enum?: string[] | boolean[] | number[];
default: string | boolean | number;
description?: string;
}
export interface ComponentsObject {
schemas?: Record<string, SchemaObject | ReferenceObject>;
responses?: Record<string, ResponseObject | ReferenceObject>;
parameters?: Record<string, ParameterObject | ReferenceObject>;
examples?: Record<string, ExampleObject | ReferenceObject>;
requestBodies?: Record<string, RequestBodyObject | ReferenceObject>;
headers?: Record<string, HeaderObject | ReferenceObject>;
securitySchemes?: Record<string, SecuritySchemeObject | ReferenceObject>;
links?: Record<string, LinkObject | ReferenceObject>;
callbacks?: Record<string, CallbackObject | ReferenceObject>;
}
export type PathsObject = Record<string, PathItemObject>;
export interface PathItemObject {
$ref?: string;
summary?: string;
description?: string;
get?: OperationObject;
put?: OperationObject;
post?: OperationObject;
delete?: OperationObject;
options?: OperationObject;
head?: OperationObject;
patch?: OperationObject;
trace?: OperationObject;
servers?: ServerObject[];
parameters?: (ParameterObject | ReferenceObject)[];
}
export interface OperationObject {
tags?: string[];
summary?: string;
description?: string;
externalDocs?: ExternalDocumentationObject;
operationId?: string;
parameters?: (ParameterObject | ReferenceObject)[];
requestBody?: RequestBodyObject | ReferenceObject;
responses: ResponsesObject;
callbacks?: CallbacksObject;
deprecated?: boolean;
security?: SecurityRequirementObject[];
servers?: ServerObject[];
}
export interface ExternalDocumentationObject {
description?: string;
url: string;
}
export type ParameterLocation = 'query' | 'header' | 'path' | 'cookie';
export type ParameterStyle =
| 'matrix'
| 'label'
| 'form'
| 'simple'
| 'spaceDelimited'
| 'pipeDelimited'
| 'deepObject';
export interface BaseParameterObject {
description?: string;
required?: boolean;
deprecated?: boolean;
allowEmptyValue?: boolean;
style?: ParameterStyle;
explode?: boolean;
allowReserved?: boolean;
schema?: SchemaObject | ReferenceObject;
examples?: Record<string, ExampleObject | ReferenceObject>;
example?: any;
content?: ContentObject;
}
export interface ParameterObject extends BaseParameterObject {
name: string;
in: ParameterLocation;
}
export interface RequestBodyObject {
description?: string;
content: ContentObject;
required?: boolean;
}
export type ContentObject = Record<string, MediaTypeObject>;
export interface MediaTypeObject {
schema?: SchemaObject | ReferenceObject;
examples?: ExamplesObject;
example?: any;
encoding?: EncodingObject;
}
export type EncodingObject = Record<string, EncodingPropertyObject>;
export interface EncodingPropertyObject {
contentType?: string;
headers?: Record<string, HeaderObject | ReferenceObject>;
style?: string;
explode?: boolean;
allowReserved?: boolean;
}
export interface ResponsesObject
extends Record<string, ResponseObject | ReferenceObject | undefined> {
default?: ResponseObject | ReferenceObject;
}
export interface ResponseObject {
description: string;
headers?: HeadersObject;
content?: ContentObject;
links?: LinksObject;
}
export type CallbacksObject = Record<string, CallbackObject | ReferenceObject>;
export type CallbackObject = Record<string, PathItemObject>;
export type HeadersObject = Record<string, HeaderObject | ReferenceObject>;
export interface ExampleObject {
summary?: string;
description?: string;
value?: any;
externalValue?: string;
}
export type LinksObject = Record<string, LinkObject | ReferenceObject>;
export interface LinkObject {
operationRef?: string;
operationId?: string;
parameters?: LinkParametersObject;
requestBody?: any | string;
description?: string;
server?: ServerObject;
}
export type LinkParametersObject = Record<string, any>;
export type HeaderObject = BaseParameterObject;
export interface TagObject {
name: string;
description?: string;
externalDocs?: ExternalDocumentationObject;
}
export type ExamplesObject = Record<string, ExampleObject | ReferenceObject>;
export interface ReferenceObject {
$ref: string | (() => string);
}
export interface SchemaObject {
nullable?: boolean;
discriminator?: DiscriminatorObject;
readOnly?: boolean;
writeOnly?: boolean;
xml?: XmlObject;
externalDocs?: ExternalDocumentationObject;
example?: any;
examples?: any[] | Record<string, any>;
deprecated?: boolean;
type?: string | (new (...args) => any) | (() => new (...args) => any);
allOf?: (SchemaObject | ReferenceObject)[];
oneOf?: (SchemaObject | ReferenceObject)[];
anyOf?: (SchemaObject | ReferenceObject)[];
not?: SchemaObject | ReferenceObject;
items?: SchemaObject | ReferenceObject;
properties?: Record<string, SchemaObject | ReferenceObject>;
additionalProperties?: SchemaObject | ReferenceObject | boolean;
patternProperties?: SchemaObject | ReferenceObject | any;
description?: string;
format?: string;
default?: any;
title?: string;
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
maxLength?: number;
minLength?: number;
pattern?: string;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
maxProperties?: number;
minProperties?: number;
required?: string[];
enum?: any[];
}
export type SchemasObject = Record<string, SchemaObject>;
export interface DiscriminatorObject {
propertyName: string;
mapping?: Record<string, string>;
}
export interface XmlObject {
name?: string;
namespace?: string;
prefix?: string;
attribute?: boolean;
wrapped?: boolean;
}
export type SecuritySchemeType = 'apiKey' | 'http' | 'oauth2' | 'openIdConnect';
export interface SecuritySchemeObject {
type: SecuritySchemeType;
description?: string;
name?: string;
in?: string;
scheme?: string;
bearerFormat?: string;
flows?: OAuthFlowsObject;
openIdConnectUrl?: string;
}
export interface OAuthFlowsObject {
implicit?: OAuthFlowObject;
password?: OAuthFlowObject;
clientCredentials?: OAuthFlowObject;
authorizationCode?: OAuthFlowObject;
}
export interface OAuthFlowObject {
authorizationUrl?: string;
tokenUrl?: string;
refreshUrl?: string;
scopes: ScopesObject;
}
export type ScopesObject = Record<string, any>;
export type SecurityRequirementObject = Record<string, string[]>;
/**
* 非 open api spec
*/
export type SwaggerEnumType =
| string[]
| number[]
| (string | number)[]
| Record<number, string>;
export interface Type<T = any> {
new (...args: any[]): T;
}
export interface SchemaObjectMetadata
extends Omit<SchemaObject, 'type' | 'required'> {
type?: Type<unknown> | [Type] | string | Record<string, any>;
isArray?: boolean;
required?: boolean;
name?: string;
enumName?: string;
}
export type AuthType =
| 'basic'
| 'bearer'
| 'cookie'
| 'oauth2'
| 'apikey'
| 'custom';
/**
* 继承自 https://swagger.io/specification/#security-scheme-object
*/
export interface AuthOptions extends Omit<SecuritySchemeObject, 'type'> {
/**
* 验权类型
* basic => http basic 验证
* bearer => http jwt 验证
* cookie => cookie 方式验证
* oauth2 => 使用 oauth2
* apikey => apiKey
* custom => 自定义方式
*/
authType: AuthType;
/**
* https://swagger.io/specification/#security-scheme-object type 字段
*/
type?: SecuritySchemeType;
/**
* authType = cookie 时可以修改,通过 ApiCookie 装饰器关联的名称
*/
securityName?: string;
/**
* authType = cookie 时可以修改,cookie 的名称
*/
cookieName?: string;
}
/**
* see https://swagger.io/specification/
*/
export interface SwaggerOptions {
/**
* 默认值: My Project
* https://swagger.io/specification/#info-object title 字段
*/
title?: string;
/**
* 默认值: This is a swagger-ui for midwayjs project
* https://swagger.io/specification/#info-object description 字段
*/
description?: string;
/**
* 默认值: 1.0.0
* https://swagger.io/specification/#info-object version 字段
*/
version?: string;
/**
* https://swagger.io/specification/#info-object contact 字段
*/
contact?: ContactObject;
/**
* https://swagger.io/specification/#info-object license 字段
*/
license?: LicenseObject;
/**
* https://swagger.io/specification/#info-object termsOfService 字段
*/
termsOfService?: string;
/**
* https://swagger.io/specification/#openapi-object externalDocs 字段
*/
externalDocs?: ExternalDocumentationObject;
/**
* https://swagger.io/specification/#openapi-object servers 字段
*/
servers?: Array<ServerObject>;
/**
* https://swagger.io/specification/#openapi-object tags 字段
*/
tags?: Array<TagObject>;
/**
* 可以参考 https://swagger.io/specification/#security-scheme-object
*/
auth?: AuthOptions | AuthOptions[];
/**
* 默认值: /swagger-ui
* 访问 swagger ui 的路径
*/
swaggerPath?: string;
/**
* 对路由 tag 进行 ascii 排序
* 可以使用 1-xxx、2-xxx、3-xxx 来定义 tag
*/
tagSortable?: boolean;
/**
* UI 展示中需要用到的配置
* 可以参考 https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md#display
*/
displayOptions?: {
deepLinking?: boolean;
displayOperationId?: boolean;
defaultModelsExpandDepth?: number;
defaultModelExpandDepth?: number;
defaultModelRendering?: 'example' | 'model';
displayRequestDuration?: boolean;
docExpansion?: 'list' | 'full' | 'none';
filter?: boolean | string;
maxDisplayedTags?: number;
showExtensions?: boolean;
showCommonExtensions?: boolean;
useUnsafeMarkdown?: boolean;
tryItOutEnabled?: boolean;
};
documentOptions?: {
/**
* 自定义 operationIdFactory,用于生成 operationId
* @default () => controllerKey_webRouter.methodKey
*/
operationIdFactory?: (
controllerKey: string,
webRouter: RouterOption
) => string;
};
swaggerUIRender?: (
config: SwaggerOptions,
swaggerExplorer: SwaggerExplorer
) => (pathname: string) => Promise<{ ext: string; content: any }>;
swaggerUIRenderOptions?: Record<string, any>;
/**
* 自定义路由过滤器
*/
routerFilter?: (url: string, options: RouterOption) => boolean;
}
export interface MixDecoratorMetadata {
key: string;
metadata: any;
propertyName?: string;
}