@nestia/core 3.17.0 → 4.0.0-dev.20241010-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 (87) hide show
  1. package/README.md +0 -1
  2. package/lib/options/INestiaTransformProject.d.ts +2 -2
  3. package/lib/programmers/PlainBodyProgrammer.d.ts +6 -2
  4. package/lib/programmers/PlainBodyProgrammer.js +31 -13
  5. package/lib/programmers/PlainBodyProgrammer.js.map +1 -1
  6. package/lib/programmers/TypedBodyProgrammer.d.ts +6 -2
  7. package/lib/programmers/TypedBodyProgrammer.js +32 -13
  8. package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
  9. package/lib/programmers/TypedFormDataBodyProgrammer.d.ts +6 -2
  10. package/lib/programmers/TypedFormDataBodyProgrammer.js +30 -17
  11. package/lib/programmers/TypedFormDataBodyProgrammer.js.map +1 -1
  12. package/lib/programmers/TypedHeadersProgrammer.d.ts +6 -2
  13. package/lib/programmers/TypedHeadersProgrammer.js +12 -7
  14. package/lib/programmers/TypedHeadersProgrammer.js.map +1 -1
  15. package/lib/programmers/TypedParamProgrammer.d.ts +7 -2
  16. package/lib/programmers/TypedParamProgrammer.js +12 -7
  17. package/lib/programmers/TypedParamProgrammer.js.map +1 -1
  18. package/lib/programmers/TypedQueryBodyProgrammer.d.ts +6 -2
  19. package/lib/programmers/TypedQueryBodyProgrammer.js +12 -7
  20. package/lib/programmers/TypedQueryBodyProgrammer.js.map +1 -1
  21. package/lib/programmers/TypedQueryProgrammer.d.ts +6 -2
  22. package/lib/programmers/TypedQueryProgrammer.js +13 -7
  23. package/lib/programmers/TypedQueryProgrammer.js.map +1 -1
  24. package/lib/programmers/TypedQueryRouteProgrammer.d.ts +6 -2
  25. package/lib/programmers/TypedQueryRouteProgrammer.js +10 -6
  26. package/lib/programmers/TypedQueryRouteProgrammer.js.map +1 -1
  27. package/lib/programmers/TypedRouteProgrammer.d.ts +6 -2
  28. package/lib/programmers/TypedRouteProgrammer.js +14 -9
  29. package/lib/programmers/TypedRouteProgrammer.js.map +1 -1
  30. package/lib/programmers/http/HttpAssertQuerifyProgrammer.d.ts +6 -2
  31. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js +22 -3
  32. package/lib/programmers/http/HttpAssertQuerifyProgrammer.js.map +1 -1
  33. package/lib/programmers/http/HttpIsQuerifyProgrammer.d.ts +6 -2
  34. package/lib/programmers/http/HttpIsQuerifyProgrammer.js +21 -3
  35. package/lib/programmers/http/HttpIsQuerifyProgrammer.js.map +1 -1
  36. package/lib/programmers/http/HttpQuerifyProgrammer.d.ts +6 -2
  37. package/lib/programmers/http/HttpQuerifyProgrammer.js +27 -15
  38. package/lib/programmers/http/HttpQuerifyProgrammer.js.map +1 -1
  39. package/lib/programmers/http/HttpValidateQuerifyProgrammer.d.ts +6 -2
  40. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js +25 -4
  41. package/lib/programmers/http/HttpValidateQuerifyProgrammer.js.map +1 -1
  42. package/lib/transform.d.ts +2 -2
  43. package/lib/transform.js.map +1 -1
  44. package/lib/transformers/FileTransformer.d.ts +2 -2
  45. package/lib/transformers/FileTransformer.js +24 -10
  46. package/lib/transformers/FileTransformer.js.map +1 -1
  47. package/lib/transformers/MethodTransformer.d.ts +5 -2
  48. package/lib/transformers/MethodTransformer.js +21 -14
  49. package/lib/transformers/MethodTransformer.js.map +1 -1
  50. package/lib/transformers/NodeTransformer.d.ts +5 -2
  51. package/lib/transformers/NodeTransformer.js +11 -5
  52. package/lib/transformers/NodeTransformer.js.map +1 -1
  53. package/lib/transformers/ParameterDecoratorTransformer.d.ts +6 -2
  54. package/lib/transformers/ParameterDecoratorTransformer.js +49 -40
  55. package/lib/transformers/ParameterDecoratorTransformer.js.map +1 -1
  56. package/lib/transformers/ParameterTransformer.d.ts +5 -2
  57. package/lib/transformers/ParameterTransformer.js +17 -9
  58. package/lib/transformers/ParameterTransformer.js.map +1 -1
  59. package/lib/transformers/TypedRouteTransformer.d.ts +6 -2
  60. package/lib/transformers/TypedRouteTransformer.js +20 -16
  61. package/lib/transformers/TypedRouteTransformer.js.map +1 -1
  62. package/lib/transformers/WebSocketRouteTransformer.d.ts +6 -2
  63. package/lib/transformers/WebSocketRouteTransformer.js +11 -11
  64. package/lib/transformers/WebSocketRouteTransformer.js.map +1 -1
  65. package/package.json +5 -5
  66. package/src/options/INestiaTransformProject.ts +3 -2
  67. package/src/programmers/PlainBodyProgrammer.ts +32 -13
  68. package/src/programmers/TypedBodyProgrammer.ts +106 -82
  69. package/src/programmers/TypedFormDataBodyProgrammer.ts +87 -77
  70. package/src/programmers/TypedHeadersProgrammer.ts +48 -41
  71. package/src/programmers/TypedParamProgrammer.ts +21 -15
  72. package/src/programmers/TypedQueryBodyProgrammer.ts +48 -41
  73. package/src/programmers/TypedQueryProgrammer.ts +50 -45
  74. package/src/programmers/TypedQueryRouteProgrammer.ts +44 -39
  75. package/src/programmers/TypedRouteProgrammer.ts +63 -61
  76. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +54 -40
  77. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +54 -41
  78. package/src/programmers/http/HttpQuerifyProgrammer.ts +60 -48
  79. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +54 -41
  80. package/src/transform.ts +2 -2
  81. package/src/transformers/FileTransformer.ts +68 -46
  82. package/src/transformers/MethodTransformer.ts +56 -51
  83. package/src/transformers/NodeTransformer.ts +16 -9
  84. package/src/transformers/ParameterDecoratorTransformer.ts +100 -91
  85. package/src/transformers/ParameterTransformer.ts +47 -38
  86. package/src/transformers/TypedRouteTransformer.ts +58 -55
  87. package/src/transformers/WebSocketRouteTransformer.ts +81 -78
@@ -2,61 +2,74 @@ import ts from "typescript";
2
2
  import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
3
3
  import { StatementFactory } from "typia/lib/factories/StatementFactory";
4
4
  import { IsProgrammer } from "typia/lib/programmers/IsProgrammer";
5
- import { IProject } from "typia/lib/transformers/IProject";
5
+ import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
6
6
 
7
7
  import { HttpQuerifyProgrammer } from "./HttpQuerifyProgrammer";
8
8
 
9
9
  export namespace HttpIsQuerifyProgrammer {
10
- export const write =
11
- (project: IProject) =>
12
- (modulo: ts.LeftHandSideExpression) =>
13
- (type: ts.Type): ts.ArrowFunction =>
14
- ts.factory.createArrowFunction(
15
- undefined,
16
- undefined,
17
- [IdentifierFactory.parameter("input")],
18
- undefined,
19
- undefined,
20
- ts.factory.createBlock([
21
- StatementFactory.constant(
22
- "is",
23
- IsProgrammer.write({
24
- ...project,
10
+ export const write = (props: {
11
+ context: ITypiaContext;
12
+ modulo: ts.LeftHandSideExpression;
13
+ type: ts.Type;
14
+ }): ts.ArrowFunction =>
15
+ ts.factory.createArrowFunction(
16
+ undefined,
17
+ undefined,
18
+ [IdentifierFactory.parameter("input")],
19
+ undefined,
20
+ undefined,
21
+ ts.factory.createBlock([
22
+ StatementFactory.constant({
23
+ name: "is",
24
+ value: IsProgrammer.write({
25
+ config: {
26
+ equals: false,
27
+ },
28
+ context: {
29
+ ...props.context,
25
30
  options: {
26
- ...project.options,
31
+ ...props.context.options,
27
32
  functional: false,
28
33
  numeric: false,
29
34
  },
30
- })(modulo)(false)(type),
31
- ),
32
- StatementFactory.constant(
33
- "stringify",
34
- HttpQuerifyProgrammer.write({
35
- ...project,
35
+ },
36
+ modulo: props.modulo,
37
+ type: props.type,
38
+ name: undefined,
39
+ }),
40
+ }),
41
+ StatementFactory.constant({
42
+ name: "stringify",
43
+ value: HttpQuerifyProgrammer.write({
44
+ context: {
45
+ ...props.context,
36
46
  options: {
37
- ...project.options,
47
+ ...props.context.options,
38
48
  functional: false,
39
49
  numeric: false,
40
50
  },
41
- })(modulo)(type),
42
- ),
43
- ts.factory.createReturnStatement(
44
- ts.factory.createConditionalExpression(
45
- ts.factory.createCallExpression(
46
- ts.factory.createIdentifier("is"),
47
- undefined,
48
- [ts.factory.createIdentifier("input")],
49
- ),
51
+ },
52
+ modulo: props.modulo,
53
+ type: props.type,
54
+ }),
55
+ }),
56
+ ts.factory.createReturnStatement(
57
+ ts.factory.createConditionalExpression(
58
+ ts.factory.createCallExpression(
59
+ ts.factory.createIdentifier("is"),
50
60
  undefined,
51
- ts.factory.createCallExpression(
52
- ts.factory.createIdentifier("stringify"),
53
- undefined,
54
- [ts.factory.createIdentifier("input")],
55
- ),
61
+ [ts.factory.createIdentifier("input")],
62
+ ),
63
+ undefined,
64
+ ts.factory.createCallExpression(
65
+ ts.factory.createIdentifier("stringify"),
56
66
  undefined,
57
- ts.factory.createNull(),
67
+ [ts.factory.createIdentifier("input")],
58
68
  ),
69
+ undefined,
70
+ ts.factory.createNull(),
59
71
  ),
60
- ]),
61
- );
72
+ ),
73
+ ]),
74
+ );
62
75
  }
@@ -3,63 +3,73 @@ import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
3
3
  import { MetadataCollection } from "typia/lib/factories/MetadataCollection";
4
4
  import { MetadataFactory } from "typia/lib/factories/MetadataFactory";
5
5
  import { StatementFactory } from "typia/lib/factories/StatementFactory";
6
- import { FunctionImporter } from "typia/lib/programmers/helpers/FunctionImporeter";
6
+ import { FunctionProgrammer } from "typia/lib/programmers/helpers/FunctionProgrammer";
7
7
  import { HttpQueryProgrammer } from "typia/lib/programmers/http/HttpQueryProgrammer";
8
8
  import { Metadata } from "typia/lib/schemas/metadata/Metadata";
9
9
  import { MetadataObject } from "typia/lib/schemas/metadata/MetadataObject";
10
- import { IProject } from "typia/lib/transformers/IProject";
10
+ import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
11
11
  import { TransformerError } from "typia/lib/transformers/TransformerError";
12
12
 
13
13
  export namespace HttpQuerifyProgrammer {
14
- export const write =
15
- (project: IProject) =>
16
- (modulo: ts.LeftHandSideExpression) =>
17
- (type: ts.Type): ts.ArrowFunction => {
18
- // GET OBJECT TYPE
19
- const importer: FunctionImporter = new FunctionImporter(modulo.getText());
20
- const collection: MetadataCollection = new MetadataCollection();
21
- const result = MetadataFactory.analyze(project.checker)({
14
+ export const write = (props: {
15
+ context: ITypiaContext;
16
+ modulo: ts.LeftHandSideExpression;
17
+ type: ts.Type;
18
+ }): ts.ArrowFunction => {
19
+ // GET OBJECT TYPE
20
+ const functor: FunctionProgrammer = new FunctionProgrammer(
21
+ props.modulo.getText(),
22
+ );
23
+ const collection: MetadataCollection = new MetadataCollection();
24
+ const result = MetadataFactory.analyze({
25
+ checker: props.context.checker,
26
+ transformer: props.context.transformer,
27
+ options: {
22
28
  escape: false,
23
29
  constant: true,
24
30
  absorb: true,
25
31
  validate: HttpQueryProgrammer.validate,
26
- })(collection)(type);
27
- if (result.success === false)
28
- throw TransformerError.from(
29
- `nestia.core.TypedQuery.${importer.method}`,
30
- )(result.errors);
32
+ },
33
+ type: props.type,
34
+ collection,
35
+ });
36
+ if (result.success === false)
37
+ throw TransformerError.from({
38
+ code: `nestia.core.TypedQuery.${functor.method}`,
39
+ errors: result.errors,
40
+ });
31
41
 
32
- const object: MetadataObject = result.data.objects[0]!;
33
- return ts.factory.createArrowFunction(
34
- undefined,
35
- undefined,
36
- [IdentifierFactory.parameter("input")],
37
- undefined,
38
- undefined,
39
- ts.factory.createBlock(
40
- [
41
- ...importer.declare(modulo),
42
- StatementFactory.constant(
43
- "output",
44
- ts.factory.createNewExpression(
45
- ts.factory.createIdentifier("URLSearchParams"),
46
- undefined,
47
- [],
48
- ),
49
- ),
50
- ...object.properties.map((p) =>
51
- ts.factory.createExpressionStatement(
52
- decode(p.key.constants[0]!.values[0].value as string)(p.value),
53
- ),
42
+ const object: MetadataObject = result.data.objects[0]!;
43
+ return ts.factory.createArrowFunction(
44
+ undefined,
45
+ undefined,
46
+ [IdentifierFactory.parameter("input")],
47
+ undefined,
48
+ undefined,
49
+ ts.factory.createBlock(
50
+ [
51
+ ...functor.declare(),
52
+ StatementFactory.constant({
53
+ name: "output",
54
+ value: ts.factory.createNewExpression(
55
+ ts.factory.createIdentifier("URLSearchParams"),
56
+ undefined,
57
+ [],
54
58
  ),
55
- ts.factory.createReturnStatement(
56
- ts.factory.createIdentifier("output"),
59
+ }),
60
+ ...object.properties.map((p) =>
61
+ ts.factory.createExpressionStatement(
62
+ decode(p.key.constants[0]!.values[0].value as string)(p.value),
57
63
  ),
58
- ],
59
- true,
60
- ),
61
- );
62
- };
64
+ ),
65
+ ts.factory.createReturnStatement(
66
+ ts.factory.createIdentifier("output"),
67
+ ),
68
+ ],
69
+ true,
70
+ ),
71
+ );
72
+ };
63
73
 
64
74
  const decode =
65
75
  (key: string) =>
@@ -67,10 +77,12 @@ export namespace HttpQuerifyProgrammer {
67
77
  !!value.arrays.length
68
78
  ? ts.factory.createCallExpression(
69
79
  IdentifierFactory.access(
70
- IdentifierFactory.access(ts.factory.createIdentifier("input"))(
80
+ IdentifierFactory.access(
81
+ ts.factory.createIdentifier("input"),
71
82
  key,
72
83
  ),
73
- )("forEach"),
84
+ "forEach",
85
+ ),
74
86
  undefined,
75
87
  [
76
88
  ts.factory.createArrowFunction(
@@ -84,12 +96,12 @@ export namespace HttpQuerifyProgrammer {
84
96
  ],
85
97
  )
86
98
  : append(key)(
87
- IdentifierFactory.access(ts.factory.createIdentifier("input"))(key),
99
+ IdentifierFactory.access(ts.factory.createIdentifier("input"), key),
88
100
  );
89
101
 
90
102
  const append = (key: string) => (elem: ts.Expression) =>
91
103
  ts.factory.createCallExpression(
92
- IdentifierFactory.access(ts.factory.createIdentifier("output"))("append"),
104
+ IdentifierFactory.access(ts.factory.createIdentifier("output"), "append"),
93
105
  undefined,
94
106
  [ts.factory.createStringLiteral(key), elem],
95
107
  );
@@ -2,62 +2,75 @@ import ts from "typescript";
2
2
  import { IdentifierFactory } from "typia/lib/factories/IdentifierFactory";
3
3
  import { StatementFactory } from "typia/lib/factories/StatementFactory";
4
4
  import { ValidateProgrammer } from "typia/lib/programmers/ValidateProgrammer";
5
- import { IProject } from "typia/lib/transformers/IProject";
5
+ import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
6
6
 
7
7
  import { HttpQuerifyProgrammer } from "./HttpQuerifyProgrammer";
8
8
 
9
9
  export namespace HttpValidateQuerifyProgrammer {
10
- export const write =
11
- (project: IProject) =>
12
- (modulo: ts.LeftHandSideExpression) =>
13
- (type: ts.Type, name?: string): ts.ArrowFunction =>
14
- ts.factory.createArrowFunction(
15
- undefined,
16
- undefined,
17
- [IdentifierFactory.parameter("input")],
18
- undefined,
19
- undefined,
20
- ts.factory.createBlock([
21
- StatementFactory.constant(
22
- "validate",
23
- ValidateProgrammer.write({
24
- ...project,
10
+ export const write = (props: {
11
+ context: ITypiaContext;
12
+ modulo: ts.LeftHandSideExpression;
13
+ type: ts.Type;
14
+ }): ts.ArrowFunction =>
15
+ ts.factory.createArrowFunction(
16
+ undefined,
17
+ undefined,
18
+ [IdentifierFactory.parameter("input")],
19
+ undefined,
20
+ undefined,
21
+ ts.factory.createBlock([
22
+ StatementFactory.constant({
23
+ name: "validate",
24
+ value: ValidateProgrammer.write({
25
+ config: {
26
+ equals: false,
27
+ },
28
+ context: {
29
+ ...props.context,
25
30
  options: {
26
- ...project.options,
31
+ ...props.context.options,
27
32
  functional: false,
28
33
  numeric: true,
29
34
  },
30
- })(modulo)(false)(type, name),
31
- ),
32
- StatementFactory.constant(
33
- "query",
34
- HttpQuerifyProgrammer.write({
35
- ...project,
35
+ },
36
+ modulo: props.modulo,
37
+ type: props.type,
38
+ name: undefined,
39
+ }),
40
+ }),
41
+ StatementFactory.constant({
42
+ name: "query",
43
+ value: HttpQuerifyProgrammer.write({
44
+ context: {
45
+ ...props.context,
36
46
  options: {
37
- ...project.options,
47
+ ...props.context.options,
38
48
  functional: false,
39
49
  numeric: false,
40
50
  },
41
- })(modulo)(type),
51
+ },
52
+ modulo: props.modulo,
53
+ type: props.type,
54
+ }),
55
+ }),
56
+ StatementFactory.constant({
57
+ name: "output",
58
+ value: ts.factory.createCallExpression(
59
+ ts.factory.createIdentifier("query"),
60
+ undefined,
61
+ [ts.factory.createIdentifier("input")],
42
62
  ),
43
- StatementFactory.constant(
44
- "output",
63
+ }),
64
+ ts.factory.createReturnStatement(
65
+ ts.factory.createAsExpression(
45
66
  ts.factory.createCallExpression(
46
- ts.factory.createIdentifier("query"),
67
+ ts.factory.createIdentifier("validate"),
47
68
  undefined,
48
- [ts.factory.createIdentifier("input")],
49
- ),
50
- ),
51
- ts.factory.createReturnStatement(
52
- ts.factory.createAsExpression(
53
- ts.factory.createCallExpression(
54
- ts.factory.createIdentifier("validate"),
55
- undefined,
56
- [ts.factory.createIdentifier("output")],
57
- ),
58
- ts.factory.createTypeReferenceNode("any"),
69
+ [ts.factory.createIdentifier("output")],
59
70
  ),
71
+ ts.factory.createTypeReferenceNode("any"),
60
72
  ),
61
- ]),
62
- );
73
+ ),
74
+ ]),
75
+ );
63
76
  }
package/src/transform.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import ts from "typescript";
2
- import { IProject } from "typia/lib/transformers/IProject";
2
+ import { ITypiaContext } from "typia/lib/transformers/ITypiaContext";
3
3
 
4
4
  import { INestiaTransformOptions } from "./options/INestiaTransformOptions";
5
5
  import { FileTransformer } from "./transformers/FileTransformer";
@@ -7,7 +7,7 @@ import { FileTransformer } from "./transformers/FileTransformer";
7
7
  export const transform = (
8
8
  program: ts.Program,
9
9
  options: INestiaTransformOptions | undefined,
10
- extras: IProject["extras"],
10
+ extras: ITypiaContext["extras"],
11
11
  ): ts.TransformerFactory<ts.SourceFile> => {
12
12
  const compilerOptions: ts.CompilerOptions = program.getCompilerOptions();
13
13
  const strict: boolean =
@@ -1,61 +1,83 @@
1
1
  import ts from "typescript";
2
+ import { ImportProgrammer } from "typia/lib/programmers/ImportProgrammer";
2
3
  import { TransformerError } from "typia/lib/transformers/TransformerError";
3
4
 
4
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
5
6
  import { NodeTransformer } from "./NodeTransformer";
6
7
 
7
8
  export namespace FileTransformer {
8
9
  export const transform =
9
- (project: Omit<INestiaTransformProject, "context">) =>
10
- (context: ts.TransformationContext) =>
11
- (file: ts.SourceFile): ts.SourceFile =>
12
- file.isDeclarationFile
13
- ? file
14
- : ts.visitEachChild(
10
+ (context: Omit<INestiaTransformContext, "importer" | "transformer">) =>
11
+ (transformer: ts.TransformationContext) =>
12
+ (file: ts.SourceFile): ts.SourceFile => {
13
+ if (file.isDeclarationFile) return file;
14
+ const importer = new ImportProgrammer();
15
+ file = ts.visitEachChild(
16
+ file,
17
+ (node) =>
18
+ iterate_node({
19
+ context: {
20
+ ...context,
21
+ transformer,
22
+ importer,
23
+ },
15
24
  file,
16
- (node) =>
17
- iterate_node({
18
- ...project,
19
- context,
20
- })(context)(file)(node),
21
- context,
22
- );
23
-
24
- const iterate_node =
25
- (project: INestiaTransformProject) =>
26
- (context: ts.TransformationContext) =>
27
- (file: ts.SourceFile) =>
28
- (node: ts.Node): ts.Node =>
29
- ts.visitEachChild(
30
- try_transform_node(project)(file)(node) ?? node,
31
- (child) => iterate_node(project)(context)(file)(child),
32
- context,
25
+ node,
26
+ }),
27
+ transformer,
28
+ );
29
+ return ts.factory.updateSourceFile(
30
+ file,
31
+ [...importer.toStatements(), ...file.statements],
32
+ false,
33
+ file.referencedFiles,
34
+ file.typeReferenceDirectives,
35
+ file.hasNoDefaultLib,
36
+ file.libReferenceDirectives,
33
37
  );
38
+ };
34
39
 
35
- const try_transform_node =
36
- (project: INestiaTransformProject) =>
37
- (file: ts.SourceFile) =>
38
- (node: ts.Node): ts.Node | null => {
39
- try {
40
- return NodeTransformer.transform(project)(node);
41
- } catch (exp) {
42
- // ONLY ACCEPT TRANSFORMER-ERROR
43
- if (!isTransformerError(exp)) throw exp;
40
+ const iterate_node = (props: {
41
+ context: INestiaTransformContext;
42
+ file: ts.SourceFile;
43
+ node: ts.Node;
44
+ }): ts.Node =>
45
+ ts.visitEachChild(
46
+ try_transform_node(props) ?? props.node,
47
+ (child) =>
48
+ iterate_node({
49
+ context: props.context,
50
+ file: props.file,
51
+ node: child,
52
+ }),
53
+ props.context.transformer,
54
+ );
44
55
 
45
- // AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
46
- (node as any).parent ??= file;
56
+ const try_transform_node = (props: {
57
+ context: INestiaTransformContext;
58
+ file: ts.SourceFile;
59
+ node: ts.Node;
60
+ }): ts.Node | null => {
61
+ try {
62
+ return NodeTransformer.transform(props);
63
+ } catch (exp) {
64
+ // ONLY ACCEPT TRANSFORMER-ERROR
65
+ if (!isTransformerError(exp)) throw exp;
47
66
 
48
- // REPORT DIAGNOSTIC
49
- const diagnostic = (ts as any).createDiagnosticForNode(node, {
50
- key: exp.code,
51
- category: ts.DiagnosticCategory.Error,
52
- message: exp.message,
53
- code: `(${exp.code})` as any,
54
- });
55
- project.extras.addDiagnostic(diagnostic);
56
- return null;
57
- }
58
- };
67
+ // AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
68
+ (props.node as any).parent ??= props.file;
69
+
70
+ // REPORT DIAGNOSTIC
71
+ const diagnostic = (ts as any).createDiagnosticForNode(props.node, {
72
+ key: exp.code,
73
+ category: ts.DiagnosticCategory.Error,
74
+ message: exp.message,
75
+ code: `(${exp.code})` as any,
76
+ });
77
+ props.context.extras.addDiagnostic(diagnostic);
78
+ return null;
79
+ }
80
+ };
59
81
  }
60
82
 
61
83
  const isTransformerError = (error: any): error is TransformerError =>
@@ -1,64 +1,69 @@
1
1
  import ts from "typescript";
2
2
 
3
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
3
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
4
  import { TypedRouteTransformer } from "./TypedRouteTransformer";
5
5
  import { WebSocketRouteTransformer } from "./WebSocketRouteTransformer";
6
6
 
7
7
  export namespace MethodTransformer {
8
- export const transform =
9
- (project: INestiaTransformProject) =>
10
- (method: ts.MethodDeclaration): ts.MethodDeclaration => {
11
- const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
12
- ? ts.getDecorators(method)
13
- : (method as any).decorators;
14
- if (!decorators?.length) return method;
8
+ export const transform = (props: {
9
+ context: INestiaTransformContext;
10
+ method: ts.MethodDeclaration;
11
+ }): ts.MethodDeclaration => {
12
+ const decorators: readonly ts.Decorator[] | undefined = ts.getDecorators
13
+ ? ts.getDecorators(props.method)
14
+ : (props.method as any).decorators;
15
+ if (!decorators?.length) return props.method;
15
16
 
16
- const signature: ts.Signature | undefined =
17
- project.checker.getSignatureFromDeclaration(method);
18
- const original: ts.Type | undefined =
19
- signature && project.checker.getReturnTypeOfSignature(signature);
20
- const escaped: ts.Type | undefined =
21
- original && get_escaped_type(project.checker)(original);
17
+ const signature: ts.Signature | undefined =
18
+ props.context.checker.getSignatureFromDeclaration(props.method);
19
+ const original: ts.Type | undefined =
20
+ signature && props.context.checker.getReturnTypeOfSignature(signature);
21
+ const type: ts.Type | undefined =
22
+ original && get_escaped_type(props.context.checker)(original);
22
23
 
23
- if (escaped === undefined) return method;
24
+ if (type === undefined) return props.method;
24
25
 
25
- const operator = (decorator: ts.Decorator): ts.Decorator => {
26
- decorator =
27
- TypedRouteTransformer.transform(project)(escaped)(decorator);
28
- decorator = WebSocketRouteTransformer.validate(project)(
29
- decorator,
30
- method,
31
- );
32
- return decorator;
33
- };
34
- if (ts.getDecorators !== undefined)
35
- return ts.factory.updateMethodDeclaration(
36
- method,
37
- (method.modifiers || []).map((mod) =>
38
- ts.isDecorator(mod) ? operator(mod) : mod,
39
- ),
40
- method.asteriskToken,
41
- method.name,
42
- method.questionToken,
43
- method.typeParameters,
44
- method.parameters,
45
- method.type,
46
- method.body,
47
- );
48
- // eslint-disable-next-line
49
- return (ts.factory.updateMethodDeclaration as any)(
50
- method,
51
- decorators.map(operator),
52
- (method as any).modifiers,
53
- method.asteriskToken,
54
- method.name,
55
- method.questionToken,
56
- method.typeParameters,
57
- method.parameters,
58
- method.type,
59
- method.body,
60
- );
26
+ const operator = (decorator: ts.Decorator): ts.Decorator => {
27
+ decorator = TypedRouteTransformer.transform({
28
+ context: props.context,
29
+ decorator,
30
+ type,
31
+ });
32
+ decorator = WebSocketRouteTransformer.validate({
33
+ context: props.context,
34
+ method: props.method,
35
+ decorator,
36
+ });
37
+ return decorator;
61
38
  };
39
+ if (ts.getDecorators !== undefined)
40
+ return ts.factory.updateMethodDeclaration(
41
+ props.method,
42
+ (props.method.modifiers || []).map((mod) =>
43
+ ts.isDecorator(mod) ? operator(mod) : mod,
44
+ ),
45
+ props.method.asteriskToken,
46
+ props.method.name,
47
+ props.method.questionToken,
48
+ props.method.typeParameters,
49
+ props.method.parameters,
50
+ props.method.type,
51
+ props.method.body,
52
+ );
53
+ // eslint-disable-next-line
54
+ return (ts.factory.updateMethodDeclaration as any)(
55
+ props.method,
56
+ decorators.map(operator),
57
+ (props.method as any).modifiers,
58
+ props.method.asteriskToken,
59
+ props.method.name,
60
+ props.method.questionToken,
61
+ props.method.typeParameters,
62
+ props.method.parameters,
63
+ props.method.type,
64
+ props.method.body,
65
+ );
66
+ };
62
67
  }
63
68
 
64
69
  const get_escaped_type =