@nestia/core 2.5.8 → 2.5.9
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 +9 -6
- package/lib/decorators/SwaggerCustomizer.d.ts +64 -0
- package/lib/decorators/SwaggerCustomizer.js +25 -0
- package/lib/decorators/SwaggerCustomizer.js.map +1 -0
- package/lib/module.d.ts +1 -0
- package/lib/module.js +1 -0
- package/lib/module.js.map +1 -1
- package/lib/structures/ISwagger.d.ts +72 -0
- package/lib/structures/ISwagger.js +3 -0
- package/lib/structures/ISwagger.js.map +1 -0
- package/lib/structures/ISwaggerComponents.d.ts +26 -0
- package/lib/structures/ISwaggerComponents.js +3 -0
- package/lib/structures/ISwaggerComponents.js.map +1 -0
- package/lib/structures/ISwaggerInfo.d.ts +71 -0
- package/lib/structures/ISwaggerInfo.js +3 -0
- package/lib/structures/ISwaggerInfo.js.map +1 -0
- package/lib/structures/ISwaggerRoute.d.ts +46 -0
- package/lib/structures/ISwaggerRoute.js +3 -0
- package/lib/structures/ISwaggerRoute.js.map +1 -0
- package/lib/structures/ISwaggerSecurityScheme.d.ts +56 -0
- package/lib/structures/ISwaggerSecurityScheme.js +3 -0
- package/lib/structures/ISwaggerSecurityScheme.js.map +1 -0
- package/package.json +3 -3
- package/src/decorators/EncryptedBody.ts +105 -105
- package/src/decorators/EncryptedModule.ts +96 -96
- package/src/decorators/PlainBody.ts +75 -75
- package/src/decorators/SwaggerCustomizer.ts +87 -0
- package/src/decorators/TypedBody.ts +62 -62
- package/src/decorators/TypedException.ts +90 -90
- package/src/decorators/TypedRoute.ts +144 -144
- package/src/decorators/internal/get_path_and_querify.ts +106 -106
- package/src/decorators/internal/load_controller.ts +51 -51
- package/src/decorators/internal/validate_request_body.ts +72 -72
- package/src/decorators/internal/validate_request_headers.ts +83 -83
- package/src/decorators/internal/validate_request_query.ts +71 -71
- package/src/module.ts +1 -0
- package/src/structures/ISwagger.ts +91 -0
- package/src/structures/ISwaggerComponents.ts +29 -0
- package/src/structures/ISwaggerInfo.ts +80 -0
- package/src/structures/ISwaggerRoute.ts +50 -0
- package/src/structures/ISwaggerSecurityScheme.ts +65 -0
- package/src/transformers/NodeTransformer.ts +16 -16
- package/src/transformers/TypedExceptionTransformer.ts +48 -48
- package/src/transformers/TypedRouteTransformer.ts +88 -88
- package/src/utils/Singleton.ts +20 -20
|
@@ -1,62 +1,62 @@
|
|
|
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
|
-
import { assert, is, misc, validate } from "typia";
|
|
9
|
-
|
|
10
|
-
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
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 from
|
|
17
|
-
* request body. Also, it validates the request body data type through
|
|
18
|
-
* [typia](https://github.com/samchon/typia) and the validation speed is
|
|
19
|
-
* maximum 20,000x times faster than `class-validator`.
|
|
20
|
-
*
|
|
21
|
-
* For reference, when the request body data is not following the promised type `T`,
|
|
22
|
-
* `BadRequestException` error (status code: 400) would be thrown.
|
|
23
|
-
*
|
|
24
|
-
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
25
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
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 (isApplicationJson(request.headers["content-type"]) === false)
|
|
39
|
-
throw new BadRequestException(
|
|
40
|
-
`Request body type is not "application/json".`,
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
const error: Error | null = checker(request.body);
|
|
44
|
-
if (error !== null) throw error;
|
|
45
|
-
return request.body;
|
|
46
|
-
})();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
Object.assign(TypedBody, misc.clone);
|
|
50
|
-
Object.assign(TypedBody, is);
|
|
51
|
-
Object.assign(TypedBody, assert);
|
|
52
|
-
Object.assign(TypedBody, validate);
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* @internal
|
|
56
|
-
*/
|
|
57
|
-
const isApplicationJson = (text?: string): boolean =>
|
|
58
|
-
text !== undefined &&
|
|
59
|
-
text
|
|
60
|
-
.split(";")
|
|
61
|
-
.map((str) => str.trim())
|
|
62
|
-
.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
|
+
import { assert, is, misc, validate } from "typia";
|
|
9
|
+
|
|
10
|
+
import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
|
|
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 from
|
|
17
|
+
* request body. Also, it validates the request body data type through
|
|
18
|
+
* [typia](https://github.com/samchon/typia) and the validation speed is
|
|
19
|
+
* maximum 20,000x times faster than `class-validator`.
|
|
20
|
+
*
|
|
21
|
+
* For reference, when the request body data is not following the promised type `T`,
|
|
22
|
+
* `BadRequestException` error (status code: 400) would be thrown.
|
|
23
|
+
*
|
|
24
|
+
* @param validator Custom validator if required. Default is `typia.assert()`
|
|
25
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
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 (isApplicationJson(request.headers["content-type"]) === false)
|
|
39
|
+
throw new BadRequestException(
|
|
40
|
+
`Request body type is not "application/json".`,
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const error: Error | null = checker(request.body);
|
|
44
|
+
if (error !== null) throw error;
|
|
45
|
+
return request.body;
|
|
46
|
+
})();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
Object.assign(TypedBody, misc.clone);
|
|
50
|
+
Object.assign(TypedBody, is);
|
|
51
|
+
Object.assign(TypedBody, assert);
|
|
52
|
+
Object.assign(TypedBody, validate);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @internal
|
|
56
|
+
*/
|
|
57
|
+
const isApplicationJson = (text?: string): boolean =>
|
|
58
|
+
text !== undefined &&
|
|
59
|
+
text
|
|
60
|
+
.split(";")
|
|
61
|
+
.map((str) => str.trim())
|
|
62
|
+
.some((str) => str === "application/json");
|
|
@@ -1,90 +1,90 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* > You must configure the generic argument `T`
|
|
3
|
-
*
|
|
4
|
-
* Exception decorator.
|
|
5
|
-
*
|
|
6
|
-
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
7
|
-
* which could be occured in the method.
|
|
8
|
-
*
|
|
9
|
-
* For reference, this decorator function does not affect to the method's behavior,
|
|
10
|
-
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
11
|
-
* the SDK library generation yet, but will be used in the future.
|
|
12
|
-
*
|
|
13
|
-
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
14
|
-
* @param description Description about the exception
|
|
15
|
-
* @returns Method decorator
|
|
16
|
-
*
|
|
17
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
18
|
-
*/
|
|
19
|
-
export function TypedException(
|
|
20
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
21
|
-
description?: string | undefined,
|
|
22
|
-
): never;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Exception decorator.
|
|
26
|
-
*
|
|
27
|
-
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
28
|
-
* which could be occured in the method.
|
|
29
|
-
*
|
|
30
|
-
* For reference, this decorator function does not affect to the method's behavior,
|
|
31
|
-
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
32
|
-
* the SDK library generation yet, but will be used in the future.
|
|
33
|
-
*
|
|
34
|
-
* @template T Type of the exception
|
|
35
|
-
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
36
|
-
* @param description Description about the exception
|
|
37
|
-
* @returns Method decorator
|
|
38
|
-
*
|
|
39
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
40
|
-
*/
|
|
41
|
-
export function TypedException<T>(
|
|
42
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
43
|
-
description?: string | undefined,
|
|
44
|
-
): MethodDecorator;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* @internal
|
|
48
|
-
*/
|
|
49
|
-
export function TypedException<T>(
|
|
50
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
51
|
-
description?: string | undefined,
|
|
52
|
-
type?: string | undefined,
|
|
53
|
-
): MethodDecorator {
|
|
54
|
-
return function TypedException(
|
|
55
|
-
target: Object | T,
|
|
56
|
-
propertyKey: string | symbol,
|
|
57
|
-
descriptor: TypedPropertyDescriptor<any>,
|
|
58
|
-
) {
|
|
59
|
-
const array: IProps[] = (() => {
|
|
60
|
-
const oldbie: IProps[] | undefined = Reflect.getMetadata(
|
|
61
|
-
`nestia/TypedException`,
|
|
62
|
-
(target as any)[propertyKey],
|
|
63
|
-
);
|
|
64
|
-
if (oldbie !== undefined) return oldbie;
|
|
65
|
-
|
|
66
|
-
const newbie: IProps[] = [];
|
|
67
|
-
Reflect.defineMetadata(
|
|
68
|
-
`nestia/TypedException`,
|
|
69
|
-
newbie,
|
|
70
|
-
(target as any)[propertyKey],
|
|
71
|
-
);
|
|
72
|
-
return newbie;
|
|
73
|
-
})();
|
|
74
|
-
array.push({
|
|
75
|
-
status,
|
|
76
|
-
description,
|
|
77
|
-
type: type!,
|
|
78
|
-
});
|
|
79
|
-
return descriptor;
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* @internal
|
|
85
|
-
*/
|
|
86
|
-
interface IProps {
|
|
87
|
-
status: number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
88
|
-
description?: string | undefined;
|
|
89
|
-
type: string;
|
|
90
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* > You must configure the generic argument `T`
|
|
3
|
+
*
|
|
4
|
+
* Exception decorator.
|
|
5
|
+
*
|
|
6
|
+
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
7
|
+
* which could be occured in the method.
|
|
8
|
+
*
|
|
9
|
+
* For reference, this decorator function does not affect to the method's behavior,
|
|
10
|
+
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
11
|
+
* the SDK library generation yet, but will be used in the future.
|
|
12
|
+
*
|
|
13
|
+
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
14
|
+
* @param description Description about the exception
|
|
15
|
+
* @returns Method decorator
|
|
16
|
+
*
|
|
17
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
18
|
+
*/
|
|
19
|
+
export function TypedException(
|
|
20
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
21
|
+
description?: string | undefined,
|
|
22
|
+
): never;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Exception decorator.
|
|
26
|
+
*
|
|
27
|
+
* `TypedException` is a decorator function describing HTTP exception and its type
|
|
28
|
+
* which could be occured in the method.
|
|
29
|
+
*
|
|
30
|
+
* For reference, this decorator function does not affect to the method's behavior,
|
|
31
|
+
* but only affects to the swagger documents generation. Also, it does not affect to
|
|
32
|
+
* the SDK library generation yet, but will be used in the future.
|
|
33
|
+
*
|
|
34
|
+
* @template T Type of the exception
|
|
35
|
+
* @param status Status number or pattern like "2XX", "3XX", "4XX", "5XX"
|
|
36
|
+
* @param description Description about the exception
|
|
37
|
+
* @returns Method decorator
|
|
38
|
+
*
|
|
39
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
40
|
+
*/
|
|
41
|
+
export function TypedException<T>(
|
|
42
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
43
|
+
description?: string | undefined,
|
|
44
|
+
): MethodDecorator;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @internal
|
|
48
|
+
*/
|
|
49
|
+
export function TypedException<T>(
|
|
50
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX",
|
|
51
|
+
description?: string | undefined,
|
|
52
|
+
type?: string | undefined,
|
|
53
|
+
): MethodDecorator {
|
|
54
|
+
return function TypedException(
|
|
55
|
+
target: Object | T,
|
|
56
|
+
propertyKey: string | symbol,
|
|
57
|
+
descriptor: TypedPropertyDescriptor<any>,
|
|
58
|
+
) {
|
|
59
|
+
const array: IProps[] = (() => {
|
|
60
|
+
const oldbie: IProps[] | undefined = Reflect.getMetadata(
|
|
61
|
+
`nestia/TypedException`,
|
|
62
|
+
(target as any)[propertyKey],
|
|
63
|
+
);
|
|
64
|
+
if (oldbie !== undefined) return oldbie;
|
|
65
|
+
|
|
66
|
+
const newbie: IProps[] = [];
|
|
67
|
+
Reflect.defineMetadata(
|
|
68
|
+
`nestia/TypedException`,
|
|
69
|
+
newbie,
|
|
70
|
+
(target as any)[propertyKey],
|
|
71
|
+
);
|
|
72
|
+
return newbie;
|
|
73
|
+
})();
|
|
74
|
+
array.push({
|
|
75
|
+
status,
|
|
76
|
+
description,
|
|
77
|
+
type: type!,
|
|
78
|
+
});
|
|
79
|
+
return descriptor;
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
interface IProps {
|
|
87
|
+
status: number | "2XX" | "3XX" | "4XX" | "5XX";
|
|
88
|
+
description?: string | undefined;
|
|
89
|
+
type: string;
|
|
90
|
+
}
|
|
@@ -1,144 +1,144 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CallHandler,
|
|
3
|
-
Delete,
|
|
4
|
-
ExecutionContext,
|
|
5
|
-
Get,
|
|
6
|
-
NestInterceptor,
|
|
7
|
-
Patch,
|
|
8
|
-
Post,
|
|
9
|
-
Put,
|
|
10
|
-
UseInterceptors,
|
|
11
|
-
applyDecorators,
|
|
12
|
-
} from "@nestjs/common";
|
|
13
|
-
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
14
|
-
import type express from "express";
|
|
15
|
-
import { catchError, map } from "rxjs/operators";
|
|
16
|
-
import typia from "typia";
|
|
17
|
-
|
|
18
|
-
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
19
|
-
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
20
|
-
import { route_error } from "./internal/route_error";
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Type safe router decorator functions.
|
|
24
|
-
*
|
|
25
|
-
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
26
|
-
* JSON string conversion speed about 200x times faster than `class-transformer`.
|
|
27
|
-
* Furthermore, such JSON string conversion is even type safe through
|
|
28
|
-
* [typia](https://github.com/samchon/typia).
|
|
29
|
-
*
|
|
30
|
-
* For reference, if you try to invalid data that is not following the promised
|
|
31
|
-
* type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
|
|
32
|
-
* JSON string through `typia.assertStringify<T>()` function, it is not possible to
|
|
33
|
-
* modify response data through interceptors.
|
|
34
|
-
*
|
|
35
|
-
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
-
*/
|
|
37
|
-
export namespace TypedRoute {
|
|
38
|
-
/**
|
|
39
|
-
* Router decorator function for the GET method.
|
|
40
|
-
*
|
|
41
|
-
* @param path Path of the HTTP request
|
|
42
|
-
* @returns Method decorator
|
|
43
|
-
*/
|
|
44
|
-
export const Get = Generator("Get");
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Router decorator function for the POST method.
|
|
48
|
-
*
|
|
49
|
-
* @param path Path of the HTTP request
|
|
50
|
-
* @returns Method decorator
|
|
51
|
-
*/
|
|
52
|
-
export const Post = Generator("Post");
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Router decorator function for the PATH method.
|
|
56
|
-
*
|
|
57
|
-
* @param path Path of the HTTP request
|
|
58
|
-
* @returns Method decorator
|
|
59
|
-
*/
|
|
60
|
-
export const Patch = Generator("Patch");
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Router decorator function for the PUT method.
|
|
64
|
-
*
|
|
65
|
-
* @param path Path of the HTTP request
|
|
66
|
-
* @returns Method decorator
|
|
67
|
-
*/
|
|
68
|
-
export const Put = Generator("Put");
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Router decorator function for the DELETE method.
|
|
72
|
-
*
|
|
73
|
-
* @param path Path of the HTTP request
|
|
74
|
-
* @returns Method decorator
|
|
75
|
-
*/
|
|
76
|
-
export const Delete = Generator("Delete");
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @internal
|
|
80
|
-
*/
|
|
81
|
-
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
82
|
-
function route(path?: string | string[]): MethodDecorator;
|
|
83
|
-
function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
|
|
84
|
-
function route<T>(
|
|
85
|
-
path: string | string[],
|
|
86
|
-
stringify?: IResponseBodyStringifier<T>,
|
|
87
|
-
): MethodDecorator;
|
|
88
|
-
|
|
89
|
-
function route(...args: any[]): MethodDecorator {
|
|
90
|
-
const [path, stringify] = get_path_and_stringify(`TypedRoute.${method}`)(
|
|
91
|
-
...args,
|
|
92
|
-
);
|
|
93
|
-
return applyDecorators(
|
|
94
|
-
ROUTERS[method](path),
|
|
95
|
-
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
96
|
-
);
|
|
97
|
-
}
|
|
98
|
-
return route;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
for (const method of [
|
|
102
|
-
typia.json.stringify,
|
|
103
|
-
typia.json.isStringify,
|
|
104
|
-
typia.json.assertStringify,
|
|
105
|
-
typia.json.validateStringify,
|
|
106
|
-
])
|
|
107
|
-
for (const [key, value] of Object.entries(method))
|
|
108
|
-
for (const deco of [
|
|
109
|
-
TypedRoute.Get,
|
|
110
|
-
TypedRoute.Delete,
|
|
111
|
-
TypedRoute.Post,
|
|
112
|
-
TypedRoute.Put,
|
|
113
|
-
TypedRoute.Patch,
|
|
114
|
-
])
|
|
115
|
-
(deco as any)[key] = value;
|
|
116
|
-
|
|
117
|
-
/**
|
|
118
|
-
* @internal
|
|
119
|
-
*/
|
|
120
|
-
class TypedRouteInterceptor implements NestInterceptor {
|
|
121
|
-
public constructor(private readonly stringify: (input: any) => string) {}
|
|
122
|
-
|
|
123
|
-
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
124
|
-
const http: HttpArgumentsHost = context.switchToHttp();
|
|
125
|
-
const response: express.Response = http.getResponse();
|
|
126
|
-
response.header("Content-Type", "application/json");
|
|
127
|
-
|
|
128
|
-
return next.handle().pipe(
|
|
129
|
-
map((value) => this.stringify(value)),
|
|
130
|
-
catchError((err) => route_error(http.getRequest(), err)),
|
|
131
|
-
);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
/**
|
|
136
|
-
* @internal
|
|
137
|
-
*/
|
|
138
|
-
const ROUTERS = {
|
|
139
|
-
Get,
|
|
140
|
-
Post,
|
|
141
|
-
Patch,
|
|
142
|
-
Put,
|
|
143
|
-
Delete,
|
|
144
|
-
};
|
|
1
|
+
import {
|
|
2
|
+
CallHandler,
|
|
3
|
+
Delete,
|
|
4
|
+
ExecutionContext,
|
|
5
|
+
Get,
|
|
6
|
+
NestInterceptor,
|
|
7
|
+
Patch,
|
|
8
|
+
Post,
|
|
9
|
+
Put,
|
|
10
|
+
UseInterceptors,
|
|
11
|
+
applyDecorators,
|
|
12
|
+
} from "@nestjs/common";
|
|
13
|
+
import { HttpArgumentsHost } from "@nestjs/common/interfaces";
|
|
14
|
+
import type express from "express";
|
|
15
|
+
import { catchError, map } from "rxjs/operators";
|
|
16
|
+
import typia from "typia";
|
|
17
|
+
|
|
18
|
+
import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
|
|
19
|
+
import { get_path_and_stringify } from "./internal/get_path_and_stringify";
|
|
20
|
+
import { route_error } from "./internal/route_error";
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Type safe router decorator functions.
|
|
24
|
+
*
|
|
25
|
+
* `TypedRoute` is a module containing router decorator functions which can boost up
|
|
26
|
+
* JSON string conversion speed about 200x times faster than `class-transformer`.
|
|
27
|
+
* Furthermore, such JSON string conversion is even type safe through
|
|
28
|
+
* [typia](https://github.com/samchon/typia).
|
|
29
|
+
*
|
|
30
|
+
* For reference, if you try to invalid data that is not following the promised
|
|
31
|
+
* type `T`, 500 internal server error would be thrown. Also, as `TypedRoute` composes
|
|
32
|
+
* JSON string through `typia.assertStringify<T>()` function, it is not possible to
|
|
33
|
+
* modify response data through interceptors.
|
|
34
|
+
*
|
|
35
|
+
* @author Jeongho Nam - https://github.com/samchon
|
|
36
|
+
*/
|
|
37
|
+
export namespace TypedRoute {
|
|
38
|
+
/**
|
|
39
|
+
* Router decorator function for the GET method.
|
|
40
|
+
*
|
|
41
|
+
* @param path Path of the HTTP request
|
|
42
|
+
* @returns Method decorator
|
|
43
|
+
*/
|
|
44
|
+
export const Get = Generator("Get");
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Router decorator function for the POST method.
|
|
48
|
+
*
|
|
49
|
+
* @param path Path of the HTTP request
|
|
50
|
+
* @returns Method decorator
|
|
51
|
+
*/
|
|
52
|
+
export const Post = Generator("Post");
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Router decorator function for the PATH method.
|
|
56
|
+
*
|
|
57
|
+
* @param path Path of the HTTP request
|
|
58
|
+
* @returns Method decorator
|
|
59
|
+
*/
|
|
60
|
+
export const Patch = Generator("Patch");
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Router decorator function for the PUT method.
|
|
64
|
+
*
|
|
65
|
+
* @param path Path of the HTTP request
|
|
66
|
+
* @returns Method decorator
|
|
67
|
+
*/
|
|
68
|
+
export const Put = Generator("Put");
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Router decorator function for the DELETE method.
|
|
72
|
+
*
|
|
73
|
+
* @param path Path of the HTTP request
|
|
74
|
+
* @returns Method decorator
|
|
75
|
+
*/
|
|
76
|
+
export const Delete = Generator("Delete");
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @internal
|
|
80
|
+
*/
|
|
81
|
+
function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
|
|
82
|
+
function route(path?: string | string[]): MethodDecorator;
|
|
83
|
+
function route<T>(stringify?: IResponseBodyStringifier<T>): MethodDecorator;
|
|
84
|
+
function route<T>(
|
|
85
|
+
path: string | string[],
|
|
86
|
+
stringify?: IResponseBodyStringifier<T>,
|
|
87
|
+
): MethodDecorator;
|
|
88
|
+
|
|
89
|
+
function route(...args: any[]): MethodDecorator {
|
|
90
|
+
const [path, stringify] = get_path_and_stringify(`TypedRoute.${method}`)(
|
|
91
|
+
...args,
|
|
92
|
+
);
|
|
93
|
+
return applyDecorators(
|
|
94
|
+
ROUTERS[method](path),
|
|
95
|
+
UseInterceptors(new TypedRouteInterceptor(stringify)),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
return route;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
for (const method of [
|
|
102
|
+
typia.json.stringify,
|
|
103
|
+
typia.json.isStringify,
|
|
104
|
+
typia.json.assertStringify,
|
|
105
|
+
typia.json.validateStringify,
|
|
106
|
+
])
|
|
107
|
+
for (const [key, value] of Object.entries(method))
|
|
108
|
+
for (const deco of [
|
|
109
|
+
TypedRoute.Get,
|
|
110
|
+
TypedRoute.Delete,
|
|
111
|
+
TypedRoute.Post,
|
|
112
|
+
TypedRoute.Put,
|
|
113
|
+
TypedRoute.Patch,
|
|
114
|
+
])
|
|
115
|
+
(deco as any)[key] = value;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @internal
|
|
119
|
+
*/
|
|
120
|
+
class TypedRouteInterceptor implements NestInterceptor {
|
|
121
|
+
public constructor(private readonly stringify: (input: any) => string) {}
|
|
122
|
+
|
|
123
|
+
public intercept(context: ExecutionContext, next: CallHandler) {
|
|
124
|
+
const http: HttpArgumentsHost = context.switchToHttp();
|
|
125
|
+
const response: express.Response = http.getResponse();
|
|
126
|
+
response.header("Content-Type", "application/json");
|
|
127
|
+
|
|
128
|
+
return next.handle().pipe(
|
|
129
|
+
map((value) => this.stringify(value)),
|
|
130
|
+
catchError((err) => route_error(http.getRequest(), err)),
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* @internal
|
|
137
|
+
*/
|
|
138
|
+
const ROUTERS = {
|
|
139
|
+
Get,
|
|
140
|
+
Post,
|
|
141
|
+
Patch,
|
|
142
|
+
Put,
|
|
143
|
+
Delete,
|
|
144
|
+
};
|