@nestia/core 0.1.4 → 0.1.5

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.
Files changed (49) hide show
  1. package/lib/decorators/DynamicModule.d.ts +3 -0
  2. package/lib/decorators/DynamicModule.js +73 -0
  3. package/lib/decorators/DynamicModule.js.map +1 -0
  4. package/lib/decorators/EncryptedModule.js +16 -137
  5. package/lib/decorators/EncryptedModule.js.map +1 -1
  6. package/lib/decorators/internal/headers_to_object.d.ts +3 -1
  7. package/lib/decorators/internal/headers_to_object.js +0 -3
  8. package/lib/decorators/internal/headers_to_object.js.map +1 -1
  9. package/lib/decorators/internal/load_controller.d.ts +2 -0
  10. package/lib/decorators/internal/load_controller.js +153 -0
  11. package/lib/decorators/internal/load_controller.js.map +1 -0
  12. package/lib/decorators/internal/route_error.d.ts +3 -1
  13. package/lib/decorators/internal/route_error.js +0 -3
  14. package/lib/decorators/internal/route_error.js.map +1 -1
  15. package/lib/module.d.ts +2 -0
  16. package/lib/module.js +2 -0
  17. package/lib/module.js.map +1 -1
  18. package/package.json +1 -1
  19. package/src/decorators/DynamicModule.ts +16 -0
  20. package/src/decorators/EncryptedBody.ts +102 -102
  21. package/src/decorators/EncryptedController.ts +43 -43
  22. package/src/decorators/EncryptedModule.ts +77 -127
  23. package/src/decorators/EncryptedRoute.ts +203 -203
  24. package/src/decorators/PlainBody.ts +38 -38
  25. package/src/decorators/TypedBody.ts +49 -49
  26. package/src/decorators/TypedParam.ts +70 -70
  27. package/src/decorators/TypedRoute.ts +149 -149
  28. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  29. package/src/decorators/internal/get_path_and_stringify.ts +77 -77
  30. package/src/decorators/internal/headers_to_object.ts +10 -13
  31. package/src/decorators/internal/load_controller.ts +35 -0
  32. package/src/decorators/internal/route_error.ts +38 -41
  33. package/src/decorators/internal/validate_request_body.ts +59 -59
  34. package/src/index.ts +5 -5
  35. package/src/module.ts +11 -9
  36. package/src/options/INestiaTransformOptions.ts +6 -6
  37. package/src/options/INestiaTransformProject.ts +7 -6
  38. package/src/options/IRequestBodyValidator.ts +20 -20
  39. package/src/options/IResponseBodyStringifier.ts +25 -25
  40. package/src/transform.ts +20 -20
  41. package/src/transformers/BodyTransformer.ts +106 -106
  42. package/src/transformers/FileTransformer.ts +49 -49
  43. package/src/transformers/MethodTransformer.ts +91 -91
  44. package/src/transformers/NodeTransformer.ts +18 -18
  45. package/src/transformers/ParameterTransformer.ts +45 -45
  46. package/src/transformers/RouteTransformer.ts +131 -131
  47. package/src/typings/Creator.ts +3 -3
  48. package/src/utils/ExceptionManager.ts +126 -126
  49. package/src/utils/Singleton.ts +20 -20
@@ -1,203 +1,203 @@
1
- import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
- import {
3
- CallHandler,
4
- Delete,
5
- ExecutionContext,
6
- Get,
7
- NestInterceptor,
8
- Patch,
9
- Post,
10
- Put,
11
- UseInterceptors,
12
- applyDecorators,
13
- } from "@nestjs/common";
14
- import { HttpArgumentsHost } from "@nestjs/common/interfaces";
15
- import express from "express";
16
- import { Observable, catchError, map } from "rxjs";
17
- import {
18
- assertStringify,
19
- isStringify,
20
- stringify,
21
- validateStringify,
22
- } from "typia";
23
-
24
- import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
25
- import { Singleton } from "../utils/Singleton";
26
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
27
- import { get_path_and_stringify } from "./internal/get_path_and_stringify";
28
- import { headers_to_object } from "./internal/headers_to_object";
29
- import { route_error } from "./internal/route_error";
30
-
31
- /**
32
- * Encrypted router decorator functions.
33
- *
34
- * `EncryptedRoute` is a module containing router decorator functions which encrypts
35
- * response body data through AES-128/250 encryption. Also, those decorator functions
36
- * can boost up JSON string conversion speed about 5x times faster, through
37
- * [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
38
- *
39
- * For reference, `EncryptedRoute` encrypts response body usnig those options.
40
- *
41
- * - AES-128/256
42
- * - CBC mode
43
- * - PKCS #5 Padding
44
- * - Base64 Encoding
45
- *
46
- * Also, router functions in `EncryptedRoute` can convert custom error classes to the
47
- * regular {@link nest.HttpException} class automatically, through
48
- * {@link ExceptionManager}.
49
- *
50
- * @author Jeongho Nam - https://github.com/samchon
51
- */
52
- export namespace EncryptedRoute {
53
- /**
54
- * Encrypted router decorator function for the GET method.
55
- *
56
- * @param paths Path(s) of the HTTP request
57
- * @returns Method decorator
58
- */
59
- export const Get = Generator("Get");
60
-
61
- /**
62
- * Encrypted router decorator function for the GET method.
63
- *
64
- * @param paths Path(s) of the HTTP request
65
- * @returns Method decorator
66
- */
67
- export const Post = Generator("Post");
68
-
69
- /**
70
- * Encrypted router decorator function for the PATCH method.
71
- *
72
- * @param path Path of the HTTP request
73
- * @returns Method decorator
74
- */
75
- export const Patch = Generator("Patch");
76
-
77
- /**
78
- * Encrypted router decorator function for the PUT method.
79
- *
80
- * @param path Path of the HTTP request
81
- * @returns Method decorator
82
- */
83
- export const Put = Generator("Put");
84
-
85
- /**
86
- * Encrypted router decorator function for the DELETE method.
87
- *
88
- * @param path Path of the HTTP request
89
- * @returns Method decorator
90
- */
91
- export const Delete = Generator("Delete");
92
-
93
- function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
94
- function route(path?: string | string[]): MethodDecorator;
95
- function route<T>(
96
- stringify?: IResponseBodyStringifier<T>,
97
- ): MethodDecorator;
98
- function route<T>(
99
- path: string | string[],
100
- stringify?: IResponseBodyStringifier<T>,
101
- ): MethodDecorator;
102
-
103
- function route(...args: any[]): MethodDecorator {
104
- const [path, stringify] = get_path_and_stringify(
105
- `EncryptedRoute.${method}`,
106
- )(...args);
107
- return applyDecorators(
108
- ROUTERS[method](path),
109
- UseInterceptors(
110
- new EncryptedRouteInterceptor(method, stringify),
111
- ),
112
- );
113
- }
114
- return route;
115
- }
116
- }
117
-
118
- for (const method of [
119
- assertStringify,
120
- isStringify,
121
- stringify,
122
- validateStringify,
123
- ]) {
124
- Object.assign(EncryptedRoute.Get, method);
125
- Object.assign(EncryptedRoute.Delete, method);
126
- Object.assign(EncryptedRoute.Post, method);
127
- Object.assign(EncryptedRoute.Put, method);
128
- Object.assign(EncryptedRoute.Patch, method);
129
- }
130
-
131
- /**
132
- * @internal
133
- */
134
- class EncryptedRouteInterceptor implements NestInterceptor {
135
- public constructor(
136
- private readonly method: string,
137
- private readonly stringify: (input: any) => string,
138
- ) {}
139
-
140
- public intercept(
141
- context: ExecutionContext,
142
- next: CallHandler,
143
- ): Observable<any> {
144
- const http: HttpArgumentsHost = context.switchToHttp();
145
- return next.handle().pipe(
146
- map((value) => {
147
- const param:
148
- | IEncryptionPassword
149
- | IEncryptionPassword.Closure
150
- | undefined = Reflect.getMetadata(
151
- ENCRYPTION_METADATA_KEY,
152
- context.getClass(),
153
- );
154
- if (!param)
155
- throw new Error(
156
- `Error on EncryptedRoute.${this.method}(): no encryption password is given.`,
157
- );
158
-
159
- const headers: Singleton<Record<string, string>> =
160
- new Singleton(() => {
161
- const request: express.Request = http.getRequest();
162
- return headers_to_object(request.headers);
163
- });
164
- const body: string | undefined = this.stringify(value);
165
- const password: IEncryptionPassword =
166
- typeof param === "function"
167
- ? param({ headers: headers.get(), body }, false)
168
- : param;
169
- const disabled: boolean =
170
- password.disabled === undefined
171
- ? false
172
- : typeof password.disabled === "function"
173
- ? password.disabled(
174
- { headers: headers.get(), body },
175
- false,
176
- )
177
- : password.disabled;
178
-
179
- const response: express.Response = http.getResponse();
180
- response.header(
181
- "Content-Type",
182
- disabled ? "application/json" : "text/plain",
183
- );
184
-
185
- if (disabled === true) return body;
186
- else if (body === undefined) return body;
187
- return AesPkcs5.encrypt(body, password.key, password.iv);
188
- }),
189
- catchError((err) => route_error(http.getRequest(), err)),
190
- );
191
- }
192
- }
193
-
194
- /**
195
- * @internal
196
- */
197
- const ROUTERS = {
198
- Get,
199
- Post,
200
- Put,
201
- Patch,
202
- Delete,
203
- };
1
+ import { AesPkcs5, IEncryptionPassword } from "@nestia/fetcher";
2
+ import {
3
+ CallHandler,
4
+ Delete,
5
+ ExecutionContext,
6
+ Get,
7
+ NestInterceptor,
8
+ Patch,
9
+ Post,
10
+ Put,
11
+ UseInterceptors,
12
+ applyDecorators,
13
+ } from "@nestjs/common";
14
+ import { HttpArgumentsHost } from "@nestjs/common/interfaces";
15
+ import express from "express";
16
+ import { Observable, catchError, map } from "rxjs";
17
+ import {
18
+ assertStringify,
19
+ isStringify,
20
+ stringify,
21
+ validateStringify,
22
+ } from "typia";
23
+
24
+ import { IResponseBodyStringifier } from "../options/IResponseBodyStringifier";
25
+ import { Singleton } from "../utils/Singleton";
26
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
27
+ import { get_path_and_stringify } from "./internal/get_path_and_stringify";
28
+ import { headers_to_object } from "./internal/headers_to_object";
29
+ import { route_error } from "./internal/route_error";
30
+
31
+ /**
32
+ * Encrypted router decorator functions.
33
+ *
34
+ * `EncryptedRoute` is a module containing router decorator functions which encrypts
35
+ * response body data through AES-128/250 encryption. Also, those decorator functions
36
+ * can boost up JSON string conversion speed about 5x times faster, through
37
+ * [`typia.stringify()`](https://github.com/samchon/typia#fastest-json-string-conversion).
38
+ *
39
+ * For reference, `EncryptedRoute` encrypts response body usnig those options.
40
+ *
41
+ * - AES-128/256
42
+ * - CBC mode
43
+ * - PKCS #5 Padding
44
+ * - Base64 Encoding
45
+ *
46
+ * Also, router functions in `EncryptedRoute` can convert custom error classes to the
47
+ * regular {@link nest.HttpException} class automatically, through
48
+ * {@link ExceptionManager}.
49
+ *
50
+ * @author Jeongho Nam - https://github.com/samchon
51
+ */
52
+ export namespace EncryptedRoute {
53
+ /**
54
+ * Encrypted router decorator function for the GET method.
55
+ *
56
+ * @param paths Path(s) of the HTTP request
57
+ * @returns Method decorator
58
+ */
59
+ export const Get = Generator("Get");
60
+
61
+ /**
62
+ * Encrypted router decorator function for the GET method.
63
+ *
64
+ * @param paths Path(s) of the HTTP request
65
+ * @returns Method decorator
66
+ */
67
+ export const Post = Generator("Post");
68
+
69
+ /**
70
+ * Encrypted router decorator function for the PATCH method.
71
+ *
72
+ * @param path Path of the HTTP request
73
+ * @returns Method decorator
74
+ */
75
+ export const Patch = Generator("Patch");
76
+
77
+ /**
78
+ * Encrypted router decorator function for the PUT method.
79
+ *
80
+ * @param path Path of the HTTP request
81
+ * @returns Method decorator
82
+ */
83
+ export const Put = Generator("Put");
84
+
85
+ /**
86
+ * Encrypted router decorator function for the DELETE method.
87
+ *
88
+ * @param path Path of the HTTP request
89
+ * @returns Method decorator
90
+ */
91
+ export const Delete = Generator("Delete");
92
+
93
+ function Generator(method: "Get" | "Post" | "Put" | "Patch" | "Delete") {
94
+ function route(path?: string | string[]): MethodDecorator;
95
+ function route<T>(
96
+ stringify?: IResponseBodyStringifier<T>,
97
+ ): MethodDecorator;
98
+ function route<T>(
99
+ path: string | string[],
100
+ stringify?: IResponseBodyStringifier<T>,
101
+ ): MethodDecorator;
102
+
103
+ function route(...args: any[]): MethodDecorator {
104
+ const [path, stringify] = get_path_and_stringify(
105
+ `EncryptedRoute.${method}`,
106
+ )(...args);
107
+ return applyDecorators(
108
+ ROUTERS[method](path),
109
+ UseInterceptors(
110
+ new EncryptedRouteInterceptor(method, stringify),
111
+ ),
112
+ );
113
+ }
114
+ return route;
115
+ }
116
+ }
117
+
118
+ for (const method of [
119
+ assertStringify,
120
+ isStringify,
121
+ stringify,
122
+ validateStringify,
123
+ ]) {
124
+ Object.assign(EncryptedRoute.Get, method);
125
+ Object.assign(EncryptedRoute.Delete, method);
126
+ Object.assign(EncryptedRoute.Post, method);
127
+ Object.assign(EncryptedRoute.Put, method);
128
+ Object.assign(EncryptedRoute.Patch, method);
129
+ }
130
+
131
+ /**
132
+ * @internal
133
+ */
134
+ class EncryptedRouteInterceptor implements NestInterceptor {
135
+ public constructor(
136
+ private readonly method: string,
137
+ private readonly stringify: (input: any) => string,
138
+ ) {}
139
+
140
+ public intercept(
141
+ context: ExecutionContext,
142
+ next: CallHandler,
143
+ ): Observable<any> {
144
+ const http: HttpArgumentsHost = context.switchToHttp();
145
+ return next.handle().pipe(
146
+ map((value) => {
147
+ const param:
148
+ | IEncryptionPassword
149
+ | IEncryptionPassword.Closure
150
+ | undefined = Reflect.getMetadata(
151
+ ENCRYPTION_METADATA_KEY,
152
+ context.getClass(),
153
+ );
154
+ if (!param)
155
+ throw new Error(
156
+ `Error on EncryptedRoute.${this.method}(): no encryption password is given.`,
157
+ );
158
+
159
+ const headers: Singleton<Record<string, string>> =
160
+ new Singleton(() => {
161
+ const request: express.Request = http.getRequest();
162
+ return headers_to_object(request.headers);
163
+ });
164
+ const body: string | undefined = this.stringify(value);
165
+ const password: IEncryptionPassword =
166
+ typeof param === "function"
167
+ ? param({ headers: headers.get(), body }, false)
168
+ : param;
169
+ const disabled: boolean =
170
+ password.disabled === undefined
171
+ ? false
172
+ : typeof password.disabled === "function"
173
+ ? password.disabled(
174
+ { headers: headers.get(), body },
175
+ false,
176
+ )
177
+ : password.disabled;
178
+
179
+ const response: express.Response = http.getResponse();
180
+ response.header(
181
+ "Content-Type",
182
+ disabled ? "application/json" : "text/plain",
183
+ );
184
+
185
+ if (disabled === true) return body;
186
+ else if (body === undefined) return body;
187
+ return AesPkcs5.encrypt(body, password.key, password.iv);
188
+ }),
189
+ catchError((err) => route_error(http.getRequest(), err)),
190
+ );
191
+ }
192
+ }
193
+
194
+ /**
195
+ * @internal
196
+ */
197
+ const ROUTERS = {
198
+ Get,
199
+ Post,
200
+ Put,
201
+ Patch,
202
+ Delete,
203
+ };
@@ -1,38 +1,38 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import raw from "raw-body";
8
-
9
- /**
10
- * Plain body decorator.
11
- *
12
- * `PlainBody` is a decorator function getting full body text from the HTTP request.
13
- *
14
- * If you adjust the regular {@link Body} decorator function to the body parameter,
15
- * you can't get the full body text because the {@link Body} tries to convert the
16
- * body text to JSON object. Therefore, `nestia-helper` provides this `PlainBody`
17
- * decorator function to get the full body text.
18
- *
19
- * ```typescript
20
- * \@TypedRoute.Post("memo")
21
- * public store
22
- * (
23
- * \@PlainBody() body: string
24
- * ): void;
25
- * ```
26
- *
27
- * @return Parameter decorator
28
- * @author Jeongho Nam - https://github.com/samchon
29
- */
30
- export const PlainBody: () => ParameterDecorator = createParamDecorator(
31
- async function PlainBody(_data: any, context: ExecutionContext) {
32
- const request: express.Request = context.switchToHttp().getRequest();
33
- if (!request.readable) throw new BadRequestException("Invalid body");
34
-
35
- const body: string = (await raw(request)).toString("utf8").trim();
36
- return body;
37
- },
38
- );
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import raw from "raw-body";
8
+
9
+ /**
10
+ * Plain body decorator.
11
+ *
12
+ * `PlainBody` is a decorator function getting full body text from the HTTP request.
13
+ *
14
+ * If you adjust the regular {@link Body} decorator function to the body parameter,
15
+ * you can't get the full body text because the {@link Body} tries to convert the
16
+ * body text to JSON object. Therefore, `nestia-helper` provides this `PlainBody`
17
+ * decorator function to get the full body text.
18
+ *
19
+ * ```typescript
20
+ * \@TypedRoute.Post("memo")
21
+ * public store
22
+ * (
23
+ * \@PlainBody() body: string
24
+ * ): void;
25
+ * ```
26
+ *
27
+ * @return Parameter decorator
28
+ * @author Jeongho Nam - https://github.com/samchon
29
+ */
30
+ export const PlainBody: () => ParameterDecorator = createParamDecorator(
31
+ async function PlainBody(_data: any, context: ExecutionContext) {
32
+ const request: express.Request = context.switchToHttp().getRequest();
33
+ if (!request.readable) throw new BadRequestException("Invalid body");
34
+
35
+ const body: string = (await raw(request)).toString("utf8").trim();
36
+ return body;
37
+ },
38
+ );
@@ -1,49 +1,49 @@
1
- import {
2
- BadRequestException,
3
- ExecutionContext,
4
- createParamDecorator,
5
- } from "@nestjs/common";
6
- import type express from "express";
7
- import raw from "raw-body";
8
- import { assert, is, validate } from "typia";
9
-
10
- import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
11
- import { validate_request_body } from "./internal/validate_request_body";
12
-
13
- /**
14
- * Safe body decorator.
15
- *
16
- * `TypedBody` is a decorator function getting JSON data from HTTP request. Also,
17
- * it validates the JSON data type through
18
- * [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
19
- * function and throws `BadRequestException` error (status code: 400), if the JSON
20
- * data is not following the promised type.
21
- *
22
- * @param validator Custom validator if required. Default is `typia.assert()`
23
- * @author Jeongho Nam - https://github.com/samchon
24
- */
25
- export function TypedBody<T>(
26
- validator?: IRequestBodyValidator<T>,
27
- ): ParameterDecorator {
28
- const checker = validate_request_body("TypedBody")(validator);
29
- return createParamDecorator(async function TypedBody(
30
- _unknown: any,
31
- context: ExecutionContext,
32
- ) {
33
- const request: express.Request = context.switchToHttp().getRequest();
34
- if (request.headers["content-type"] !== "application/json") {
35
- throw new BadRequestException(
36
- "Request body is not the application/json.",
37
- );
38
- }
39
- const data: any = request.body
40
- ? request.body
41
- : JSON.parse((await raw(request, "utf8")).trim());
42
- checker(data);
43
- return data;
44
- })();
45
- }
46
-
47
- Object.assign(TypedBody, assert);
48
- Object.assign(TypedBody, is);
49
- Object.assign(TypedBody, validate);
1
+ import {
2
+ BadRequestException,
3
+ ExecutionContext,
4
+ createParamDecorator,
5
+ } from "@nestjs/common";
6
+ import type express from "express";
7
+ import raw from "raw-body";
8
+ import { assert, is, validate } from "typia";
9
+
10
+ import { IRequestBodyValidator } from "../options/IRequestBodyValidator";
11
+ import { validate_request_body } from "./internal/validate_request_body";
12
+
13
+ /**
14
+ * Safe body decorator.
15
+ *
16
+ * `TypedBody` is a decorator function getting JSON data from HTTP request. Also,
17
+ * it validates the JSON data type through
18
+ * [`typia.assert()`](https://github.com/samchon/typia#runtime-type-checkers)
19
+ * function and throws `BadRequestException` error (status code: 400), if the JSON
20
+ * data is not following the promised type.
21
+ *
22
+ * @param validator Custom validator if required. Default is `typia.assert()`
23
+ * @author Jeongho Nam - https://github.com/samchon
24
+ */
25
+ export function TypedBody<T>(
26
+ validator?: IRequestBodyValidator<T>,
27
+ ): ParameterDecorator {
28
+ const checker = validate_request_body("TypedBody")(validator);
29
+ return createParamDecorator(async function TypedBody(
30
+ _unknown: any,
31
+ context: ExecutionContext,
32
+ ) {
33
+ const request: express.Request = context.switchToHttp().getRequest();
34
+ if (request.headers["content-type"] !== "application/json") {
35
+ throw new BadRequestException(
36
+ "Request body is not the application/json.",
37
+ );
38
+ }
39
+ const data: any = request.body
40
+ ? request.body
41
+ : JSON.parse((await raw(request, "utf8")).trim());
42
+ checker(data);
43
+ return data;
44
+ })();
45
+ }
46
+
47
+ Object.assign(TypedBody, assert);
48
+ Object.assign(TypedBody, is);
49
+ Object.assign(TypedBody, validate);