@nestia/core 2.5.8 → 2.5.9-dev.20240223

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 (45) hide show
  1. package/README.md +9 -6
  2. package/lib/decorators/SwaggerCustomizer.d.ts +16 -0
  3. package/lib/decorators/SwaggerCustomizer.js +11 -0
  4. package/lib/decorators/SwaggerCustomizer.js.map +1 -0
  5. package/lib/module.d.ts +1 -0
  6. package/lib/module.js +1 -0
  7. package/lib/module.js.map +1 -1
  8. package/lib/structures/ISwagger.d.ts +72 -0
  9. package/lib/structures/ISwagger.js +3 -0
  10. package/lib/structures/ISwagger.js.map +1 -0
  11. package/lib/structures/ISwaggerComponents.d.ts +26 -0
  12. package/lib/structures/ISwaggerComponents.js +3 -0
  13. package/lib/structures/ISwaggerComponents.js.map +1 -0
  14. package/lib/structures/ISwaggerInfo.d.ts +71 -0
  15. package/lib/structures/ISwaggerInfo.js +3 -0
  16. package/lib/structures/ISwaggerInfo.js.map +1 -0
  17. package/lib/structures/ISwaggerRoute.d.ts +46 -0
  18. package/lib/structures/ISwaggerRoute.js +3 -0
  19. package/lib/structures/ISwaggerRoute.js.map +1 -0
  20. package/lib/structures/ISwaggerSecurityScheme.d.ts +56 -0
  21. package/lib/structures/ISwaggerSecurityScheme.js +3 -0
  22. package/lib/structures/ISwaggerSecurityScheme.js.map +1 -0
  23. package/package.json +3 -3
  24. package/src/decorators/EncryptedBody.ts +105 -105
  25. package/src/decorators/EncryptedModule.ts +96 -96
  26. package/src/decorators/PlainBody.ts +75 -75
  27. package/src/decorators/SwaggerCustomizer.ts +33 -0
  28. package/src/decorators/TypedBody.ts +62 -62
  29. package/src/decorators/TypedException.ts +90 -90
  30. package/src/decorators/TypedRoute.ts +144 -144
  31. package/src/decorators/internal/get_path_and_querify.ts +106 -106
  32. package/src/decorators/internal/load_controller.ts +51 -51
  33. package/src/decorators/internal/validate_request_body.ts +72 -72
  34. package/src/decorators/internal/validate_request_headers.ts +83 -83
  35. package/src/decorators/internal/validate_request_query.ts +71 -71
  36. package/src/module.ts +1 -0
  37. package/src/structures/ISwagger.ts +91 -0
  38. package/src/structures/ISwaggerComponents.ts +29 -0
  39. package/src/structures/ISwaggerInfo.ts +80 -0
  40. package/src/structures/ISwaggerRoute.ts +50 -0
  41. package/src/structures/ISwaggerSecurityScheme.ts +65 -0
  42. package/src/transformers/NodeTransformer.ts +16 -16
  43. package/src/transformers/TypedExceptionTransformer.ts +48 -48
  44. package/src/transformers/TypedRouteTransformer.ts +88 -88
  45. package/src/utils/Singleton.ts +20 -20
@@ -1,96 +1,96 @@
1
- import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
- import { Module } from "@nestjs/common";
3
-
4
- import { Creator } from "../typings/Creator";
5
- import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
6
- import { load_controllers } from "./internal/load_controller";
7
-
8
- /**
9
- * Encrypted module.
10
- *
11
- * `EncryptedModule` is an extension of the {@link Module} class decorator function
12
- * who configures encryption password of the AES-128/256 algorithm. The encryption
13
- * algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
14
- * to encrypt the request and response bod of the HTTP protocol.
15
- *
16
- * By using this `EncryptedModule` decorator function, all of the
17
- * {@link Controller controllers} configured in the *metadata* would be automatically
18
- * changed to the {@link EncryptedController} with the *password*. If there're some
19
- * original {@link EncryptedController} decorated classes in the *metadata*, their
20
- * encryption password would be kept.
21
- *
22
- * Therefore, if you're planning to place original {@link EncryptedController} decorated
23
- * classes in the *metadata*, I hope them to have different encryption password from the
24
- * module level. If not, I recommend you use the {@link Controller} decorator
25
- * function instead.
26
- *
27
- * In addition, the `EncryptedModule` supports a convenient dynamic controller importing
28
- * function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
29
- * path of the controller classes, it imports and configures the controller classes into
30
- * the `Module`, automatically.
31
- *
32
- * @param metadata Module configuration metadata
33
- * @param password Encryption password or its getter function
34
- * @returns Class decorator
35
- *
36
- * @author Jeongho Nam - https://github.com/samchon
37
- */
38
- export function EncryptedModule(
39
- metadata: Parameters<typeof Module>[0],
40
- password: IEncryptionPassword.Closure,
41
- ): ClassDecorator {
42
- return function (target: any) {
43
- Module(metadata)(target);
44
- iterate(password)(target);
45
- };
46
- }
47
-
48
- export namespace EncryptedModule {
49
- /**
50
- * Dynamic encrypted module.
51
- *
52
- * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
53
- * who configures controller classes by the dynamic importing. By specifying directory
54
- * path of the controller classes, those controllers would be automatically imported
55
- * and configured.
56
- *
57
- * @param path Directory path of the controller classes
58
- * @param password Encryption password or its getter function
59
- * @param options Additional options except controller
60
- * @returns Class decorated module instance
61
- */
62
- export async function dynamic(
63
- path: string | string[] | { include: string[]; exclude?: string[] },
64
- password: IEncryptionPassword | IEncryptionPassword.Closure,
65
- options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
66
- ): Promise<object> {
67
- // LOAD CONTROLLERS
68
- const controllers: Creator<object>[] = await load_controllers(path);
69
-
70
- // RETURNS WITH DECORATING
71
- @EncryptedModule(
72
- { ...options, controllers },
73
- typeof password === "object" ? () => password : password,
74
- )
75
- class NestiaModule {}
76
- return NestiaModule;
77
- }
78
- }
79
-
80
- /**
81
- * @internal
82
- */
83
- const iterate =
84
- (password: IEncryptionPassword.Closure) =>
85
- (modulo: any): void => {
86
- const imports = Reflect.getMetadata("imports", modulo);
87
- if (Array.isArray(imports))
88
- for (const imp of imports)
89
- if (typeof imp === "function") iterate(password)(imp);
90
-
91
- const controllers = Reflect.getMetadata("controllers", modulo);
92
- if (Array.isArray(controllers))
93
- for (const c of controllers)
94
- if (typeof c === "function")
95
- Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
96
- };
1
+ import { IEncryptionPassword } from "@nestia/fetcher/lib/IEncryptionPassword";
2
+ import { Module } from "@nestjs/common";
3
+
4
+ import { Creator } from "../typings/Creator";
5
+ import { ENCRYPTION_METADATA_KEY } from "./internal/EncryptedConstant";
6
+ import { load_controllers } from "./internal/load_controller";
7
+
8
+ /**
9
+ * Encrypted module.
10
+ *
11
+ * `EncryptedModule` is an extension of the {@link Module} class decorator function
12
+ * who configures encryption password of the AES-128/256 algorithm. The encryption
13
+ * algorithm and password would be used by {@link EncryptedRoute} and {@link EncryptedBody}
14
+ * to encrypt the request and response bod of the HTTP protocol.
15
+ *
16
+ * By using this `EncryptedModule` decorator function, all of the
17
+ * {@link Controller controllers} configured in the *metadata* would be automatically
18
+ * changed to the {@link EncryptedController} with the *password*. If there're some
19
+ * original {@link EncryptedController} decorated classes in the *metadata*, their
20
+ * encryption password would be kept.
21
+ *
22
+ * Therefore, if you're planning to place original {@link EncryptedController} decorated
23
+ * classes in the *metadata*, I hope them to have different encryption password from the
24
+ * module level. If not, I recommend you use the {@link Controller} decorator
25
+ * function instead.
26
+ *
27
+ * In addition, the `EncryptedModule` supports a convenient dynamic controller importing
28
+ * function, {@link EncryptedModule.dynamic}. If you utilize the function with directory
29
+ * path of the controller classes, it imports and configures the controller classes into
30
+ * the `Module`, automatically.
31
+ *
32
+ * @param metadata Module configuration metadata
33
+ * @param password Encryption password or its getter function
34
+ * @returns Class decorator
35
+ *
36
+ * @author Jeongho Nam - https://github.com/samchon
37
+ */
38
+ export function EncryptedModule(
39
+ metadata: Parameters<typeof Module>[0],
40
+ password: IEncryptionPassword.Closure,
41
+ ): ClassDecorator {
42
+ return function (target: any) {
43
+ Module(metadata)(target);
44
+ iterate(password)(target);
45
+ };
46
+ }
47
+
48
+ export namespace EncryptedModule {
49
+ /**
50
+ * Dynamic encrypted module.
51
+ *
52
+ * `EncryptedModule.dynamic` is an extension of the {@link EncryptedModule} function
53
+ * who configures controller classes by the dynamic importing. By specifying directory
54
+ * path of the controller classes, those controllers would be automatically imported
55
+ * and configured.
56
+ *
57
+ * @param path Directory path of the controller classes
58
+ * @param password Encryption password or its getter function
59
+ * @param options Additional options except controller
60
+ * @returns Class decorated module instance
61
+ */
62
+ export async function dynamic(
63
+ path: string | string[] | { include: string[]; exclude?: string[] },
64
+ password: IEncryptionPassword | IEncryptionPassword.Closure,
65
+ options: Omit<Parameters<typeof Module>[0], "controllers"> = {},
66
+ ): Promise<object> {
67
+ // LOAD CONTROLLERS
68
+ const controllers: Creator<object>[] = await load_controllers(path);
69
+
70
+ // RETURNS WITH DECORATING
71
+ @EncryptedModule(
72
+ { ...options, controllers },
73
+ typeof password === "object" ? () => password : password,
74
+ )
75
+ class NestiaModule {}
76
+ return NestiaModule;
77
+ }
78
+ }
79
+
80
+ /**
81
+ * @internal
82
+ */
83
+ const iterate =
84
+ (password: IEncryptionPassword.Closure) =>
85
+ (modulo: any): void => {
86
+ const imports = Reflect.getMetadata("imports", modulo);
87
+ if (Array.isArray(imports))
88
+ for (const imp of imports)
89
+ if (typeof imp === "function") iterate(password)(imp);
90
+
91
+ const controllers = Reflect.getMetadata("controllers", modulo);
92
+ if (Array.isArray(controllers))
93
+ for (const c of controllers)
94
+ if (typeof c === "function")
95
+ Reflect.defineMetadata(ENCRYPTION_METADATA_KEY, password, c);
96
+ };
@@ -1,75 +1,75 @@
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 } from "typia";
9
-
10
- import { get_text_body } from "./internal/get_text_body";
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 request.
17
- *
18
- * If you adjust the regular {@link Body} decorator function to the body parameter,
19
- * you can't get the full body text because the {@link Body} tries to convert the
20
- * body text to JSON object. Therefore, `@nestia/core` provides this `PlainBody`
21
- * decorator function to get the full body text.
22
- *
23
- * ```typescript
24
- * \@TypedRoute.Post("memo")
25
- * public store
26
- * (
27
- * \@PlainBody() body: string
28
- * ): void;
29
- * ```
30
- *
31
- * @return Parameter decorator
32
- * @author Jeongho Nam - https://github.com/samchon
33
- */
34
- export function PlainBody(): ParameterDecorator;
35
-
36
- /**
37
- * @internal
38
- */
39
- export function PlainBody(
40
- assert?: (input: unknown) => string,
41
- ): ParameterDecorator {
42
- const checker = assert
43
- ? validate_request_body("PlainBody")({
44
- type: "assert",
45
- assert,
46
- })
47
- : null;
48
- return createParamDecorator(async function PlainBody(
49
- _data: any,
50
- context: ExecutionContext,
51
- ) {
52
- const request: express.Request | FastifyRequest = context
53
- .switchToHttp()
54
- .getRequest();
55
- if (!isTextPlain(request.headers["content-type"]))
56
- throw new BadRequestException(`Request body type is not "text/plain".`);
57
- const value: string = await get_text_body(request);
58
- if (checker) {
59
- const error: Error | null = checker(value);
60
- if (error !== null) throw error;
61
- }
62
- return value;
63
- })();
64
- }
65
- Object.assign(PlainBody, assert);
66
-
67
- /**
68
- * @internal
69
- */
70
- const isTextPlain = (text?: string): boolean =>
71
- text !== undefined &&
72
- text
73
- .split(";")
74
- .map((str) => str.trim())
75
- .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
+ import { assert } from "typia";
9
+
10
+ import { get_text_body } from "./internal/get_text_body";
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 request.
17
+ *
18
+ * If you adjust the regular {@link Body} decorator function to the body parameter,
19
+ * you can't get the full body text because the {@link Body} tries to convert the
20
+ * body text to JSON object. Therefore, `@nestia/core` provides this `PlainBody`
21
+ * decorator function to get the full body text.
22
+ *
23
+ * ```typescript
24
+ * \@TypedRoute.Post("memo")
25
+ * public store
26
+ * (
27
+ * \@PlainBody() body: string
28
+ * ): void;
29
+ * ```
30
+ *
31
+ * @return Parameter decorator
32
+ * @author Jeongho Nam - https://github.com/samchon
33
+ */
34
+ export function PlainBody(): ParameterDecorator;
35
+
36
+ /**
37
+ * @internal
38
+ */
39
+ export function PlainBody(
40
+ assert?: (input: unknown) => string,
41
+ ): ParameterDecorator {
42
+ const checker = assert
43
+ ? validate_request_body("PlainBody")({
44
+ type: "assert",
45
+ assert,
46
+ })
47
+ : null;
48
+ return createParamDecorator(async function PlainBody(
49
+ _data: any,
50
+ context: ExecutionContext,
51
+ ) {
52
+ const request: express.Request | FastifyRequest = context
53
+ .switchToHttp()
54
+ .getRequest();
55
+ if (!isTextPlain(request.headers["content-type"]))
56
+ throw new BadRequestException(`Request body type is not "text/plain".`);
57
+ const value: string = await get_text_body(request);
58
+ if (checker) {
59
+ const error: Error | null = checker(value);
60
+ if (error !== null) throw error;
61
+ }
62
+ return value;
63
+ })();
64
+ }
65
+ Object.assign(PlainBody, assert);
66
+
67
+ /**
68
+ * @internal
69
+ */
70
+ const isTextPlain = (text?: string): boolean =>
71
+ text !== undefined &&
72
+ text
73
+ .split(";")
74
+ .map((str) => str.trim())
75
+ .some((str) => str === "text/plain");
@@ -0,0 +1,33 @@
1
+ import { ISwagger } from "../structures/ISwagger";
2
+ import { ISwaggerRoute } from "../structures/ISwaggerRoute";
3
+
4
+ export function SwaggerCustomizer(
5
+ closure: (props: SwaggerCustomizer.IProps) => unknown,
6
+ ): MethodDecorator {
7
+ return function SwaggerCustomizer(
8
+ target: Object,
9
+ propertyKey: string | symbol,
10
+ descriptor: TypedPropertyDescriptor<any>,
11
+ ) {
12
+ Reflect.defineMetadata(
13
+ "nestia/SwaggerCustomizer",
14
+ closure,
15
+ target,
16
+ propertyKey,
17
+ );
18
+ return descriptor;
19
+ };
20
+ }
21
+ export namespace SwaggerCustomizer {
22
+ export interface IProps {
23
+ swagger: ISwagger;
24
+ method: string;
25
+ path: string;
26
+ route: ISwaggerRoute;
27
+ get(accessor: IAccessor): ISwaggerRoute | undefined;
28
+ }
29
+ export interface IAccessor {
30
+ path: string;
31
+ method: string;
32
+ }
33
+ }
@@ -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");