typia 4.1.4 → 4.1.5

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.
@@ -1,104 +1,104 @@
1
- import ts from "typescript";
2
-
3
- import { LiteralFactory } from "../../../factories/LiteralFactory";
4
- import { MetadataCollection } from "../../../factories/MetadataCollection";
5
- import { MetadataFactory } from "../../../factories/MetadataFactory";
6
-
7
- import { Metadata } from "../../../metadata/Metadata";
8
- import { IJsonApplication } from "../../../schemas/IJsonApplication";
9
-
10
- import { ApplicationProgrammer } from "../../../programmers/ApplicationProgrammer";
11
-
12
- import { IProject } from "../../IProject";
13
-
14
- export namespace ApplicationTransformer {
15
- export const transform =
16
- ({ checker }: IProject) =>
17
- (expression: ts.CallExpression): ts.Expression => {
18
- if (!expression.typeArguments?.length)
19
- throw new Error(NO_GENERIC_ARGUMENT);
20
-
21
- //----
22
- // GET ARGUMENTS
23
- //----
24
- // VALIDATE TUPLE ARGUMENTS
25
- const top: ts.Node = expression.typeArguments[0]!;
26
- if (!ts.isTupleTypeNode(top)) return expression;
27
- else if (top.elements.some((child) => !ts.isTypeNode(child)))
28
- return expression;
29
-
30
- // GET TYPES
31
- const types: ts.Type[] = top.elements.map((child) =>
32
- checker.getTypeFromTypeNode(child as ts.TypeNode),
33
- );
34
- if (types.some((t) => t.isTypeParameter()))
35
- throw new Error(GENERIC_ARGUMENT);
36
-
37
- // ADDITIONAL PARAMETERS
38
- const purpose: "swagger" | "ajv" = get_parameter(
39
- checker,
40
- "Purpose",
41
- expression.typeArguments[1],
42
- (str) => str === "swagger" || str === "ajv",
43
- () => "swagger",
44
- );
45
-
46
- //----
47
- // GENERATORS
48
- //----
49
- // METADATA
50
- const collection: MetadataCollection = new MetadataCollection({
51
- replace: MetadataCollection.replace,
52
- });
53
- const metadatas: Array<Metadata> = types.map((type) =>
54
- MetadataFactory.analyze(checker)({
55
- resolve: true,
56
- constant: true,
57
- absorb: false,
58
- validate: (meta) => {
59
- if (meta.atomics.find((str) => str === "bigint"))
60
- throw new Error(NO_BIGIT);
61
- },
62
- })(collection)(type),
63
- );
64
-
65
- // APPLICATION
66
- const app: IJsonApplication = ApplicationProgrammer.write({
67
- purpose,
68
- })(metadatas);
69
-
70
- // RETURNS WITH LITERAL EXPRESSION
71
- return LiteralFactory.generate(app);
72
- };
73
-
74
- const get_parameter = <T extends string>(
75
- checker: ts.TypeChecker,
76
- name: string,
77
- node: ts.TypeNode | undefined,
78
- predicator: (value: string) => boolean,
79
- defaulter: () => T,
80
- ): T => {
81
- if (!node) return defaulter();
82
-
83
- // CHECK LITERAL TYPE
84
- const type: ts.Type = checker.getTypeFromTypeNode(node);
85
- if (!type.isLiteral())
86
- throw new Error(
87
- `Error on typia.application(): generic argument "${name}" must be constant.`,
88
- );
89
-
90
- // GET VALUE AND VALIDATE IT
91
- const value = type.value;
92
- if (typeof value !== "string" || predicator(value) === false)
93
- throw new Error(
94
- `Error on typia.application(): invalid value on generic argument "${name}".`,
95
- );
96
- return value as T;
97
- };
98
- }
99
-
100
- const NO_GENERIC_ARGUMENT =
101
- "Error on typia.application(): no generic argument.";
102
- const GENERIC_ARGUMENT =
103
- "Error on typia.application(): non-specified generic argument(s).";
104
- const NO_BIGIT = "Error on typia.application(): does not allow bigint type.";
1
+ import ts from "typescript";
2
+
3
+ import { LiteralFactory } from "../../../factories/LiteralFactory";
4
+ import { MetadataCollection } from "../../../factories/MetadataCollection";
5
+ import { MetadataFactory } from "../../../factories/MetadataFactory";
6
+
7
+ import { Metadata } from "../../../metadata/Metadata";
8
+ import { IJsonApplication } from "../../../schemas/IJsonApplication";
9
+
10
+ import { ApplicationProgrammer } from "../../../programmers/ApplicationProgrammer";
11
+
12
+ import { IProject } from "../../IProject";
13
+
14
+ export namespace ApplicationTransformer {
15
+ export const transform =
16
+ ({ checker }: IProject) =>
17
+ (expression: ts.CallExpression): ts.Expression => {
18
+ if (!expression.typeArguments?.length)
19
+ throw new Error(NO_GENERIC_ARGUMENT);
20
+
21
+ //----
22
+ // GET ARGUMENTS
23
+ //----
24
+ // VALIDATE TUPLE ARGUMENTS
25
+ const top: ts.Node = expression.typeArguments[0]!;
26
+ if (!ts.isTupleTypeNode(top)) return expression;
27
+ else if (top.elements.some((child) => !ts.isTypeNode(child)))
28
+ return expression;
29
+
30
+ // GET TYPES
31
+ const types: ts.Type[] = top.elements.map((child) =>
32
+ checker.getTypeFromTypeNode(child as ts.TypeNode),
33
+ );
34
+ if (types.some((t) => t.isTypeParameter()))
35
+ throw new Error(GENERIC_ARGUMENT);
36
+
37
+ // ADDITIONAL PARAMETERS
38
+ const purpose: "swagger" | "ajv" = get_parameter(
39
+ checker,
40
+ "Purpose",
41
+ expression.typeArguments[1],
42
+ (str) => str === "swagger" || str === "ajv",
43
+ () => "swagger",
44
+ );
45
+
46
+ //----
47
+ // GENERATORS
48
+ //----
49
+ // METADATA
50
+ const collection: MetadataCollection = new MetadataCollection({
51
+ replace: MetadataCollection.replace,
52
+ });
53
+ const metadatas: Array<Metadata> = types.map((type) =>
54
+ MetadataFactory.analyze(checker)({
55
+ resolve: true,
56
+ constant: true,
57
+ absorb: false,
58
+ validate: (meta) => {
59
+ if (meta.atomics.find((str) => str === "bigint"))
60
+ throw new Error(NO_BIGIT);
61
+ },
62
+ })(collection)(type),
63
+ );
64
+
65
+ // APPLICATION
66
+ const app: IJsonApplication = ApplicationProgrammer.write({
67
+ purpose,
68
+ })(metadatas);
69
+
70
+ // RETURNS WITH LITERAL EXPRESSION
71
+ return LiteralFactory.generate(app);
72
+ };
73
+
74
+ const get_parameter = <T extends string>(
75
+ checker: ts.TypeChecker,
76
+ name: string,
77
+ node: ts.TypeNode | undefined,
78
+ predicator: (value: string) => boolean,
79
+ defaulter: () => T,
80
+ ): T => {
81
+ if (!node) return defaulter();
82
+
83
+ // CHECK LITERAL TYPE
84
+ const type: ts.Type = checker.getTypeFromTypeNode(node);
85
+ if (!type.isLiteral())
86
+ throw new Error(
87
+ `Error on typia.application(): generic argument "${name}" must be constant.`,
88
+ );
89
+
90
+ // GET VALUE AND VALIDATE IT
91
+ const value = type.value;
92
+ if (typeof value !== "string" || predicator(value) === false)
93
+ throw new Error(
94
+ `Error on typia.application(): invalid value on generic argument "${name}".`,
95
+ );
96
+ return value as T;
97
+ };
98
+ }
99
+
100
+ const NO_GENERIC_ARGUMENT =
101
+ "Error on typia.application(): no generic argument.";
102
+ const GENERIC_ARGUMENT =
103
+ "Error on typia.application(): non-specified generic argument(s).";
104
+ const NO_BIGIT = "Error on typia.application(): does not allow bigint type.";