@moostjs/swagger 0.3.40 → 0.3.42
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 +25 -5
- package/dist/index.d.ts +9 -4
- package/dist/index.mjs +25 -6
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -15,8 +15,15 @@ function getSwaggerMate() {
|
|
|
15
15
|
const SwaggerTag = (tag) => getSwaggerMate().decorate('swaggerTags', tag, true);
|
|
16
16
|
const SwaggerExclude = () => getSwaggerMate().decorate('swaggerExclude', true);
|
|
17
17
|
const SwaggerDescription = (descr) => getSwaggerMate().decorate('swaggerDescription', descr);
|
|
18
|
-
function SwaggerResponse(code, opts) {
|
|
18
|
+
function SwaggerResponse(code, opts, example) {
|
|
19
19
|
return getSwaggerMate().decorate(meta => {
|
|
20
|
+
let ex;
|
|
21
|
+
if (example) {
|
|
22
|
+
ex = example;
|
|
23
|
+
}
|
|
24
|
+
if (typeof code !== 'number' && opts) {
|
|
25
|
+
ex = opts;
|
|
26
|
+
}
|
|
20
27
|
meta.swaggerResponses = meta.swaggerResponses || {};
|
|
21
28
|
const keyCode = typeof code === 'number' ? code : 0;
|
|
22
29
|
const opt = typeof code === 'number' ? opts : code;
|
|
@@ -27,7 +34,7 @@ function SwaggerResponse(code, opts) {
|
|
|
27
34
|
? opt.response
|
|
28
35
|
: opt);
|
|
29
36
|
meta.swaggerResponses[keyCode] = meta.swaggerResponses[keyCode] || {};
|
|
30
|
-
meta.swaggerResponses[keyCode][contentType] = response;
|
|
37
|
+
meta.swaggerResponses[keyCode][contentType] = { response, example: ex };
|
|
31
38
|
return meta;
|
|
32
39
|
});
|
|
33
40
|
}
|
|
@@ -47,6 +54,9 @@ function SwaggerRequestBody(opt) {
|
|
|
47
54
|
function SwaggerParam(opts) {
|
|
48
55
|
return getSwaggerMate().decorate('swaggerParams', opts, true);
|
|
49
56
|
}
|
|
57
|
+
function SwaggerExample(example) {
|
|
58
|
+
return getSwaggerMate().decorate('swaggerExample', example);
|
|
59
|
+
}
|
|
50
60
|
|
|
51
61
|
/******************************************************************************
|
|
52
62
|
Copyright (c) Microsoft Corporation.
|
|
@@ -121,11 +131,16 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
121
131
|
if (hmeta?.swaggerResponses) {
|
|
122
132
|
for (const [code, responseConfigs] of Object.entries(hmeta.swaggerResponses)) {
|
|
123
133
|
const newCode = code === '0' ? getDefaultStatusCode(handlerMethod) : code;
|
|
124
|
-
for (const [contentType,
|
|
125
|
-
const
|
|
134
|
+
for (const [contentType, conf] of Object.entries(responseConfigs)) {
|
|
135
|
+
const { response, example } = conf;
|
|
136
|
+
const schema = getSwaggerSchemaFromSwaggerConfigType(response);
|
|
126
137
|
if (schema) {
|
|
127
138
|
responses = responses || {};
|
|
128
|
-
responses[newCode] = {
|
|
139
|
+
responses[newCode] = {
|
|
140
|
+
content: {
|
|
141
|
+
[contentType]: { schema: { ...schema, example: example || schema.example } },
|
|
142
|
+
},
|
|
143
|
+
};
|
|
129
144
|
}
|
|
130
145
|
}
|
|
131
146
|
}
|
|
@@ -300,6 +315,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
300
315
|
const globalSchemas = {};
|
|
301
316
|
function getSwaggerSchema(parsed, forParam) {
|
|
302
317
|
const zodType = parsed.$ref;
|
|
318
|
+
const meta = zodType.__type_ref ? getSwaggerMate().read(zodType.__type_ref) : undefined;
|
|
303
319
|
if (!forParam && zodType.__type_ref && globalSchemas[zodType.__type_ref.name]) {
|
|
304
320
|
return { $ref: `#/components/schemas/${zodType.__type_ref.name}` };
|
|
305
321
|
}
|
|
@@ -337,6 +353,9 @@ function getSwaggerSchema(parsed, forParam) {
|
|
|
337
353
|
schema.enum = Object.keys(parsed.$value);
|
|
338
354
|
}
|
|
339
355
|
}
|
|
356
|
+
if (meta?.swaggerExample) {
|
|
357
|
+
schema.example = meta.swaggerExample;
|
|
358
|
+
}
|
|
340
359
|
if (forParam) {
|
|
341
360
|
switch (parsed.$type) {
|
|
342
361
|
case 'ZodAny':
|
|
@@ -666,6 +685,7 @@ exports.SwaggerController = __decorate([
|
|
|
666
685
|
], exports.SwaggerController);
|
|
667
686
|
|
|
668
687
|
exports.SwaggerDescription = SwaggerDescription;
|
|
688
|
+
exports.SwaggerExample = SwaggerExample;
|
|
669
689
|
exports.SwaggerExclude = SwaggerExclude;
|
|
670
690
|
exports.SwaggerParam = SwaggerParam;
|
|
671
691
|
exports.SwaggerRequestBody = SwaggerRequestBody;
|
package/dist/index.d.ts
CHANGED
|
@@ -49,8 +49,12 @@ interface TSwaggerMate {
|
|
|
49
49
|
swaggerTags: string[];
|
|
50
50
|
swaggerExclude: boolean;
|
|
51
51
|
swaggerDescription: string;
|
|
52
|
-
swaggerResponses: Record<number, Record<string,
|
|
52
|
+
swaggerResponses: Record<number, Record<string, {
|
|
53
|
+
response: TSwaggerConfigType;
|
|
54
|
+
example?: unknown;
|
|
55
|
+
}>>;
|
|
53
56
|
swaggerRequestBody: Record<string, TSwaggerConfigType>;
|
|
57
|
+
swaggerExample?: unknown;
|
|
54
58
|
swaggerParams: Array<{
|
|
55
59
|
name: string;
|
|
56
60
|
in: 'query' | 'header' | 'path' | 'formData' | 'body';
|
|
@@ -70,10 +74,11 @@ type TSwaggerResponseOpts = TSwaggerConfigType | TSwaggerResponseConfig;
|
|
|
70
74
|
declare const SwaggerTag: (tag: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
71
75
|
declare const SwaggerExclude: () => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
72
76
|
declare const SwaggerDescription: (descr: string) => MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
73
|
-
declare function SwaggerResponse(opts: TSwaggerResponseOpts): MethodDecorator;
|
|
74
|
-
declare function SwaggerResponse(code: number, opts: TSwaggerResponseOpts): MethodDecorator;
|
|
77
|
+
declare function SwaggerResponse(opts: TSwaggerResponseOpts, exmaple?: unknown): MethodDecorator;
|
|
78
|
+
declare function SwaggerResponse(code: number, opts: TSwaggerResponseOpts, exmaple?: unknown): MethodDecorator;
|
|
75
79
|
declare function SwaggerRequestBody(opt: TSwaggerResponseOpts): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
76
80
|
declare function SwaggerParam(opts: TSwaggerMate['swaggerParams'][number]): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
81
|
+
declare function SwaggerExample(example: unknown): MethodDecorator & ClassDecorator & ParameterDecorator & PropertyDecorator;
|
|
77
82
|
|
|
78
83
|
declare class SwaggerController {
|
|
79
84
|
protected title: string;
|
|
@@ -87,4 +92,4 @@ declare class SwaggerController {
|
|
|
87
92
|
'serve'(path: string): Promise<unknown>;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, type TSwaggerConfigType, type TSwaggerMate, type TSwaggerResponseOpts, getSwaggerMate };
|
|
95
|
+
export { SwaggerController, SwaggerDescription, SwaggerExample, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, type TSwaggerConfigType, type TSwaggerMate, type TSwaggerResponseOpts, getSwaggerMate };
|
package/dist/index.mjs
CHANGED
|
@@ -13,8 +13,15 @@ function getSwaggerMate() {
|
|
|
13
13
|
const SwaggerTag = (tag) => getSwaggerMate().decorate('swaggerTags', tag, true);
|
|
14
14
|
const SwaggerExclude = () => getSwaggerMate().decorate('swaggerExclude', true);
|
|
15
15
|
const SwaggerDescription = (descr) => getSwaggerMate().decorate('swaggerDescription', descr);
|
|
16
|
-
function SwaggerResponse(code, opts) {
|
|
16
|
+
function SwaggerResponse(code, opts, example) {
|
|
17
17
|
return getSwaggerMate().decorate(meta => {
|
|
18
|
+
let ex;
|
|
19
|
+
if (example) {
|
|
20
|
+
ex = example;
|
|
21
|
+
}
|
|
22
|
+
if (typeof code !== 'number' && opts) {
|
|
23
|
+
ex = opts;
|
|
24
|
+
}
|
|
18
25
|
meta.swaggerResponses = meta.swaggerResponses || {};
|
|
19
26
|
const keyCode = typeof code === 'number' ? code : 0;
|
|
20
27
|
const opt = typeof code === 'number' ? opts : code;
|
|
@@ -25,7 +32,7 @@ function SwaggerResponse(code, opts) {
|
|
|
25
32
|
? opt.response
|
|
26
33
|
: opt);
|
|
27
34
|
meta.swaggerResponses[keyCode] = meta.swaggerResponses[keyCode] || {};
|
|
28
|
-
meta.swaggerResponses[keyCode][contentType] = response;
|
|
35
|
+
meta.swaggerResponses[keyCode][contentType] = { response, example: ex };
|
|
29
36
|
return meta;
|
|
30
37
|
});
|
|
31
38
|
}
|
|
@@ -45,6 +52,9 @@ function SwaggerRequestBody(opt) {
|
|
|
45
52
|
function SwaggerParam(opts) {
|
|
46
53
|
return getSwaggerMate().decorate('swaggerParams', opts, true);
|
|
47
54
|
}
|
|
55
|
+
function SwaggerExample(example) {
|
|
56
|
+
return getSwaggerMate().decorate('swaggerExample', example);
|
|
57
|
+
}
|
|
48
58
|
|
|
49
59
|
/******************************************************************************
|
|
50
60
|
Copyright (c) Microsoft Corporation.
|
|
@@ -119,11 +129,16 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
119
129
|
if (hmeta?.swaggerResponses) {
|
|
120
130
|
for (const [code, responseConfigs] of Object.entries(hmeta.swaggerResponses)) {
|
|
121
131
|
const newCode = code === '0' ? getDefaultStatusCode(handlerMethod) : code;
|
|
122
|
-
for (const [contentType,
|
|
123
|
-
const
|
|
132
|
+
for (const [contentType, conf] of Object.entries(responseConfigs)) {
|
|
133
|
+
const { response, example } = conf;
|
|
134
|
+
const schema = getSwaggerSchemaFromSwaggerConfigType(response);
|
|
124
135
|
if (schema) {
|
|
125
136
|
responses = responses || {};
|
|
126
|
-
responses[newCode] = {
|
|
137
|
+
responses[newCode] = {
|
|
138
|
+
content: {
|
|
139
|
+
[contentType]: { schema: { ...schema, example: example || schema.example } },
|
|
140
|
+
},
|
|
141
|
+
};
|
|
127
142
|
}
|
|
128
143
|
}
|
|
129
144
|
}
|
|
@@ -298,6 +313,7 @@ function mapToSwaggerSpec(metadata, options, logger) {
|
|
|
298
313
|
const globalSchemas = {};
|
|
299
314
|
function getSwaggerSchema(parsed, forParam) {
|
|
300
315
|
const zodType = parsed.$ref;
|
|
316
|
+
const meta = zodType.__type_ref ? getSwaggerMate().read(zodType.__type_ref) : undefined;
|
|
301
317
|
if (!forParam && zodType.__type_ref && globalSchemas[zodType.__type_ref.name]) {
|
|
302
318
|
return { $ref: `#/components/schemas/${zodType.__type_ref.name}` };
|
|
303
319
|
}
|
|
@@ -335,6 +351,9 @@ function getSwaggerSchema(parsed, forParam) {
|
|
|
335
351
|
schema.enum = Object.keys(parsed.$value);
|
|
336
352
|
}
|
|
337
353
|
}
|
|
354
|
+
if (meta?.swaggerExample) {
|
|
355
|
+
schema.example = meta.swaggerExample;
|
|
356
|
+
}
|
|
338
357
|
if (forParam) {
|
|
339
358
|
switch (parsed.$type) {
|
|
340
359
|
case 'ZodAny':
|
|
@@ -663,4 +682,4 @@ SwaggerController = __decorate([
|
|
|
663
682
|
__metadata("design:paramtypes", [Object])
|
|
664
683
|
], SwaggerController);
|
|
665
684
|
|
|
666
|
-
export { SwaggerController, SwaggerDescription, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
|
|
685
|
+
export { SwaggerController, SwaggerDescription, SwaggerExample, SwaggerExclude, SwaggerParam, SwaggerRequestBody, SwaggerResponse, SwaggerTag, getSwaggerMate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moostjs/swagger",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.42",
|
|
4
4
|
"description": "@moostjs/swagger",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -37,14 +37,14 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/moostjs/moostjs/tree/main/packages/swagger#readme",
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"moost": "0.3.
|
|
41
|
-
"@moostjs/event-http": "0.3.
|
|
40
|
+
"moost": "0.3.42",
|
|
41
|
+
"@moostjs/event-http": "0.3.42"
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@wooksjs/event-http": "^0.4.35",
|
|
45
45
|
"swagger-ui-dist": "^5.10.5",
|
|
46
46
|
"zod-parser": "^0.0.3",
|
|
47
|
-
"@moostjs/zod": "0.3.
|
|
47
|
+
"@moostjs/zod": "0.3.42",
|
|
48
48
|
"@wooksjs/http-static": "^0.4.35"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|