@nestia/core 2.4.3 → 3.0.0-dev.20231209

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 (67) hide show
  1. package/lib/decorators/EncryptedBody.js +13 -11
  2. package/lib/decorators/EncryptedBody.js.map +1 -1
  3. package/lib/decorators/EncryptedRoute.js +29 -12
  4. package/lib/decorators/EncryptedRoute.js.map +1 -1
  5. package/lib/decorators/internal/get_binary_body.d.ts +1 -0
  6. package/lib/decorators/internal/get_binary_body.js +66 -0
  7. package/lib/decorators/internal/get_binary_body.js.map +1 -0
  8. package/package.json +3 -3
  9. package/src/decorators/DynamicModule.ts +39 -39
  10. package/src/decorators/EncryptedBody.ts +17 -12
  11. package/src/decorators/EncryptedController.ts +38 -38
  12. package/src/decorators/EncryptedModule.ts +79 -79
  13. package/src/decorators/EncryptedRoute.ts +38 -14
  14. package/src/decorators/PlainBody.ts +72 -72
  15. package/src/decorators/TypedBody.ts +59 -59
  16. package/src/decorators/TypedException.ts +89 -89
  17. package/src/decorators/TypedHeaders.ts +69 -69
  18. package/src/decorators/TypedParam.ts +65 -65
  19. package/src/decorators/TypedQuery.ts +246 -246
  20. package/src/decorators/TypedRoute.ts +144 -144
  21. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  22. package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
  23. package/src/decorators/internal/get_binary_body.ts +18 -0
  24. package/src/decorators/internal/get_path_and_querify.ts +103 -103
  25. package/src/decorators/internal/get_path_and_stringify.ts +91 -91
  26. package/src/decorators/internal/get_text_body.ts +20 -20
  27. package/src/decorators/internal/headers_to_object.ts +13 -13
  28. package/src/decorators/internal/load_controller.ts +51 -51
  29. package/src/decorators/internal/route_error.ts +45 -45
  30. package/src/decorators/internal/validate_request_body.ts +57 -57
  31. package/src/decorators/internal/validate_request_headers.ts +68 -68
  32. package/src/decorators/internal/validate_request_query.ts +56 -56
  33. package/src/index.ts +5 -5
  34. package/src/module.ts +14 -14
  35. package/src/options/INestiaTransformOptions.ts +17 -17
  36. package/src/options/INestiaTransformProject.ts +7 -7
  37. package/src/options/IRequestBodyValidator.ts +20 -20
  38. package/src/options/IRequestHeadersValidator.ts +22 -22
  39. package/src/options/IRequestQueryValidator.ts +20 -20
  40. package/src/options/IResponseBodyQuerifier.ts +25 -25
  41. package/src/options/IResponseBodyStringifier.ts +25 -25
  42. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  43. package/src/programmers/TypedBodyProgrammer.ts +108 -108
  44. package/src/programmers/TypedExceptionProgrammer.ts +72 -72
  45. package/src/programmers/TypedHeadersProgrammer.ts +56 -56
  46. package/src/programmers/TypedParamProgrammer.ts +24 -24
  47. package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
  48. package/src/programmers/TypedQueryProgrammer.ts +56 -56
  49. package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
  50. package/src/programmers/TypedRouteProgrammer.ts +51 -51
  51. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
  52. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
  53. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  54. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
  55. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  56. package/src/transform.ts +35 -35
  57. package/src/transformers/FileTransformer.ts +66 -66
  58. package/src/transformers/MethodTransformer.ts +94 -94
  59. package/src/transformers/NodeTransformer.ts +16 -16
  60. package/src/transformers/ParameterDecoratorTransformer.ts +121 -121
  61. package/src/transformers/ParameterTransformer.ts +48 -48
  62. package/src/transformers/TypedExceptionTransformer.ts +49 -49
  63. package/src/transformers/TypedRouteTransformer.ts +95 -95
  64. package/src/typings/Creator.ts +3 -3
  65. package/src/utils/ExceptionManager.ts +112 -112
  66. package/src/utils/Singleton.ts +20 -20
  67. package/src/utils/SourceFinder.ts +57 -57
@@ -1,48 +1,48 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
4
- import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
5
-
6
- export namespace ParameterTransformer {
7
- export const transform =
8
- (project: INestiaTransformProject) =>
9
- (param: ts.ParameterDeclaration): ts.ParameterDeclaration => {
10
- // CHECK DECORATOR
11
- const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
12
- ? ts.getDecorators(param)
13
- : (param as any).decorators;
14
- if (!decorators?.length) return param;
15
-
16
- // GET TYPE INFO
17
- const type: ts.Type = project.checker.getTypeAtLocation(param);
18
-
19
- // WHEN LATEST TS VERSION
20
- if (ts.getDecorators !== undefined)
21
- return ts.factory.updateParameterDeclaration(
22
- param,
23
- (param.modifiers || []).map((mod) =>
24
- ts.isDecorator(mod)
25
- ? ParameterDecoratorTransformer.transform(project)(type)(mod)
26
- : mod,
27
- ),
28
- param.dotDotDotToken,
29
- param.name,
30
- param.questionToken,
31
- param.type,
32
- param.initializer,
33
- );
34
- // eslint-disable-next-line
35
- return (ts.factory.updateParameterDeclaration as any)(
36
- param,
37
- decorators.map((deco) =>
38
- ParameterDecoratorTransformer.transform(project)(type)(deco),
39
- ),
40
- (param as any).modifiers,
41
- param.dotDotDotToken,
42
- param.name,
43
- param.questionToken,
44
- param.type,
45
- param.initializer,
46
- );
47
- };
48
- }
1
+ import ts from "typescript";
2
+
3
+ import { INestiaTransformProject } from "../options/INestiaTransformProject";
4
+ import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
5
+
6
+ export namespace ParameterTransformer {
7
+ export const transform =
8
+ (project: INestiaTransformProject) =>
9
+ (param: ts.ParameterDeclaration): ts.ParameterDeclaration => {
10
+ // CHECK DECORATOR
11
+ const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
12
+ ? ts.getDecorators(param)
13
+ : (param as any).decorators;
14
+ if (!decorators?.length) return param;
15
+
16
+ // GET TYPE INFO
17
+ const type: ts.Type = project.checker.getTypeAtLocation(param);
18
+
19
+ // WHEN LATEST TS VERSION
20
+ if (ts.getDecorators !== undefined)
21
+ return ts.factory.updateParameterDeclaration(
22
+ param,
23
+ (param.modifiers || []).map((mod) =>
24
+ ts.isDecorator(mod)
25
+ ? ParameterDecoratorTransformer.transform(project)(type)(mod)
26
+ : mod,
27
+ ),
28
+ param.dotDotDotToken,
29
+ param.name,
30
+ param.questionToken,
31
+ param.type,
32
+ param.initializer,
33
+ );
34
+ // eslint-disable-next-line
35
+ return (ts.factory.updateParameterDeclaration as any)(
36
+ param,
37
+ decorators.map((deco) =>
38
+ ParameterDecoratorTransformer.transform(project)(type)(deco),
39
+ ),
40
+ (param as any).modifiers,
41
+ param.dotDotDotToken,
42
+ param.name,
43
+ param.questionToken,
44
+ param.type,
45
+ param.initializer,
46
+ );
47
+ };
48
+ }
@@ -1,49 +1,49 @@
1
- import path from "path";
2
- import ts from "typescript";
3
-
4
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
- import { TypedExceptionProgrammer } from "../programmers/TypedExceptionProgrammer";
6
-
7
- export namespace TypedExceptionTransformer {
8
- export const transform =
9
- (project: INestiaTransformProject) =>
10
- (decorator: ts.Decorator): ts.Decorator => {
11
- if (!ts.isCallExpression(decorator.expression)) return decorator;
12
-
13
- // CHECK SIGNATURE
14
- const signature: ts.Signature | undefined =
15
- project.checker.getResolvedSignature(decorator.expression);
16
- if (!signature || !signature.declaration) return decorator;
17
-
18
- // CHECK TO BE TRANSFORMED
19
- const done: boolean = (() => {
20
- // CHECK FILENAME
21
- const location: string = path.resolve(
22
- signature.declaration.getSourceFile().fileName,
23
- );
24
- if (location.indexOf(LIB_PATH) === -1 && location !== SRC_PATH)
25
- return false;
26
-
27
- // CHECK DUPLICATED
28
- return decorator.expression.arguments.length !== 3;
29
- })();
30
- if (done === false) return decorator;
31
-
32
- // DO TRANSFORM
33
- return ts.factory.createDecorator(
34
- TypedExceptionProgrammer.generate(project)(decorator.expression),
35
- );
36
- };
37
-
38
- const LIB_PATH = path.join(
39
- "node_modules",
40
- "@nestia",
41
- "core",
42
- "lib",
43
- "decorators",
44
- `TypedException.d.ts`,
45
- );
46
- const SRC_PATH = path.resolve(
47
- path.join(__dirname, "..", "decorators", `TypedException.ts`),
48
- );
49
- }
1
+ import path from "path";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
+ import { TypedExceptionProgrammer } from "../programmers/TypedExceptionProgrammer";
6
+
7
+ export namespace TypedExceptionTransformer {
8
+ export const transform =
9
+ (project: INestiaTransformProject) =>
10
+ (decorator: ts.Decorator): ts.Decorator => {
11
+ if (!ts.isCallExpression(decorator.expression)) return decorator;
12
+
13
+ // CHECK SIGNATURE
14
+ const signature: ts.Signature | undefined =
15
+ project.checker.getResolvedSignature(decorator.expression);
16
+ if (!signature || !signature.declaration) return decorator;
17
+
18
+ // CHECK TO BE TRANSFORMED
19
+ const done: boolean = (() => {
20
+ // CHECK FILENAME
21
+ const location: string = path.resolve(
22
+ signature.declaration.getSourceFile().fileName,
23
+ );
24
+ if (location.indexOf(LIB_PATH) === -1 && location !== SRC_PATH)
25
+ return false;
26
+
27
+ // CHECK DUPLICATED
28
+ return decorator.expression.arguments.length !== 3;
29
+ })();
30
+ if (done === false) return decorator;
31
+
32
+ // DO TRANSFORM
33
+ return ts.factory.createDecorator(
34
+ TypedExceptionProgrammer.generate(project)(decorator.expression),
35
+ );
36
+ };
37
+
38
+ const LIB_PATH = path.join(
39
+ "node_modules",
40
+ "@nestia",
41
+ "core",
42
+ "lib",
43
+ "decorators",
44
+ `TypedException.d.ts`,
45
+ );
46
+ const SRC_PATH = path.resolve(
47
+ path.join(__dirname, "..", "decorators", `TypedException.ts`),
48
+ );
49
+ }
@@ -1,95 +1,95 @@
1
- import path from "path";
2
- import ts from "typescript";
3
-
4
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
- import { TypedQueryRouteProgrammer } from "../programmers/TypedQueryRouteProgrammer";
6
- import { TypedRouteProgrammer } from "../programmers/TypedRouteProgrammer";
7
-
8
- export namespace TypedRouteTransformer {
9
- export const transform =
10
- (project: INestiaTransformProject) =>
11
- (type: ts.Type) =>
12
- (decorator: ts.Decorator): ts.Decorator => {
13
- if (!ts.isCallExpression(decorator.expression)) return decorator;
14
-
15
- // CHECK SIGNATURE
16
- const signature: ts.Signature | undefined =
17
- project.checker.getResolvedSignature(decorator.expression);
18
- if (!signature || !signature.declaration) return decorator;
19
-
20
- // CHECK TO BE TRANSFORMED
21
- const modulo = (() => {
22
- // CHECK FILENAME
23
- const location: string = path.resolve(
24
- signature.declaration.getSourceFile().fileName,
25
- );
26
- if (
27
- LIB_PATHS.every((str) => location.indexOf(str) === -1) &&
28
- SRC_PATHS.every((str) => location !== str)
29
- )
30
- return null;
31
-
32
- // CHECK DUPLICATE BOOSTER
33
- if (decorator.expression.arguments.length >= 2) return false;
34
- else if (decorator.expression.arguments.length === 1) {
35
- const last: ts.Expression =
36
- decorator.expression.arguments[
37
- decorator.expression.arguments.length - 1
38
- ];
39
- const type: ts.Type = project.checker.getTypeAtLocation(last);
40
- if (isObject(project.checker)(type)) return false;
41
- }
42
- return location.split(path.sep).at(-1)?.split(".")[0] === "TypedQuery"
43
- ? "TypedQuery"
44
- : "TypedRoute";
45
- })();
46
- if (modulo === null) return decorator;
47
-
48
- // CHECK TYPE NODE
49
- const typeNode: ts.TypeNode | undefined = project.checker.typeToTypeNode(
50
- type,
51
- undefined,
52
- undefined,
53
- );
54
- if (typeNode === undefined) return decorator;
55
-
56
- // DO TRANSFORM
57
- return ts.factory.createDecorator(
58
- ts.factory.updateCallExpression(
59
- decorator.expression,
60
- decorator.expression.expression,
61
- decorator.expression.typeArguments,
62
- [
63
- ...decorator.expression.arguments,
64
- (modulo === "TypedQuery"
65
- ? TypedQueryRouteProgrammer
66
- : TypedRouteProgrammer
67
- ).generate(project)(decorator.expression.expression)(type),
68
- ],
69
- ),
70
- );
71
- };
72
-
73
- const isObject =
74
- (checker: ts.TypeChecker) =>
75
- (type: ts.Type): boolean =>
76
- (type.getFlags() & ts.TypeFlags.Object) !== 0 &&
77
- !(checker as any).isTupleType(type) &&
78
- !(checker as any).isArrayType(type) &&
79
- !(checker as any).isArrayLikeType(type);
80
-
81
- const CLASSES = ["EncryptedRoute", "TypedRoute", "TypedQuery"];
82
- const LIB_PATHS = CLASSES.map((cla) =>
83
- path.join(
84
- "node_modules",
85
- "@nestia",
86
- "core",
87
- "lib",
88
- "decorators",
89
- `${cla}.d.ts`,
90
- ),
91
- );
92
- const SRC_PATHS = CLASSES.map((cla) =>
93
- path.resolve(path.join(__dirname, "..", "decorators", `${cla}.ts`)),
94
- );
95
- }
1
+ import path from "path";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
+ import { TypedQueryRouteProgrammer } from "../programmers/TypedQueryRouteProgrammer";
6
+ import { TypedRouteProgrammer } from "../programmers/TypedRouteProgrammer";
7
+
8
+ export namespace TypedRouteTransformer {
9
+ export const transform =
10
+ (project: INestiaTransformProject) =>
11
+ (type: ts.Type) =>
12
+ (decorator: ts.Decorator): ts.Decorator => {
13
+ if (!ts.isCallExpression(decorator.expression)) return decorator;
14
+
15
+ // CHECK SIGNATURE
16
+ const signature: ts.Signature | undefined =
17
+ project.checker.getResolvedSignature(decorator.expression);
18
+ if (!signature || !signature.declaration) return decorator;
19
+
20
+ // CHECK TO BE TRANSFORMED
21
+ const modulo = (() => {
22
+ // CHECK FILENAME
23
+ const location: string = path.resolve(
24
+ signature.declaration.getSourceFile().fileName,
25
+ );
26
+ if (
27
+ LIB_PATHS.every((str) => location.indexOf(str) === -1) &&
28
+ SRC_PATHS.every((str) => location !== str)
29
+ )
30
+ return null;
31
+
32
+ // CHECK DUPLICATE BOOSTER
33
+ if (decorator.expression.arguments.length >= 2) return false;
34
+ else if (decorator.expression.arguments.length === 1) {
35
+ const last: ts.Expression =
36
+ decorator.expression.arguments[
37
+ decorator.expression.arguments.length - 1
38
+ ];
39
+ const type: ts.Type = project.checker.getTypeAtLocation(last);
40
+ if (isObject(project.checker)(type)) return false;
41
+ }
42
+ return location.split(path.sep).at(-1)?.split(".")[0] === "TypedQuery"
43
+ ? "TypedQuery"
44
+ : "TypedRoute";
45
+ })();
46
+ if (modulo === null) return decorator;
47
+
48
+ // CHECK TYPE NODE
49
+ const typeNode: ts.TypeNode | undefined = project.checker.typeToTypeNode(
50
+ type,
51
+ undefined,
52
+ undefined,
53
+ );
54
+ if (typeNode === undefined) return decorator;
55
+
56
+ // DO TRANSFORM
57
+ return ts.factory.createDecorator(
58
+ ts.factory.updateCallExpression(
59
+ decorator.expression,
60
+ decorator.expression.expression,
61
+ decorator.expression.typeArguments,
62
+ [
63
+ ...decorator.expression.arguments,
64
+ (modulo === "TypedQuery"
65
+ ? TypedQueryRouteProgrammer
66
+ : TypedRouteProgrammer
67
+ ).generate(project)(decorator.expression.expression)(type),
68
+ ],
69
+ ),
70
+ );
71
+ };
72
+
73
+ const isObject =
74
+ (checker: ts.TypeChecker) =>
75
+ (type: ts.Type): boolean =>
76
+ (type.getFlags() & ts.TypeFlags.Object) !== 0 &&
77
+ !(checker as any).isTupleType(type) &&
78
+ !(checker as any).isArrayType(type) &&
79
+ !(checker as any).isArrayLikeType(type);
80
+
81
+ const CLASSES = ["EncryptedRoute", "TypedRoute", "TypedQuery"];
82
+ const LIB_PATHS = CLASSES.map((cla) =>
83
+ path.join(
84
+ "node_modules",
85
+ "@nestia",
86
+ "core",
87
+ "lib",
88
+ "decorators",
89
+ `${cla}.d.ts`,
90
+ ),
91
+ );
92
+ const SRC_PATHS = CLASSES.map((cla) =>
93
+ path.resolve(path.join(__dirname, "..", "decorators", `${cla}.ts`)),
94
+ );
95
+ }
@@ -1,3 +1,3 @@
1
- export type Creator<T extends object> = {
2
- new (...args: any[]): T;
3
- };
1
+ export type Creator<T extends object> = {
2
+ new (...args: any[]): T;
3
+ };
@@ -1,112 +1,112 @@
1
- import { HttpError } from "@nestia/fetcher";
2
- import { HttpException } from "@nestjs/common";
3
-
4
- import { Creator } from "../typings/Creator";
5
-
6
- /**
7
- * Exception manager for HTTP server.
8
- *
9
- * `ExceptionManager` is an utility class who can insert or erase custom error class with
10
- * its convertion method to a regular {@link nest.HttpException} instance.
11
- *
12
- * If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
13
- * instead of the basic router decorator functions like {@link nest.Get} or
14
- * {@link nest.Post} and the API function throws a custom error whose class has been
15
- * {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
16
- * automatically converted to the regular {@link nest.HttpException} instance by the
17
- * {@link ExceptionManager.Closure} function.
18
- *
19
- * Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
20
- * you can manage your custom error classes much systemtically. You can avoid 500 internal
21
- * server error or hard coding implementation about the custom error classes.
22
- *
23
- * Below error classes are defaultly configured in this `ExceptionManager`
24
- *
25
- * - `typia.TypeGuardError`
26
- * - `@nestia/fetcher.HttpError`
27
- *
28
- * @author Jeongho Nam - https://github.com/samchon
29
- */
30
- export namespace ExceptionManager {
31
- /**
32
- * Insert an error class with converter.
33
- *
34
- * If you've inserted an duplicated error class, the closure would be overwritten.
35
- *
36
- * @param creator Target error class
37
- * @param closure A closure function converting to the `HttpException` class
38
- */
39
- export function insert<T extends Error>(
40
- creator: Creator<T>,
41
- closure: Closure<T>,
42
- ): void {
43
- const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
44
- if (index !== -1) tuples.splice(index, 1);
45
-
46
- tuples.push([creator, closure]);
47
- tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
48
- }
49
-
50
- /**
51
- * Erase an error class.
52
- *
53
- * @param creator Target error class
54
- * @returns Whether be erased or not
55
- */
56
- export function erase<T extends Error>(creator: Creator<T>): boolean {
57
- const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
58
- if (index === -1) return false;
59
-
60
- tuples.splice(index, 1);
61
- return true;
62
- }
63
-
64
- export function on(closure: (error: any) => any): void {
65
- listeners.add(closure);
66
- }
67
-
68
- export function off(closure: (error: any) => any): void {
69
- listeners.delete(closure);
70
- }
71
-
72
- /**
73
- * Type of a closure function converting to the regular {@link nest.HttpException}.
74
- *
75
- * `ExceptionManager.Closure` is a type of closure function who are converting from
76
- * custom error to the regular {@link nest.HttpException} instance. It would be used
77
- * in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
78
- */
79
- export interface Closure<T extends Error> {
80
- /**
81
- * Error converter.
82
- *
83
- * Convert from custom error to the regular {@link nest.HttpException} instance.
84
- *
85
- * @param exception Custom error instance
86
- * @return Regular {@link nest.HttpException} instance
87
- */
88
- (exception: T): HttpException;
89
- }
90
-
91
- /**
92
- * @internal
93
- */
94
- export const tuples: Array<[Creator<any>, Closure<any>]> = [];
95
-
96
- /**
97
- * @internal
98
- */
99
- export const listeners: Set<(error: any) => any> = new Set();
100
- }
101
-
102
- ExceptionManager.insert(
103
- HttpError,
104
- (error) =>
105
- new HttpException(
106
- {
107
- path: error.path,
108
- message: error.message,
109
- },
110
- error.status,
111
- ),
112
- );
1
+ import { HttpError } from "@nestia/fetcher";
2
+ import { HttpException } from "@nestjs/common";
3
+
4
+ import { Creator } from "../typings/Creator";
5
+
6
+ /**
7
+ * Exception manager for HTTP server.
8
+ *
9
+ * `ExceptionManager` is an utility class who can insert or erase custom error class with
10
+ * its convertion method to a regular {@link nest.HttpException} instance.
11
+ *
12
+ * If you define an API function through {@link TypedRoute} or {@link EncryptedRoute}
13
+ * instead of the basic router decorator functions like {@link nest.Get} or
14
+ * {@link nest.Post} and the API function throws a custom error whose class has been
15
+ * {@link ExceptionManager.insert inserted} in this `EntityManager`, the error would be
16
+ * automatically converted to the regular {@link nest.HttpException} instance by the
17
+ * {@link ExceptionManager.Closure} function.
18
+ *
19
+ * Therefore, with this `ExceptionManager` and {@link TypedRoute} or {@link EncryptedRoute},
20
+ * you can manage your custom error classes much systemtically. You can avoid 500 internal
21
+ * server error or hard coding implementation about the custom error classes.
22
+ *
23
+ * Below error classes are defaultly configured in this `ExceptionManager`
24
+ *
25
+ * - `typia.TypeGuardError`
26
+ * - `@nestia/fetcher.HttpError`
27
+ *
28
+ * @author Jeongho Nam - https://github.com/samchon
29
+ */
30
+ export namespace ExceptionManager {
31
+ /**
32
+ * Insert an error class with converter.
33
+ *
34
+ * If you've inserted an duplicated error class, the closure would be overwritten.
35
+ *
36
+ * @param creator Target error class
37
+ * @param closure A closure function converting to the `HttpException` class
38
+ */
39
+ export function insert<T extends Error>(
40
+ creator: Creator<T>,
41
+ closure: Closure<T>,
42
+ ): void {
43
+ const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
44
+ if (index !== -1) tuples.splice(index, 1);
45
+
46
+ tuples.push([creator, closure]);
47
+ tuples.sort(([x], [y]) => (x.prototype instanceof y ? -1 : 1));
48
+ }
49
+
50
+ /**
51
+ * Erase an error class.
52
+ *
53
+ * @param creator Target error class
54
+ * @returns Whether be erased or not
55
+ */
56
+ export function erase<T extends Error>(creator: Creator<T>): boolean {
57
+ const index: number = tuples.findIndex((tuple) => tuple[0] === creator);
58
+ if (index === -1) return false;
59
+
60
+ tuples.splice(index, 1);
61
+ return true;
62
+ }
63
+
64
+ export function on(closure: (error: any) => any): void {
65
+ listeners.add(closure);
66
+ }
67
+
68
+ export function off(closure: (error: any) => any): void {
69
+ listeners.delete(closure);
70
+ }
71
+
72
+ /**
73
+ * Type of a closure function converting to the regular {@link nest.HttpException}.
74
+ *
75
+ * `ExceptionManager.Closure` is a type of closure function who are converting from
76
+ * custom error to the regular {@link nest.HttpException} instance. It would be used
77
+ * in the {@link ExceptionManager} with {@link TypedRoute} or {@link EncryptedRoute}.
78
+ */
79
+ export interface Closure<T extends Error> {
80
+ /**
81
+ * Error converter.
82
+ *
83
+ * Convert from custom error to the regular {@link nest.HttpException} instance.
84
+ *
85
+ * @param exception Custom error instance
86
+ * @return Regular {@link nest.HttpException} instance
87
+ */
88
+ (exception: T): HttpException;
89
+ }
90
+
91
+ /**
92
+ * @internal
93
+ */
94
+ export const tuples: Array<[Creator<any>, Closure<any>]> = [];
95
+
96
+ /**
97
+ * @internal
98
+ */
99
+ export const listeners: Set<(error: any) => any> = new Set();
100
+ }
101
+
102
+ ExceptionManager.insert(
103
+ HttpError,
104
+ (error) =>
105
+ new HttpException(
106
+ {
107
+ path: error.path,
108
+ message: error.message,
109
+ },
110
+ error.status,
111
+ ),
112
+ );
@@ -1,20 +1,20 @@
1
- /**
2
- * @internal
3
- */
4
- export class Singleton<T> {
5
- private value_: T | object;
6
-
7
- public constructor(private readonly closure_: () => T) {
8
- this.value_ = NOT_MOUNTED_YET;
9
- }
10
-
11
- public get(): T {
12
- if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
13
- return this.value_ as T;
14
- }
15
- }
16
-
17
- /**
18
- * @internal
19
- */
20
- const NOT_MOUNTED_YET = {};
1
+ /**
2
+ * @internal
3
+ */
4
+ export class Singleton<T> {
5
+ private value_: T | object;
6
+
7
+ public constructor(private readonly closure_: () => T) {
8
+ this.value_ = NOT_MOUNTED_YET;
9
+ }
10
+
11
+ public get(): T {
12
+ if (this.value_ === NOT_MOUNTED_YET) this.value_ = this.closure_();
13
+ return this.value_ as T;
14
+ }
15
+ }
16
+
17
+ /**
18
+ * @internal
19
+ */
20
+ const NOT_MOUNTED_YET = {};