@moostjs/swagger 0.5.33 → 0.6.1
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/README.md +79 -0
- package/dist/index.cjs +451 -43
- package/dist/index.d.ts +286 -19
- package/dist/index.mjs +445 -45
- package/package.json +32 -30
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Mate, TMoostMetadata, TMateParamMeta } from 'moost';
|
|
2
|
-
import { THeaderHook, TStatusHook } from '@
|
|
2
|
+
import { THeaderHook, TStatusHook } from '@moostjs/event-http';
|
|
3
3
|
|
|
4
4
|
type TFunction = Function;
|
|
5
5
|
|
|
@@ -7,10 +7,33 @@ interface TSwaggerOptions {
|
|
|
7
7
|
title?: string;
|
|
8
8
|
description?: string;
|
|
9
9
|
version?: string;
|
|
10
|
+
contact?: {
|
|
11
|
+
name?: string;
|
|
12
|
+
url?: string;
|
|
13
|
+
email?: string;
|
|
14
|
+
};
|
|
15
|
+
license?: {
|
|
16
|
+
name: string;
|
|
17
|
+
url?: string;
|
|
18
|
+
};
|
|
19
|
+
termsOfService?: string;
|
|
10
20
|
cors?: boolean | string;
|
|
21
|
+
servers?: {
|
|
22
|
+
url: string;
|
|
23
|
+
description?: string;
|
|
24
|
+
}[];
|
|
25
|
+
tags?: {
|
|
26
|
+
name: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
externalDocs?: TSwaggerExternalDocs;
|
|
29
|
+
}[];
|
|
30
|
+
externalDocs?: TSwaggerExternalDocs;
|
|
31
|
+
openapiVersion?: '3.0' | '3.1';
|
|
32
|
+
securitySchemes?: Record<string, TSwaggerSecurityScheme>;
|
|
33
|
+
security?: TSwaggerSecurityRequirement[];
|
|
11
34
|
}
|
|
12
35
|
interface TSwaggerSchema {
|
|
13
|
-
type?: string;
|
|
36
|
+
type?: string | string[];
|
|
14
37
|
$ref?: string;
|
|
15
38
|
title?: string;
|
|
16
39
|
description?: string;
|
|
@@ -41,62 +64,306 @@ interface TSwaggerSchema {
|
|
|
41
64
|
oneOf?: TSwaggerSchema[];
|
|
42
65
|
not?: TSwaggerSchema;
|
|
43
66
|
const?: unknown;
|
|
67
|
+
discriminator?: {
|
|
68
|
+
propertyName: string;
|
|
69
|
+
mapping?: Record<string, string>;
|
|
70
|
+
};
|
|
71
|
+
$defs?: Record<string, TSwaggerSchema>;
|
|
44
72
|
}
|
|
45
73
|
|
|
74
|
+
/** Returns the shared `Mate` instance extended with Swagger/OpenAPI metadata fields. */
|
|
46
75
|
declare function getSwaggerMate(): Mate<TMoostMetadata & TSwaggerMate & {
|
|
47
|
-
params:
|
|
76
|
+
params: TMateParamMeta[];
|
|
48
77
|
}, TMoostMetadata & TSwaggerMate & {
|
|
49
|
-
params:
|
|
78
|
+
params: TMateParamMeta[];
|
|
50
79
|
}>;
|
|
80
|
+
/** Swagger/OpenAPI metadata fields attached to classes and methods by swagger decorators. */
|
|
51
81
|
interface TSwaggerMate {
|
|
52
82
|
swaggerTags: string[];
|
|
53
83
|
swaggerExclude: boolean;
|
|
54
84
|
swaggerDescription: string;
|
|
55
|
-
swaggerResponses: Record<number,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
85
|
+
swaggerResponses: Record<number, {
|
|
86
|
+
description?: string;
|
|
87
|
+
headers?: Record<string, TSwaggerResponseHeader>;
|
|
88
|
+
content: Record<string, {
|
|
89
|
+
response: TSwaggerConfigType;
|
|
90
|
+
example?: unknown;
|
|
91
|
+
}>;
|
|
92
|
+
}>;
|
|
59
93
|
swaggerRequestBody: Record<string, TSwaggerConfigType>;
|
|
60
94
|
swaggerExample?: unknown;
|
|
61
|
-
swaggerParams:
|
|
95
|
+
swaggerParams: {
|
|
62
96
|
name: string;
|
|
63
97
|
in: 'query' | 'header' | 'path' | 'formData' | 'body';
|
|
64
98
|
description?: string;
|
|
65
99
|
required?: boolean;
|
|
66
100
|
type?: TSwaggerConfigType;
|
|
67
|
-
}
|
|
101
|
+
}[];
|
|
102
|
+
swaggerLinks?: TSwaggerLinkConfig[];
|
|
103
|
+
swaggerCallbacks?: TSwaggerCallbackConfig[];
|
|
104
|
+
swaggerSecurity?: TSwaggerSecurityRequirement[];
|
|
105
|
+
swaggerPublic?: boolean;
|
|
106
|
+
swaggerDeprecated?: boolean;
|
|
107
|
+
swaggerOperationId?: string;
|
|
108
|
+
swaggerExternalDocs?: TSwaggerExternalDocs;
|
|
68
109
|
}
|
|
110
|
+
/** A type reference for OpenAPI schema generation — class, schema object, or array wrapper. */
|
|
69
111
|
type TSwaggerConfigType = TFunction | {
|
|
70
112
|
toJsonSchema?: () => unknown;
|
|
71
|
-
} | TSwaggerSchema;
|
|
113
|
+
} | TSwaggerSchema | [TSwaggerConfigType];
|
|
114
|
+
/** Describes a single response header in the OpenAPI spec. */
|
|
115
|
+
interface TSwaggerResponseHeader {
|
|
116
|
+
description?: string;
|
|
117
|
+
required?: boolean;
|
|
118
|
+
type?: TSwaggerConfigType;
|
|
119
|
+
example?: unknown;
|
|
120
|
+
}
|
|
72
121
|
interface TSwaggerResponseConfig {
|
|
73
122
|
contentType?: string;
|
|
74
123
|
description?: string;
|
|
75
124
|
response: TSwaggerConfigType;
|
|
125
|
+
example?: unknown;
|
|
126
|
+
headers?: Record<string, TSwaggerResponseHeader>;
|
|
76
127
|
}
|
|
128
|
+
/** Options accepted by `@SwaggerResponse` — either a bare schema type or a full config. */
|
|
77
129
|
type TSwaggerResponseOpts = TSwaggerConfigType | TSwaggerResponseConfig;
|
|
130
|
+
/** External documentation link for an OpenAPI operation. */
|
|
131
|
+
interface TSwaggerExternalDocs {
|
|
132
|
+
url: string;
|
|
133
|
+
description?: string;
|
|
134
|
+
}
|
|
135
|
+
/** Configuration for an OpenAPI 3.0 link object on a response. */
|
|
136
|
+
interface TSwaggerLinkConfig {
|
|
137
|
+
/** Status code this link applies to. 0 = use default for the HTTP method. */
|
|
138
|
+
statusCode: number;
|
|
139
|
+
/** The link name (key in the response `links` object). */
|
|
140
|
+
name: string;
|
|
141
|
+
/** Target operation by explicit operationId string. */
|
|
142
|
+
operationId?: string;
|
|
143
|
+
/** Target operation by JSON pointer (operationRef). */
|
|
144
|
+
operationRef?: string;
|
|
145
|
+
/** Target operation by controller class + method name, resolved at mapping time. */
|
|
146
|
+
handler?: [TFunction, string];
|
|
147
|
+
/** Map of parameter name to runtime expression (e.g. `'$response.body#/id'`). */
|
|
148
|
+
parameters?: Record<string, string>;
|
|
149
|
+
/** Runtime expression for the request body. */
|
|
150
|
+
requestBody?: string;
|
|
151
|
+
/** Human-readable description. */
|
|
152
|
+
description?: string;
|
|
153
|
+
/** Alternative server for the link target. */
|
|
154
|
+
server?: {
|
|
155
|
+
url: string;
|
|
156
|
+
description?: string;
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
/** Configuration for an OpenAPI 3.0 callback (webhook) on an operation. */
|
|
160
|
+
interface TSwaggerCallbackConfig {
|
|
161
|
+
/** Callback name (key in the operation's `callbacks` object). */
|
|
162
|
+
name: string;
|
|
163
|
+
/** Runtime expression for the callback URL (e.g. `'{$request.body#/callbackUrl}'`). */
|
|
164
|
+
expression: string;
|
|
165
|
+
/** HTTP method your server uses to call the webhook (default: `'post'`). */
|
|
166
|
+
method?: string;
|
|
167
|
+
/** Schema for the request body your server sends. Resolved via the schema pipeline. */
|
|
168
|
+
requestBody?: TSwaggerConfigType;
|
|
169
|
+
/** Content type for the request body (default: `'application/json'`). */
|
|
170
|
+
contentType?: string;
|
|
171
|
+
/** Description for the callback operation. */
|
|
172
|
+
description?: string;
|
|
173
|
+
/** Expected response status code from the webhook receiver (default: `200`). */
|
|
174
|
+
responseStatus?: number;
|
|
175
|
+
/** Description for the expected response (default: `'OK'`). */
|
|
176
|
+
responseDescription?: string;
|
|
177
|
+
}
|
|
178
|
+
/** OpenAPI security requirement — maps scheme name to required scopes. */
|
|
179
|
+
type TSwaggerSecurityRequirement = Record<string, string[]>;
|
|
180
|
+
/** OpenAPI HTTP security scheme (e.g. bearer, basic). */
|
|
181
|
+
interface TSwaggerSecuritySchemeHttp {
|
|
182
|
+
type: 'http';
|
|
183
|
+
scheme: string;
|
|
184
|
+
bearerFormat?: string;
|
|
185
|
+
description?: string;
|
|
186
|
+
}
|
|
187
|
+
/** OpenAPI API key security scheme. */
|
|
188
|
+
interface TSwaggerSecuritySchemeApiKey {
|
|
189
|
+
type: 'apiKey';
|
|
190
|
+
in: 'header' | 'query' | 'cookie';
|
|
191
|
+
name: string;
|
|
192
|
+
description?: string;
|
|
193
|
+
}
|
|
194
|
+
/** Configuration for a single OAuth2 flow. */
|
|
195
|
+
interface TSwaggerSecuritySchemeOAuth2Flow {
|
|
196
|
+
authorizationUrl?: string;
|
|
197
|
+
tokenUrl?: string;
|
|
198
|
+
refreshUrl?: string;
|
|
199
|
+
scopes: Record<string, string>;
|
|
200
|
+
}
|
|
201
|
+
/** OpenAPI OAuth2 security scheme with one or more flows. */
|
|
202
|
+
interface TSwaggerSecuritySchemeOAuth2 {
|
|
203
|
+
type: 'oauth2';
|
|
204
|
+
flows: {
|
|
205
|
+
implicit?: TSwaggerSecuritySchemeOAuth2Flow;
|
|
206
|
+
password?: TSwaggerSecuritySchemeOAuth2Flow;
|
|
207
|
+
clientCredentials?: TSwaggerSecuritySchemeOAuth2Flow;
|
|
208
|
+
authorizationCode?: TSwaggerSecuritySchemeOAuth2Flow;
|
|
209
|
+
};
|
|
210
|
+
description?: string;
|
|
211
|
+
}
|
|
212
|
+
/** OpenAPI OpenID Connect security scheme. */
|
|
213
|
+
interface TSwaggerSecuritySchemeOpenIdConnect {
|
|
214
|
+
type: 'openIdConnect';
|
|
215
|
+
openIdConnectUrl: string;
|
|
216
|
+
description?: string;
|
|
217
|
+
}
|
|
218
|
+
/** Union of all supported OpenAPI security scheme types. */
|
|
219
|
+
type TSwaggerSecurityScheme = TSwaggerSecuritySchemeHttp | TSwaggerSecuritySchemeApiKey | TSwaggerSecuritySchemeOAuth2 | TSwaggerSecuritySchemeOpenIdConnect;
|
|
78
220
|
|
|
221
|
+
/** Adds an OpenAPI tag to a controller or handler for grouping in the Swagger UI. */
|
|
79
222
|
declare const SwaggerTag: (tag: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
223
|
+
/** Excludes a controller or handler from the generated OpenAPI spec. */
|
|
80
224
|
declare const SwaggerExclude: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
225
|
+
/** Sets the OpenAPI description for a handler. */
|
|
81
226
|
declare const SwaggerDescription: (descr: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
82
|
-
|
|
83
|
-
|
|
227
|
+
/**
|
|
228
|
+
* Defines a response schema in the OpenAPI spec.
|
|
229
|
+
* Can be called with just options (uses status 200) or with an explicit status code.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* @SwaggerResponse({ response: MyDto })
|
|
234
|
+
* @SwaggerResponse(404, { response: ErrorDto })
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
declare function SwaggerResponse(opts: TSwaggerResponseOpts, example?: unknown): MethodDecorator;
|
|
238
|
+
declare function SwaggerResponse(code: number, opts: TSwaggerResponseOpts, example?: unknown): MethodDecorator;
|
|
239
|
+
/**
|
|
240
|
+
* Defines the request body schema in the OpenAPI spec.
|
|
241
|
+
*
|
|
242
|
+
* @param opt - Schema definition. Use `{ response: MyDto }` for a typed schema,
|
|
243
|
+
* or `{ response: MyDto, contentType: 'multipart/form-data' }` for a specific content type.
|
|
244
|
+
*/
|
|
84
245
|
declare function SwaggerRequestBody(opt: TSwaggerResponseOpts): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
246
|
+
/** Defines a parameter (query, path, header, cookie) in the OpenAPI spec. */
|
|
85
247
|
declare function SwaggerParam(opts: TSwaggerMate['swaggerParams'][number]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
248
|
+
/** Attaches an example value to a handler's OpenAPI documentation. */
|
|
86
249
|
declare function SwaggerExample(example: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
250
|
+
/** Marks a handler or controller as public, opting out of inherited security requirements. */
|
|
251
|
+
declare const SwaggerPublic: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
252
|
+
/** Marks a handler or controller as deprecated in the OpenAPI spec. */
|
|
253
|
+
declare const SwaggerDeprecated: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
254
|
+
/** Overrides the auto-generated operationId for an endpoint. */
|
|
255
|
+
declare const SwaggerOperationId: (id: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
256
|
+
/** Links an operation to external documentation. */
|
|
257
|
+
declare const SwaggerExternalDocs: (url: string, description?: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
258
|
+
/**
|
|
259
|
+
* Attaches a security requirement to a handler or controller (OR semantics).
|
|
260
|
+
* Multiple calls add alternative requirements — any one suffices.
|
|
261
|
+
*
|
|
262
|
+
* @param schemeName - The name of the security scheme (must match a key in securitySchemes)
|
|
263
|
+
* @param scopes - OAuth2/OIDC scopes required (default: [])
|
|
264
|
+
*/
|
|
265
|
+
declare function SwaggerSecurity(schemeName: string, scopes?: string[]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
266
|
+
/**
|
|
267
|
+
* Attaches a combined security requirement (AND semantics).
|
|
268
|
+
* All schemes in the requirement must be satisfied simultaneously.
|
|
269
|
+
*/
|
|
270
|
+
declare function SwaggerSecurityAll(requirement: TSwaggerSecurityRequirement): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
271
|
+
/** Options for the `@SwaggerLink` decorator — identifies the target operation and parameter mapping. */
|
|
272
|
+
interface TSwaggerLinkOptions {
|
|
273
|
+
/** Target by explicit operationId string. */
|
|
274
|
+
operationId?: string;
|
|
275
|
+
/** Target by JSON pointer (operationRef). */
|
|
276
|
+
operationRef?: string;
|
|
277
|
+
/** Target by controller class + method name, resolved at mapping time. */
|
|
278
|
+
handler?: [TFunction, string];
|
|
279
|
+
/** Map of parameter name to runtime expression (e.g. `'$response.body#/id'`). */
|
|
280
|
+
parameters?: Record<string, string>;
|
|
281
|
+
/** Runtime expression for the request body. */
|
|
282
|
+
requestBody?: string;
|
|
283
|
+
/** Human-readable description. */
|
|
284
|
+
description?: string;
|
|
285
|
+
/** Alternative server for the link target. */
|
|
286
|
+
server?: {
|
|
287
|
+
url: string;
|
|
288
|
+
description?: string;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Adds an OpenAPI link to a response, describing how response values
|
|
293
|
+
* can be used as input to another operation.
|
|
294
|
+
*
|
|
295
|
+
* @example
|
|
296
|
+
* ```ts
|
|
297
|
+
* // Reference by operationId
|
|
298
|
+
* @SwaggerLink('GetUser', {
|
|
299
|
+
* operationId: 'getUser',
|
|
300
|
+
* parameters: { userId: '$response.body#/id' },
|
|
301
|
+
* })
|
|
302
|
+
*
|
|
303
|
+
* // Reference by controller class + method (resolved at mapping time)
|
|
304
|
+
* @SwaggerLink('GetUser', {
|
|
305
|
+
* handler: [UserController, 'getUser'],
|
|
306
|
+
* parameters: { userId: '$response.body#/id' },
|
|
307
|
+
* })
|
|
308
|
+
*
|
|
309
|
+
* // On a specific status code
|
|
310
|
+
* @SwaggerLink(201, 'GetUser', {
|
|
311
|
+
* operationId: 'getUser',
|
|
312
|
+
* parameters: { userId: '$response.body#/id' },
|
|
313
|
+
* })
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
declare function SwaggerLink(name: string, options: TSwaggerLinkOptions): MethodDecorator;
|
|
317
|
+
declare function SwaggerLink(statusCode: number, name: string, options: TSwaggerLinkOptions): MethodDecorator;
|
|
318
|
+
/** Options for the `@SwaggerCallback` decorator — describes a webhook your server sends. */
|
|
319
|
+
interface TSwaggerCallbackOptions {
|
|
320
|
+
/** Runtime expression for the callback URL (e.g. `'{$request.body#/callbackUrl}'`). */
|
|
321
|
+
expression: string;
|
|
322
|
+
/** HTTP method your server uses to call the webhook (default: `'post'`). */
|
|
323
|
+
method?: string;
|
|
324
|
+
/** Schema for the request body your server sends. Resolved via the schema pipeline. */
|
|
325
|
+
requestBody?: TSwaggerConfigType;
|
|
326
|
+
/** Content type for the request body (default: `'application/json'`). */
|
|
327
|
+
contentType?: string;
|
|
328
|
+
/** Description for the callback operation. */
|
|
329
|
+
description?: string;
|
|
330
|
+
/** Expected response status code from the webhook receiver (default: `200`). */
|
|
331
|
+
responseStatus?: number;
|
|
332
|
+
/** Description for the expected response (default: `'OK'`). */
|
|
333
|
+
responseDescription?: string;
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* Documents an OpenAPI callback (webhook) on an operation.
|
|
337
|
+
* Describes a request your server sends to a client-provided URL.
|
|
338
|
+
*
|
|
339
|
+
* @example
|
|
340
|
+
* ```ts
|
|
341
|
+
* @SwaggerCallback('onEvent', {
|
|
342
|
+
* expression: '{$request.body#/callbackUrl}',
|
|
343
|
+
* requestBody: EventPayloadDto,
|
|
344
|
+
* description: 'Event notification sent to subscriber',
|
|
345
|
+
* })
|
|
346
|
+
* @Post('subscribe')
|
|
347
|
+
* subscribe() { ... }
|
|
348
|
+
* ```
|
|
349
|
+
*/
|
|
350
|
+
declare function SwaggerCallback(name: string, options: TSwaggerCallbackOptions): MethodDecorator;
|
|
87
351
|
|
|
352
|
+
/** Serves the Swagger UI, spec.json, and spec.yaml under the `/api-docs` route prefix. */
|
|
88
353
|
declare class SwaggerController {
|
|
89
354
|
protected opts: TSwaggerOptions;
|
|
90
355
|
constructor(opts?: TSwaggerOptions);
|
|
91
356
|
'assetPath': string;
|
|
92
|
-
protected
|
|
93
|
-
|
|
357
|
+
protected processCors(): void;
|
|
358
|
+
serveIndex(url: string, location: THeaderHook, status: TStatusHook): string;
|
|
94
359
|
'swagger-initializer.js'(): string;
|
|
95
360
|
'spec'?: Record<string, unknown>;
|
|
361
|
+
protected resolveSpec(): Promise<Record<string, unknown>>;
|
|
96
362
|
'spec.json'(): Promise<Record<string, unknown>>;
|
|
97
|
-
'
|
|
98
|
-
|
|
363
|
+
'spec.yaml'(): Promise<string>;
|
|
364
|
+
files(url: string): Promise<unknown>;
|
|
365
|
+
serve(path: string): Promise<unknown>;
|
|
99
366
|
}
|
|
100
367
|
|
|
101
|
-
export { SwaggerController, SwaggerDescription, SwaggerExample, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
|
|
102
|
-
export type { TSwaggerConfigType, TSwaggerMate, TSwaggerResponseOpts };
|
|
368
|
+
export { SwaggerCallback, SwaggerController, SwaggerDeprecated, SwaggerDescription, SwaggerExample, SwaggerExclude, SwaggerExternalDocs, SwaggerLink, SwaggerOperationId, SwaggerParam, SwaggerPublic, SwaggerRequestBody, SwaggerResponse, SwaggerSecurity, SwaggerSecurityAll, SwaggerTag, getSwaggerMate };
|
|
369
|
+
export type { TSwaggerCallbackConfig, TSwaggerCallbackOptions, TSwaggerConfigType, TSwaggerExternalDocs, TSwaggerLinkConfig, TSwaggerLinkOptions, TSwaggerMate, TSwaggerResponseHeader, TSwaggerResponseOpts, TSwaggerSecurityRequirement, TSwaggerSecurityScheme, TSwaggerSecuritySchemeApiKey, TSwaggerSecuritySchemeHttp, TSwaggerSecuritySchemeOAuth2, TSwaggerSecuritySchemeOAuth2Flow, TSwaggerSecuritySchemeOpenIdConnect };
|