@nestia/core 3.0.0-dev.20231209 → 3.0.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 (132) hide show
  1. package/README.md +12 -9
  2. package/lib/decorators/DynamicModule.js +3 -3
  3. package/lib/decorators/DynamicModule.js.map +1 -1
  4. package/lib/decorators/EncryptedBody.js +14 -13
  5. package/lib/decorators/EncryptedBody.js.map +1 -1
  6. package/lib/decorators/EncryptedModule.d.ts +1 -1
  7. package/lib/decorators/EncryptedModule.js +45 -21
  8. package/lib/decorators/EncryptedModule.js.map +1 -1
  9. package/lib/decorators/EncryptedRoute.d.ts +5 -5
  10. package/lib/decorators/EncryptedRoute.js +12 -29
  11. package/lib/decorators/EncryptedRoute.js.map +1 -1
  12. package/lib/decorators/PlainBody.js +3 -0
  13. package/lib/decorators/PlainBody.js.map +1 -1
  14. package/lib/decorators/SwaggerCustomizer.d.ts +79 -0
  15. package/lib/decorators/SwaggerCustomizer.js +32 -0
  16. package/lib/decorators/SwaggerCustomizer.js.map +1 -0
  17. package/lib/decorators/TypedBody.js +3 -0
  18. package/lib/decorators/TypedBody.js.map +1 -1
  19. package/lib/decorators/TypedException.d.ts +0 -1
  20. package/lib/decorators/TypedException.js +0 -1
  21. package/lib/decorators/TypedException.js.map +1 -1
  22. package/lib/decorators/TypedFormData.d.ts +60 -0
  23. package/lib/decorators/TypedFormData.js +403 -0
  24. package/lib/decorators/TypedFormData.js.map +1 -0
  25. package/lib/decorators/TypedQuery.d.ts +11 -6
  26. package/lib/decorators/TypedQuery.js +6 -1
  27. package/lib/decorators/TypedQuery.js.map +1 -1
  28. package/lib/decorators/TypedRoute.d.ts +5 -5
  29. package/lib/decorators/internal/get_path_and_querify.js +3 -0
  30. package/lib/decorators/internal/get_path_and_querify.js.map +1 -1
  31. package/lib/decorators/internal/load_controller.js.map +1 -1
  32. package/lib/decorators/internal/validate_request_body.d.ts +1 -3
  33. package/lib/decorators/internal/validate_request_body.js +15 -0
  34. package/lib/decorators/internal/validate_request_body.js.map +1 -1
  35. package/lib/decorators/internal/validate_request_form_data.js +79 -0
  36. package/lib/decorators/internal/validate_request_form_data.js.map +1 -0
  37. package/lib/decorators/internal/validate_request_headers.d.ts +1 -3
  38. package/lib/decorators/internal/validate_request_headers.js +15 -0
  39. package/lib/decorators/internal/validate_request_headers.js.map +1 -1
  40. package/lib/decorators/internal/validate_request_query.d.ts +1 -3
  41. package/lib/decorators/internal/validate_request_query.js +15 -0
  42. package/lib/decorators/internal/validate_request_query.js.map +1 -1
  43. package/lib/module.d.ts +2 -0
  44. package/lib/module.js +2 -0
  45. package/lib/module.js.map +1 -1
  46. package/lib/options/IRequestFormDataProps.d.ts +23 -0
  47. package/lib/options/IRequestFormDataProps.js +3 -0
  48. package/lib/options/IRequestFormDataProps.js.map +1 -0
  49. package/lib/programmers/PlainBodyProgrammer.js +1 -1
  50. package/lib/programmers/PlainBodyProgrammer.js.map +1 -1
  51. package/lib/programmers/TypedExceptionProgrammer.js +5 -5
  52. package/lib/programmers/TypedExceptionProgrammer.js.map +1 -1
  53. package/lib/programmers/TypedFormDataBodyProgrammer.d.ts +6 -0
  54. package/lib/programmers/TypedFormDataBodyProgrammer.js +84 -0
  55. package/lib/programmers/TypedFormDataBodyProgrammer.js.map +1 -0
  56. package/lib/programmers/http/HttpQuerifyProgrammer.js +2 -2
  57. package/lib/programmers/http/HttpQuerifyProgrammer.js.map +1 -1
  58. package/lib/transformers/NodeTransformer.js.map +1 -1
  59. package/lib/transformers/ParameterDecoratorTransformer.js +7 -1
  60. package/lib/transformers/ParameterDecoratorTransformer.js.map +1 -1
  61. package/lib/transformers/TypedExceptionTransformer.js +1 -1
  62. package/lib/transformers/TypedExceptionTransformer.js.map +1 -1
  63. package/lib/transformers/TypedRouteTransformer.js +1 -1
  64. package/lib/transformers/TypedRouteTransformer.js.map +1 -1
  65. package/package.json +15 -16
  66. package/src/decorators/DynamicModule.ts +39 -39
  67. package/src/decorators/EncryptedBody.ts +105 -107
  68. package/src/decorators/EncryptedController.ts +38 -38
  69. package/src/decorators/EncryptedModule.ts +96 -79
  70. package/src/decorators/EncryptedRoute.ts +182 -206
  71. package/src/decorators/PlainBody.ts +75 -72
  72. package/src/decorators/SwaggerCustomizer.ts +115 -0
  73. package/src/decorators/TypedBody.ts +62 -59
  74. package/src/decorators/TypedException.ts +90 -89
  75. package/src/decorators/TypedFormData.ts +219 -0
  76. package/src/decorators/TypedHeaders.ts +69 -69
  77. package/src/decorators/TypedParam.ts +65 -65
  78. package/src/decorators/TypedQuery.ts +251 -246
  79. package/src/decorators/TypedRoute.ts +144 -144
  80. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  81. package/src/decorators/internal/NoTransformConfigureError.ts +8 -8
  82. package/src/decorators/internal/get_path_and_querify.ts +106 -103
  83. package/src/decorators/internal/get_path_and_stringify.ts +91 -91
  84. package/src/decorators/internal/get_text_body.ts +20 -20
  85. package/src/decorators/internal/headers_to_object.ts +13 -13
  86. package/src/decorators/internal/load_controller.ts +51 -51
  87. package/src/decorators/internal/route_error.ts +45 -45
  88. package/src/decorators/internal/validate_request_body.ts +72 -57
  89. package/src/decorators/internal/validate_request_form_data.ts +75 -0
  90. package/src/decorators/internal/validate_request_headers.ts +83 -68
  91. package/src/decorators/internal/validate_request_query.ts +71 -56
  92. package/src/index.ts +5 -5
  93. package/src/module.ts +16 -14
  94. package/src/options/INestiaTransformOptions.ts +17 -17
  95. package/src/options/INestiaTransformProject.ts +7 -7
  96. package/src/options/IRequestBodyValidator.ts +20 -20
  97. package/src/options/IRequestFormDataProps.ts +27 -0
  98. package/src/options/IRequestHeadersValidator.ts +22 -22
  99. package/src/options/IRequestQueryValidator.ts +20 -20
  100. package/src/options/IResponseBodyQuerifier.ts +25 -25
  101. package/src/options/IResponseBodyStringifier.ts +25 -25
  102. package/src/programmers/PlainBodyProgrammer.ts +52 -52
  103. package/src/programmers/TypedBodyProgrammer.ts +108 -108
  104. package/src/programmers/TypedExceptionProgrammer.ts +71 -72
  105. package/src/programmers/TypedFormDataBodyProgrammer.ts +108 -0
  106. package/src/programmers/TypedHeadersProgrammer.ts +56 -56
  107. package/src/programmers/TypedParamProgrammer.ts +24 -24
  108. package/src/programmers/TypedQueryBodyProgrammer.ts +56 -56
  109. package/src/programmers/TypedQueryProgrammer.ts +56 -56
  110. package/src/programmers/TypedQueryRouteProgrammer.ts +51 -51
  111. package/src/programmers/TypedRouteProgrammer.ts +51 -51
  112. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +58 -58
  113. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +62 -62
  114. package/src/programmers/http/HttpQuerifyProgrammer.ts +96 -96
  115. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +63 -63
  116. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  117. package/src/transform.ts +35 -35
  118. package/src/transformers/FileTransformer.ts +66 -66
  119. package/src/transformers/MethodTransformer.ts +94 -94
  120. package/src/transformers/NodeTransformer.ts +16 -16
  121. package/src/transformers/ParameterDecoratorTransformer.ts +120 -121
  122. package/src/transformers/ParameterTransformer.ts +48 -48
  123. package/src/transformers/TypedExceptionTransformer.ts +48 -49
  124. package/src/transformers/TypedRouteTransformer.ts +88 -95
  125. package/src/typings/Creator.ts +3 -3
  126. package/src/utils/ExceptionManager.ts +112 -112
  127. package/src/utils/Singleton.ts +20 -20
  128. package/src/utils/SourceFinder.ts +57 -57
  129. package/lib/decorators/internal/get_binary_body.js +0 -66
  130. package/lib/decorators/internal/get_binary_body.js.map +0 -1
  131. package/src/decorators/internal/get_binary_body.ts +0 -18
  132. /package/lib/decorators/internal/{get_binary_body.d.ts → validate_request_form_data.d.ts} +0 -0
@@ -1,91 +1,91 @@
1
- import { InternalServerErrorException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IResponseBodyStringifier } from "../../options/IResponseBodyStringifier";
5
- import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
-
7
- /**
8
- * @internal
9
- */
10
- export const get_path_and_stringify =
11
- (method: string) =>
12
- (...args: any[]): [string | string[] | undefined, (input: any) => string] => {
13
- const path: string | string[] | null | undefined =
14
- args[0] === undefined ||
15
- typeof args[0] === "string" ||
16
- Array.isArray(args[0])
17
- ? args[0]
18
- : null;
19
- const functor: IResponseBodyStringifier<any> | undefined =
20
- path === null ? args[0] : args[1];
21
- return [path ?? undefined, take(method)(functor)];
22
- };
23
-
24
- /**
25
- * @internal
26
- */
27
- const take =
28
- (method: string) =>
29
- <T>(functor?: IResponseBodyStringifier<T> | null) => {
30
- if (functor === undefined) throw NoTransformConfigureError(method);
31
- else if (functor === null) return JSON.stringify;
32
- else if (functor.type === "stringify") return functor.stringify;
33
- else if (functor.type === "assert") return assert(functor.assert);
34
- else if (functor.type === "is") return is(functor.is);
35
- else if (functor.type === "validate") return validate(functor.validate);
36
- throw new Error(
37
- `Error on nestia.core.${method}(): invalid typed stringify function.`,
38
- );
39
- };
40
-
41
- /**
42
- * @internal
43
- */
44
- const assert =
45
- <T>(closure: (data: T) => string) =>
46
- (data: T) => {
47
- try {
48
- return closure(data);
49
- } catch (exp) {
50
- if (typia.is<TypeGuardError>(exp))
51
- throw new InternalServerErrorException({
52
- path: exp.path,
53
- reason: exp.message,
54
- expected: exp.expected,
55
- value: exp.value,
56
- message: MESSAGE,
57
- });
58
- throw exp;
59
- }
60
- };
61
-
62
- /**
63
- * @internal
64
- */
65
- const is =
66
- <T>(closure: (data: T) => string | null) =>
67
- (data: T) => {
68
- const result: string | null = closure(data);
69
- if (result === null) throw new InternalServerErrorException(MESSAGE);
70
- return result;
71
- };
72
-
73
- /**
74
- * @internal
75
- */
76
- const validate =
77
- <T>(closure: (data: T) => IValidation<string>) =>
78
- (data: T) => {
79
- const result: IValidation<string> = closure(data);
80
- if (result.success === false)
81
- throw new InternalServerErrorException({
82
- errors: result.errors,
83
- message: MESSAGE,
84
- });
85
- return result.data;
86
- };
87
-
88
- /**
89
- * @internal
90
- */
91
- 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 { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const get_path_and_stringify =
11
+ (method: string) =>
12
+ (...args: any[]): [string | string[] | undefined, (input: any) => string] => {
13
+ const path: string | string[] | null | undefined =
14
+ args[0] === undefined ||
15
+ typeof args[0] === "string" ||
16
+ Array.isArray(args[0])
17
+ ? args[0]
18
+ : null;
19
+ const functor: IResponseBodyStringifier<any> | undefined =
20
+ path === null ? args[0] : args[1];
21
+ return [path ?? undefined, take(method)(functor)];
22
+ };
23
+
24
+ /**
25
+ * @internal
26
+ */
27
+ const take =
28
+ (method: string) =>
29
+ <T>(functor?: IResponseBodyStringifier<T> | null) => {
30
+ if (functor === undefined) throw NoTransformConfigureError(method);
31
+ else if (functor === null) return JSON.stringify;
32
+ else if (functor.type === "stringify") return functor.stringify;
33
+ else if (functor.type === "assert") return assert(functor.assert);
34
+ else if (functor.type === "is") return is(functor.is);
35
+ else if (functor.type === "validate") return validate(functor.validate);
36
+ throw new Error(
37
+ `Error on nestia.core.${method}(): invalid typed stringify function.`,
38
+ );
39
+ };
40
+
41
+ /**
42
+ * @internal
43
+ */
44
+ const assert =
45
+ <T>(closure: (data: T) => string) =>
46
+ (data: T) => {
47
+ try {
48
+ return closure(data);
49
+ } catch (exp) {
50
+ if (typia.is<TypeGuardError>(exp))
51
+ throw new InternalServerErrorException({
52
+ path: exp.path,
53
+ reason: exp.message,
54
+ expected: exp.expected,
55
+ value: exp.value,
56
+ message: MESSAGE,
57
+ });
58
+ throw exp;
59
+ }
60
+ };
61
+
62
+ /**
63
+ * @internal
64
+ */
65
+ const is =
66
+ <T>(closure: (data: T) => string | null) =>
67
+ (data: T) => {
68
+ const result: string | null = closure(data);
69
+ if (result === null) throw new InternalServerErrorException(MESSAGE);
70
+ return result;
71
+ };
72
+
73
+ /**
74
+ * @internal
75
+ */
76
+ const validate =
77
+ <T>(closure: (data: T) => IValidation<string>) =>
78
+ (data: T) => {
79
+ const result: IValidation<string> = closure(data);
80
+ if (result.success === false)
81
+ throw new InternalServerErrorException({
82
+ errors: result.errors,
83
+ message: MESSAGE,
84
+ });
85
+ return result.data;
86
+ };
87
+
88
+ /**
89
+ * @internal
90
+ */
91
+ 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,51 +1,51 @@
1
- import is_ts_node from "detect-ts-node";
2
-
3
- import { Creator } from "../../typings/Creator";
4
- import { SourceFinder } from "../../utils/SourceFinder";
5
-
6
- /**
7
- * @internal
8
- */
9
- export const load_controllers = async (
10
- path: string | string[] | { include: string[]; exclude?: string[] },
11
- ): Promise<Creator<object>[]> => {
12
- const sources: string[] = await SourceFinder.find({
13
- include: Array.isArray(path)
14
- ? path
15
- : typeof path === "object"
16
- ? path.include
17
- : [path],
18
- exclude:
19
- typeof path === "object" && !Array.isArray(path)
20
- ? path.exclude ?? []
21
- : [],
22
- filter:
23
- EXTENSION === "ts"
24
- ? (file) =>
25
- file.substring(file.length - 3) === ".ts" &&
26
- file.substring(file.length - 5) !== ".d.ts"
27
- : (flle) => flle.substring(flle.length - 3) === ".js",
28
- });
29
- return mount(sources);
30
- };
31
-
32
- /**
33
- * @internal
34
- */
35
- async function mount(sources: string[]): Promise<any[]> {
36
- const controllers: any[] = [];
37
- for (const file of sources) {
38
- const external: any = await import(file);
39
- for (const key in external) {
40
- const instance: Creator<object> = external[key];
41
- if (Reflect.getMetadata("path", instance) !== undefined)
42
- controllers.push(instance);
43
- }
44
- }
45
- return controllers;
46
- }
47
-
48
- /**
49
- * @internal
50
- */
51
- 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
+ /**
7
+ * @internal
8
+ */
9
+ export const load_controllers = async (
10
+ path: string | string[] | { include: string[]; exclude?: string[] },
11
+ ): Promise<Creator<object>[]> => {
12
+ const sources: string[] = await SourceFinder.find({
13
+ include: Array.isArray(path)
14
+ ? path
15
+ : typeof path === "object"
16
+ ? path.include
17
+ : [path],
18
+ exclude:
19
+ typeof path === "object" && !Array.isArray(path)
20
+ ? path.exclude ?? []
21
+ : [],
22
+ filter:
23
+ EXTENSION === "ts"
24
+ ? (file) =>
25
+ file.substring(file.length - 3) === ".ts" &&
26
+ file.substring(file.length - 5) !== ".d.ts"
27
+ : (flle) => flle.substring(flle.length - 3) === ".js",
28
+ });
29
+ return mount(sources);
30
+ };
31
+
32
+ /**
33
+ * @internal
34
+ */
35
+ async function mount(sources: string[]): Promise<any[]> {
36
+ const controllers: any[] = [];
37
+ for (const file of sources) {
38
+ const external: any = await import(file);
39
+ for (const key in external) {
40
+ const instance: Creator<object> = external[key];
41
+ if (Reflect.getMetadata("path", instance) !== undefined)
42
+ controllers.push(instance);
43
+ }
44
+ }
45
+ return controllers;
46
+ }
47
+
48
+ /**
49
+ * @internal
50
+ */
51
+ const EXTENSION = is_ts_node ? "ts" : "js";
@@ -1,45 +1,45 @@
1
- import { HttpException } from "@nestjs/common";
2
- import express from "express";
3
- import type { FastifyRequest } from "fastify";
4
- import { throwError } from "rxjs";
5
-
6
- import { ExceptionManager } from "../../utils/ExceptionManager";
7
-
8
- /**
9
- * @internal
10
- */
11
- export function route_error(
12
- request: express.Request | FastifyRequest,
13
- error: any,
14
- ) {
15
- error = (() => {
16
- // HTTP-ERROR
17
- if (error instanceof HttpException) return error;
18
-
19
- // CUSTOM-REGISTERED ERROR
20
- for (const [creator, closure] of ExceptionManager.tuples)
21
- if (error instanceof creator) return closure(error);
22
-
23
- // MAYBE INTERNAL ERROR
24
- return error;
25
- })();
26
-
27
- try {
28
- error.method = request.method;
29
- error.path =
30
- (request as express.Request).path ??
31
- (request as FastifyRequest).routeOptions?.url ??
32
- (request as FastifyRequest).routerPath;
33
- } catch {}
34
-
35
- setTimeout(() => {
36
- for (const listener of ExceptionManager.listeners) {
37
- try {
38
- const res: any | Promise<any> = listener(error);
39
- if (typeof res === "object" && typeof res.catch === "function")
40
- res.catch(() => {});
41
- } catch {}
42
- }
43
- }, 0);
44
- return throwError(() => error);
45
- }
1
+ import { HttpException } from "@nestjs/common";
2
+ import express from "express";
3
+ import type { FastifyRequest } from "fastify";
4
+ import { throwError } from "rxjs";
5
+
6
+ import { ExceptionManager } from "../../utils/ExceptionManager";
7
+
8
+ /**
9
+ * @internal
10
+ */
11
+ export function route_error(
12
+ request: express.Request | FastifyRequest,
13
+ error: any,
14
+ ) {
15
+ error = (() => {
16
+ // HTTP-ERROR
17
+ if (error instanceof HttpException) return error;
18
+
19
+ // CUSTOM-REGISTERED ERROR
20
+ for (const [creator, closure] of ExceptionManager.tuples)
21
+ if (error instanceof creator) return closure(error);
22
+
23
+ // MAYBE INTERNAL ERROR
24
+ return error;
25
+ })();
26
+
27
+ try {
28
+ error.method = request.method;
29
+ error.path =
30
+ (request as express.Request).path ??
31
+ (request as FastifyRequest).routeOptions?.url ??
32
+ (request as FastifyRequest).routerPath;
33
+ } catch {}
34
+
35
+ setTimeout(() => {
36
+ for (const listener of ExceptionManager.listeners) {
37
+ try {
38
+ const res: any | Promise<any> = listener(error);
39
+ if (typeof res === "object" && typeof res.catch === "function")
40
+ res.catch(() => {});
41
+ } catch {}
42
+ }
43
+ }, 0);
44
+ return throwError(() => error);
45
+ }
@@ -1,57 +1,72 @@
1
- import { BadRequestException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
5
- import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
-
7
- export const validate_request_body =
8
- (method: string) =>
9
- <T>(validator?: IRequestBodyValidator<T>) => {
10
- if (!validator) return () => NoTransformConfigureError(method);
11
- else if (validator.type === "assert") return assert(validator.assert);
12
- else if (validator.type === "is") return is(validator.is);
13
- else if (validator.type === "validate") return validate(validator.validate);
14
- return () =>
15
- new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
16
- };
17
-
18
- const assert =
19
- <T>(closure: (data: T) => T) =>
20
- (input: T) => {
21
- try {
22
- closure(input);
23
- return null;
24
- } catch (exp) {
25
- if (typia.is<TypeGuardError>(exp)) {
26
- return new BadRequestException({
27
- path: exp.path,
28
- reason: exp.message,
29
- expected: exp.expected,
30
- value: exp.value,
31
- message: MESSAGE,
32
- });
33
- }
34
- throw exp;
35
- }
36
- };
37
-
38
- const is =
39
- <T>(closure: (data: T) => boolean) =>
40
- (input: T) => {
41
- const success: boolean = closure(input);
42
- return success ? null : new BadRequestException(MESSAGE);
43
- };
44
-
45
- const validate =
46
- <T>(closure: (data: T) => IValidation<T>) =>
47
- (input: T) => {
48
- const result: IValidation<T> = closure(input);
49
- return result.success
50
- ? null
51
- : new BadRequestException({
52
- errors: result.errors,
53
- message: MESSAGE,
54
- });
55
- };
56
-
57
- const MESSAGE = "Request body data is not following the promised type.";
1
+ import { BadRequestException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
5
+ import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const validate_request_body =
11
+ (method: string) =>
12
+ <T>(validator?: IRequestBodyValidator<T>) => {
13
+ if (!validator) return () => NoTransformConfigureError(method);
14
+ else if (validator.type === "assert") return assert(validator.assert);
15
+ else if (validator.type === "is") return is(validator.is);
16
+ else if (validator.type === "validate") return validate(validator.validate);
17
+ return () =>
18
+ new Error(`Error on nestia.core.${method}(): invalid typed validator.`);
19
+ };
20
+
21
+ /**
22
+ * @internal
23
+ */
24
+ const assert =
25
+ <T>(closure: (data: T) => T) =>
26
+ (input: T) => {
27
+ try {
28
+ closure(input);
29
+ return null;
30
+ } catch (exp) {
31
+ if (typia.is<TypeGuardError>(exp)) {
32
+ return new BadRequestException({
33
+ path: exp.path,
34
+ reason: exp.message,
35
+ expected: exp.expected,
36
+ value: exp.value,
37
+ message: MESSAGE,
38
+ });
39
+ }
40
+ throw exp;
41
+ }
42
+ };
43
+
44
+ /**
45
+ * @internal
46
+ */
47
+ const is =
48
+ <T>(closure: (data: T) => boolean) =>
49
+ (input: T) => {
50
+ const success: boolean = closure(input);
51
+ return success ? null : new BadRequestException(MESSAGE);
52
+ };
53
+
54
+ /**
55
+ * @internal
56
+ */
57
+ const validate =
58
+ <T>(closure: (data: T) => IValidation<T>) =>
59
+ (input: T) => {
60
+ const result: IValidation<T> = closure(input);
61
+ return result.success
62
+ ? null
63
+ : new BadRequestException({
64
+ errors: result.errors,
65
+ message: MESSAGE,
66
+ });
67
+ };
68
+
69
+ /**
70
+ * @internal
71
+ */
72
+ const MESSAGE = "Request body data is not following the promised type.";
@@ -0,0 +1,75 @@
1
+ import { BadRequestException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IRequestFormDataProps } from "../../options/IRequestFormDataProps";
5
+ import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const validate_request_form_data = <T>(
11
+ props?: IRequestFormDataProps<T>,
12
+ ) => {
13
+ if (!props) return () => NoTransformConfigureError("TypedFormData.Bpdu");
14
+ else if (props.validator.type === "assert")
15
+ return assert(props.validator.assert);
16
+ else if (props.validator.type === "is") return is(props.validator.is);
17
+ else if (props.validator.type === "validate")
18
+ return validate(props.validator.validate);
19
+ return () =>
20
+ new Error(
21
+ `Error on nestia.core.TypedFormData.Body(): invalid typed validator.`,
22
+ );
23
+ };
24
+
25
+ /**
26
+ * @internal
27
+ */
28
+ const assert =
29
+ <T>(closure: (input: FormData) => T) =>
30
+ (input: FormData): T | BadRequestException => {
31
+ try {
32
+ return closure(input);
33
+ } catch (exp) {
34
+ if (typia.is<TypeGuardError>(exp)) {
35
+ return new BadRequestException({
36
+ path: exp.path,
37
+ reason: exp.message,
38
+ expected: exp.expected,
39
+ value: exp.value,
40
+ message: MESSAGE,
41
+ });
42
+ }
43
+ throw exp;
44
+ }
45
+ };
46
+
47
+ /**
48
+ * @internal
49
+ */
50
+ const is =
51
+ <T>(closure: (input: FormData) => T | null) =>
52
+ (input: FormData): T | BadRequestException => {
53
+ const result: T | null = closure(input);
54
+ return result !== null ? result : new BadRequestException(MESSAGE);
55
+ };
56
+
57
+ /**
58
+ * @internal
59
+ */
60
+ const validate =
61
+ <T>(closure: (input: FormData) => IValidation<T>) =>
62
+ (input: FormData): T | BadRequestException => {
63
+ const result: IValidation<T> = closure(input);
64
+ return result.success
65
+ ? result.data
66
+ : new BadRequestException({
67
+ errors: result.errors,
68
+ message: MESSAGE,
69
+ });
70
+ };
71
+
72
+ /**
73
+ * @internal
74
+ */
75
+ const MESSAGE = "Request multipart data is not following the promised type.";