@nestia/core 1.2.0-dev.20230504 → 1.2.1-dev.20230505

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 (35) hide show
  1. package/README.md +7 -3
  2. package/lib/decorators/EncryptedBody.js +35 -73
  3. package/lib/decorators/EncryptedBody.js.map +1 -1
  4. package/lib/decorators/EncryptedController.d.ts +0 -5
  5. package/lib/decorators/EncryptedController.js +0 -5
  6. package/lib/decorators/EncryptedController.js.map +1 -1
  7. package/lib/decorators/EncryptedRoute.js +2 -9
  8. package/lib/decorators/EncryptedRoute.js.map +1 -1
  9. package/lib/decorators/PlainBody.js +14 -55
  10. package/lib/decorators/PlainBody.js.map +1 -1
  11. package/lib/decorators/TypedBody.js +20 -65
  12. package/lib/decorators/TypedBody.js.map +1 -1
  13. package/lib/decorators/TypedParam.js +6 -5
  14. package/lib/decorators/TypedParam.js.map +1 -1
  15. package/lib/decorators/TypedQuery.js +42 -59
  16. package/lib/decorators/TypedQuery.js.map +1 -1
  17. package/lib/decorators/internal/get_path_and_stringify.js +5 -3
  18. package/lib/decorators/internal/get_path_and_stringify.js.map +1 -1
  19. package/lib/decorators/internal/send_bad_request.d.ts +2 -0
  20. package/lib/decorators/internal/send_bad_request.js +13 -0
  21. package/lib/decorators/internal/send_bad_request.js.map +1 -0
  22. package/lib/decorators/internal/validate_request_body.d.ts +2 -1
  23. package/lib/decorators/internal/validate_request_body.js +15 -9
  24. package/lib/decorators/internal/validate_request_body.js.map +1 -1
  25. package/package.json +3 -3
  26. package/src/decorators/EncryptedBody.ts +30 -21
  27. package/src/decorators/EncryptedController.ts +0 -5
  28. package/src/decorators/EncryptedRoute.ts +2 -15
  29. package/src/decorators/PlainBody.ts +21 -7
  30. package/src/decorators/TypedBody.ts +26 -12
  31. package/src/decorators/TypedParam.ts +16 -8
  32. package/src/decorators/TypedQuery.ts +21 -15
  33. package/src/decorators/internal/get_path_and_stringify.ts +2 -3
  34. package/src/decorators/internal/send_bad_request.ts +12 -0
  35. package/src/decorators/internal/validate_request_body.ts +16 -13
@@ -1,6 +1,6 @@
1
1
  import { BadRequestException } from "@nestjs/common";
2
2
 
3
- import { IValidation, TypeGuardError } from "typia";
3
+ import typia, { IValidation, TypeGuardError } from "typia";
4
4
 
5
5
  import { IRequestBodyValidator } from "../../options/IRequestBodyValidator";
6
6
  import { TransformError } from "./TransformError";
@@ -8,14 +8,15 @@ import { TransformError } from "./TransformError";
8
8
  export const validate_request_body =
9
9
  (method: string) =>
10
10
  <T>(validator?: IRequestBodyValidator<T>) => {
11
- if (!validator) throw TransformError(method);
11
+ if (!validator) return () => TransformError(method);
12
12
  else if (validator.type === "assert") return assert(validator.assert);
13
13
  else if (validator.type === "is") return is(validator.is);
14
14
  else if (validator.type === "validate")
15
15
  return validate(validator.validate);
16
- throw new Error(
17
- `Error on nestia.core.${method}(): invalid typed validator.`,
18
- );
16
+ return () =>
17
+ new Error(
18
+ `Error on nestia.core.${method}(): invalid typed validator.`,
19
+ );
19
20
  };
20
21
 
21
22
  const assert =
@@ -23,9 +24,10 @@ const assert =
23
24
  (data: T) => {
24
25
  try {
25
26
  closure(data);
27
+ return null;
26
28
  } catch (exp) {
27
- if (exp instanceof TypeGuardError) {
28
- throw new BadRequestException({
29
+ if (typia.is<TypeGuardError>(exp)) {
30
+ return new BadRequestException({
29
31
  path: exp.path,
30
32
  reason: exp.message,
31
33
  expected: exp.expected,
@@ -41,18 +43,19 @@ const is =
41
43
  <T>(closure: (data: T) => boolean) =>
42
44
  (data: T) => {
43
45
  const success: boolean = closure(data);
44
- if (success === false) throw new BadRequestException(MESSAGE);
46
+ return success ? null : new BadRequestException(MESSAGE);
45
47
  };
46
48
 
47
49
  const validate =
48
50
  <T>(closure: (data: T) => IValidation<T>) =>
49
51
  (data: T) => {
50
52
  const result: IValidation<T> = closure(data);
51
- if (result.success === false)
52
- throw new BadRequestException({
53
- errors: result.errors,
54
- message: MESSAGE,
55
- });
53
+ return result.success
54
+ ? null
55
+ : new BadRequestException({
56
+ errors: result.errors,
57
+ message: MESSAGE,
58
+ });
56
59
  };
57
60
 
58
61
  const MESSAGE = "Request body data is not following the promised type.";