@nestia/core 4.6.1-dev.20250117 → 4.6.2

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 (86) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +92 -87
  3. package/lib/decorators/DynamicModule.d.ts +1 -1
  4. package/lib/decorators/DynamicModule.js +1 -1
  5. package/lib/decorators/EncryptedBody.d.ts +2 -2
  6. package/lib/decorators/EncryptedBody.js +2 -2
  7. package/lib/decorators/TypedException.d.ts +4 -4
  8. package/lib/decorators/TypedRoute.d.ts +1 -1
  9. package/lib/decorators/WebSocketRoute.d.ts +1 -1
  10. package/lib/decorators/WebSocketRoute.js +1 -1
  11. package/lib/decorators/internal/route_error.js.map +1 -1
  12. package/lib/utils/ExceptionManager.d.ts +1 -1
  13. package/lib/utils/ExceptionManager.js +1 -1
  14. package/package.json +5 -5
  15. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  16. package/src/decorators/DynamicModule.ts +43 -43
  17. package/src/decorators/EncryptedBody.ts +101 -101
  18. package/src/decorators/EncryptedController.ts +38 -38
  19. package/src/decorators/EncryptedModule.ts +100 -100
  20. package/src/decorators/EncryptedRoute.ts +219 -219
  21. package/src/decorators/HumanRoute.ts +22 -22
  22. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  23. package/src/decorators/PlainBody.ts +79 -79
  24. package/src/decorators/SwaggerCustomizer.ts +115 -115
  25. package/src/decorators/SwaggerExample.ts +100 -100
  26. package/src/decorators/TypedBody.ts +59 -59
  27. package/src/decorators/TypedException.ts +166 -166
  28. package/src/decorators/TypedFormData.ts +195 -195
  29. package/src/decorators/TypedHeaders.ts +64 -64
  30. package/src/decorators/TypedParam.ts +77 -77
  31. package/src/decorators/TypedQuery.ts +245 -245
  32. package/src/decorators/TypedRoute.ts +214 -214
  33. package/src/decorators/WebSocketRoute.ts +242 -242
  34. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  35. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  36. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  37. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  38. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  39. package/src/decorators/internal/get_text_body.ts +20 -20
  40. package/src/decorators/internal/headers_to_object.ts +13 -13
  41. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  42. package/src/decorators/internal/load_controller.ts +49 -49
  43. package/src/decorators/internal/route_error.ts +45 -45
  44. package/src/decorators/internal/validate_request_body.ts +74 -74
  45. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  46. package/src/decorators/internal/validate_request_headers.ts +86 -86
  47. package/src/decorators/internal/validate_request_query.ts +74 -74
  48. package/src/index.ts +5 -5
  49. package/src/module.ts +22 -22
  50. package/src/options/INestiaTransformOptions.ts +38 -38
  51. package/src/options/INestiaTransformProject.ts +8 -8
  52. package/src/options/IRequestBodyValidator.ts +20 -20
  53. package/src/options/IRequestFormDataProps.ts +27 -27
  54. package/src/options/IRequestHeadersValidator.ts +22 -22
  55. package/src/options/IRequestQueryValidator.ts +20 -20
  56. package/src/options/IResponseBodyQuerifier.ts +25 -25
  57. package/src/options/IResponseBodyStringifier.ts +30 -30
  58. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  59. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  60. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  61. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  62. package/src/programmers/TypedParamProgrammer.ts +33 -33
  63. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  64. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  65. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  66. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  67. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  68. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  69. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  70. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  71. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  72. package/src/transform.ts +35 -35
  73. package/src/transformers/FileTransformer.ts +110 -110
  74. package/src/transformers/MethodTransformer.ts +103 -103
  75. package/src/transformers/NodeTransformer.ts +23 -23
  76. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  77. package/src/transformers/ParameterTransformer.ts +57 -57
  78. package/src/transformers/TypedRouteTransformer.ts +85 -85
  79. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  80. package/src/typings/Creator.ts +3 -3
  81. package/src/typings/get-function-location.d.ts +7 -7
  82. package/src/utils/ArrayUtil.ts +7 -7
  83. package/src/utils/ExceptionManager.ts +112 -112
  84. package/src/utils/Singleton.ts +20 -20
  85. package/src/utils/SourceFinder.ts +57 -57
  86. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,143 +1,143 @@
1
- import path from "path";
2
- import ts from "typescript";
3
-
4
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
5
- import { PlainBodyProgrammer } from "../programmers/PlainBodyProgrammer";
6
- import { TypedBodyProgrammer } from "../programmers/TypedBodyProgrammer";
7
- import { TypedFormDataBodyProgrammer } from "../programmers/TypedFormDataBodyProgrammer";
8
- import { TypedHeadersProgrammer } from "../programmers/TypedHeadersProgrammer";
9
- import { TypedParamProgrammer } from "../programmers/TypedParamProgrammer";
10
- import { TypedQueryBodyProgrammer } from "../programmers/TypedQueryBodyProgrammer";
11
- import { TypedQueryProgrammer } from "../programmers/TypedQueryProgrammer";
12
-
13
- export namespace ParameterDecoratorTransformer {
14
- export const transform = (props: {
15
- context: INestiaTransformContext;
16
- type: ts.Type;
17
- decorator: ts.Decorator;
18
- }): ts.Decorator => {
19
- //----
20
- // VALIDATIONS
21
- //----
22
- // CHECK DECORATOR
23
- if (!ts.isCallExpression(props.decorator.expression))
24
- return props.decorator;
25
-
26
- // SIGNATURE DECLARATION
27
- const declaration: ts.Declaration | undefined =
28
- props.context.checker.getResolvedSignature(
29
- props.decorator.expression,
30
- )?.declaration;
31
- if (declaration === undefined) return props.decorator;
32
-
33
- // FILE PATH
34
- const file: string = path.resolve(declaration.getSourceFile().fileName);
35
- if (file.indexOf(LIB_PATH) === -1 && file.indexOf(MONO_PATH) === -1)
36
- return props.decorator;
37
-
38
- //----
39
- // TRANSFORMATION
40
- //----
41
- // FIND PROGRAMMER
42
- const programmer: Programmer | undefined =
43
- FUNCTORS[
44
- getName(props.context.checker.getTypeAtLocation(declaration).symbol)
45
- ];
46
- if (programmer === undefined) return props.decorator;
47
-
48
- // GET TYPE INFO
49
- const typeNode: ts.TypeNode | undefined =
50
- props.context.checker.typeToTypeNode(props.type, undefined, undefined);
51
- if (typeNode === undefined) return props.decorator;
52
-
53
- // DO TRANSFORM
54
- return ts.factory.createDecorator(
55
- ts.factory.updateCallExpression(
56
- props.decorator.expression,
57
- props.decorator.expression.expression,
58
- props.decorator.expression.typeArguments,
59
- programmer({
60
- context: props.context,
61
- modulo: props.decorator.expression.expression,
62
- arguments: props.decorator.expression.arguments,
63
- type: props.type,
64
- }),
65
- ),
66
- );
67
- };
68
- }
69
-
70
- type Programmer = (props: {
71
- context: INestiaTransformContext;
72
- modulo: ts.LeftHandSideExpression;
73
- arguments: readonly ts.Expression[];
74
- type: ts.Type;
75
- }) => readonly ts.Expression[];
76
-
77
- const FUNCTORS: Record<string, Programmer> = {
78
- EncryptedBody: (props) =>
79
- props.arguments.length
80
- ? props.arguments
81
- : [TypedBodyProgrammer.generate(props)],
82
- TypedBody: (props) =>
83
- props.arguments.length
84
- ? props.arguments
85
- : [TypedBodyProgrammer.generate(props)],
86
- TypedHeaders: (props) =>
87
- props.arguments.length
88
- ? props.arguments
89
- : [TypedHeadersProgrammer.generate(props)],
90
- TypedParam: (props) =>
91
- props.arguments.length !== 1
92
- ? props.arguments
93
- : TypedParamProgrammer.generate(props),
94
- TypedQuery: (props) =>
95
- props.arguments.length
96
- ? props.arguments
97
- : [TypedQueryProgrammer.generate(props)],
98
- "TypedQuery.Body": (props) =>
99
- props.arguments.length
100
- ? props.arguments
101
- : [TypedQueryBodyProgrammer.generate(props)],
102
- "TypedFormData.Body": (props) =>
103
- props.arguments.length === 0
104
- ? [
105
- ts.factory.createIdentifier("undefined"),
106
- TypedFormDataBodyProgrammer.generate(props),
107
- ]
108
- : props.arguments.length === 1
109
- ? [props.arguments[0], TypedFormDataBodyProgrammer.generate(props)]
110
- : props.arguments,
111
- PlainBody: (props) =>
112
- props.arguments.length
113
- ? props.arguments
114
- : [PlainBodyProgrammer.generate(props)],
115
- "WebSocketRoute.Header": (props) =>
116
- props.arguments.length
117
- ? props.arguments
118
- : [TypedBodyProgrammer.generate(props)],
119
- "WebSocketRoute.Param": (props) =>
120
- props.arguments.length !== 1
121
- ? props.arguments
122
- : TypedParamProgrammer.generate(props),
123
- "WebSocketRoute.Query": (props) =>
124
- props.arguments.length
125
- ? props.arguments
126
- : [TypedQueryProgrammer.generate(props)],
127
- };
128
-
129
- const LIB_PATH = path.join("@nestia", "core", "lib", "decorators");
130
- const MONO_PATH = path.join("packages", "core", "lib", "decorators");
131
-
132
- const getName = (symbol: ts.Symbol): string => {
133
- const parent = symbol.getDeclarations()?.[0]?.parent;
134
- return parent ? exploreName(parent)(symbol.escapedName.toString()) : "__type";
135
- };
136
- const exploreName =
137
- (decl: ts.Node) =>
138
- (name: string): string =>
139
- ts.isModuleBlock(decl)
140
- ? exploreName(decl.parent.parent)(
141
- `${decl.parent.name.getFullText().trim()}.${name}`,
142
- )
143
- : name;
1
+ import path from "path";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
5
+ import { PlainBodyProgrammer } from "../programmers/PlainBodyProgrammer";
6
+ import { TypedBodyProgrammer } from "../programmers/TypedBodyProgrammer";
7
+ import { TypedFormDataBodyProgrammer } from "../programmers/TypedFormDataBodyProgrammer";
8
+ import { TypedHeadersProgrammer } from "../programmers/TypedHeadersProgrammer";
9
+ import { TypedParamProgrammer } from "../programmers/TypedParamProgrammer";
10
+ import { TypedQueryBodyProgrammer } from "../programmers/TypedQueryBodyProgrammer";
11
+ import { TypedQueryProgrammer } from "../programmers/TypedQueryProgrammer";
12
+
13
+ export namespace ParameterDecoratorTransformer {
14
+ export const transform = (props: {
15
+ context: INestiaTransformContext;
16
+ type: ts.Type;
17
+ decorator: ts.Decorator;
18
+ }): ts.Decorator => {
19
+ //----
20
+ // VALIDATIONS
21
+ //----
22
+ // CHECK DECORATOR
23
+ if (!ts.isCallExpression(props.decorator.expression))
24
+ return props.decorator;
25
+
26
+ // SIGNATURE DECLARATION
27
+ const declaration: ts.Declaration | undefined =
28
+ props.context.checker.getResolvedSignature(
29
+ props.decorator.expression,
30
+ )?.declaration;
31
+ if (declaration === undefined) return props.decorator;
32
+
33
+ // FILE PATH
34
+ const file: string = path.resolve(declaration.getSourceFile().fileName);
35
+ if (file.indexOf(LIB_PATH) === -1 && file.indexOf(MONO_PATH) === -1)
36
+ return props.decorator;
37
+
38
+ //----
39
+ // TRANSFORMATION
40
+ //----
41
+ // FIND PROGRAMMER
42
+ const programmer: Programmer | undefined =
43
+ FUNCTORS[
44
+ getName(props.context.checker.getTypeAtLocation(declaration).symbol)
45
+ ];
46
+ if (programmer === undefined) return props.decorator;
47
+
48
+ // GET TYPE INFO
49
+ const typeNode: ts.TypeNode | undefined =
50
+ props.context.checker.typeToTypeNode(props.type, undefined, undefined);
51
+ if (typeNode === undefined) return props.decorator;
52
+
53
+ // DO TRANSFORM
54
+ return ts.factory.createDecorator(
55
+ ts.factory.updateCallExpression(
56
+ props.decorator.expression,
57
+ props.decorator.expression.expression,
58
+ props.decorator.expression.typeArguments,
59
+ programmer({
60
+ context: props.context,
61
+ modulo: props.decorator.expression.expression,
62
+ arguments: props.decorator.expression.arguments,
63
+ type: props.type,
64
+ }),
65
+ ),
66
+ );
67
+ };
68
+ }
69
+
70
+ type Programmer = (props: {
71
+ context: INestiaTransformContext;
72
+ modulo: ts.LeftHandSideExpression;
73
+ arguments: readonly ts.Expression[];
74
+ type: ts.Type;
75
+ }) => readonly ts.Expression[];
76
+
77
+ const FUNCTORS: Record<string, Programmer> = {
78
+ EncryptedBody: (props) =>
79
+ props.arguments.length
80
+ ? props.arguments
81
+ : [TypedBodyProgrammer.generate(props)],
82
+ TypedBody: (props) =>
83
+ props.arguments.length
84
+ ? props.arguments
85
+ : [TypedBodyProgrammer.generate(props)],
86
+ TypedHeaders: (props) =>
87
+ props.arguments.length
88
+ ? props.arguments
89
+ : [TypedHeadersProgrammer.generate(props)],
90
+ TypedParam: (props) =>
91
+ props.arguments.length !== 1
92
+ ? props.arguments
93
+ : TypedParamProgrammer.generate(props),
94
+ TypedQuery: (props) =>
95
+ props.arguments.length
96
+ ? props.arguments
97
+ : [TypedQueryProgrammer.generate(props)],
98
+ "TypedQuery.Body": (props) =>
99
+ props.arguments.length
100
+ ? props.arguments
101
+ : [TypedQueryBodyProgrammer.generate(props)],
102
+ "TypedFormData.Body": (props) =>
103
+ props.arguments.length === 0
104
+ ? [
105
+ ts.factory.createIdentifier("undefined"),
106
+ TypedFormDataBodyProgrammer.generate(props),
107
+ ]
108
+ : props.arguments.length === 1
109
+ ? [props.arguments[0], TypedFormDataBodyProgrammer.generate(props)]
110
+ : props.arguments,
111
+ PlainBody: (props) =>
112
+ props.arguments.length
113
+ ? props.arguments
114
+ : [PlainBodyProgrammer.generate(props)],
115
+ "WebSocketRoute.Header": (props) =>
116
+ props.arguments.length
117
+ ? props.arguments
118
+ : [TypedBodyProgrammer.generate(props)],
119
+ "WebSocketRoute.Param": (props) =>
120
+ props.arguments.length !== 1
121
+ ? props.arguments
122
+ : TypedParamProgrammer.generate(props),
123
+ "WebSocketRoute.Query": (props) =>
124
+ props.arguments.length
125
+ ? props.arguments
126
+ : [TypedQueryProgrammer.generate(props)],
127
+ };
128
+
129
+ const LIB_PATH = path.join("@nestia", "core", "lib", "decorators");
130
+ const MONO_PATH = path.join("packages", "core", "lib", "decorators");
131
+
132
+ const getName = (symbol: ts.Symbol): string => {
133
+ const parent = symbol.getDeclarations()?.[0]?.parent;
134
+ return parent ? exploreName(parent)(symbol.escapedName.toString()) : "__type";
135
+ };
136
+ const exploreName =
137
+ (decl: ts.Node) =>
138
+ (name: string): string =>
139
+ ts.isModuleBlock(decl)
140
+ ? exploreName(decl.parent.parent)(
141
+ `${decl.parent.name.getFullText().trim()}.${name}`,
142
+ )
143
+ : name;
@@ -1,57 +1,57 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
- import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
5
-
6
- export namespace ParameterTransformer {
7
- export const transform = (props: {
8
- context: INestiaTransformContext;
9
- param: ts.ParameterDeclaration;
10
- }): ts.ParameterDeclaration => {
11
- // CHECK DECORATOR
12
- const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
13
- ? ts.getDecorators(props.param)
14
- : (props.param as any).decorators;
15
- if (!decorators?.length) return props.param;
16
-
17
- // GET TYPE INFO
18
- const type: ts.Type = props.context.checker.getTypeAtLocation(props.param);
19
-
20
- // WHEN LATEST TS VERSION
21
- if (ts.getDecorators !== undefined)
22
- return ts.factory.updateParameterDeclaration(
23
- props.param,
24
- (props.param.modifiers || []).map((mod) =>
25
- ts.isDecorator(mod)
26
- ? ParameterDecoratorTransformer.transform({
27
- context: props.context,
28
- decorator: mod,
29
- type,
30
- })
31
- : mod,
32
- ),
33
- props.param.dotDotDotToken,
34
- props.param.name,
35
- props.param.questionToken,
36
- props.param.type,
37
- props.param.initializer,
38
- );
39
- // eslint-disable-next-line
40
- return (ts.factory.updateParameterDeclaration as any)(
41
- props.param,
42
- decorators.map((deco) =>
43
- ParameterDecoratorTransformer.transform({
44
- context: props.context,
45
- decorator: deco,
46
- type,
47
- }),
48
- ),
49
- (props.param as any).modifiers,
50
- props.param.dotDotDotToken,
51
- props.param.name,
52
- props.param.questionToken,
53
- props.param.type,
54
- props.param.initializer,
55
- );
56
- };
57
- }
1
+ import ts from "typescript";
2
+
3
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
+ import { ParameterDecoratorTransformer } from "./ParameterDecoratorTransformer";
5
+
6
+ export namespace ParameterTransformer {
7
+ export const transform = (props: {
8
+ context: INestiaTransformContext;
9
+ param: ts.ParameterDeclaration;
10
+ }): ts.ParameterDeclaration => {
11
+ // CHECK DECORATOR
12
+ const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
13
+ ? ts.getDecorators(props.param)
14
+ : (props.param as any).decorators;
15
+ if (!decorators?.length) return props.param;
16
+
17
+ // GET TYPE INFO
18
+ const type: ts.Type = props.context.checker.getTypeAtLocation(props.param);
19
+
20
+ // WHEN LATEST TS VERSION
21
+ if (ts.getDecorators !== undefined)
22
+ return ts.factory.updateParameterDeclaration(
23
+ props.param,
24
+ (props.param.modifiers || []).map((mod) =>
25
+ ts.isDecorator(mod)
26
+ ? ParameterDecoratorTransformer.transform({
27
+ context: props.context,
28
+ decorator: mod,
29
+ type,
30
+ })
31
+ : mod,
32
+ ),
33
+ props.param.dotDotDotToken,
34
+ props.param.name,
35
+ props.param.questionToken,
36
+ props.param.type,
37
+ props.param.initializer,
38
+ );
39
+ // eslint-disable-next-line
40
+ return (ts.factory.updateParameterDeclaration as any)(
41
+ props.param,
42
+ decorators.map((deco) =>
43
+ ParameterDecoratorTransformer.transform({
44
+ context: props.context,
45
+ decorator: deco,
46
+ type,
47
+ }),
48
+ ),
49
+ (props.param as any).modifiers,
50
+ props.param.dotDotDotToken,
51
+ props.param.name,
52
+ props.param.questionToken,
53
+ props.param.type,
54
+ props.param.initializer,
55
+ );
56
+ };
57
+ }
@@ -1,85 +1,85 @@
1
- import path from "path";
2
- import ts from "typescript";
3
-
4
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
5
- import { TypedQueryRouteProgrammer } from "../programmers/TypedQueryRouteProgrammer";
6
- import { TypedRouteProgrammer } from "../programmers/TypedRouteProgrammer";
7
-
8
- export namespace TypedRouteTransformer {
9
- export const transform = (props: {
10
- context: INestiaTransformContext;
11
- decorator: ts.Decorator;
12
- type: ts.Type;
13
- }): ts.Decorator => {
14
- if (!ts.isCallExpression(props.decorator.expression))
15
- return props.decorator;
16
-
17
- // CHECK SIGNATURE
18
- const signature: ts.Signature | undefined =
19
- props.context.checker.getResolvedSignature(props.decorator.expression);
20
- if (!signature || !signature.declaration) return props.decorator;
21
-
22
- // CHECK TO BE TRANSFORMED
23
- const modulo = (() => {
24
- // CHECK FILENAME
25
- const location: string = path.resolve(
26
- signature.declaration.getSourceFile().fileName,
27
- );
28
- if (LIB_PATHS.every((str) => location.indexOf(str) === -1)) return null;
29
-
30
- // CHECK DUPLICATE BOOSTER
31
- if (props.decorator.expression.arguments.length >= 2) return false;
32
- else if (props.decorator.expression.arguments.length === 1) {
33
- const last: ts.Expression =
34
- props.decorator.expression.arguments[
35
- props.decorator.expression.arguments.length - 1
36
- ];
37
- const type: ts.Type = props.context.checker.getTypeAtLocation(last);
38
- if (isObject(props.context.checker)(type)) return false;
39
- }
40
- return location.split(path.sep).at(-1)?.split(".")[0] === "TypedQuery"
41
- ? "TypedQuery"
42
- : "TypedRoute";
43
- })();
44
- if (modulo === null) return props.decorator;
45
-
46
- // CHECK TYPE NODE
47
- const typeNode: ts.TypeNode | undefined =
48
- props.context.checker.typeToTypeNode(props.type, undefined, undefined);
49
- if (typeNode === undefined) return props.decorator;
50
-
51
- // DO TRANSFORM
52
- return ts.factory.createDecorator(
53
- ts.factory.updateCallExpression(
54
- props.decorator.expression,
55
- props.decorator.expression.expression,
56
- props.decorator.expression.typeArguments,
57
- [
58
- ...props.decorator.expression.arguments,
59
- (modulo === "TypedQuery"
60
- ? TypedQueryRouteProgrammer
61
- : TypedRouteProgrammer
62
- ).generate({
63
- context: props.context,
64
- type: props.type,
65
- modulo: props.decorator.expression.expression,
66
- }),
67
- ],
68
- ),
69
- );
70
- };
71
-
72
- const isObject =
73
- (checker: ts.TypeChecker) =>
74
- (type: ts.Type): boolean =>
75
- (type.getFlags() & ts.TypeFlags.Object) !== 0 &&
76
- !(checker as any).isTupleType(type) &&
77
- !(checker as any).isArrayType(type) &&
78
- !(checker as any).isArrayLikeType(type);
79
-
80
- const CLASSES: string[] = ["EncryptedRoute", "TypedRoute", "TypedQuery"];
81
- const LIB_PATHS: string[] = CLASSES.map((cla) => [
82
- path.join("@nestia", "core", "lib", "decorators", `${cla}.d.ts`),
83
- path.join("packages", "core", "lib", "decorators", `${cla}.d.ts`),
84
- ]).flat();
85
- }
1
+ import path from "path";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
5
+ import { TypedQueryRouteProgrammer } from "../programmers/TypedQueryRouteProgrammer";
6
+ import { TypedRouteProgrammer } from "../programmers/TypedRouteProgrammer";
7
+
8
+ export namespace TypedRouteTransformer {
9
+ export const transform = (props: {
10
+ context: INestiaTransformContext;
11
+ decorator: ts.Decorator;
12
+ type: ts.Type;
13
+ }): ts.Decorator => {
14
+ if (!ts.isCallExpression(props.decorator.expression))
15
+ return props.decorator;
16
+
17
+ // CHECK SIGNATURE
18
+ const signature: ts.Signature | undefined =
19
+ props.context.checker.getResolvedSignature(props.decorator.expression);
20
+ if (!signature || !signature.declaration) return props.decorator;
21
+
22
+ // CHECK TO BE TRANSFORMED
23
+ const modulo = (() => {
24
+ // CHECK FILENAME
25
+ const location: string = path.resolve(
26
+ signature.declaration.getSourceFile().fileName,
27
+ );
28
+ if (LIB_PATHS.every((str) => location.indexOf(str) === -1)) return null;
29
+
30
+ // CHECK DUPLICATE BOOSTER
31
+ if (props.decorator.expression.arguments.length >= 2) return false;
32
+ else if (props.decorator.expression.arguments.length === 1) {
33
+ const last: ts.Expression =
34
+ props.decorator.expression.arguments[
35
+ props.decorator.expression.arguments.length - 1
36
+ ];
37
+ const type: ts.Type = props.context.checker.getTypeAtLocation(last);
38
+ if (isObject(props.context.checker)(type)) return false;
39
+ }
40
+ return location.split(path.sep).at(-1)?.split(".")[0] === "TypedQuery"
41
+ ? "TypedQuery"
42
+ : "TypedRoute";
43
+ })();
44
+ if (modulo === null) return props.decorator;
45
+
46
+ // CHECK TYPE NODE
47
+ const typeNode: ts.TypeNode | undefined =
48
+ props.context.checker.typeToTypeNode(props.type, undefined, undefined);
49
+ if (typeNode === undefined) return props.decorator;
50
+
51
+ // DO TRANSFORM
52
+ return ts.factory.createDecorator(
53
+ ts.factory.updateCallExpression(
54
+ props.decorator.expression,
55
+ props.decorator.expression.expression,
56
+ props.decorator.expression.typeArguments,
57
+ [
58
+ ...props.decorator.expression.arguments,
59
+ (modulo === "TypedQuery"
60
+ ? TypedQueryRouteProgrammer
61
+ : TypedRouteProgrammer
62
+ ).generate({
63
+ context: props.context,
64
+ type: props.type,
65
+ modulo: props.decorator.expression.expression,
66
+ }),
67
+ ],
68
+ ),
69
+ );
70
+ };
71
+
72
+ const isObject =
73
+ (checker: ts.TypeChecker) =>
74
+ (type: ts.Type): boolean =>
75
+ (type.getFlags() & ts.TypeFlags.Object) !== 0 &&
76
+ !(checker as any).isTupleType(type) &&
77
+ !(checker as any).isArrayType(type) &&
78
+ !(checker as any).isArrayLikeType(type);
79
+
80
+ const CLASSES: string[] = ["EncryptedRoute", "TypedRoute", "TypedQuery"];
81
+ const LIB_PATHS: string[] = CLASSES.map((cla) => [
82
+ path.join("@nestia", "core", "lib", "decorators", `${cla}.d.ts`),
83
+ path.join("packages", "core", "lib", "decorators", `${cla}.d.ts`),
84
+ ]).flat();
85
+ }