@nestia/core 4.5.0-dev.20241218-2 → 4.5.0

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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +87 -87
  3. package/lib/decorators/HumanRoute.d.ts +16 -0
  4. package/lib/decorators/HumanRoute.js +25 -0
  5. package/lib/decorators/HumanRoute.js.map +1 -0
  6. package/lib/module.d.ts +1 -0
  7. package/lib/module.js +1 -0
  8. package/lib/module.js.map +1 -1
  9. package/package.json +6 -6
  10. package/src/adaptors/WebSocketAdaptor.ts +426 -426
  11. package/src/decorators/DynamicModule.ts +43 -43
  12. package/src/decorators/EncryptedBody.ts +101 -101
  13. package/src/decorators/EncryptedController.ts +38 -38
  14. package/src/decorators/EncryptedModule.ts +100 -100
  15. package/src/decorators/EncryptedRoute.ts +219 -219
  16. package/src/decorators/HumanRoute.ts +22 -0
  17. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  18. package/src/decorators/PlainBody.ts +79 -79
  19. package/src/decorators/SwaggerCustomizer.ts +115 -115
  20. package/src/decorators/SwaggerExample.ts +100 -100
  21. package/src/decorators/TypedBody.ts +59 -59
  22. package/src/decorators/TypedException.ts +128 -128
  23. package/src/decorators/TypedFormData.ts +195 -195
  24. package/src/decorators/TypedHeaders.ts +64 -64
  25. package/src/decorators/TypedParam.ts +77 -77
  26. package/src/decorators/TypedQuery.ts +245 -245
  27. package/src/decorators/TypedRoute.ts +214 -214
  28. package/src/decorators/WebSocketRoute.ts +242 -242
  29. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  30. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  31. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  32. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  33. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  34. package/src/decorators/internal/get_text_body.ts +20 -20
  35. package/src/decorators/internal/headers_to_object.ts +13 -13
  36. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  37. package/src/decorators/internal/load_controller.ts +49 -49
  38. package/src/decorators/internal/route_error.ts +45 -45
  39. package/src/decorators/internal/validate_request_body.ts +74 -74
  40. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  41. package/src/decorators/internal/validate_request_headers.ts +86 -86
  42. package/src/decorators/internal/validate_request_query.ts +74 -74
  43. package/src/index.ts +5 -5
  44. package/src/module.ts +22 -21
  45. package/src/options/INestiaTransformOptions.ts +38 -38
  46. package/src/options/INestiaTransformProject.ts +8 -8
  47. package/src/options/IRequestBodyValidator.ts +20 -20
  48. package/src/options/IRequestFormDataProps.ts +27 -27
  49. package/src/options/IRequestHeadersValidator.ts +22 -22
  50. package/src/options/IRequestQueryValidator.ts +20 -20
  51. package/src/options/IResponseBodyQuerifier.ts +25 -25
  52. package/src/options/IResponseBodyStringifier.ts +30 -30
  53. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  54. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  55. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  56. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  57. package/src/programmers/TypedParamProgrammer.ts +33 -33
  58. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  59. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  60. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  61. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  62. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  63. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  64. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  65. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  66. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  67. package/src/transform.ts +35 -35
  68. package/src/transformers/FileTransformer.ts +110 -110
  69. package/src/transformers/MethodTransformer.ts +103 -103
  70. package/src/transformers/NodeTransformer.ts +23 -23
  71. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  72. package/src/transformers/ParameterTransformer.ts +57 -57
  73. package/src/transformers/TypedRouteTransformer.ts +85 -85
  74. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  75. package/src/typings/Creator.ts +3 -3
  76. package/src/typings/get-function-location.d.ts +7 -7
  77. package/src/utils/ArrayUtil.ts +7 -7
  78. package/src/utils/ExceptionManager.ts +112 -112
  79. package/src/utils/Singleton.ts +20 -20
  80. package/src/utils/SourceFinder.ts +57 -57
  81. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,108 +1,108 @@
1
- import { InternalServerErrorException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier";
5
- import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
6
-
7
- /**
8
- * @internal
9
- */
10
- export const get_path_and_querify =
11
- (method: string) =>
12
- (
13
- ...args: any[]
14
- ): [string | string[] | undefined, (input: any) => URLSearchParams] => {
15
- const path: string | string[] | null | undefined =
16
- args[0] === undefined ||
17
- typeof args[0] === "string" ||
18
- Array.isArray(args[0])
19
- ? args[0]
20
- : null;
21
- const functor: IResponseBodyQuerifier<any> | undefined =
22
- path === null ? args[0] : args[1];
23
- return [path ?? undefined, take(method)(functor)];
24
- };
25
-
26
- /**
27
- * @internal
28
- */
29
- const take =
30
- (method: string) =>
31
- <T>(functor?: IResponseBodyQuerifier<T> | null) => {
32
- if (functor === undefined) {
33
- NoTransformConfigurationError(method);
34
- return querify;
35
- } else if (functor === null) return querify;
36
- else if (functor.type === "stringify") return functor.stringify;
37
- else if (functor.type === "assert") return assert(functor.assert);
38
- else if (functor.type === "is") return is(functor.is);
39
- else if (functor.type === "validate") return validate(functor.validate);
40
- throw new Error(
41
- `Error on nestia.core.${method}(): invalid typed stringify function.`,
42
- );
43
- };
44
-
45
- /**
46
- * @internal
47
- */
48
- const querify = (input: Record<string, any>): URLSearchParams => {
49
- const output: URLSearchParams = new URLSearchParams();
50
- for (const [key, value] of Object.entries(input))
51
- if (key === undefined) continue;
52
- else if (Array.isArray(value))
53
- for (const elem of value) output.append(key, String(elem));
54
- else output.append(key, String(value));
55
- return output;
56
- };
57
-
58
- /**
59
- * @internal
60
- */
61
- const assert =
62
- <T>(closure: (data: T) => URLSearchParams) =>
63
- (data: T) => {
64
- try {
65
- return closure(data);
66
- } catch (exp) {
67
- if (typia.is<TypeGuardError>(exp))
68
- throw new InternalServerErrorException({
69
- path: exp.path,
70
- reason: exp.message,
71
- expected: exp.expected,
72
- value: exp.value,
73
- message: MESSAGE,
74
- });
75
- throw exp;
76
- }
77
- };
78
-
79
- /**
80
- * @internal
81
- */
82
- const is =
83
- <T>(closure: (data: T) => URLSearchParams | null) =>
84
- (data: T) => {
85
- const result: URLSearchParams | null = closure(data);
86
- if (result === null) throw new InternalServerErrorException(MESSAGE);
87
- return result;
88
- };
89
-
90
- /**
91
- * @internal
92
- */
93
- const validate =
94
- <T>(closure: (data: T) => IValidation<URLSearchParams>) =>
95
- (data: T) => {
96
- const result: IValidation<URLSearchParams> = closure(data);
97
- if (result.success === false)
98
- throw new InternalServerErrorException({
99
- errors: result.errors,
100
- message: MESSAGE,
101
- });
102
- return result.data;
103
- };
104
-
105
- /**
106
- * @internal
107
- */
108
- const MESSAGE = "Response body data is not following the promised type.";
1
+ import { InternalServerErrorException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IResponseBodyQuerifier } from "../../options/IResponseBodyQuerifier";
5
+ import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const get_path_and_querify =
11
+ (method: string) =>
12
+ (
13
+ ...args: any[]
14
+ ): [string | string[] | undefined, (input: any) => URLSearchParams] => {
15
+ const path: string | string[] | null | undefined =
16
+ args[0] === undefined ||
17
+ typeof args[0] === "string" ||
18
+ Array.isArray(args[0])
19
+ ? args[0]
20
+ : null;
21
+ const functor: IResponseBodyQuerifier<any> | undefined =
22
+ path === null ? args[0] : args[1];
23
+ return [path ?? undefined, take(method)(functor)];
24
+ };
25
+
26
+ /**
27
+ * @internal
28
+ */
29
+ const take =
30
+ (method: string) =>
31
+ <T>(functor?: IResponseBodyQuerifier<T> | null) => {
32
+ if (functor === undefined) {
33
+ NoTransformConfigurationError(method);
34
+ return querify;
35
+ } else if (functor === null) return querify;
36
+ else if (functor.type === "stringify") return functor.stringify;
37
+ else if (functor.type === "assert") return assert(functor.assert);
38
+ else if (functor.type === "is") return is(functor.is);
39
+ else if (functor.type === "validate") return validate(functor.validate);
40
+ throw new Error(
41
+ `Error on nestia.core.${method}(): invalid typed stringify function.`,
42
+ );
43
+ };
44
+
45
+ /**
46
+ * @internal
47
+ */
48
+ const querify = (input: Record<string, any>): URLSearchParams => {
49
+ const output: URLSearchParams = new URLSearchParams();
50
+ for (const [key, value] of Object.entries(input))
51
+ if (key === undefined) continue;
52
+ else if (Array.isArray(value))
53
+ for (const elem of value) output.append(key, String(elem));
54
+ else output.append(key, String(value));
55
+ return output;
56
+ };
57
+
58
+ /**
59
+ * @internal
60
+ */
61
+ const assert =
62
+ <T>(closure: (data: T) => URLSearchParams) =>
63
+ (data: T) => {
64
+ try {
65
+ return closure(data);
66
+ } catch (exp) {
67
+ if (typia.is<TypeGuardError>(exp))
68
+ throw new InternalServerErrorException({
69
+ path: exp.path,
70
+ reason: exp.message,
71
+ expected: exp.expected,
72
+ value: exp.value,
73
+ message: MESSAGE,
74
+ });
75
+ throw exp;
76
+ }
77
+ };
78
+
79
+ /**
80
+ * @internal
81
+ */
82
+ const is =
83
+ <T>(closure: (data: T) => URLSearchParams | null) =>
84
+ (data: T) => {
85
+ const result: URLSearchParams | null = closure(data);
86
+ if (result === null) throw new InternalServerErrorException(MESSAGE);
87
+ return result;
88
+ };
89
+
90
+ /**
91
+ * @internal
92
+ */
93
+ const validate =
94
+ <T>(closure: (data: T) => IValidation<URLSearchParams>) =>
95
+ (data: T) => {
96
+ const result: IValidation<URLSearchParams> = closure(data);
97
+ if (result.success === false)
98
+ throw new InternalServerErrorException({
99
+ errors: result.errors,
100
+ message: MESSAGE,
101
+ });
102
+ return result.data;
103
+ };
104
+
105
+ /**
106
+ * @internal
107
+ */
108
+ const MESSAGE = "Response body data is not following the promised type.";
@@ -1,122 +1,122 @@
1
- import { InternalServerErrorException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IResponseBodyStringifier } from "../../options/IResponseBodyStringifier";
5
- import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
6
- import { TypedRoute } from "../TypedRoute";
7
-
8
- /**
9
- * @internal
10
- */
11
- export const get_path_and_stringify =
12
- (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
13
- (method: string) =>
14
- (
15
- ...args: any[]
16
- ): [
17
- string | string[] | undefined,
18
- (input: any, _method: string, _path: string) => string,
19
- ] => {
20
- const path: string | string[] | null | undefined =
21
- args[0] === undefined ||
22
- typeof args[0] === "string" ||
23
- Array.isArray(args[0])
24
- ? args[0]
25
- : null;
26
- const functor: IResponseBodyStringifier<any> | undefined =
27
- path === null ? args[0] : args[1];
28
- return [path ?? undefined, take(logger)(method)(functor)];
29
- };
30
-
31
- /**
32
- * @internal
33
- */
34
- const take =
35
- (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
36
- (method: string) =>
37
- <T>(functor?: IResponseBodyStringifier<T> | null) => {
38
- if (functor === undefined) {
39
- NoTransformConfigurationError(method);
40
- return (input: T, _method: string, _path: string) =>
41
- JSON.stringify(input);
42
- } else if (functor === null)
43
- return (input: T, _method: string, _path: string) =>
44
- JSON.stringify(input);
45
- else if (functor.type === "stringify") return functor.stringify;
46
- else if (functor.type === "assert") return assert(functor.assert);
47
- else if (functor.type === "is") return is(functor.is);
48
- else if (functor.type === "validate") return validate(functor.validate);
49
- else if (functor.type === "validate.log")
50
- return validateLog(logger)(functor.validate);
51
- throw new Error(
52
- `Error on nestia.core.${method}(): invalid typed stringify function.`,
53
- );
54
- };
55
-
56
- /**
57
- * @internal
58
- */
59
- const assert =
60
- <T>(closure: (data: T) => string) =>
61
- (data: T): string => {
62
- try {
63
- return closure(data);
64
- } catch (exp) {
65
- if (typia.is<TypeGuardError>(exp))
66
- throw new InternalServerErrorException({
67
- path: exp.path,
68
- reason: exp.message,
69
- expected: exp.expected,
70
- value: exp.value,
71
- message: MESSAGE,
72
- });
73
- throw exp;
74
- }
75
- };
76
-
77
- /**
78
- * @internal
79
- */
80
- const is =
81
- <T>(closure: (data: T) => string | null) =>
82
- (data: T, _method: string, _path: string) => {
83
- const result: string | null = closure(data);
84
- if (result === null) throw new InternalServerErrorException(MESSAGE);
85
- return result;
86
- };
87
-
88
- /**
89
- * @internal
90
- */
91
- const validate =
92
- <T>(closure: (data: T) => IValidation<string>) =>
93
- (data: T, _method: string, _path: string): string => {
94
- const result: IValidation<string> = closure(data);
95
- if (result.success === false)
96
- throw new InternalServerErrorException({
97
- errors: result.errors,
98
- message: MESSAGE,
99
- });
100
- return result.data;
101
- };
102
-
103
- const validateLog =
104
- (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
105
- <T>(closure: (data: T) => IValidation<any>) =>
106
- (data: T, method: string, path: string): string => {
107
- const result: IValidation<string> = closure(data);
108
- if (result.success === true) return result.data;
109
- if (result.success === false)
110
- logger()({
111
- errors: result.errors,
112
- method,
113
- path,
114
- data,
115
- });
116
- return JSON.stringify(data);
117
- };
118
-
119
- /**
120
- * @internal
121
- */
122
- const MESSAGE = "Response body data is not following the promised type.";
1
+ import { InternalServerErrorException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IResponseBodyStringifier } from "../../options/IResponseBodyStringifier";
5
+ import { NoTransformConfigurationError } from "../NoTransformConfigurationError";
6
+ import { TypedRoute } from "../TypedRoute";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export const get_path_and_stringify =
12
+ (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
13
+ (method: string) =>
14
+ (
15
+ ...args: any[]
16
+ ): [
17
+ string | string[] | undefined,
18
+ (input: any, _method: string, _path: string) => string,
19
+ ] => {
20
+ const path: string | string[] | null | undefined =
21
+ args[0] === undefined ||
22
+ typeof args[0] === "string" ||
23
+ Array.isArray(args[0])
24
+ ? args[0]
25
+ : null;
26
+ const functor: IResponseBodyStringifier<any> | undefined =
27
+ path === null ? args[0] : args[1];
28
+ return [path ?? undefined, take(logger)(method)(functor)];
29
+ };
30
+
31
+ /**
32
+ * @internal
33
+ */
34
+ const take =
35
+ (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
36
+ (method: string) =>
37
+ <T>(functor?: IResponseBodyStringifier<T> | null) => {
38
+ if (functor === undefined) {
39
+ NoTransformConfigurationError(method);
40
+ return (input: T, _method: string, _path: string) =>
41
+ JSON.stringify(input);
42
+ } else if (functor === null)
43
+ return (input: T, _method: string, _path: string) =>
44
+ JSON.stringify(input);
45
+ else if (functor.type === "stringify") return functor.stringify;
46
+ else if (functor.type === "assert") return assert(functor.assert);
47
+ else if (functor.type === "is") return is(functor.is);
48
+ else if (functor.type === "validate") return validate(functor.validate);
49
+ else if (functor.type === "validate.log")
50
+ return validateLog(logger)(functor.validate);
51
+ throw new Error(
52
+ `Error on nestia.core.${method}(): invalid typed stringify function.`,
53
+ );
54
+ };
55
+
56
+ /**
57
+ * @internal
58
+ */
59
+ const assert =
60
+ <T>(closure: (data: T) => string) =>
61
+ (data: T): string => {
62
+ try {
63
+ return closure(data);
64
+ } catch (exp) {
65
+ if (typia.is<TypeGuardError>(exp))
66
+ throw new InternalServerErrorException({
67
+ path: exp.path,
68
+ reason: exp.message,
69
+ expected: exp.expected,
70
+ value: exp.value,
71
+ message: MESSAGE,
72
+ });
73
+ throw exp;
74
+ }
75
+ };
76
+
77
+ /**
78
+ * @internal
79
+ */
80
+ const is =
81
+ <T>(closure: (data: T) => string | null) =>
82
+ (data: T, _method: string, _path: string) => {
83
+ const result: string | null = closure(data);
84
+ if (result === null) throw new InternalServerErrorException(MESSAGE);
85
+ return result;
86
+ };
87
+
88
+ /**
89
+ * @internal
90
+ */
91
+ const validate =
92
+ <T>(closure: (data: T) => IValidation<string>) =>
93
+ (data: T, _method: string, _path: string): string => {
94
+ const result: IValidation<string> = closure(data);
95
+ if (result.success === false)
96
+ throw new InternalServerErrorException({
97
+ errors: result.errors,
98
+ message: MESSAGE,
99
+ });
100
+ return result.data;
101
+ };
102
+
103
+ const validateLog =
104
+ (logger: () => (log: TypedRoute.IValidateErrorLog) => void) =>
105
+ <T>(closure: (data: T) => IValidation<any>) =>
106
+ (data: T, method: string, path: string): string => {
107
+ const result: IValidation<string> = closure(data);
108
+ if (result.success === true) return result.data;
109
+ if (result.success === false)
110
+ logger()({
111
+ errors: result.errors,
112
+ method,
113
+ path,
114
+ data,
115
+ });
116
+ return JSON.stringify(data);
117
+ };
118
+
119
+ /**
120
+ * @internal
121
+ */
122
+ const MESSAGE = "Response body data is not following the promised type.";
@@ -1,20 +1,20 @@
1
- import type express from "express";
2
- import type { FastifyRequest } from "fastify";
3
- import raw from "raw-body";
4
-
5
- /**
6
- * @internal
7
- */
8
- export const get_text_body = async (
9
- request: express.Request | FastifyRequest,
10
- ): Promise<string> =>
11
- isExpressRequest(request)
12
- ? (await raw(request)).toString("utf8")
13
- : (request.body as string);
14
-
15
- /**
16
- * @internal
17
- */
18
- const isExpressRequest = (
19
- request: express.Request | FastifyRequest,
20
- ): request is express.Request => (request as express.Request).app !== undefined;
1
+ import type express from "express";
2
+ import type { FastifyRequest } from "fastify";
3
+ import raw from "raw-body";
4
+
5
+ /**
6
+ * @internal
7
+ */
8
+ export const get_text_body = async (
9
+ request: express.Request | FastifyRequest,
10
+ ): Promise<string> =>
11
+ isExpressRequest(request)
12
+ ? (await raw(request)).toString("utf8")
13
+ : (request.body as string);
14
+
15
+ /**
16
+ * @internal
17
+ */
18
+ const isExpressRequest = (
19
+ request: express.Request | FastifyRequest,
20
+ ): request is express.Request => (request as express.Request).app !== undefined;
@@ -1,13 +1,13 @@
1
- import http from "http";
2
-
3
- /**
4
- * @internal
5
- */
6
- export function headers_to_object(
7
- headers: http.IncomingHttpHeaders,
8
- ): Record<string, string> {
9
- const output: Record<string, string> = {};
10
- for (const [key, value] of Object.entries(headers))
11
- output[key] = value instanceof Array ? value[0] : value || "";
12
- return output;
13
- }
1
+ import http from "http";
2
+
3
+ /**
4
+ * @internal
5
+ */
6
+ export function headers_to_object(
7
+ headers: http.IncomingHttpHeaders,
8
+ ): Record<string, string> {
9
+ const output: Record<string, string> = {};
10
+ for (const [key, value] of Object.entries(headers))
11
+ output[key] = value instanceof Array ? value[0] : value || "";
12
+ return output;
13
+ }
@@ -1,14 +1,14 @@
1
- import type express from "express";
2
- import type { FastifyRequest } from "fastify";
3
-
4
- /**
5
- * @internal
6
- */
7
- export const is_request_body_undefined = (
8
- request: express.Request | FastifyRequest,
9
- ): boolean =>
10
- request.headers["content-type"] === undefined &&
11
- (request.body === undefined ||
12
- (typeof request.body === "object" &&
13
- request.body !== null &&
14
- Object.keys(request.body).length === 0));
1
+ import type express from "express";
2
+ import type { FastifyRequest } from "fastify";
3
+
4
+ /**
5
+ * @internal
6
+ */
7
+ export const is_request_body_undefined = (
8
+ request: express.Request | FastifyRequest,
9
+ ): boolean =>
10
+ request.headers["content-type"] === undefined &&
11
+ (request.body === undefined ||
12
+ (typeof request.body === "object" &&
13
+ request.body !== null &&
14
+ Object.keys(request.body).length === 0));
@@ -1,49 +1,49 @@
1
- import is_ts_node from "detect-ts-node";
2
-
3
- import { Creator } from "../../typings/Creator";
4
- import { SourceFinder } from "../../utils/SourceFinder";
5
-
6
- export const load_controllers = async (
7
- path: string | string[] | { include: string[]; exclude?: string[] },
8
- isTsNode?: boolean,
9
- ): Promise<Creator<object>[]> => {
10
- const sources: string[] = await SourceFinder.find({
11
- include: Array.isArray(path)
12
- ? path
13
- : typeof path === "object"
14
- ? path.include
15
- : [path],
16
- exclude:
17
- typeof path === "object" && !Array.isArray(path)
18
- ? path.exclude ?? []
19
- : [],
20
- filter:
21
- isTsNode === true || EXTENSION === "ts"
22
- ? (file) =>
23
- file.substring(file.length - 3) === ".ts" &&
24
- file.substring(file.length - 5) !== ".d.ts"
25
- : (flle) => flle.substring(flle.length - 3) === ".js",
26
- });
27
- return mount(sources);
28
- };
29
-
30
- /**
31
- * @internal
32
- */
33
- async function mount(sources: string[]): Promise<any[]> {
34
- const controllers: any[] = [];
35
- for (const file of sources) {
36
- const external: any = await import(file);
37
- for (const key in external) {
38
- const instance: Creator<object> = external[key];
39
- if (Reflect.getMetadata("path", instance) !== undefined)
40
- controllers.push(instance);
41
- }
42
- }
43
- return controllers;
44
- }
45
-
46
- /**
47
- * @internal
48
- */
49
- const EXTENSION = is_ts_node ? "ts" : "js";
1
+ import is_ts_node from "detect-ts-node";
2
+
3
+ import { Creator } from "../../typings/Creator";
4
+ import { SourceFinder } from "../../utils/SourceFinder";
5
+
6
+ export const load_controllers = async (
7
+ path: string | string[] | { include: string[]; exclude?: string[] },
8
+ isTsNode?: boolean,
9
+ ): Promise<Creator<object>[]> => {
10
+ const sources: string[] = await SourceFinder.find({
11
+ include: Array.isArray(path)
12
+ ? path
13
+ : typeof path === "object"
14
+ ? path.include
15
+ : [path],
16
+ exclude:
17
+ typeof path === "object" && !Array.isArray(path)
18
+ ? path.exclude ?? []
19
+ : [],
20
+ filter:
21
+ isTsNode === true || EXTENSION === "ts"
22
+ ? (file) =>
23
+ file.substring(file.length - 3) === ".ts" &&
24
+ file.substring(file.length - 5) !== ".d.ts"
25
+ : (flle) => flle.substring(flle.length - 3) === ".js",
26
+ });
27
+ return mount(sources);
28
+ };
29
+
30
+ /**
31
+ * @internal
32
+ */
33
+ async function mount(sources: string[]): Promise<any[]> {
34
+ const controllers: any[] = [];
35
+ for (const file of sources) {
36
+ const external: any = await import(file);
37
+ for (const key in external) {
38
+ const instance: Creator<object> = external[key];
39
+ if (Reflect.getMetadata("path", instance) !== undefined)
40
+ controllers.push(instance);
41
+ }
42
+ }
43
+ return controllers;
44
+ }
45
+
46
+ /**
47
+ * @internal
48
+ */
49
+ const EXTENSION = is_ts_node ? "ts" : "js";