typia 6.7.0 → 6.7.1
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.
- package/lib/programmers/AssertProgrammer.d.ts +0 -1
- package/lib/programmers/AssertProgrammer.js.map +1 -1
- package/lib/programmers/CheckerProgrammer.d.ts +1 -1
- package/lib/programmers/FeatureProgrammer.d.ts +1 -1
- package/lib/programmers/IsProgrammer.d.ts +1 -1
- package/lib/programmers/RandomProgrammer.d.ts +10 -1
- package/lib/programmers/RandomProgrammer.js +62 -37
- package/lib/programmers/RandomProgrammer.js.map +1 -1
- package/lib/programmers/ValidateProgrammer.d.ts +1 -1
- package/lib/programmers/json/JsonIsParseProgrammer.d.ts +1 -1
- package/lib/programmers/json/JsonIsStringifyProgrammer.d.ts +1 -1
- package/lib/programmers/json/JsonStringifyProgrammer.d.ts +1 -1
- package/lib/programmers/json/JsonStringifyProgrammer.js +1 -3
- package/lib/programmers/json/JsonStringifyProgrammer.js.map +1 -1
- package/lib/programmers/json/JsonValidateParseProgrammer.d.ts +1 -1
- package/lib/programmers/json/JsonValidateStringifyProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscCloneProgrammer.js +33 -14
- package/lib/programmers/misc/MiscCloneProgrammer.js.map +1 -1
- package/lib/programmers/misc/MiscIsCloneProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscIsPruneProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscPruneProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscValidateCloneProgrammer.d.ts +1 -1
- package/lib/programmers/misc/MiscValidatePruneProgrammer.d.ts +1 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.d.ts +1 -1
- package/lib/programmers/notations/NotationGeneralProgrammer.js +30 -11
- package/lib/programmers/notations/NotationGeneralProgrammer.js.map +1 -1
- package/lib/programmers/notations/NotationValidateGeneralProgrammer.d.ts +1 -1
- package/package.json +1 -1
- package/src/programmers/AssertProgrammer.ts +397 -398
- package/src/programmers/CheckerProgrammer.ts +1138 -1138
- package/src/programmers/FeatureProgrammer.ts +549 -549
- package/src/programmers/IsProgrammer.ts +1 -1
- package/src/programmers/RandomProgrammer.ts +112 -77
- package/src/programmers/ValidateProgrammer.ts +382 -382
- package/src/programmers/functional/FunctionalAssertFunctionProgrammer.ts +141 -141
- package/src/programmers/functional/FunctionalAssertParametersProgrammer.ts +108 -108
- package/src/programmers/functional/FunctionalAssertReturnProgrammer.ts +98 -98
- package/src/programmers/functional/FunctionalIsFunctionProgrammer.ts +72 -72
- package/src/programmers/functional/FunctionalIsParametersProgrammer.ts +101 -101
- package/src/programmers/functional/FunctionalIsReturnProgrammer.ts +106 -106
- package/src/programmers/functional/FunctionalValidateFunctionProgrammer.ts +123 -123
- package/src/programmers/functional/FunctionalValidateParametersProgrammer.ts +267 -267
- package/src/programmers/functional/FunctionalValidateReturnProgrammer.ts +126 -126
- package/src/programmers/helpers/FunctionImporter.ts +97 -97
- package/src/programmers/http/HttpAssertFormDataProgrammer.ts +91 -91
- package/src/programmers/http/HttpAssertHeadersProgrammer.ts +91 -91
- package/src/programmers/http/HttpAssertQueryProgrammer.ts +93 -93
- package/src/programmers/http/HttpFormDataProgrammer.ts +278 -278
- package/src/programmers/http/HttpHeadersProgrammer.ts +347 -347
- package/src/programmers/http/HttpIsFormDataProgrammer.ts +102 -102
- package/src/programmers/http/HttpIsHeadersProgrammer.ts +102 -102
- package/src/programmers/http/HttpIsQueryProgrammer.ts +104 -104
- package/src/programmers/http/HttpQueryProgrammer.ts +298 -298
- package/src/programmers/http/HttpValidateFormDataProgrammer.ts +85 -85
- package/src/programmers/http/HttpValidateHeadersProgrammer.ts +85 -85
- package/src/programmers/http/HttpValidateQueryProgrammer.ts +87 -87
- package/src/programmers/json/JsonAssertParseProgrammer.ts +96 -96
- package/src/programmers/json/JsonAssertStringifyProgrammer.ts +104 -104
- package/src/programmers/json/JsonIsParseProgrammer.ts +110 -110
- package/src/programmers/json/JsonIsStringifyProgrammer.ts +102 -102
- package/src/programmers/json/JsonStringifyProgrammer.ts +909 -910
- package/src/programmers/json/JsonValidateParseProgrammer.ts +87 -87
- package/src/programmers/json/JsonValidateStringifyProgrammer.ts +111 -111
- package/src/programmers/misc/MiscAssertCloneProgrammer.ts +87 -87
- package/src/programmers/misc/MiscAssertPruneProgrammer.ts +87 -87
- package/src/programmers/misc/MiscCloneProgrammer.ts +781 -759
- package/src/programmers/misc/MiscIsCloneProgrammer.ts +93 -93
- package/src/programmers/misc/MiscIsPruneProgrammer.ts +94 -94
- package/src/programmers/misc/MiscPruneProgrammer.ts +560 -560
- package/src/programmers/misc/MiscValidateCloneProgrammer.ts +104 -104
- package/src/programmers/misc/MiscValidatePruneProgrammer.ts +98 -98
- package/src/programmers/notations/NotationAssertGeneralProgrammer.ts +91 -91
- package/src/programmers/notations/NotationGeneralProgrammer.ts +709 -685
- package/src/programmers/notations/NotationIsGeneralProgrammer.ts +97 -97
- package/src/programmers/notations/NotationValidateGeneralProgrammer.ts +107 -107
- package/src/programmers/protobuf/ProtobufAssertDecodeProgrammer.ts +91 -91
- package/src/programmers/protobuf/ProtobufAssertEncodeProgrammer.ts +95 -95
- package/src/programmers/protobuf/ProtobufDecodeProgrammer.ts +646 -646
- package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +852 -852
- package/src/programmers/protobuf/ProtobufIsDecodeProgrammer.ts +104 -104
- package/src/programmers/protobuf/ProtobufIsEncodeProgrammer.ts +93 -93
- package/src/programmers/protobuf/ProtobufValidateDecodeProgrammer.ts +85 -85
- package/src/programmers/protobuf/ProtobufValidateEncodeProgrammer.ts +109 -109
- package/src/transformers/internal/GenericTransformer.ts +104 -104
|
@@ -1,298 +1,298 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
|
-
|
|
3
|
-
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
4
|
-
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
5
|
-
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
6
|
-
import { StatementFactory } from "../../factories/StatementFactory";
|
|
7
|
-
import { TypeFactory } from "../../factories/TypeFactory";
|
|
8
|
-
|
|
9
|
-
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
10
|
-
import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
|
|
11
|
-
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
12
|
-
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
13
|
-
|
|
14
|
-
import { IProject } from "../../transformers/IProject";
|
|
15
|
-
import { TransformerError } from "../../transformers/TransformerError";
|
|
16
|
-
|
|
17
|
-
import { Atomic } from "../../typings/Atomic";
|
|
18
|
-
|
|
19
|
-
import { Escaper } from "../../utils/Escaper";
|
|
20
|
-
|
|
21
|
-
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
22
|
-
import { FunctionImporter } from "../helpers/FunctionImporter";
|
|
23
|
-
import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
|
|
24
|
-
|
|
25
|
-
export namespace HttpQueryProgrammer {
|
|
26
|
-
export const INPUT_TYPE = "string | URLSearchParams";
|
|
27
|
-
|
|
28
|
-
export const decompose = (props: {
|
|
29
|
-
project: IProject;
|
|
30
|
-
importer: FunctionImporter;
|
|
31
|
-
allowOptional: boolean;
|
|
32
|
-
type: ts.Type;
|
|
33
|
-
name: string | undefined;
|
|
34
|
-
}): FeatureProgrammer.IDecomposed => {
|
|
35
|
-
// ANALYZE TYPE
|
|
36
|
-
const collection: MetadataCollection = new MetadataCollection();
|
|
37
|
-
const result = MetadataFactory.analyze(
|
|
38
|
-
props.project.checker,
|
|
39
|
-
props.project.context,
|
|
40
|
-
)({
|
|
41
|
-
escape: false,
|
|
42
|
-
constant: true,
|
|
43
|
-
absorb: true,
|
|
44
|
-
validate: (meta, explore) => validate(meta, explore, props.allowOptional),
|
|
45
|
-
})(collection)(props.type);
|
|
46
|
-
if (result.success === false)
|
|
47
|
-
throw TransformerError.from(`typia.http.${props.importer.method}`)(
|
|
48
|
-
result.errors,
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
// DO TRANSFORM
|
|
52
|
-
const object: MetadataObject = result.data.objects[0]!;
|
|
53
|
-
const statements: ts.Statement[] = decode_object(props.importer)(object);
|
|
54
|
-
return {
|
|
55
|
-
functions: {},
|
|
56
|
-
statements: [],
|
|
57
|
-
arrow: ts.factory.createArrowFunction(
|
|
58
|
-
undefined,
|
|
59
|
-
undefined,
|
|
60
|
-
[
|
|
61
|
-
IdentifierFactory.parameter(
|
|
62
|
-
"input",
|
|
63
|
-
ts.factory.createTypeReferenceNode(INPUT_TYPE),
|
|
64
|
-
),
|
|
65
|
-
],
|
|
66
|
-
ts.factory.createImportTypeNode(
|
|
67
|
-
ts.factory.createLiteralTypeNode(
|
|
68
|
-
ts.factory.createStringLiteral("typia"),
|
|
69
|
-
),
|
|
70
|
-
undefined,
|
|
71
|
-
ts.factory.createIdentifier("Resolved"),
|
|
72
|
-
[
|
|
73
|
-
ts.factory.createTypeReferenceNode(
|
|
74
|
-
props.name ??
|
|
75
|
-
TypeFactory.getFullName(props.project.checker)(props.type),
|
|
76
|
-
),
|
|
77
|
-
],
|
|
78
|
-
false,
|
|
79
|
-
),
|
|
80
|
-
undefined,
|
|
81
|
-
ts.factory.createBlock(statements, true),
|
|
82
|
-
),
|
|
83
|
-
};
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export const write =
|
|
87
|
-
(project: IProject) =>
|
|
88
|
-
(modulo: ts.LeftHandSideExpression, allowOptional: boolean = false) =>
|
|
89
|
-
(type: ts.Type, name?: string): ts.CallExpression => {
|
|
90
|
-
const importer: FunctionImporter = new FunctionImporter(modulo.getText());
|
|
91
|
-
const result: FeatureProgrammer.IDecomposed = decompose({
|
|
92
|
-
project,
|
|
93
|
-
importer,
|
|
94
|
-
type,
|
|
95
|
-
name,
|
|
96
|
-
allowOptional,
|
|
97
|
-
});
|
|
98
|
-
return FeatureProgrammer.writeDecomposed({
|
|
99
|
-
modulo,
|
|
100
|
-
importer,
|
|
101
|
-
result,
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
export const validate = (
|
|
106
|
-
meta: Metadata,
|
|
107
|
-
explore: MetadataFactory.IExplore,
|
|
108
|
-
allowOptional: boolean = false,
|
|
109
|
-
): string[] => {
|
|
110
|
-
const errors: string[] = [];
|
|
111
|
-
const insert = (msg: string) => errors.push(msg);
|
|
112
|
-
|
|
113
|
-
if (explore.top === true) {
|
|
114
|
-
// TOP MUST BE ONLY OBJECT
|
|
115
|
-
if (meta.objects.length !== 1 || meta.bucket() !== 1)
|
|
116
|
-
insert("only one object type is allowed.");
|
|
117
|
-
if (meta.nullable === true) insert("query parameters cannot be null.");
|
|
118
|
-
if (meta.isRequired() === false) {
|
|
119
|
-
if (allowOptional === true) {
|
|
120
|
-
const everyPropertiesAreOptional: boolean =
|
|
121
|
-
meta.size() === 1 &&
|
|
122
|
-
meta.objects.length === 1 &&
|
|
123
|
-
meta.objects[0]!.properties.every(
|
|
124
|
-
(p) => p.value.isRequired() === false,
|
|
125
|
-
);
|
|
126
|
-
if (everyPropertiesAreOptional === false)
|
|
127
|
-
insert(
|
|
128
|
-
"query parameters can be optional only when every properties are optional.",
|
|
129
|
-
);
|
|
130
|
-
} else insert("query parameters cannot be undefined.");
|
|
131
|
-
}
|
|
132
|
-
} else if (
|
|
133
|
-
explore.nested !== null &&
|
|
134
|
-
explore.nested instanceof MetadataArrayType
|
|
135
|
-
) {
|
|
136
|
-
//----
|
|
137
|
-
// ARRAY
|
|
138
|
-
//----
|
|
139
|
-
const atomics = HttpMetadataUtil.atomics(meta);
|
|
140
|
-
const expected: number =
|
|
141
|
-
meta.atomics.length +
|
|
142
|
-
meta.templates.length +
|
|
143
|
-
meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
|
|
144
|
-
if (atomics.size > 1) insert("union type is not allowed in array.");
|
|
145
|
-
if (meta.size() !== expected)
|
|
146
|
-
insert("only atomic or constant types are allowed in array.");
|
|
147
|
-
} else if (explore.object && explore.property !== null) {
|
|
148
|
-
//----
|
|
149
|
-
// COMMON
|
|
150
|
-
//----
|
|
151
|
-
// PROPERTY MUST BE SOLE
|
|
152
|
-
if (typeof explore.property === "object")
|
|
153
|
-
insert("dynamic property is not allowed.");
|
|
154
|
-
// DO NOT ALLOW TUPLE TYPE
|
|
155
|
-
if (meta.tuples.length) insert("tuple type is not allowed.");
|
|
156
|
-
// DO NOT ALLOW UNION TYPE
|
|
157
|
-
if (HttpMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
|
|
158
|
-
// DO NOT ALLOW NESTED OBJECT
|
|
159
|
-
if (
|
|
160
|
-
meta.objects.length ||
|
|
161
|
-
meta.sets.length ||
|
|
162
|
-
meta.maps.length ||
|
|
163
|
-
meta.natives.length
|
|
164
|
-
)
|
|
165
|
-
insert("nested object type is not allowed.");
|
|
166
|
-
}
|
|
167
|
-
return errors;
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const decode_object =
|
|
171
|
-
(importer: FunctionImporter) =>
|
|
172
|
-
(object: MetadataObject): ts.Statement[] => {
|
|
173
|
-
const input: ts.Identifier = ts.factory.createIdentifier("input");
|
|
174
|
-
const output: ts.Identifier = ts.factory.createIdentifier("output");
|
|
175
|
-
|
|
176
|
-
return [
|
|
177
|
-
ts.factory.createExpressionStatement(
|
|
178
|
-
ts.factory.createBinaryExpression(
|
|
179
|
-
input,
|
|
180
|
-
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
181
|
-
ts.factory.createAsExpression(
|
|
182
|
-
ts.factory.createCallExpression(
|
|
183
|
-
importer.use("params"),
|
|
184
|
-
undefined,
|
|
185
|
-
[input],
|
|
186
|
-
),
|
|
187
|
-
ts.factory.createTypeReferenceNode("URLSearchParams"),
|
|
188
|
-
),
|
|
189
|
-
),
|
|
190
|
-
),
|
|
191
|
-
StatementFactory.constant(
|
|
192
|
-
"output",
|
|
193
|
-
ts.factory.createObjectLiteralExpression(
|
|
194
|
-
object.properties.map((prop) =>
|
|
195
|
-
decode_regular_property(importer)(prop),
|
|
196
|
-
),
|
|
197
|
-
true,
|
|
198
|
-
),
|
|
199
|
-
),
|
|
200
|
-
ts.factory.createReturnStatement(
|
|
201
|
-
ts.factory.createAsExpression(output, TypeFactory.keyword("any")),
|
|
202
|
-
),
|
|
203
|
-
];
|
|
204
|
-
};
|
|
205
|
-
|
|
206
|
-
const decode_regular_property =
|
|
207
|
-
(importer: FunctionImporter) =>
|
|
208
|
-
(property: MetadataProperty): ts.PropertyAssignment => {
|
|
209
|
-
const key: string = property.key.constants[0]!.values[0]!.value as string;
|
|
210
|
-
const value: Metadata = property.value;
|
|
211
|
-
|
|
212
|
-
const [type, isArray]: [Atomic.Literal, boolean] = value.atomics.length
|
|
213
|
-
? [value.atomics[0]!.type, false]
|
|
214
|
-
: value.constants.length
|
|
215
|
-
? [value.constants[0]!.type, false]
|
|
216
|
-
: value.templates.length
|
|
217
|
-
? ["string", false]
|
|
218
|
-
: (() => {
|
|
219
|
-
const meta =
|
|
220
|
-
value.arrays[0]?.type.value ??
|
|
221
|
-
value.tuples[0]!.type.elements[0]!;
|
|
222
|
-
return meta.atomics.length
|
|
223
|
-
? [meta.atomics[0]!.type, true]
|
|
224
|
-
: meta.templates.length
|
|
225
|
-
? ["string", true]
|
|
226
|
-
: [meta.constants[0]!.type, true];
|
|
227
|
-
})();
|
|
228
|
-
return ts.factory.createPropertyAssignment(
|
|
229
|
-
Escaper.variable(key) ? key : ts.factory.createStringLiteral(key),
|
|
230
|
-
isArray
|
|
231
|
-
? decode_array(importer)(value)(
|
|
232
|
-
ts.factory.createCallExpression(
|
|
233
|
-
IdentifierFactory.access(
|
|
234
|
-
ts.factory.createCallExpression(
|
|
235
|
-
ts.factory.createIdentifier("input.getAll"),
|
|
236
|
-
undefined,
|
|
237
|
-
[ts.factory.createStringLiteral(key)],
|
|
238
|
-
),
|
|
239
|
-
)("map"),
|
|
240
|
-
undefined,
|
|
241
|
-
[
|
|
242
|
-
ts.factory.createArrowFunction(
|
|
243
|
-
undefined,
|
|
244
|
-
undefined,
|
|
245
|
-
[IdentifierFactory.parameter("elem")],
|
|
246
|
-
undefined,
|
|
247
|
-
undefined,
|
|
248
|
-
decode_value(importer)(type)(false)(
|
|
249
|
-
ts.factory.createIdentifier("elem"),
|
|
250
|
-
),
|
|
251
|
-
),
|
|
252
|
-
],
|
|
253
|
-
),
|
|
254
|
-
)
|
|
255
|
-
: decode_value(importer)(type)(
|
|
256
|
-
value.nullable === false && value.isRequired() === false,
|
|
257
|
-
)(
|
|
258
|
-
ts.factory.createCallExpression(
|
|
259
|
-
ts.factory.createIdentifier("input.get"),
|
|
260
|
-
undefined,
|
|
261
|
-
[ts.factory.createStringLiteral(key)],
|
|
262
|
-
),
|
|
263
|
-
),
|
|
264
|
-
);
|
|
265
|
-
};
|
|
266
|
-
|
|
267
|
-
const decode_value =
|
|
268
|
-
(importer: FunctionImporter) =>
|
|
269
|
-
(type: Atomic.Literal) =>
|
|
270
|
-
(onlyUndefindable: boolean) =>
|
|
271
|
-
(value: ts.Expression) => {
|
|
272
|
-
const call = ts.factory.createCallExpression(
|
|
273
|
-
importer.use(type),
|
|
274
|
-
undefined,
|
|
275
|
-
[value],
|
|
276
|
-
);
|
|
277
|
-
return onlyUndefindable
|
|
278
|
-
? ts.factory.createBinaryExpression(
|
|
279
|
-
call,
|
|
280
|
-
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
|
|
281
|
-
ts.factory.createIdentifier("undefined"),
|
|
282
|
-
)
|
|
283
|
-
: call;
|
|
284
|
-
};
|
|
285
|
-
|
|
286
|
-
const decode_array =
|
|
287
|
-
(importer: FunctionImporter) =>
|
|
288
|
-
(value: Metadata) =>
|
|
289
|
-
(expression: ts.Expression): ts.Expression =>
|
|
290
|
-
value.nullable || value.isRequired() === false
|
|
291
|
-
? ts.factory.createCallExpression(importer.use("array"), undefined, [
|
|
292
|
-
expression,
|
|
293
|
-
value.nullable
|
|
294
|
-
? ts.factory.createNull()
|
|
295
|
-
: ts.factory.createIdentifier("undefined"),
|
|
296
|
-
])
|
|
297
|
-
: expression;
|
|
298
|
-
}
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
import { IdentifierFactory } from "../../factories/IdentifierFactory";
|
|
4
|
+
import { MetadataCollection } from "../../factories/MetadataCollection";
|
|
5
|
+
import { MetadataFactory } from "../../factories/MetadataFactory";
|
|
6
|
+
import { StatementFactory } from "../../factories/StatementFactory";
|
|
7
|
+
import { TypeFactory } from "../../factories/TypeFactory";
|
|
8
|
+
|
|
9
|
+
import { Metadata } from "../../schemas/metadata/Metadata";
|
|
10
|
+
import { MetadataArrayType } from "../../schemas/metadata/MetadataArrayType";
|
|
11
|
+
import { MetadataObject } from "../../schemas/metadata/MetadataObject";
|
|
12
|
+
import { MetadataProperty } from "../../schemas/metadata/MetadataProperty";
|
|
13
|
+
|
|
14
|
+
import { IProject } from "../../transformers/IProject";
|
|
15
|
+
import { TransformerError } from "../../transformers/TransformerError";
|
|
16
|
+
|
|
17
|
+
import { Atomic } from "../../typings/Atomic";
|
|
18
|
+
|
|
19
|
+
import { Escaper } from "../../utils/Escaper";
|
|
20
|
+
|
|
21
|
+
import { FeatureProgrammer } from "../FeatureProgrammer";
|
|
22
|
+
import { FunctionImporter } from "../helpers/FunctionImporter";
|
|
23
|
+
import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
|
|
24
|
+
|
|
25
|
+
export namespace HttpQueryProgrammer {
|
|
26
|
+
export const INPUT_TYPE = "string | URLSearchParams";
|
|
27
|
+
|
|
28
|
+
export const decompose = (props: {
|
|
29
|
+
project: IProject;
|
|
30
|
+
importer: FunctionImporter;
|
|
31
|
+
allowOptional: boolean;
|
|
32
|
+
type: ts.Type;
|
|
33
|
+
name: string | undefined;
|
|
34
|
+
}): FeatureProgrammer.IDecomposed => {
|
|
35
|
+
// ANALYZE TYPE
|
|
36
|
+
const collection: MetadataCollection = new MetadataCollection();
|
|
37
|
+
const result = MetadataFactory.analyze(
|
|
38
|
+
props.project.checker,
|
|
39
|
+
props.project.context,
|
|
40
|
+
)({
|
|
41
|
+
escape: false,
|
|
42
|
+
constant: true,
|
|
43
|
+
absorb: true,
|
|
44
|
+
validate: (meta, explore) => validate(meta, explore, props.allowOptional),
|
|
45
|
+
})(collection)(props.type);
|
|
46
|
+
if (result.success === false)
|
|
47
|
+
throw TransformerError.from(`typia.http.${props.importer.method}`)(
|
|
48
|
+
result.errors,
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
// DO TRANSFORM
|
|
52
|
+
const object: MetadataObject = result.data.objects[0]!;
|
|
53
|
+
const statements: ts.Statement[] = decode_object(props.importer)(object);
|
|
54
|
+
return {
|
|
55
|
+
functions: {},
|
|
56
|
+
statements: [],
|
|
57
|
+
arrow: ts.factory.createArrowFunction(
|
|
58
|
+
undefined,
|
|
59
|
+
undefined,
|
|
60
|
+
[
|
|
61
|
+
IdentifierFactory.parameter(
|
|
62
|
+
"input",
|
|
63
|
+
ts.factory.createTypeReferenceNode(INPUT_TYPE),
|
|
64
|
+
),
|
|
65
|
+
],
|
|
66
|
+
ts.factory.createImportTypeNode(
|
|
67
|
+
ts.factory.createLiteralTypeNode(
|
|
68
|
+
ts.factory.createStringLiteral("typia"),
|
|
69
|
+
),
|
|
70
|
+
undefined,
|
|
71
|
+
ts.factory.createIdentifier("Resolved"),
|
|
72
|
+
[
|
|
73
|
+
ts.factory.createTypeReferenceNode(
|
|
74
|
+
props.name ??
|
|
75
|
+
TypeFactory.getFullName(props.project.checker)(props.type),
|
|
76
|
+
),
|
|
77
|
+
],
|
|
78
|
+
false,
|
|
79
|
+
),
|
|
80
|
+
undefined,
|
|
81
|
+
ts.factory.createBlock(statements, true),
|
|
82
|
+
),
|
|
83
|
+
};
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
export const write =
|
|
87
|
+
(project: IProject) =>
|
|
88
|
+
(modulo: ts.LeftHandSideExpression, allowOptional: boolean = false) =>
|
|
89
|
+
(type: ts.Type, name?: string): ts.CallExpression => {
|
|
90
|
+
const importer: FunctionImporter = new FunctionImporter(modulo.getText());
|
|
91
|
+
const result: FeatureProgrammer.IDecomposed = decompose({
|
|
92
|
+
project,
|
|
93
|
+
importer,
|
|
94
|
+
type,
|
|
95
|
+
name,
|
|
96
|
+
allowOptional,
|
|
97
|
+
});
|
|
98
|
+
return FeatureProgrammer.writeDecomposed({
|
|
99
|
+
modulo,
|
|
100
|
+
importer,
|
|
101
|
+
result,
|
|
102
|
+
});
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export const validate = (
|
|
106
|
+
meta: Metadata,
|
|
107
|
+
explore: MetadataFactory.IExplore,
|
|
108
|
+
allowOptional: boolean = false,
|
|
109
|
+
): string[] => {
|
|
110
|
+
const errors: string[] = [];
|
|
111
|
+
const insert = (msg: string) => errors.push(msg);
|
|
112
|
+
|
|
113
|
+
if (explore.top === true) {
|
|
114
|
+
// TOP MUST BE ONLY OBJECT
|
|
115
|
+
if (meta.objects.length !== 1 || meta.bucket() !== 1)
|
|
116
|
+
insert("only one object type is allowed.");
|
|
117
|
+
if (meta.nullable === true) insert("query parameters cannot be null.");
|
|
118
|
+
if (meta.isRequired() === false) {
|
|
119
|
+
if (allowOptional === true) {
|
|
120
|
+
const everyPropertiesAreOptional: boolean =
|
|
121
|
+
meta.size() === 1 &&
|
|
122
|
+
meta.objects.length === 1 &&
|
|
123
|
+
meta.objects[0]!.properties.every(
|
|
124
|
+
(p) => p.value.isRequired() === false,
|
|
125
|
+
);
|
|
126
|
+
if (everyPropertiesAreOptional === false)
|
|
127
|
+
insert(
|
|
128
|
+
"query parameters can be optional only when every properties are optional.",
|
|
129
|
+
);
|
|
130
|
+
} else insert("query parameters cannot be undefined.");
|
|
131
|
+
}
|
|
132
|
+
} else if (
|
|
133
|
+
explore.nested !== null &&
|
|
134
|
+
explore.nested instanceof MetadataArrayType
|
|
135
|
+
) {
|
|
136
|
+
//----
|
|
137
|
+
// ARRAY
|
|
138
|
+
//----
|
|
139
|
+
const atomics = HttpMetadataUtil.atomics(meta);
|
|
140
|
+
const expected: number =
|
|
141
|
+
meta.atomics.length +
|
|
142
|
+
meta.templates.length +
|
|
143
|
+
meta.constants.map((c) => c.values.length).reduce((a, b) => a + b, 0);
|
|
144
|
+
if (atomics.size > 1) insert("union type is not allowed in array.");
|
|
145
|
+
if (meta.size() !== expected)
|
|
146
|
+
insert("only atomic or constant types are allowed in array.");
|
|
147
|
+
} else if (explore.object && explore.property !== null) {
|
|
148
|
+
//----
|
|
149
|
+
// COMMON
|
|
150
|
+
//----
|
|
151
|
+
// PROPERTY MUST BE SOLE
|
|
152
|
+
if (typeof explore.property === "object")
|
|
153
|
+
insert("dynamic property is not allowed.");
|
|
154
|
+
// DO NOT ALLOW TUPLE TYPE
|
|
155
|
+
if (meta.tuples.length) insert("tuple type is not allowed.");
|
|
156
|
+
// DO NOT ALLOW UNION TYPE
|
|
157
|
+
if (HttpMetadataUtil.isUnion(meta)) insert("union type is not allowed.");
|
|
158
|
+
// DO NOT ALLOW NESTED OBJECT
|
|
159
|
+
if (
|
|
160
|
+
meta.objects.length ||
|
|
161
|
+
meta.sets.length ||
|
|
162
|
+
meta.maps.length ||
|
|
163
|
+
meta.natives.length
|
|
164
|
+
)
|
|
165
|
+
insert("nested object type is not allowed.");
|
|
166
|
+
}
|
|
167
|
+
return errors;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const decode_object =
|
|
171
|
+
(importer: FunctionImporter) =>
|
|
172
|
+
(object: MetadataObject): ts.Statement[] => {
|
|
173
|
+
const input: ts.Identifier = ts.factory.createIdentifier("input");
|
|
174
|
+
const output: ts.Identifier = ts.factory.createIdentifier("output");
|
|
175
|
+
|
|
176
|
+
return [
|
|
177
|
+
ts.factory.createExpressionStatement(
|
|
178
|
+
ts.factory.createBinaryExpression(
|
|
179
|
+
input,
|
|
180
|
+
ts.factory.createToken(ts.SyntaxKind.EqualsToken),
|
|
181
|
+
ts.factory.createAsExpression(
|
|
182
|
+
ts.factory.createCallExpression(
|
|
183
|
+
importer.use("params"),
|
|
184
|
+
undefined,
|
|
185
|
+
[input],
|
|
186
|
+
),
|
|
187
|
+
ts.factory.createTypeReferenceNode("URLSearchParams"),
|
|
188
|
+
),
|
|
189
|
+
),
|
|
190
|
+
),
|
|
191
|
+
StatementFactory.constant(
|
|
192
|
+
"output",
|
|
193
|
+
ts.factory.createObjectLiteralExpression(
|
|
194
|
+
object.properties.map((prop) =>
|
|
195
|
+
decode_regular_property(importer)(prop),
|
|
196
|
+
),
|
|
197
|
+
true,
|
|
198
|
+
),
|
|
199
|
+
),
|
|
200
|
+
ts.factory.createReturnStatement(
|
|
201
|
+
ts.factory.createAsExpression(output, TypeFactory.keyword("any")),
|
|
202
|
+
),
|
|
203
|
+
];
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const decode_regular_property =
|
|
207
|
+
(importer: FunctionImporter) =>
|
|
208
|
+
(property: MetadataProperty): ts.PropertyAssignment => {
|
|
209
|
+
const key: string = property.key.constants[0]!.values[0]!.value as string;
|
|
210
|
+
const value: Metadata = property.value;
|
|
211
|
+
|
|
212
|
+
const [type, isArray]: [Atomic.Literal, boolean] = value.atomics.length
|
|
213
|
+
? [value.atomics[0]!.type, false]
|
|
214
|
+
: value.constants.length
|
|
215
|
+
? [value.constants[0]!.type, false]
|
|
216
|
+
: value.templates.length
|
|
217
|
+
? ["string", false]
|
|
218
|
+
: (() => {
|
|
219
|
+
const meta =
|
|
220
|
+
value.arrays[0]?.type.value ??
|
|
221
|
+
value.tuples[0]!.type.elements[0]!;
|
|
222
|
+
return meta.atomics.length
|
|
223
|
+
? [meta.atomics[0]!.type, true]
|
|
224
|
+
: meta.templates.length
|
|
225
|
+
? ["string", true]
|
|
226
|
+
: [meta.constants[0]!.type, true];
|
|
227
|
+
})();
|
|
228
|
+
return ts.factory.createPropertyAssignment(
|
|
229
|
+
Escaper.variable(key) ? key : ts.factory.createStringLiteral(key),
|
|
230
|
+
isArray
|
|
231
|
+
? decode_array(importer)(value)(
|
|
232
|
+
ts.factory.createCallExpression(
|
|
233
|
+
IdentifierFactory.access(
|
|
234
|
+
ts.factory.createCallExpression(
|
|
235
|
+
ts.factory.createIdentifier("input.getAll"),
|
|
236
|
+
undefined,
|
|
237
|
+
[ts.factory.createStringLiteral(key)],
|
|
238
|
+
),
|
|
239
|
+
)("map"),
|
|
240
|
+
undefined,
|
|
241
|
+
[
|
|
242
|
+
ts.factory.createArrowFunction(
|
|
243
|
+
undefined,
|
|
244
|
+
undefined,
|
|
245
|
+
[IdentifierFactory.parameter("elem")],
|
|
246
|
+
undefined,
|
|
247
|
+
undefined,
|
|
248
|
+
decode_value(importer)(type)(false)(
|
|
249
|
+
ts.factory.createIdentifier("elem"),
|
|
250
|
+
),
|
|
251
|
+
),
|
|
252
|
+
],
|
|
253
|
+
),
|
|
254
|
+
)
|
|
255
|
+
: decode_value(importer)(type)(
|
|
256
|
+
value.nullable === false && value.isRequired() === false,
|
|
257
|
+
)(
|
|
258
|
+
ts.factory.createCallExpression(
|
|
259
|
+
ts.factory.createIdentifier("input.get"),
|
|
260
|
+
undefined,
|
|
261
|
+
[ts.factory.createStringLiteral(key)],
|
|
262
|
+
),
|
|
263
|
+
),
|
|
264
|
+
);
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
const decode_value =
|
|
268
|
+
(importer: FunctionImporter) =>
|
|
269
|
+
(type: Atomic.Literal) =>
|
|
270
|
+
(onlyUndefindable: boolean) =>
|
|
271
|
+
(value: ts.Expression) => {
|
|
272
|
+
const call = ts.factory.createCallExpression(
|
|
273
|
+
importer.use(type),
|
|
274
|
+
undefined,
|
|
275
|
+
[value],
|
|
276
|
+
);
|
|
277
|
+
return onlyUndefindable
|
|
278
|
+
? ts.factory.createBinaryExpression(
|
|
279
|
+
call,
|
|
280
|
+
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
|
|
281
|
+
ts.factory.createIdentifier("undefined"),
|
|
282
|
+
)
|
|
283
|
+
: call;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const decode_array =
|
|
287
|
+
(importer: FunctionImporter) =>
|
|
288
|
+
(value: Metadata) =>
|
|
289
|
+
(expression: ts.Expression): ts.Expression =>
|
|
290
|
+
value.nullable || value.isRequired() === false
|
|
291
|
+
? ts.factory.createCallExpression(importer.use("array"), undefined, [
|
|
292
|
+
expression,
|
|
293
|
+
value.nullable
|
|
294
|
+
? ts.factory.createNull()
|
|
295
|
+
: ts.factory.createIdentifier("undefined"),
|
|
296
|
+
])
|
|
297
|
+
: expression;
|
|
298
|
+
}
|