typia 5.1.3 → 5.1.4-dev.20230929

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 (66) hide show
  1. package/lib/executable/TypiaSetupWizard.js +1 -19
  2. package/lib/executable/TypiaSetupWizard.js.map +1 -1
  3. package/package.json +1 -1
  4. package/src/executable/TypiaSetupWizard.ts +1 -16
  5. package/src/factories/MetadataCollection.ts +277 -277
  6. package/src/factories/MetadataFactory.ts +238 -238
  7. package/src/factories/MetadataTypeTagFactory.ts +325 -325
  8. package/src/factories/internal/metadata/emend_metadata_atomics.ts +41 -41
  9. package/src/factories/internal/metadata/iterate_metadata_intersection.ts +259 -259
  10. package/src/functional/$HeadersReader.ts +28 -28
  11. package/src/functional/$ParameterReader.ts +31 -31
  12. package/src/functional/$QueryReader.ts +56 -56
  13. package/src/functional/Namespace.ts +142 -142
  14. package/src/http.ts +1149 -1149
  15. package/src/json.ts +648 -648
  16. package/src/misc.ts +651 -651
  17. package/src/module.ts +657 -657
  18. package/src/programmers/helpers/HttpMetadataUtil.ts +21 -21
  19. package/src/programmers/http/HttpAssertHeadersProgrammer.ts +77 -77
  20. package/src/programmers/http/HttpAssertQueryProgrammer.ts +77 -77
  21. package/src/programmers/http/HttpHeadersProgrammer.ts +339 -339
  22. package/src/programmers/http/HttpIsHeadersProgrammer.ts +87 -87
  23. package/src/programmers/http/HttpIsQueryProgrammer.ts +87 -87
  24. package/src/programmers/http/HttpParameterProgrammer.ts +104 -104
  25. package/src/programmers/http/HttpQueryProgrammer.ts +273 -273
  26. package/src/programmers/http/HttpValidateHeadersProgrammer.ts +77 -77
  27. package/src/programmers/http/HttpValidateQueryProgrammer.ts +77 -77
  28. package/src/programmers/internal/application_boolean.ts +30 -30
  29. package/src/programmers/internal/application_number.ts +90 -90
  30. package/src/programmers/internal/application_schema.ts +180 -180
  31. package/src/programmers/internal/application_string.ts +54 -54
  32. package/src/programmers/internal/check_array_length.ts +44 -44
  33. package/src/programmers/internal/check_bigint.ts +48 -48
  34. package/src/programmers/internal/check_number.ts +108 -108
  35. package/src/programmers/internal/check_string.ts +48 -48
  36. package/src/programmers/protobuf/ProtobufEncodeProgrammer.ts +882 -882
  37. package/src/protobuf.ts +887 -887
  38. package/src/schemas/json/IJsonComponents.ts +34 -34
  39. package/src/schemas/json/IJsonSchema.ts +112 -112
  40. package/src/schemas/metadata/IMetadataConstant.ts +25 -25
  41. package/src/schemas/metadata/IMetadataTypeTag.ts +8 -8
  42. package/src/schemas/metadata/Metadata.ts +686 -686
  43. package/src/tags/Default.ts +15 -15
  44. package/src/tags/Format.ts +30 -30
  45. package/src/tags/Pattern.ts +9 -9
  46. package/src/tags/TagBase.ts +68 -68
  47. package/src/tags/index.ts +14 -14
  48. package/src/transformers/CallExpressionTransformer.ts +289 -289
  49. package/src/transformers/features/http/CreateHttpAssertHeadersTransformer.ts +12 -12
  50. package/src/transformers/features/http/CreateHttpAssertQueryTransformer.ts +12 -12
  51. package/src/transformers/features/http/CreateHttpHeadersTransformer.ts +9 -9
  52. package/src/transformers/features/http/CreateHttpIsHeadersTransformer.ts +9 -9
  53. package/src/transformers/features/http/CreateHttpIsQueryTransformer.ts +9 -9
  54. package/src/transformers/features/http/CreateHttpParameterTransformer.ts +9 -9
  55. package/src/transformers/features/http/CreateHttpQueryTransformer.ts +9 -9
  56. package/src/transformers/features/http/CreateHttpValidateHeadersTransformer.ts +12 -12
  57. package/src/transformers/features/http/CreateHttpValidateQueryTransformer.ts +12 -12
  58. package/src/transformers/features/http/HttpAssertHeadersTransformer.ts +10 -10
  59. package/src/transformers/features/http/HttpAssertQueryTransformer.ts +10 -10
  60. package/src/transformers/features/http/HttpHeadersTransformer.ts +9 -9
  61. package/src/transformers/features/http/HttpIsHeadersTransformer.ts +9 -9
  62. package/src/transformers/features/http/HttpIsQueryTransformer.ts +9 -9
  63. package/src/transformers/features/http/HttpParameterTransformer.ts +9 -9
  64. package/src/transformers/features/http/HttpQueryTransformer.ts +9 -9
  65. package/src/transformers/features/http/HttpValidateHeadersTransformer.ts +10 -10
  66. package/src/transformers/features/http/HttpValidateQueryTransformer.ts +10 -10
@@ -1,273 +1,273 @@
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 { FunctionImporter } from "../helpers/FunctionImporeter";
22
- import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
23
-
24
- export namespace HttpQueryProgrammer {
25
- export const INPUT_TYPE = "string | URLSearchParams";
26
-
27
- export const write =
28
- (project: IProject) =>
29
- (modulo: ts.LeftHandSideExpression) =>
30
- (type: ts.Type, name?: string): ts.ArrowFunction => {
31
- // GET OBJECT TYPE
32
- const importer: FunctionImporter = new FunctionImporter(
33
- modulo.getText(),
34
- );
35
- const collection: MetadataCollection = new MetadataCollection();
36
- const result = MetadataFactory.analyze(project.checker)({
37
- escape: false,
38
- constant: true,
39
- absorb: true,
40
- validate,
41
- })(collection)(type);
42
- if (result.success === false)
43
- throw TransformerError.from(`typia.http.${importer.method}`)(
44
- result.errors,
45
- );
46
-
47
- // DO TRANSFORM
48
- const object: MetadataObject = result.data.objects[0]!;
49
- const statements: ts.Statement[] = decode_object(importer)(object);
50
- return ts.factory.createArrowFunction(
51
- undefined,
52
- undefined,
53
- [
54
- IdentifierFactory.parameter(
55
- "input",
56
- ts.factory.createTypeReferenceNode(INPUT_TYPE),
57
- ),
58
- ],
59
- ts.factory.createTypeReferenceNode(
60
- `typia.Resolved<${
61
- name ?? TypeFactory.getFullName(project.checker)(type)
62
- }>`,
63
- ),
64
- undefined,
65
- ts.factory.createBlock(
66
- [...importer.declare(modulo), ...statements],
67
- true,
68
- ),
69
- );
70
- };
71
-
72
- export const validate = (
73
- meta: Metadata,
74
- explore: MetadataFactory.IExplore,
75
- ): string[] => {
76
- const errors: string[] = [];
77
- const insert = (msg: string) => errors.push(msg);
78
-
79
- if (explore.top === true) {
80
- // TOP MUST BE ONLY OBJECT
81
- if (meta.objects.length !== 1 || meta.bucket() !== 1)
82
- insert("only one object type is allowed.");
83
- if (meta.nullable === true)
84
- insert("query parameters cannot be null.");
85
- if (meta.isRequired() === false)
86
- insert("query parameters cannot be undefined.");
87
- } else if (
88
- explore.nested !== null &&
89
- explore.nested instanceof MetadataArrayType
90
- ) {
91
- //----
92
- // ARRAY
93
- //----
94
- const atomics = HttpMetadataUtil.atomics(meta);
95
- const expected: number =
96
- meta.atomics.length +
97
- meta.templates.length +
98
- meta.constants
99
- .map((c) => c.values.length)
100
- .reduce((a, b) => a + b, 0);
101
- if (atomics.size > 1) insert("union type is not allowed in array.");
102
- if (meta.size() !== expected)
103
- insert("only atomic or constant types are allowed in array.");
104
- } else if (explore.object && explore.property !== null) {
105
- //----
106
- // COMMON
107
- //----
108
- // PROPERTY MUST BE SOLE
109
- if (typeof explore.property === "object")
110
- insert("dynamic property is not allowed.");
111
- // DO NOT ALLOW TUPLE TYPE
112
- if (meta.tuples.length) insert("tuple type is not allowed.");
113
- // DO NOT ALLOW UNION TYPE
114
- if (HttpMetadataUtil.isUnion(meta))
115
- insert("union type is not allowed.");
116
- // DO NOT ALLOW NESTED OBJECT
117
- if (
118
- meta.objects.length ||
119
- meta.sets.length ||
120
- meta.maps.length ||
121
- meta.natives.length
122
- )
123
- insert("nested object type is not allowed.");
124
- }
125
- return errors;
126
- };
127
-
128
- const decode_object =
129
- (importer: FunctionImporter) =>
130
- (object: MetadataObject): ts.Statement[] => {
131
- const input: ts.Identifier = ts.factory.createIdentifier("input");
132
- const output: ts.Identifier = ts.factory.createIdentifier("output");
133
-
134
- return [
135
- ts.factory.createExpressionStatement(
136
- ts.factory.createBinaryExpression(
137
- input,
138
- ts.factory.createToken(ts.SyntaxKind.EqualsToken),
139
- ts.factory.createAsExpression(
140
- ts.factory.createCallExpression(
141
- importer.use("params"),
142
- undefined,
143
- [input],
144
- ),
145
- ts.factory.createTypeReferenceNode(
146
- "URLSearchParams",
147
- ),
148
- ),
149
- ),
150
- ),
151
- StatementFactory.constant(
152
- "output",
153
- ts.factory.createObjectLiteralExpression(
154
- object.properties.map((prop) =>
155
- decode_regular_property(importer)(prop),
156
- ),
157
- true,
158
- ),
159
- ),
160
- ts.factory.createReturnStatement(
161
- ts.factory.createAsExpression(
162
- output,
163
- TypeFactory.keyword("any"),
164
- ),
165
- ),
166
- ];
167
- };
168
-
169
- const decode_regular_property =
170
- (importer: FunctionImporter) =>
171
- (property: MetadataProperty): ts.PropertyAssignment => {
172
- const key: string = property.key.constants[0]!.values[0] as string;
173
- const value: Metadata = property.value;
174
-
175
- const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
176
- .length
177
- ? [value.atomics[0]!.type, false]
178
- : value.constants.length
179
- ? [value.constants[0]!.type, false]
180
- : value.templates.length
181
- ? ["string", false]
182
- : (() => {
183
- const meta =
184
- value.arrays[0]?.type.value ??
185
- value.tuples[0]!.type.elements[0]!;
186
- return meta.atomics.length
187
- ? [meta.atomics[0]!.type, true]
188
- : meta.templates.length
189
- ? ["string", true]
190
- : [meta.constants[0]!.type, true];
191
- })();
192
- return ts.factory.createPropertyAssignment(
193
- Escaper.variable(key)
194
- ? key
195
- : ts.factory.createStringLiteral(key),
196
- isArray
197
- ? decode_array(importer)(value)(
198
- ts.factory.createCallExpression(
199
- IdentifierFactory.access(
200
- ts.factory.createCallExpression(
201
- ts.factory.createIdentifier(
202
- "input.getAll",
203
- ),
204
- undefined,
205
- [ts.factory.createStringLiteral(key)],
206
- ),
207
- )("map"),
208
- undefined,
209
- [
210
- ts.factory.createArrowFunction(
211
- undefined,
212
- undefined,
213
- [IdentifierFactory.parameter("elem")],
214
- undefined,
215
- undefined,
216
- decode_value(importer)(type)(false)(
217
- ts.factory.createIdentifier("elem"),
218
- ),
219
- ),
220
- ],
221
- ),
222
- )
223
- : decode_value(importer)(type)(
224
- value.nullable === false &&
225
- value.isRequired() === false,
226
- )(
227
- ts.factory.createCallExpression(
228
- ts.factory.createIdentifier("input.get"),
229
- undefined,
230
- [ts.factory.createStringLiteral(key)],
231
- ),
232
- ),
233
- );
234
- };
235
-
236
- const decode_value =
237
- (importer: FunctionImporter) =>
238
- (type: Atomic.Literal) =>
239
- (onlyUndefindable: boolean) =>
240
- (value: ts.Expression) => {
241
- const call = ts.factory.createCallExpression(
242
- importer.use(type),
243
- undefined,
244
- [value],
245
- );
246
- return onlyUndefindable
247
- ? ts.factory.createBinaryExpression(
248
- call,
249
- ts.factory.createToken(
250
- ts.SyntaxKind.QuestionQuestionToken,
251
- ),
252
- ts.factory.createIdentifier("undefined"),
253
- )
254
- : call;
255
- };
256
-
257
- const decode_array =
258
- (importer: FunctionImporter) =>
259
- (value: Metadata) =>
260
- (expression: ts.Expression): ts.Expression =>
261
- value.nullable || value.isRequired() === false
262
- ? ts.factory.createCallExpression(
263
- importer.use("array"),
264
- undefined,
265
- [
266
- expression,
267
- value.nullable
268
- ? ts.factory.createNull()
269
- : ts.factory.createIdentifier("undefined"),
270
- ],
271
- )
272
- : expression;
273
- }
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 { FunctionImporter } from "../helpers/FunctionImporeter";
22
+ import { HttpMetadataUtil } from "../helpers/HttpMetadataUtil";
23
+
24
+ export namespace HttpQueryProgrammer {
25
+ export const INPUT_TYPE = "string | URLSearchParams";
26
+
27
+ export const write =
28
+ (project: IProject) =>
29
+ (modulo: ts.LeftHandSideExpression) =>
30
+ (type: ts.Type, name?: string): ts.ArrowFunction => {
31
+ // GET OBJECT TYPE
32
+ const importer: FunctionImporter = new FunctionImporter(
33
+ modulo.getText(),
34
+ );
35
+ const collection: MetadataCollection = new MetadataCollection();
36
+ const result = MetadataFactory.analyze(project.checker)({
37
+ escape: false,
38
+ constant: true,
39
+ absorb: true,
40
+ validate,
41
+ })(collection)(type);
42
+ if (result.success === false)
43
+ throw TransformerError.from(`typia.http.${importer.method}`)(
44
+ result.errors,
45
+ );
46
+
47
+ // DO TRANSFORM
48
+ const object: MetadataObject = result.data.objects[0]!;
49
+ const statements: ts.Statement[] = decode_object(importer)(object);
50
+ return ts.factory.createArrowFunction(
51
+ undefined,
52
+ undefined,
53
+ [
54
+ IdentifierFactory.parameter(
55
+ "input",
56
+ ts.factory.createTypeReferenceNode(INPUT_TYPE),
57
+ ),
58
+ ],
59
+ ts.factory.createTypeReferenceNode(
60
+ `typia.Resolved<${
61
+ name ?? TypeFactory.getFullName(project.checker)(type)
62
+ }>`,
63
+ ),
64
+ undefined,
65
+ ts.factory.createBlock(
66
+ [...importer.declare(modulo), ...statements],
67
+ true,
68
+ ),
69
+ );
70
+ };
71
+
72
+ export const validate = (
73
+ meta: Metadata,
74
+ explore: MetadataFactory.IExplore,
75
+ ): string[] => {
76
+ const errors: string[] = [];
77
+ const insert = (msg: string) => errors.push(msg);
78
+
79
+ if (explore.top === true) {
80
+ // TOP MUST BE ONLY OBJECT
81
+ if (meta.objects.length !== 1 || meta.bucket() !== 1)
82
+ insert("only one object type is allowed.");
83
+ if (meta.nullable === true)
84
+ insert("query parameters cannot be null.");
85
+ if (meta.isRequired() === false)
86
+ insert("query parameters cannot be undefined.");
87
+ } else if (
88
+ explore.nested !== null &&
89
+ explore.nested instanceof MetadataArrayType
90
+ ) {
91
+ //----
92
+ // ARRAY
93
+ //----
94
+ const atomics = HttpMetadataUtil.atomics(meta);
95
+ const expected: number =
96
+ meta.atomics.length +
97
+ meta.templates.length +
98
+ meta.constants
99
+ .map((c) => c.values.length)
100
+ .reduce((a, b) => a + b, 0);
101
+ if (atomics.size > 1) insert("union type is not allowed in array.");
102
+ if (meta.size() !== expected)
103
+ insert("only atomic or constant types are allowed in array.");
104
+ } else if (explore.object && explore.property !== null) {
105
+ //----
106
+ // COMMON
107
+ //----
108
+ // PROPERTY MUST BE SOLE
109
+ if (typeof explore.property === "object")
110
+ insert("dynamic property is not allowed.");
111
+ // DO NOT ALLOW TUPLE TYPE
112
+ if (meta.tuples.length) insert("tuple type is not allowed.");
113
+ // DO NOT ALLOW UNION TYPE
114
+ if (HttpMetadataUtil.isUnion(meta))
115
+ insert("union type is not allowed.");
116
+ // DO NOT ALLOW NESTED OBJECT
117
+ if (
118
+ meta.objects.length ||
119
+ meta.sets.length ||
120
+ meta.maps.length ||
121
+ meta.natives.length
122
+ )
123
+ insert("nested object type is not allowed.");
124
+ }
125
+ return errors;
126
+ };
127
+
128
+ const decode_object =
129
+ (importer: FunctionImporter) =>
130
+ (object: MetadataObject): ts.Statement[] => {
131
+ const input: ts.Identifier = ts.factory.createIdentifier("input");
132
+ const output: ts.Identifier = ts.factory.createIdentifier("output");
133
+
134
+ return [
135
+ ts.factory.createExpressionStatement(
136
+ ts.factory.createBinaryExpression(
137
+ input,
138
+ ts.factory.createToken(ts.SyntaxKind.EqualsToken),
139
+ ts.factory.createAsExpression(
140
+ ts.factory.createCallExpression(
141
+ importer.use("params"),
142
+ undefined,
143
+ [input],
144
+ ),
145
+ ts.factory.createTypeReferenceNode(
146
+ "URLSearchParams",
147
+ ),
148
+ ),
149
+ ),
150
+ ),
151
+ StatementFactory.constant(
152
+ "output",
153
+ ts.factory.createObjectLiteralExpression(
154
+ object.properties.map((prop) =>
155
+ decode_regular_property(importer)(prop),
156
+ ),
157
+ true,
158
+ ),
159
+ ),
160
+ ts.factory.createReturnStatement(
161
+ ts.factory.createAsExpression(
162
+ output,
163
+ TypeFactory.keyword("any"),
164
+ ),
165
+ ),
166
+ ];
167
+ };
168
+
169
+ const decode_regular_property =
170
+ (importer: FunctionImporter) =>
171
+ (property: MetadataProperty): ts.PropertyAssignment => {
172
+ const key: string = property.key.constants[0]!.values[0] as string;
173
+ const value: Metadata = property.value;
174
+
175
+ const [type, isArray]: [Atomic.Literal, boolean] = value.atomics
176
+ .length
177
+ ? [value.atomics[0]!.type, false]
178
+ : value.constants.length
179
+ ? [value.constants[0]!.type, false]
180
+ : value.templates.length
181
+ ? ["string", false]
182
+ : (() => {
183
+ const meta =
184
+ value.arrays[0]?.type.value ??
185
+ value.tuples[0]!.type.elements[0]!;
186
+ return meta.atomics.length
187
+ ? [meta.atomics[0]!.type, true]
188
+ : meta.templates.length
189
+ ? ["string", true]
190
+ : [meta.constants[0]!.type, true];
191
+ })();
192
+ return ts.factory.createPropertyAssignment(
193
+ Escaper.variable(key)
194
+ ? key
195
+ : ts.factory.createStringLiteral(key),
196
+ isArray
197
+ ? decode_array(importer)(value)(
198
+ ts.factory.createCallExpression(
199
+ IdentifierFactory.access(
200
+ ts.factory.createCallExpression(
201
+ ts.factory.createIdentifier(
202
+ "input.getAll",
203
+ ),
204
+ undefined,
205
+ [ts.factory.createStringLiteral(key)],
206
+ ),
207
+ )("map"),
208
+ undefined,
209
+ [
210
+ ts.factory.createArrowFunction(
211
+ undefined,
212
+ undefined,
213
+ [IdentifierFactory.parameter("elem")],
214
+ undefined,
215
+ undefined,
216
+ decode_value(importer)(type)(false)(
217
+ ts.factory.createIdentifier("elem"),
218
+ ),
219
+ ),
220
+ ],
221
+ ),
222
+ )
223
+ : decode_value(importer)(type)(
224
+ value.nullable === false &&
225
+ value.isRequired() === false,
226
+ )(
227
+ ts.factory.createCallExpression(
228
+ ts.factory.createIdentifier("input.get"),
229
+ undefined,
230
+ [ts.factory.createStringLiteral(key)],
231
+ ),
232
+ ),
233
+ );
234
+ };
235
+
236
+ const decode_value =
237
+ (importer: FunctionImporter) =>
238
+ (type: Atomic.Literal) =>
239
+ (onlyUndefindable: boolean) =>
240
+ (value: ts.Expression) => {
241
+ const call = ts.factory.createCallExpression(
242
+ importer.use(type),
243
+ undefined,
244
+ [value],
245
+ );
246
+ return onlyUndefindable
247
+ ? ts.factory.createBinaryExpression(
248
+ call,
249
+ ts.factory.createToken(
250
+ ts.SyntaxKind.QuestionQuestionToken,
251
+ ),
252
+ ts.factory.createIdentifier("undefined"),
253
+ )
254
+ : call;
255
+ };
256
+
257
+ const decode_array =
258
+ (importer: FunctionImporter) =>
259
+ (value: Metadata) =>
260
+ (expression: ts.Expression): ts.Expression =>
261
+ value.nullable || value.isRequired() === false
262
+ ? ts.factory.createCallExpression(
263
+ importer.use("array"),
264
+ undefined,
265
+ [
266
+ expression,
267
+ value.nullable
268
+ ? ts.factory.createNull()
269
+ : ts.factory.createIdentifier("undefined"),
270
+ ],
271
+ )
272
+ : expression;
273
+ }