@nestia/core 8.0.8 → 9.0.0-dev.20251107
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/README.md +93 -93
- package/package.json +7 -8
- 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 +22 -22
- package/src/decorators/NoTransformConfigurationError.ts +34 -34
- package/src/decorators/PlainBody.ts +76 -76
- package/src/decorators/SwaggerCustomizer.ts +97 -97
- package/src/decorators/SwaggerExample.ts +100 -100
- package/src/decorators/TypedBody.ts +57 -57
- package/src/decorators/TypedException.ts +147 -147
- package/src/decorators/TypedFormData.ts +195 -195
- 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 +196 -196
- package/src/decorators/WebSocketRoute.ts +242 -242
- package/src/decorators/internal/EncryptedConstant.ts +2 -2
- package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
- package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
- 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 +45 -45
- 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 +64 -64
- package/src/index.ts +5 -5
- package/src/module.ts +22 -22
- package/src/options/INestiaTransformOptions.ts +38 -38
- package/src/options/INestiaTransformProject.ts +8 -8
- 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/programmers/PlainBodyProgrammer.ts +70 -70
- package/src/programmers/TypedBodyProgrammer.ts +142 -142
- package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
- package/src/programmers/TypedHeadersProgrammer.ts +63 -63
- package/src/programmers/TypedParamProgrammer.ts +33 -33
- package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
- package/src/programmers/TypedQueryProgrammer.ts +114 -114
- package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
- package/src/programmers/TypedRouteProgrammer.ts +94 -94
- package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
- package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
- package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
- package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
- package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
- package/src/transform.ts +35 -35
- package/src/transformers/FileTransformer.ts +110 -110
- package/src/transformers/MethodTransformer.ts +103 -103
- package/src/transformers/NodeTransformer.ts +23 -23
- package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
- package/src/transformers/ParameterTransformer.ts +57 -57
- package/src/transformers/TypedRouteTransformer.ts +85 -85
- package/src/transformers/WebSocketRouteTransformer.ts +120 -120
- 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
|
@@ -1,76 +1,76 @@
|
|
|
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 { get_text_body } from "./internal/get_text_body";
|
|
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
|
-
* Plain body decorator.
|
|
15
|
-
*
|
|
16
|
-
* `PlainBody` is a decorator function getting full body text from the HTTP
|
|
17
|
-
* request.
|
|
18
|
-
*
|
|
19
|
-
* If you adjust the regular {@link Body} decorator function to the body
|
|
20
|
-
* parameter, you can't get the full body text because the {@link Body} tries to
|
|
21
|
-
* convert the body text to JSON object. Therefore, `@nestia/core` provides this
|
|
22
|
-
* `PlainBody` decorator function to get the full body text.
|
|
23
|
-
*
|
|
24
|
-
* ```typescript
|
|
25
|
-
* \@TypedRoute.Post("memo")
|
|
26
|
-
* public store
|
|
27
|
-
* (
|
|
28
|
-
* \@PlainBody() body: string
|
|
29
|
-
* ): void;
|
|
30
|
-
* ```
|
|
31
|
-
*
|
|
32
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
33
|
-
* @returns Parameter decorator
|
|
34
|
-
*/
|
|
35
|
-
export function PlainBody(): ParameterDecorator;
|
|
36
|
-
|
|
37
|
-
/** @internal */
|
|
38
|
-
export function PlainBody(
|
|
39
|
-
assert?: (input: unknown) => string,
|
|
40
|
-
): ParameterDecorator {
|
|
41
|
-
const checker = assert
|
|
42
|
-
? validate_request_body("PlainBody")({
|
|
43
|
-
type: "assert",
|
|
44
|
-
assert,
|
|
45
|
-
})
|
|
46
|
-
: null;
|
|
47
|
-
return createParamDecorator(async function PlainBody(
|
|
48
|
-
_data: any,
|
|
49
|
-
context: ExecutionContext,
|
|
50
|
-
) {
|
|
51
|
-
const request: express.Request | FastifyRequest = context
|
|
52
|
-
.switchToHttp()
|
|
53
|
-
.getRequest();
|
|
54
|
-
if (
|
|
55
|
-
is_request_body_undefined(request) &&
|
|
56
|
-
(checker ?? (() => null))(undefined as any) === null
|
|
57
|
-
)
|
|
58
|
-
return undefined;
|
|
59
|
-
else if (!isTextPlain(request.headers["content-type"]))
|
|
60
|
-
throw new BadRequestException(`Request body type is not "text/plain".`);
|
|
61
|
-
const value: string = await get_text_body(request);
|
|
62
|
-
if (checker) {
|
|
63
|
-
const error: Error | null = checker(value);
|
|
64
|
-
if (error !== null) throw error;
|
|
65
|
-
}
|
|
66
|
-
return value;
|
|
67
|
-
})();
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/** @internal */
|
|
71
|
-
const isTextPlain = (text?: string): boolean =>
|
|
72
|
-
text !== undefined &&
|
|
73
|
-
text
|
|
74
|
-
.split(";")
|
|
75
|
-
.map((str) => str.trim())
|
|
76
|
-
.some((str) => str === "text/plain");
|
|
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 { get_text_body } from "./internal/get_text_body";
|
|
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
|
+
* Plain body decorator.
|
|
15
|
+
*
|
|
16
|
+
* `PlainBody` is a decorator function getting full body text from the HTTP
|
|
17
|
+
* request.
|
|
18
|
+
*
|
|
19
|
+
* If you adjust the regular {@link Body} decorator function to the body
|
|
20
|
+
* parameter, you can't get the full body text because the {@link Body} tries to
|
|
21
|
+
* convert the body text to JSON object. Therefore, `@nestia/core` provides this
|
|
22
|
+
* `PlainBody` decorator function to get the full body text.
|
|
23
|
+
*
|
|
24
|
+
* ```typescript
|
|
25
|
+
* \@TypedRoute.Post("memo")
|
|
26
|
+
* public store
|
|
27
|
+
* (
|
|
28
|
+
* \@PlainBody() body: string
|
|
29
|
+
* ): void;
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
33
|
+
* @returns Parameter decorator
|
|
34
|
+
*/
|
|
35
|
+
export function PlainBody(): ParameterDecorator;
|
|
36
|
+
|
|
37
|
+
/** @internal */
|
|
38
|
+
export function PlainBody(
|
|
39
|
+
assert?: (input: unknown) => string,
|
|
40
|
+
): ParameterDecorator {
|
|
41
|
+
const checker = assert
|
|
42
|
+
? validate_request_body("PlainBody")({
|
|
43
|
+
type: "assert",
|
|
44
|
+
assert,
|
|
45
|
+
})
|
|
46
|
+
: null;
|
|
47
|
+
return createParamDecorator(async function PlainBody(
|
|
48
|
+
_data: any,
|
|
49
|
+
context: ExecutionContext,
|
|
50
|
+
) {
|
|
51
|
+
const request: express.Request | FastifyRequest = context
|
|
52
|
+
.switchToHttp()
|
|
53
|
+
.getRequest();
|
|
54
|
+
if (
|
|
55
|
+
is_request_body_undefined(request) &&
|
|
56
|
+
(checker ?? (() => null))(undefined as any) === null
|
|
57
|
+
)
|
|
58
|
+
return undefined;
|
|
59
|
+
else if (!isTextPlain(request.headers["content-type"]))
|
|
60
|
+
throw new BadRequestException(`Request body type is not "text/plain".`);
|
|
61
|
+
const value: string = await get_text_body(request);
|
|
62
|
+
if (checker) {
|
|
63
|
+
const error: Error | null = checker(value);
|
|
64
|
+
if (error !== null) throw error;
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
})();
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @internal */
|
|
71
|
+
const isTextPlain = (text?: string): boolean =>
|
|
72
|
+
text !== undefined &&
|
|
73
|
+
text
|
|
74
|
+
.split(";")
|
|
75
|
+
.map((str) => str.trim())
|
|
76
|
+
.some((str) => str === "text/plain");
|
|
@@ -1,97 +1,97 @@
|
|
|
1
|
-
import { OpenApi } from "@samchon/openapi";
|
|
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 "@samchon/openapi";
|
|
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,100 +1,100 @@
|
|
|
1
|
-
export namespace SwaggerExample {
|
|
2
|
-
export function Response<T>(value: T): MethodDecorator;
|
|
3
|
-
export function Response<T>(key: string, value: T): MethodDecorator;
|
|
4
|
-
export function Response(...args: any[]): MethodDecorator {
|
|
5
|
-
return function SwaggerExampleResponse(
|
|
6
|
-
_target: Object,
|
|
7
|
-
_propertyKey: string | symbol,
|
|
8
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
9
|
-
): TypedPropertyDescriptor<any> {
|
|
10
|
-
emplaceValue(emplaceOfResponse(descriptor))(args);
|
|
11
|
-
return descriptor;
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export function Parameter<T>(value: T): ParameterDecorator;
|
|
16
|
-
export function Parameter<T>(key: string, value: T): ParameterDecorator;
|
|
17
|
-
export function Parameter(...args: any[]): ParameterDecorator {
|
|
18
|
-
return function SwaggerExampleParameter(
|
|
19
|
-
target: Object,
|
|
20
|
-
propertyKey: string | symbol | undefined,
|
|
21
|
-
index: number,
|
|
22
|
-
): void {
|
|
23
|
-
emplaceValue(emplaceOfParameter(target, propertyKey ?? "", index))(args);
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export interface IData<T> {
|
|
28
|
-
examples?: Record<string, T>;
|
|
29
|
-
example?: T;
|
|
30
|
-
index?: number;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const emplaceValue =
|
|
35
|
-
<T>(data: SwaggerExample.IData<T>) =>
|
|
36
|
-
(args: any[]) => {
|
|
37
|
-
if (args.length === 1) data.example = args[0];
|
|
38
|
-
else {
|
|
39
|
-
const key: string = args[0];
|
|
40
|
-
const value: T = args[1];
|
|
41
|
-
data.examples ??= {};
|
|
42
|
-
data.examples[key] = value;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
const emplaceOfResponse = <T>(
|
|
47
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
48
|
-
): SwaggerExample.IData<T> => {
|
|
49
|
-
const oldbie: SwaggerExample.IData<T> | undefined = Reflect.getMetadata(
|
|
50
|
-
"nestia/SwaggerExample/Response",
|
|
51
|
-
descriptor.value,
|
|
52
|
-
);
|
|
53
|
-
if (oldbie !== undefined) return oldbie;
|
|
54
|
-
const newbie: SwaggerExample.IData<T> = {};
|
|
55
|
-
Reflect.defineMetadata(
|
|
56
|
-
"nestia/SwaggerExample/Response",
|
|
57
|
-
newbie,
|
|
58
|
-
descriptor.value,
|
|
59
|
-
);
|
|
60
|
-
return newbie;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const emplaceOfParameter = (
|
|
64
|
-
target: Object,
|
|
65
|
-
propertyKey: string | symbol,
|
|
66
|
-
index: number,
|
|
67
|
-
): SwaggerExample.IData<any> => {
|
|
68
|
-
const array: SwaggerExample.IData<any>[] = emplaceArrayOfParameters(
|
|
69
|
-
target,
|
|
70
|
-
propertyKey,
|
|
71
|
-
);
|
|
72
|
-
const oldibe: SwaggerExample.IData<any> | undefined = array.find(
|
|
73
|
-
(e) => e.index === index,
|
|
74
|
-
);
|
|
75
|
-
if (oldibe !== undefined) return oldibe;
|
|
76
|
-
|
|
77
|
-
const data: SwaggerExample.IData<any> = { index };
|
|
78
|
-
array.push(data);
|
|
79
|
-
return data;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const emplaceArrayOfParameters = (
|
|
83
|
-
target: Object,
|
|
84
|
-
propertyKey: string | symbol,
|
|
85
|
-
): SwaggerExample.IData<any>[] => {
|
|
86
|
-
const array: SwaggerExample.IData<any>[] | undefined = Reflect.getMetadata(
|
|
87
|
-
"nestia/SwaggerExample/Parameters",
|
|
88
|
-
target,
|
|
89
|
-
propertyKey,
|
|
90
|
-
);
|
|
91
|
-
if (array !== undefined) return array;
|
|
92
|
-
const newbie: SwaggerExample.IData<any>[] = [];
|
|
93
|
-
Reflect.defineMetadata(
|
|
94
|
-
"nestia/SwaggerExample/Parameters",
|
|
95
|
-
newbie,
|
|
96
|
-
target,
|
|
97
|
-
propertyKey,
|
|
98
|
-
);
|
|
99
|
-
return newbie;
|
|
100
|
-
};
|
|
1
|
+
export namespace SwaggerExample {
|
|
2
|
+
export function Response<T>(value: T): MethodDecorator;
|
|
3
|
+
export function Response<T>(key: string, value: T): MethodDecorator;
|
|
4
|
+
export function Response(...args: any[]): MethodDecorator {
|
|
5
|
+
return function SwaggerExampleResponse(
|
|
6
|
+
_target: Object,
|
|
7
|
+
_propertyKey: string | symbol,
|
|
8
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
9
|
+
): TypedPropertyDescriptor<any> {
|
|
10
|
+
emplaceValue(emplaceOfResponse(descriptor))(args);
|
|
11
|
+
return descriptor;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function Parameter<T>(value: T): ParameterDecorator;
|
|
16
|
+
export function Parameter<T>(key: string, value: T): ParameterDecorator;
|
|
17
|
+
export function Parameter(...args: any[]): ParameterDecorator {
|
|
18
|
+
return function SwaggerExampleParameter(
|
|
19
|
+
target: Object,
|
|
20
|
+
propertyKey: string | symbol | undefined,
|
|
21
|
+
index: number,
|
|
22
|
+
): void {
|
|
23
|
+
emplaceValue(emplaceOfParameter(target, propertyKey ?? "", index))(args);
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface IData<T> {
|
|
28
|
+
examples?: Record<string, T>;
|
|
29
|
+
example?: T;
|
|
30
|
+
index?: number;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const emplaceValue =
|
|
35
|
+
<T>(data: SwaggerExample.IData<T>) =>
|
|
36
|
+
(args: any[]) => {
|
|
37
|
+
if (args.length === 1) data.example = args[0];
|
|
38
|
+
else {
|
|
39
|
+
const key: string = args[0];
|
|
40
|
+
const value: T = args[1];
|
|
41
|
+
data.examples ??= {};
|
|
42
|
+
data.examples[key] = value;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const emplaceOfResponse = <T>(
|
|
47
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
48
|
+
): SwaggerExample.IData<T> => {
|
|
49
|
+
const oldbie: SwaggerExample.IData<T> | undefined = Reflect.getMetadata(
|
|
50
|
+
"nestia/SwaggerExample/Response",
|
|
51
|
+
descriptor.value,
|
|
52
|
+
);
|
|
53
|
+
if (oldbie !== undefined) return oldbie;
|
|
54
|
+
const newbie: SwaggerExample.IData<T> = {};
|
|
55
|
+
Reflect.defineMetadata(
|
|
56
|
+
"nestia/SwaggerExample/Response",
|
|
57
|
+
newbie,
|
|
58
|
+
descriptor.value,
|
|
59
|
+
);
|
|
60
|
+
return newbie;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
const emplaceOfParameter = (
|
|
64
|
+
target: Object,
|
|
65
|
+
propertyKey: string | symbol,
|
|
66
|
+
index: number,
|
|
67
|
+
): SwaggerExample.IData<any> => {
|
|
68
|
+
const array: SwaggerExample.IData<any>[] = emplaceArrayOfParameters(
|
|
69
|
+
target,
|
|
70
|
+
propertyKey,
|
|
71
|
+
);
|
|
72
|
+
const oldibe: SwaggerExample.IData<any> | undefined = array.find(
|
|
73
|
+
(e) => e.index === index,
|
|
74
|
+
);
|
|
75
|
+
if (oldibe !== undefined) return oldibe;
|
|
76
|
+
|
|
77
|
+
const data: SwaggerExample.IData<any> = { index };
|
|
78
|
+
array.push(data);
|
|
79
|
+
return data;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const emplaceArrayOfParameters = (
|
|
83
|
+
target: Object,
|
|
84
|
+
propertyKey: string | symbol,
|
|
85
|
+
): SwaggerExample.IData<any>[] => {
|
|
86
|
+
const array: SwaggerExample.IData<any>[] | undefined = Reflect.getMetadata(
|
|
87
|
+
"nestia/SwaggerExample/Parameters",
|
|
88
|
+
target,
|
|
89
|
+
propertyKey,
|
|
90
|
+
);
|
|
91
|
+
if (array !== undefined) return array;
|
|
92
|
+
const newbie: SwaggerExample.IData<any>[] = [];
|
|
93
|
+
Reflect.defineMetadata(
|
|
94
|
+
"nestia/SwaggerExample/Parameters",
|
|
95
|
+
newbie,
|
|
96
|
+
target,
|
|
97
|
+
propertyKey,
|
|
98
|
+
);
|
|
99
|
+
return newbie;
|
|
100
|
+
};
|
|
@@ -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");
|