@nestia/core 4.6.2 → 5.0.0-dev.20250301

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 (77) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +91 -92
  3. package/lib/programmers/TypedBodyProgrammer.js +4 -4
  4. package/lib/programmers/TypedBodyProgrammer.js.map +1 -1
  5. package/package.json +7 -7
  6. package/src/adaptors/WebSocketAdaptor.ts +429 -429
  7. package/src/decorators/DynamicModule.ts +43 -43
  8. package/src/decorators/EncryptedBody.ts +101 -101
  9. package/src/decorators/EncryptedController.ts +38 -38
  10. package/src/decorators/EncryptedModule.ts +100 -100
  11. package/src/decorators/EncryptedRoute.ts +219 -219
  12. package/src/decorators/HumanRoute.ts +22 -22
  13. package/src/decorators/NoTransformConfigurationError.ts +32 -32
  14. package/src/decorators/PlainBody.ts +79 -79
  15. package/src/decorators/SwaggerCustomizer.ts +115 -115
  16. package/src/decorators/SwaggerExample.ts +100 -100
  17. package/src/decorators/TypedBody.ts +59 -59
  18. package/src/decorators/TypedException.ts +166 -166
  19. package/src/decorators/TypedFormData.ts +195 -195
  20. package/src/decorators/TypedHeaders.ts +64 -64
  21. package/src/decorators/TypedParam.ts +77 -77
  22. package/src/decorators/TypedQuery.ts +245 -245
  23. package/src/decorators/TypedRoute.ts +214 -214
  24. package/src/decorators/WebSocketRoute.ts +242 -242
  25. package/src/decorators/internal/EncryptedConstant.ts +4 -4
  26. package/src/decorators/internal/IWebSocketRouteReflect.ts +23 -23
  27. package/src/decorators/internal/NoTransformConfigureError.ts +2 -2
  28. package/src/decorators/internal/get_path_and_querify.ts +108 -108
  29. package/src/decorators/internal/get_path_and_stringify.ts +122 -122
  30. package/src/decorators/internal/get_text_body.ts +20 -20
  31. package/src/decorators/internal/headers_to_object.ts +13 -13
  32. package/src/decorators/internal/is_request_body_undefined.ts +14 -14
  33. package/src/decorators/internal/load_controller.ts +49 -49
  34. package/src/decorators/internal/route_error.ts +45 -45
  35. package/src/decorators/internal/validate_request_body.ts +74 -74
  36. package/src/decorators/internal/validate_request_form_data.ts +77 -77
  37. package/src/decorators/internal/validate_request_headers.ts +86 -86
  38. package/src/decorators/internal/validate_request_query.ts +74 -74
  39. package/src/index.ts +5 -5
  40. package/src/module.ts +22 -22
  41. package/src/options/INestiaTransformOptions.ts +38 -38
  42. package/src/options/INestiaTransformProject.ts +8 -8
  43. package/src/options/IRequestBodyValidator.ts +20 -20
  44. package/src/options/IRequestFormDataProps.ts +27 -27
  45. package/src/options/IRequestHeadersValidator.ts +22 -22
  46. package/src/options/IRequestQueryValidator.ts +20 -20
  47. package/src/options/IResponseBodyQuerifier.ts +25 -25
  48. package/src/options/IResponseBodyStringifier.ts +30 -30
  49. package/src/programmers/PlainBodyProgrammer.ts +70 -70
  50. package/src/programmers/TypedBodyProgrammer.ts +142 -142
  51. package/src/programmers/TypedFormDataBodyProgrammer.ts +118 -118
  52. package/src/programmers/TypedHeadersProgrammer.ts +63 -63
  53. package/src/programmers/TypedParamProgrammer.ts +33 -33
  54. package/src/programmers/TypedQueryBodyProgrammer.ts +112 -112
  55. package/src/programmers/TypedQueryProgrammer.ts +114 -114
  56. package/src/programmers/TypedQueryRouteProgrammer.ts +105 -105
  57. package/src/programmers/TypedRouteProgrammer.ts +94 -94
  58. package/src/programmers/http/HttpAssertQuerifyProgrammer.ts +72 -72
  59. package/src/programmers/http/HttpIsQuerifyProgrammer.ts +75 -75
  60. package/src/programmers/http/HttpQuerifyProgrammer.ts +108 -108
  61. package/src/programmers/http/HttpValidateQuerifyProgrammer.ts +76 -76
  62. package/src/programmers/internal/CoreMetadataUtil.ts +21 -21
  63. package/src/transform.ts +35 -35
  64. package/src/transformers/FileTransformer.ts +110 -110
  65. package/src/transformers/MethodTransformer.ts +103 -103
  66. package/src/transformers/NodeTransformer.ts +23 -23
  67. package/src/transformers/ParameterDecoratorTransformer.ts +143 -143
  68. package/src/transformers/ParameterTransformer.ts +57 -57
  69. package/src/transformers/TypedRouteTransformer.ts +85 -85
  70. package/src/transformers/WebSocketRouteTransformer.ts +120 -120
  71. package/src/typings/Creator.ts +3 -3
  72. package/src/typings/get-function-location.d.ts +7 -7
  73. package/src/utils/ArrayUtil.ts +7 -7
  74. package/src/utils/ExceptionManager.ts +112 -112
  75. package/src/utils/Singleton.ts +20 -20
  76. package/src/utils/SourceFinder.ts +57 -57
  77. package/src/utils/VersioningStrategy.ts +27 -27
@@ -1,110 +1,110 @@
1
- import ts from "typescript";
2
- import { ImportProgrammer } from "typia/lib/programmers/ImportProgrammer";
3
- import { TransformerError } from "typia/lib/transformers/TransformerError";
4
-
5
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
6
- import { NodeTransformer } from "./NodeTransformer";
7
-
8
- export namespace FileTransformer {
9
- export const transform =
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
- internalPrefix: "nestia_core_transform",
16
- });
17
- file = ts.visitEachChild(
18
- file,
19
- (node) =>
20
- iterate_node({
21
- context: {
22
- ...context,
23
- transformer,
24
- importer,
25
- },
26
- file,
27
- node,
28
- }),
29
- transformer,
30
- );
31
- const index: number = find_import_injection_index(file);
32
- return ts.factory.updateSourceFile(
33
- file,
34
- [
35
- ...file.statements.slice(0, index),
36
- ...importer.toStatements(),
37
- ...file.statements.slice(index),
38
- ],
39
- false,
40
- file.referencedFiles,
41
- file.typeReferenceDirectives,
42
- file.hasNoDefaultLib,
43
- file.libReferenceDirectives,
44
- );
45
- };
46
-
47
- const iterate_node = (props: {
48
- context: INestiaTransformContext;
49
- file: ts.SourceFile;
50
- node: ts.Node;
51
- }): ts.Node =>
52
- ts.visitEachChild(
53
- try_transform_node(props) ?? props.node,
54
- (child) =>
55
- iterate_node({
56
- context: props.context,
57
- file: props.file,
58
- node: child,
59
- }),
60
- props.context.transformer,
61
- );
62
-
63
- const try_transform_node = (props: {
64
- context: INestiaTransformContext;
65
- file: ts.SourceFile;
66
- node: ts.Node;
67
- }): ts.Node | null => {
68
- try {
69
- return NodeTransformer.transform(props);
70
- } catch (exp) {
71
- // ONLY ACCEPT TRANSFORMER-ERROR
72
- if (!isTransformerError(exp)) throw exp;
73
-
74
- // AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
75
- (props.node as any).parent ??= props.file;
76
-
77
- // REPORT DIAGNOSTIC
78
- const diagnostic = (ts as any).createDiagnosticForNode(props.node, {
79
- key: exp.code,
80
- category: ts.DiagnosticCategory.Error,
81
- message: exp.message,
82
- code: `(${exp.code})` as any,
83
- });
84
- props.context.extras.addDiagnostic(diagnostic);
85
- return null;
86
- }
87
- };
88
-
89
- const find_import_injection_index = (file: ts.SourceFile): number => {
90
- let i: number = 0;
91
- for (; i < file.statements.length; ++i) {
92
- const stmt: ts.Statement = file.statements[i]!;
93
- if (
94
- ts.isExpressionStatement(stmt) &&
95
- ts.isStringLiteralLike(stmt.expression) &&
96
- stmt.expression.text.startsWith("use ")
97
- )
98
- continue;
99
- break;
100
- }
101
- return i;
102
- };
103
- }
104
-
105
- const isTransformerError = (error: any): error is TransformerError =>
106
- typeof error === "object" &&
107
- error !== null &&
108
- error.constructor.name === "TransformerError" &&
109
- typeof error.code === "string" &&
110
- typeof error.message === "string";
1
+ import ts from "typescript";
2
+ import { ImportProgrammer } from "typia/lib/programmers/ImportProgrammer";
3
+ import { TransformerError } from "typia/lib/transformers/TransformerError";
4
+
5
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
6
+ import { NodeTransformer } from "./NodeTransformer";
7
+
8
+ export namespace FileTransformer {
9
+ export const transform =
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
+ internalPrefix: "nestia_core_transform",
16
+ });
17
+ file = ts.visitEachChild(
18
+ file,
19
+ (node) =>
20
+ iterate_node({
21
+ context: {
22
+ ...context,
23
+ transformer,
24
+ importer,
25
+ },
26
+ file,
27
+ node,
28
+ }),
29
+ transformer,
30
+ );
31
+ const index: number = find_import_injection_index(file);
32
+ return ts.factory.updateSourceFile(
33
+ file,
34
+ [
35
+ ...file.statements.slice(0, index),
36
+ ...importer.toStatements(),
37
+ ...file.statements.slice(index),
38
+ ],
39
+ false,
40
+ file.referencedFiles,
41
+ file.typeReferenceDirectives,
42
+ file.hasNoDefaultLib,
43
+ file.libReferenceDirectives,
44
+ );
45
+ };
46
+
47
+ const iterate_node = (props: {
48
+ context: INestiaTransformContext;
49
+ file: ts.SourceFile;
50
+ node: ts.Node;
51
+ }): ts.Node =>
52
+ ts.visitEachChild(
53
+ try_transform_node(props) ?? props.node,
54
+ (child) =>
55
+ iterate_node({
56
+ context: props.context,
57
+ file: props.file,
58
+ node: child,
59
+ }),
60
+ props.context.transformer,
61
+ );
62
+
63
+ const try_transform_node = (props: {
64
+ context: INestiaTransformContext;
65
+ file: ts.SourceFile;
66
+ node: ts.Node;
67
+ }): ts.Node | null => {
68
+ try {
69
+ return NodeTransformer.transform(props);
70
+ } catch (exp) {
71
+ // ONLY ACCEPT TRANSFORMER-ERROR
72
+ if (!isTransformerError(exp)) throw exp;
73
+
74
+ // AVOID SPECIAL BUG OF TYPESCRIPT COMPILER API
75
+ (props.node as any).parent ??= props.file;
76
+
77
+ // REPORT DIAGNOSTIC
78
+ const diagnostic = (ts as any).createDiagnosticForNode(props.node, {
79
+ key: exp.code,
80
+ category: ts.DiagnosticCategory.Error,
81
+ message: exp.message,
82
+ code: `(${exp.code})` as any,
83
+ });
84
+ props.context.extras.addDiagnostic(diagnostic);
85
+ return null;
86
+ }
87
+ };
88
+
89
+ const find_import_injection_index = (file: ts.SourceFile): number => {
90
+ let i: number = 0;
91
+ for (; i < file.statements.length; ++i) {
92
+ const stmt: ts.Statement = file.statements[i]!;
93
+ if (
94
+ ts.isExpressionStatement(stmt) &&
95
+ ts.isStringLiteralLike(stmt.expression) &&
96
+ stmt.expression.text.startsWith("use ")
97
+ )
98
+ continue;
99
+ break;
100
+ }
101
+ return i;
102
+ };
103
+ }
104
+
105
+ const isTransformerError = (error: any): error is TransformerError =>
106
+ typeof error === "object" &&
107
+ error !== null &&
108
+ error.constructor.name === "TransformerError" &&
109
+ typeof error.code === "string" &&
110
+ typeof error.message === "string";
@@ -1,103 +1,103 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
- import { TypedRouteTransformer } from "./TypedRouteTransformer";
5
- import { WebSocketRouteTransformer } from "./WebSocketRouteTransformer";
6
-
7
- export namespace MethodTransformer {
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;
16
-
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);
23
-
24
- if (type === undefined) return props.method;
25
-
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;
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
- };
67
- }
68
-
69
- const get_escaped_type =
70
- (checker: ts.TypeChecker) =>
71
- (type: ts.Type): ts.Type => {
72
- const symbol: ts.Symbol | undefined = type.getSymbol() || type.aliasSymbol;
73
- return symbol && get_name(symbol) === "Promise"
74
- ? escape_promise(checker)(type)
75
- : type;
76
- };
77
-
78
- const escape_promise =
79
- (checker: ts.TypeChecker) =>
80
- (type: ts.Type): ts.Type => {
81
- const generic: readonly ts.Type[] = checker.getTypeArguments(
82
- type as ts.TypeReference,
83
- );
84
- if (generic.length !== 1)
85
- throw new Error(
86
- "Error on ImportAnalyzer.analyze(): invalid promise type.",
87
- );
88
- return generic[0];
89
- };
90
-
91
- const get_name = (symbol: ts.Symbol): string =>
92
- explore_name(symbol.getDeclarations()![0].parent)(
93
- symbol.escapedName.toString(),
94
- );
95
-
96
- const explore_name =
97
- (decl: ts.Node) =>
98
- (name: string): string =>
99
- ts.isModuleBlock(decl)
100
- ? explore_name(decl.parent.parent)(
101
- `${decl.parent.name.getFullText().trim()}.${name}`,
102
- )
103
- : name;
1
+ import ts from "typescript";
2
+
3
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
+ import { TypedRouteTransformer } from "./TypedRouteTransformer";
5
+ import { WebSocketRouteTransformer } from "./WebSocketRouteTransformer";
6
+
7
+ export namespace MethodTransformer {
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;
16
+
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);
23
+
24
+ if (type === undefined) return props.method;
25
+
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;
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
+ };
67
+ }
68
+
69
+ const get_escaped_type =
70
+ (checker: ts.TypeChecker) =>
71
+ (type: ts.Type): ts.Type => {
72
+ const symbol: ts.Symbol | undefined = type.getSymbol() || type.aliasSymbol;
73
+ return symbol && get_name(symbol) === "Promise"
74
+ ? escape_promise(checker)(type)
75
+ : type;
76
+ };
77
+
78
+ const escape_promise =
79
+ (checker: ts.TypeChecker) =>
80
+ (type: ts.Type): ts.Type => {
81
+ const generic: readonly ts.Type[] = checker.getTypeArguments(
82
+ type as ts.TypeReference,
83
+ );
84
+ if (generic.length !== 1)
85
+ throw new Error(
86
+ "Error on ImportAnalyzer.analyze(): invalid promise type.",
87
+ );
88
+ return generic[0];
89
+ };
90
+
91
+ const get_name = (symbol: ts.Symbol): string =>
92
+ explore_name(symbol.getDeclarations()![0].parent)(
93
+ symbol.escapedName.toString(),
94
+ );
95
+
96
+ const explore_name =
97
+ (decl: ts.Node) =>
98
+ (name: string): string =>
99
+ ts.isModuleBlock(decl)
100
+ ? explore_name(decl.parent.parent)(
101
+ `${decl.parent.name.getFullText().trim()}.${name}`,
102
+ )
103
+ : name;
@@ -1,23 +1,23 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
- import { MethodTransformer } from "./MethodTransformer";
5
- import { ParameterTransformer } from "./ParameterTransformer";
6
-
7
- export namespace NodeTransformer {
8
- export const transform = (props: {
9
- context: INestiaTransformContext;
10
- node: ts.Node;
11
- }): ts.Node =>
12
- ts.isMethodDeclaration(props.node)
13
- ? MethodTransformer.transform({
14
- context: props.context,
15
- method: props.node,
16
- })
17
- : ts.isParameter(props.node)
18
- ? ParameterTransformer.transform({
19
- context: props.context,
20
- param: props.node,
21
- })
22
- : props.node;
23
- }
1
+ import ts from "typescript";
2
+
3
+ import { INestiaTransformContext } from "../options/INestiaTransformProject";
4
+ import { MethodTransformer } from "./MethodTransformer";
5
+ import { ParameterTransformer } from "./ParameterTransformer";
6
+
7
+ export namespace NodeTransformer {
8
+ export const transform = (props: {
9
+ context: INestiaTransformContext;
10
+ node: ts.Node;
11
+ }): ts.Node =>
12
+ ts.isMethodDeclaration(props.node)
13
+ ? MethodTransformer.transform({
14
+ context: props.context,
15
+ method: props.node,
16
+ })
17
+ : ts.isParameter(props.node)
18
+ ? ParameterTransformer.transform({
19
+ context: props.context,
20
+ param: props.node,
21
+ })
22
+ : props.node;
23
+ }