@nestia/core 12.0.0-rc.2 → 12.0.0-rc.4
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/LICENSE +21 -21
- package/MIGRATION.md +138 -169
- package/lib/decorators/NoTransformConfigurationError.js +5 -5
- package/lib/decorators/NoTransformConfigurationError.js.map +1 -1
- package/native/cmd/ttsc-nestia/main.go +11 -11
- package/native/go.mod +32 -32
- package/native/go.sum +54 -54
- package/native/plugin/plan.go +102 -102
- package/native/transform/ast.go +32 -32
- package/native/transform/build.go +388 -380
- package/native/transform/cleanup.go +408 -408
- package/native/transform/contributor.go +97 -97
- package/native/transform/core_querify.go +231 -231
- package/native/transform/core_transform.go +1996 -1996
- package/native/transform/core_websocket.go +115 -115
- package/native/transform/exports.go +13 -13
- package/native/transform/mcp_transform.go +414 -414
- package/native/transform/node_transform.go +357 -357
- package/native/transform/path_rewrite.go +285 -285
- package/native/transform/printer.go +244 -244
- package/native/transform/rewrite.go +668 -668
- package/native/transform/run.go +73 -73
- package/native/transform/transform.go +336 -336
- package/native/transform/typia_fast.go +375 -352
- package/native/transform/typia_replacement.go +24 -24
- package/native/transform.cjs +43 -43
- package/package.json +9 -9
- package/src/adaptors/McpAdaptor.ts +276 -276
- package/src/adaptors/WebSocketAdaptor.ts +429 -429
- package/src/decorators/DynamicModule.ts +44 -44
- package/src/decorators/EncryptedBody.ts +97 -97
- package/src/decorators/EncryptedController.ts +40 -40
- package/src/decorators/EncryptedModule.ts +98 -98
- package/src/decorators/EncryptedRoute.ts +213 -213
- package/src/decorators/HumanRoute.ts +21 -21
- package/src/decorators/McpRoute.ts +154 -154
- package/src/decorators/NoTransformConfigurationError.ts +40 -40
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +180 -180
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +187 -187
- package/src/decorators/TypedHeaders.ts +66 -66
- package/src/decorators/TypedParam.ts +77 -77
- package/src/decorators/TypedQuery.ts +234 -234
- package/src/decorators/TypedRoute.ts +198 -198
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/doNotThrowTransformError.ts +5 -5
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IMcpRouteReflect.ts +40 -40
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/get_path_and_querify.ts +94 -94
- package/src/decorators/internal/get_path_and_stringify.ts +110 -110
- package/src/decorators/internal/get_text_body.ts +16 -16
- package/src/decorators/internal/headers_to_object.ts +11 -11
- package/src/decorators/internal/is_request_body_undefined.ts +12 -12
- package/src/decorators/internal/load_controller.ts +94 -94
- package/src/decorators/internal/route_error.ts +43 -43
- package/src/decorators/internal/validate_request_body.ts +64 -64
- package/src/decorators/internal/validate_request_form_data.ts +67 -67
- package/src/decorators/internal/validate_request_headers.ts +76 -76
- package/src/decorators/internal/validate_request_query.ts +83 -83
- package/src/index.ts +5 -5
- package/src/module.ts +25 -25
- package/src/options/IRequestBodyValidator.ts +20 -20
- package/src/options/IRequestFormDataProps.ts +27 -27
- package/src/options/IRequestHeadersValidator.ts +22 -22
- package/src/options/IRequestQueryValidator.ts +20 -20
- package/src/options/IResponseBodyQuerifier.ts +25 -25
- package/src/options/IResponseBodyStringifier.ts +30 -30
- package/src/transform.ts +26 -26
- package/src/typings/Creator.ts +3 -3
- package/src/typings/get-function-location.d.ts +7 -7
- package/src/utils/ArrayUtil.ts +7 -7
- package/src/utils/ExceptionManager.ts +115 -115
- package/src/utils/Singleton.ts +16 -16
- package/src/utils/SourceFinder.ts +54 -54
- package/src/utils/VersioningStrategy.ts +27 -27
- package/README.md +0 -93
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { OpenApi } from "@typia/interface";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Swagger customization decorator.
|
|
5
|
-
*
|
|
6
|
-
* `SwaggerCustomizer` is a method decorator function which can used for
|
|
7
|
-
* customizing the swagger data with `npx nestia swagger` command. Furthermore,
|
|
8
|
-
* it is possible to add plugin properties starting with `x-` characters.
|
|
9
|
-
*
|
|
10
|
-
* In other words, this decorator function does not affect to the runtime, but
|
|
11
|
-
* only for the swagger data customization.
|
|
12
|
-
*
|
|
13
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
14
|
-
* @param closure Callback function which can customize the swagger data
|
|
15
|
-
* @returns Method decorator
|
|
16
|
-
*/
|
|
17
|
-
export function SwaggerCustomizer(
|
|
18
|
-
closure: (props: SwaggerCustomizer.IProps) => unknown,
|
|
19
|
-
): MethodDecorator {
|
|
20
|
-
return function SwaggerCustomizer(
|
|
21
|
-
target: Object,
|
|
22
|
-
propertyKey: string | symbol,
|
|
23
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
24
|
-
) {
|
|
25
|
-
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = (() => {
|
|
26
|
-
if (Reflect.hasMetadata("nestia/SwaggerCustomizer", target, propertyKey))
|
|
27
|
-
return Reflect.getMetadata(
|
|
28
|
-
"nestia/SwaggerCustomizer",
|
|
29
|
-
target,
|
|
30
|
-
propertyKey,
|
|
31
|
-
);
|
|
32
|
-
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = [];
|
|
33
|
-
Reflect.defineMetadata(
|
|
34
|
-
"nestia/SwaggerCustomizer",
|
|
35
|
-
array,
|
|
36
|
-
target,
|
|
37
|
-
propertyKey,
|
|
38
|
-
);
|
|
39
|
-
return array;
|
|
40
|
-
})();
|
|
41
|
-
array.push(closure);
|
|
42
|
-
return descriptor;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
export namespace SwaggerCustomizer {
|
|
46
|
-
/**
|
|
47
|
-
* Properties for the `SwaggerCustomizer` decorator.
|
|
48
|
-
*
|
|
49
|
-
* `SwaggerCustomizer.IProps` is a type for the `closure` parameter of the
|
|
50
|
-
* `SwaggerCustomizer` decorator. It's a callback function which can customize
|
|
51
|
-
* the swagger data.
|
|
52
|
-
*/
|
|
53
|
-
export interface IProps {
|
|
54
|
-
/** Swagger data. */
|
|
55
|
-
swagger: OpenApi.IDocument;
|
|
56
|
-
|
|
57
|
-
/** Method of the route. */
|
|
58
|
-
method: string;
|
|
59
|
-
|
|
60
|
-
/** Path of the route. */
|
|
61
|
-
path: string;
|
|
62
|
-
|
|
63
|
-
/** Route data. */
|
|
64
|
-
route: OpenApi.IOperation;
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Get neighbor endpoint data through the controller method.
|
|
68
|
-
*
|
|
69
|
-
* @param func Controller method to find the neighbor endpoint
|
|
70
|
-
* @returns Neighbor endpoint data
|
|
71
|
-
*/
|
|
72
|
-
at(func: Function): ISwaggerEndpoint | undefined;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Get neighbor route data.
|
|
76
|
-
*
|
|
77
|
-
* @param accessor Accessor for getting neighbor route data
|
|
78
|
-
* @returns Neighbor route data
|
|
79
|
-
*/
|
|
80
|
-
get(accessor: IAccessor): OpenApi.IOperation | undefined;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/** Accessor for getting neighbor route data. */
|
|
84
|
-
export interface IAccessor {
|
|
85
|
-
/** Path of the neighbor route. */
|
|
86
|
-
path: string;
|
|
87
|
-
|
|
88
|
-
/** Method of the neighbor route. */
|
|
89
|
-
method: string;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/** Endpoint info of the route. */
|
|
93
|
-
export interface ISwaggerEndpoint extends IAccessor {
|
|
94
|
-
/** Route data. */
|
|
95
|
-
route: OpenApi.IOperation;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
1
|
+
import { OpenApi } from "@typia/interface";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Swagger customization decorator.
|
|
5
|
+
*
|
|
6
|
+
* `SwaggerCustomizer` is a method decorator function which can used for
|
|
7
|
+
* customizing the swagger data with `npx nestia swagger` command. Furthermore,
|
|
8
|
+
* it is possible to add plugin properties starting with `x-` characters.
|
|
9
|
+
*
|
|
10
|
+
* In other words, this decorator function does not affect to the runtime, but
|
|
11
|
+
* only for the swagger data customization.
|
|
12
|
+
*
|
|
13
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
14
|
+
* @param closure Callback function which can customize the swagger data
|
|
15
|
+
* @returns Method decorator
|
|
16
|
+
*/
|
|
17
|
+
export function SwaggerCustomizer(
|
|
18
|
+
closure: (props: SwaggerCustomizer.IProps) => unknown,
|
|
19
|
+
): MethodDecorator {
|
|
20
|
+
return function SwaggerCustomizer(
|
|
21
|
+
target: Object,
|
|
22
|
+
propertyKey: string | symbol,
|
|
23
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
24
|
+
) {
|
|
25
|
+
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = (() => {
|
|
26
|
+
if (Reflect.hasMetadata("nestia/SwaggerCustomizer", target, propertyKey))
|
|
27
|
+
return Reflect.getMetadata(
|
|
28
|
+
"nestia/SwaggerCustomizer",
|
|
29
|
+
target,
|
|
30
|
+
propertyKey,
|
|
31
|
+
);
|
|
32
|
+
const array: Array<(props: SwaggerCustomizer.IProps) => unknown> = [];
|
|
33
|
+
Reflect.defineMetadata(
|
|
34
|
+
"nestia/SwaggerCustomizer",
|
|
35
|
+
array,
|
|
36
|
+
target,
|
|
37
|
+
propertyKey,
|
|
38
|
+
);
|
|
39
|
+
return array;
|
|
40
|
+
})();
|
|
41
|
+
array.push(closure);
|
|
42
|
+
return descriptor;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export namespace SwaggerCustomizer {
|
|
46
|
+
/**
|
|
47
|
+
* Properties for the `SwaggerCustomizer` decorator.
|
|
48
|
+
*
|
|
49
|
+
* `SwaggerCustomizer.IProps` is a type for the `closure` parameter of the
|
|
50
|
+
* `SwaggerCustomizer` decorator. It's a callback function which can customize
|
|
51
|
+
* the swagger data.
|
|
52
|
+
*/
|
|
53
|
+
export interface IProps {
|
|
54
|
+
/** Swagger data. */
|
|
55
|
+
swagger: OpenApi.IDocument;
|
|
56
|
+
|
|
57
|
+
/** Method of the route. */
|
|
58
|
+
method: string;
|
|
59
|
+
|
|
60
|
+
/** Path of the route. */
|
|
61
|
+
path: string;
|
|
62
|
+
|
|
63
|
+
/** Route data. */
|
|
64
|
+
route: OpenApi.IOperation;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get neighbor endpoint data through the controller method.
|
|
68
|
+
*
|
|
69
|
+
* @param func Controller method to find the neighbor endpoint
|
|
70
|
+
* @returns Neighbor endpoint data
|
|
71
|
+
*/
|
|
72
|
+
at(func: Function): ISwaggerEndpoint | undefined;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Get neighbor route data.
|
|
76
|
+
*
|
|
77
|
+
* @param accessor Accessor for getting neighbor route data
|
|
78
|
+
* @returns Neighbor route data
|
|
79
|
+
*/
|
|
80
|
+
get(accessor: IAccessor): OpenApi.IOperation | undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Accessor for getting neighbor route data. */
|
|
84
|
+
export interface IAccessor {
|
|
85
|
+
/** Path of the neighbor route. */
|
|
86
|
+
path: string;
|
|
87
|
+
|
|
88
|
+
/** Method of the neighbor route. */
|
|
89
|
+
method: string;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Endpoint info of the route. */
|
|
93
|
+
export interface ISwaggerEndpoint extends IAccessor {
|
|
94
|
+
/** Route data. */
|
|
95
|
+
route: OpenApi.IOperation;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -1,180 +1,180 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Attach example values to Swagger documents.
|
|
3
|
-
*
|
|
4
|
-
* `SwaggerExample` is a namespace of decorators that attach example values
|
|
5
|
-
* to controller methods (request/response bodies, parameters), so that
|
|
6
|
-
* `@nestia/sdk`'s Swagger generator can populate the `example` / `examples`
|
|
7
|
-
* fields of the generated OpenAPI document.
|
|
8
|
-
*
|
|
9
|
-
* The decorators only affect Swagger document generation. They do not change
|
|
10
|
-
* runtime behavior, validation, or SDK function signatures.
|
|
11
|
-
*
|
|
12
|
-
* @example
|
|
13
|
-
*
|
|
14
|
-
* ```typescript
|
|
15
|
-
* import core from "@nestia/core";
|
|
16
|
-
* import { Controller } from "@nestjs/common";
|
|
17
|
-
* import typia from "typia";
|
|
18
|
-
*
|
|
19
|
-
* @Controller("bbs/articles")
|
|
20
|
-
* export class BbsArticlesController {
|
|
21
|
-
* // Single response example.
|
|
22
|
-
* @core.SwaggerExample.Response(typia.random<IBbsArticle>())
|
|
23
|
-
* @core.TypedRoute.Post()
|
|
24
|
-
* public async create(
|
|
25
|
-
* // Multiple named parameter examples plus a default one.
|
|
26
|
-
* @core.SwaggerExample.Parameter(typia.random<IBbsArticle.ICreate>())
|
|
27
|
-
* @core.SwaggerExample.Parameter("x", typia.random<IBbsArticle.ICreate>())
|
|
28
|
-
* @core.SwaggerExample.Parameter("y", typia.random<IBbsArticle.ICreate>())
|
|
29
|
-
* @core.TypedBody()
|
|
30
|
-
* input: IBbsArticle.ICreate,
|
|
31
|
-
* ): Promise<IBbsArticle> {
|
|
32
|
-
* // ...
|
|
33
|
-
* }
|
|
34
|
-
* }
|
|
35
|
-
* ```
|
|
36
|
-
*
|
|
37
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
38
|
-
*/
|
|
39
|
-
export namespace SwaggerExample {
|
|
40
|
-
/**
|
|
41
|
-
* Attach an example value to the response body of a controller method.
|
|
42
|
-
*
|
|
43
|
-
* Two forms are supported:
|
|
44
|
-
*
|
|
45
|
-
* - {@link Response | `Response(value)`}: registers `value` as the single
|
|
46
|
-
* default `example`.
|
|
47
|
-
* - {@link Response | `Response(key, value)`}: registers `value` under
|
|
48
|
-
* `examples[key]`. May be applied multiple times to attach several
|
|
49
|
-
* named examples.
|
|
50
|
-
*
|
|
51
|
-
* Both forms can coexist on the same method — the default `example` and
|
|
52
|
-
* any number of named `examples` will all surface in the generated
|
|
53
|
-
* Swagger document.
|
|
54
|
-
*
|
|
55
|
-
* @template T Type of the example value (typically the route's response DTO).
|
|
56
|
-
*/
|
|
57
|
-
export function Response<T>(value: T): MethodDecorator;
|
|
58
|
-
export function Response<T>(key: string, value: T): MethodDecorator;
|
|
59
|
-
export function Response(...args: any[]): MethodDecorator {
|
|
60
|
-
return function SwaggerExampleResponse(
|
|
61
|
-
_target: Object,
|
|
62
|
-
_propertyKey: string | symbol,
|
|
63
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
64
|
-
): TypedPropertyDescriptor<any> {
|
|
65
|
-
emplaceValue(emplaceOfResponse(descriptor))(args);
|
|
66
|
-
return descriptor;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Attach an example value to a request parameter (body, path, query, etc.)
|
|
72
|
-
* of a controller method.
|
|
73
|
-
*
|
|
74
|
-
* Two forms are supported:
|
|
75
|
-
*
|
|
76
|
-
* - {@link Parameter | `Parameter(value)`}: registers `value` as the single
|
|
77
|
-
* default `example` for that parameter.
|
|
78
|
-
* - {@link Parameter | `Parameter(key, value)`}: registers `value` under
|
|
79
|
-
* `examples[key]`. May be applied multiple times to attach several
|
|
80
|
-
* named examples to the same parameter.
|
|
81
|
-
*
|
|
82
|
-
* Apply alongside the actual parameter decorator
|
|
83
|
-
* (`@TypedBody()`, `@TypedParam()`, `@TypedQuery()`, etc.); decorator
|
|
84
|
-
* order does not matter.
|
|
85
|
-
*
|
|
86
|
-
* @template T Type of the example value (typically the parameter's DTO).
|
|
87
|
-
*/
|
|
88
|
-
export function Parameter<T>(value: T): ParameterDecorator;
|
|
89
|
-
export function Parameter<T>(key: string, value: T): ParameterDecorator;
|
|
90
|
-
export function Parameter(...args: any[]): ParameterDecorator {
|
|
91
|
-
return function SwaggerExampleParameter(
|
|
92
|
-
target: Object,
|
|
93
|
-
propertyKey: string | symbol | undefined,
|
|
94
|
-
index: number,
|
|
95
|
-
): void {
|
|
96
|
-
emplaceValue(emplaceOfParameter(target, propertyKey ?? "", index))(args);
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
/**
|
|
101
|
-
* Internal storage shape for `SwaggerExample` metadata.
|
|
102
|
-
*
|
|
103
|
-
* Reflects the OpenAPI spec's two example fields: a single default
|
|
104
|
-
* `example`, and/or a named map `examples`. `index` is used internally
|
|
105
|
-
* to associate parameter examples with the right parameter position.
|
|
106
|
-
*/
|
|
107
|
-
export interface IData<T> {
|
|
108
|
-
examples?: Record<string, T>;
|
|
109
|
-
example?: T;
|
|
110
|
-
index?: number;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
const emplaceValue =
|
|
115
|
-
<T>(data: SwaggerExample.IData<T>) =>
|
|
116
|
-
(args: any[]) => {
|
|
117
|
-
if (args.length === 1) data.example = args[0];
|
|
118
|
-
else {
|
|
119
|
-
const key: string = args[0];
|
|
120
|
-
const value: T = args[1];
|
|
121
|
-
data.examples ??= {};
|
|
122
|
-
data.examples[key] = value;
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
const emplaceOfResponse = <T>(
|
|
127
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
128
|
-
): SwaggerExample.IData<T> => {
|
|
129
|
-
const oldbie: SwaggerExample.IData<T> | undefined = Reflect.getMetadata(
|
|
130
|
-
"nestia/SwaggerExample/Response",
|
|
131
|
-
descriptor.value,
|
|
132
|
-
);
|
|
133
|
-
if (oldbie !== undefined) return oldbie;
|
|
134
|
-
const newbie: SwaggerExample.IData<T> = {};
|
|
135
|
-
Reflect.defineMetadata(
|
|
136
|
-
"nestia/SwaggerExample/Response",
|
|
137
|
-
newbie,
|
|
138
|
-
descriptor.value,
|
|
139
|
-
);
|
|
140
|
-
return newbie;
|
|
141
|
-
};
|
|
142
|
-
|
|
143
|
-
const emplaceOfParameter = (
|
|
144
|
-
target: Object,
|
|
145
|
-
propertyKey: string | symbol,
|
|
146
|
-
index: number,
|
|
147
|
-
): SwaggerExample.IData<any> => {
|
|
148
|
-
const array: SwaggerExample.IData<any>[] = emplaceArrayOfParameters(
|
|
149
|
-
target,
|
|
150
|
-
propertyKey,
|
|
151
|
-
);
|
|
152
|
-
const oldibe: SwaggerExample.IData<any> | undefined = array.find(
|
|
153
|
-
(e) => e.index === index,
|
|
154
|
-
);
|
|
155
|
-
if (oldibe !== undefined) return oldibe;
|
|
156
|
-
|
|
157
|
-
const data: SwaggerExample.IData<any> = { index };
|
|
158
|
-
array.push(data);
|
|
159
|
-
return data;
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
const emplaceArrayOfParameters = (
|
|
163
|
-
target: Object,
|
|
164
|
-
propertyKey: string | symbol,
|
|
165
|
-
): SwaggerExample.IData<any>[] => {
|
|
166
|
-
const array: SwaggerExample.IData<any>[] | undefined = Reflect.getMetadata(
|
|
167
|
-
"nestia/SwaggerExample/Parameters",
|
|
168
|
-
target,
|
|
169
|
-
propertyKey,
|
|
170
|
-
);
|
|
171
|
-
if (array !== undefined) return array;
|
|
172
|
-
const newbie: SwaggerExample.IData<any>[] = [];
|
|
173
|
-
Reflect.defineMetadata(
|
|
174
|
-
"nestia/SwaggerExample/Parameters",
|
|
175
|
-
newbie,
|
|
176
|
-
target,
|
|
177
|
-
propertyKey,
|
|
178
|
-
);
|
|
179
|
-
return newbie;
|
|
180
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* Attach example values to Swagger documents.
|
|
3
|
+
*
|
|
4
|
+
* `SwaggerExample` is a namespace of decorators that attach example values
|
|
5
|
+
* to controller methods (request/response bodies, parameters), so that
|
|
6
|
+
* `@nestia/sdk`'s Swagger generator can populate the `example` / `examples`
|
|
7
|
+
* fields of the generated OpenAPI document.
|
|
8
|
+
*
|
|
9
|
+
* The decorators only affect Swagger document generation. They do not change
|
|
10
|
+
* runtime behavior, validation, or SDK function signatures.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```typescript
|
|
15
|
+
* import core from "@nestia/core";
|
|
16
|
+
* import { Controller } from "@nestjs/common";
|
|
17
|
+
* import typia from "typia";
|
|
18
|
+
*
|
|
19
|
+
* @Controller("bbs/articles")
|
|
20
|
+
* export class BbsArticlesController {
|
|
21
|
+
* // Single response example.
|
|
22
|
+
* @core.SwaggerExample.Response(typia.random<IBbsArticle>())
|
|
23
|
+
* @core.TypedRoute.Post()
|
|
24
|
+
* public async create(
|
|
25
|
+
* // Multiple named parameter examples plus a default one.
|
|
26
|
+
* @core.SwaggerExample.Parameter(typia.random<IBbsArticle.ICreate>())
|
|
27
|
+
* @core.SwaggerExample.Parameter("x", typia.random<IBbsArticle.ICreate>())
|
|
28
|
+
* @core.SwaggerExample.Parameter("y", typia.random<IBbsArticle.ICreate>())
|
|
29
|
+
* @core.TypedBody()
|
|
30
|
+
* input: IBbsArticle.ICreate,
|
|
31
|
+
* ): Promise<IBbsArticle> {
|
|
32
|
+
* // ...
|
|
33
|
+
* }
|
|
34
|
+
* }
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
38
|
+
*/
|
|
39
|
+
export namespace SwaggerExample {
|
|
40
|
+
/**
|
|
41
|
+
* Attach an example value to the response body of a controller method.
|
|
42
|
+
*
|
|
43
|
+
* Two forms are supported:
|
|
44
|
+
*
|
|
45
|
+
* - {@link Response | `Response(value)`}: registers `value` as the single
|
|
46
|
+
* default `example`.
|
|
47
|
+
* - {@link Response | `Response(key, value)`}: registers `value` under
|
|
48
|
+
* `examples[key]`. May be applied multiple times to attach several
|
|
49
|
+
* named examples.
|
|
50
|
+
*
|
|
51
|
+
* Both forms can coexist on the same method — the default `example` and
|
|
52
|
+
* any number of named `examples` will all surface in the generated
|
|
53
|
+
* Swagger document.
|
|
54
|
+
*
|
|
55
|
+
* @template T Type of the example value (typically the route's response DTO).
|
|
56
|
+
*/
|
|
57
|
+
export function Response<T>(value: T): MethodDecorator;
|
|
58
|
+
export function Response<T>(key: string, value: T): MethodDecorator;
|
|
59
|
+
export function Response(...args: any[]): MethodDecorator {
|
|
60
|
+
return function SwaggerExampleResponse(
|
|
61
|
+
_target: Object,
|
|
62
|
+
_propertyKey: string | symbol,
|
|
63
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
64
|
+
): TypedPropertyDescriptor<any> {
|
|
65
|
+
emplaceValue(emplaceOfResponse(descriptor))(args);
|
|
66
|
+
return descriptor;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Attach an example value to a request parameter (body, path, query, etc.)
|
|
72
|
+
* of a controller method.
|
|
73
|
+
*
|
|
74
|
+
* Two forms are supported:
|
|
75
|
+
*
|
|
76
|
+
* - {@link Parameter | `Parameter(value)`}: registers `value` as the single
|
|
77
|
+
* default `example` for that parameter.
|
|
78
|
+
* - {@link Parameter | `Parameter(key, value)`}: registers `value` under
|
|
79
|
+
* `examples[key]`. May be applied multiple times to attach several
|
|
80
|
+
* named examples to the same parameter.
|
|
81
|
+
*
|
|
82
|
+
* Apply alongside the actual parameter decorator
|
|
83
|
+
* (`@TypedBody()`, `@TypedParam()`, `@TypedQuery()`, etc.); decorator
|
|
84
|
+
* order does not matter.
|
|
85
|
+
*
|
|
86
|
+
* @template T Type of the example value (typically the parameter's DTO).
|
|
87
|
+
*/
|
|
88
|
+
export function Parameter<T>(value: T): ParameterDecorator;
|
|
89
|
+
export function Parameter<T>(key: string, value: T): ParameterDecorator;
|
|
90
|
+
export function Parameter(...args: any[]): ParameterDecorator {
|
|
91
|
+
return function SwaggerExampleParameter(
|
|
92
|
+
target: Object,
|
|
93
|
+
propertyKey: string | symbol | undefined,
|
|
94
|
+
index: number,
|
|
95
|
+
): void {
|
|
96
|
+
emplaceValue(emplaceOfParameter(target, propertyKey ?? "", index))(args);
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Internal storage shape for `SwaggerExample` metadata.
|
|
102
|
+
*
|
|
103
|
+
* Reflects the OpenAPI spec's two example fields: a single default
|
|
104
|
+
* `example`, and/or a named map `examples`. `index` is used internally
|
|
105
|
+
* to associate parameter examples with the right parameter position.
|
|
106
|
+
*/
|
|
107
|
+
export interface IData<T> {
|
|
108
|
+
examples?: Record<string, T>;
|
|
109
|
+
example?: T;
|
|
110
|
+
index?: number;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const emplaceValue =
|
|
115
|
+
<T>(data: SwaggerExample.IData<T>) =>
|
|
116
|
+
(args: any[]) => {
|
|
117
|
+
if (args.length === 1) data.example = args[0];
|
|
118
|
+
else {
|
|
119
|
+
const key: string = args[0];
|
|
120
|
+
const value: T = args[1];
|
|
121
|
+
data.examples ??= {};
|
|
122
|
+
data.examples[key] = value;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const emplaceOfResponse = <T>(
|
|
127
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
128
|
+
): SwaggerExample.IData<T> => {
|
|
129
|
+
const oldbie: SwaggerExample.IData<T> | undefined = Reflect.getMetadata(
|
|
130
|
+
"nestia/SwaggerExample/Response",
|
|
131
|
+
descriptor.value,
|
|
132
|
+
);
|
|
133
|
+
if (oldbie !== undefined) return oldbie;
|
|
134
|
+
const newbie: SwaggerExample.IData<T> = {};
|
|
135
|
+
Reflect.defineMetadata(
|
|
136
|
+
"nestia/SwaggerExample/Response",
|
|
137
|
+
newbie,
|
|
138
|
+
descriptor.value,
|
|
139
|
+
);
|
|
140
|
+
return newbie;
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const emplaceOfParameter = (
|
|
144
|
+
target: Object,
|
|
145
|
+
propertyKey: string | symbol,
|
|
146
|
+
index: number,
|
|
147
|
+
): SwaggerExample.IData<any> => {
|
|
148
|
+
const array: SwaggerExample.IData<any>[] = emplaceArrayOfParameters(
|
|
149
|
+
target,
|
|
150
|
+
propertyKey,
|
|
151
|
+
);
|
|
152
|
+
const oldibe: SwaggerExample.IData<any> | undefined = array.find(
|
|
153
|
+
(e) => e.index === index,
|
|
154
|
+
);
|
|
155
|
+
if (oldibe !== undefined) return oldibe;
|
|
156
|
+
|
|
157
|
+
const data: SwaggerExample.IData<any> = { index };
|
|
158
|
+
array.push(data);
|
|
159
|
+
return data;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const emplaceArrayOfParameters = (
|
|
163
|
+
target: Object,
|
|
164
|
+
propertyKey: string | symbol,
|
|
165
|
+
): SwaggerExample.IData<any>[] => {
|
|
166
|
+
const array: SwaggerExample.IData<any>[] | undefined = Reflect.getMetadata(
|
|
167
|
+
"nestia/SwaggerExample/Parameters",
|
|
168
|
+
target,
|
|
169
|
+
propertyKey,
|
|
170
|
+
);
|
|
171
|
+
if (array !== undefined) return array;
|
|
172
|
+
const newbie: SwaggerExample.IData<any>[] = [];
|
|
173
|
+
Reflect.defineMetadata(
|
|
174
|
+
"nestia/SwaggerExample/Parameters",
|
|
175
|
+
newbie,
|
|
176
|
+
target,
|
|
177
|
+
propertyKey,
|
|
178
|
+
);
|
|
179
|
+
return newbie;
|
|
180
|
+
};
|
|
@@ -1,57 +1,57 @@
|
|
|
1
|
-
import {
|
|
2
|
-
BadRequestException,
|
|
3
|
-
ExecutionContext,
|
|
4
|
-
createParamDecorator,
|
|
5
|
-
} from "@nestjs/common";
|
|
6
|
-
import type express from "express";
|
|
7
|
-
import type { FastifyRequest } from "fastify";
|
|
8
|
-
|
|
9
|
-
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
10
|
-
import { is_request_body_undefined } from "./internal/is_request_body_undefined";
|
|
11
|
-
import { validate_request_body } from "./internal/validate_request_body";
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Type safe body decorator.
|
|
15
|
-
*
|
|
16
|
-
* `TypedBody` is a decorator function getting `application/json` typed data
|
|
17
|
-
* from request body. Also, it validates the request body data type through
|
|
18
|
-
* [typia](https://github.com/samchon/typia) and the validation speed is maximum
|
|
19
|
-
* 20,000x times faster than `class-validator`.
|
|
20
|
-
*
|
|
21
|
-
* For reference, when the request body data is not following the promised type
|
|
22
|
-
* `T`, `BadRequestException` error (status code: 400) would be thrown.
|
|
23
|
-
*
|
|
24
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
25
|
-
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
26
|
-
*/
|
|
27
|
-
export function TypedBody<T>(
|
|
28
|
-
validator?: IRequestBodyValidator<T>,
|
|
29
|
-
): ParameterDecorator {
|
|
30
|
-
const checker = validate_request_body("TypedBody")(validator);
|
|
31
|
-
return createParamDecorator(function TypedBody(
|
|
32
|
-
_unknown: any,
|
|
33
|
-
context: ExecutionContext,
|
|
34
|
-
) {
|
|
35
|
-
const request: express.Request | FastifyRequest = context
|
|
36
|
-
.switchToHttp()
|
|
37
|
-
.getRequest();
|
|
38
|
-
if (is_request_body_undefined(request) && checker(undefined as T) === null)
|
|
39
|
-
return undefined;
|
|
40
|
-
else if (isApplicationJson(request.headers["content-type"]) === false)
|
|
41
|
-
throw new BadRequestException(
|
|
42
|
-
`Request body type is not "application/json".`,
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const error: Error | null = checker(request.body);
|
|
46
|
-
if (error !== null) throw error;
|
|
47
|
-
return request.body;
|
|
48
|
-
})();
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** @internal */
|
|
52
|
-
const isApplicationJson = (text?: string): boolean =>
|
|
53
|
-
text !== undefined &&
|
|
54
|
-
text
|
|
55
|
-
.split(";")
|
|
56
|
-
.map((str) => str.trim())
|
|
57
|
-
.some((str) => str === "application/json");
|
|
1
|
+
import {
|
|
2
|
+
BadRequestException,
|
|
3
|
+
ExecutionContext,
|
|
4
|
+
createParamDecorator,
|
|
5
|
+
} from "@nestjs/common";
|
|
6
|
+
import type express from "express";
|
|
7
|
+
import type { FastifyRequest } from "fastify";
|
|
8
|
+
|
|
9
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
10
|
+
import { is_request_body_undefined } from "./internal/is_request_body_undefined";
|
|
11
|
+
import { validate_request_body } from "./internal/validate_request_body";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Type safe body decorator.
|
|
15
|
+
*
|
|
16
|
+
* `TypedBody` is a decorator function getting `application/json` typed data
|
|
17
|
+
* from request body. Also, it validates the request body data type through
|
|
18
|
+
* [typia](https://github.com/samchon/typia) and the validation speed is maximum
|
|
19
|
+
* 20,000x times faster than `class-validator`.
|
|
20
|
+
*
|
|
21
|
+
* For reference, when the request body data is not following the promised type
|
|
22
|
+
* `T`, `BadRequestException` error (status code: 400) would be thrown.
|
|
23
|
+
*
|
|
24
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
25
|
+
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
26
|
+
*/
|
|
27
|
+
export function TypedBody<T>(
|
|
28
|
+
validator?: IRequestBodyValidator<T>,
|
|
29
|
+
): ParameterDecorator {
|
|
30
|
+
const checker = validate_request_body("TypedBody")(validator);
|
|
31
|
+
return createParamDecorator(function TypedBody(
|
|
32
|
+
_unknown: any,
|
|
33
|
+
context: ExecutionContext,
|
|
34
|
+
) {
|
|
35
|
+
const request: express.Request | FastifyRequest = context
|
|
36
|
+
.switchToHttp()
|
|
37
|
+
.getRequest();
|
|
38
|
+
if (is_request_body_undefined(request) && checker(undefined as T) === null)
|
|
39
|
+
return undefined;
|
|
40
|
+
else if (isApplicationJson(request.headers["content-type"]) === false)
|
|
41
|
+
throw new BadRequestException(
|
|
42
|
+
`Request body type is not "application/json".`,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const error: Error | null = checker(request.body);
|
|
46
|
+
if (error !== null) throw error;
|
|
47
|
+
return request.body;
|
|
48
|
+
})();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** @internal */
|
|
52
|
+
const isApplicationJson = (text?: string): boolean =>
|
|
53
|
+
text !== undefined &&
|
|
54
|
+
text
|
|
55
|
+
.split(";")
|
|
56
|
+
.map((str) => str.trim())
|
|
57
|
+
.some((str) => str === "application/json");
|