@arrirpc/codegen-ts 0.78.0 → 0.79.0
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/dist/index.cjs +47 -47
- package/dist/index.mjs +47 -47
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -10,7 +10,7 @@ const prettier__default = /*#__PURE__*/_interopDefaultCompat(prettier);
|
|
|
10
10
|
|
|
11
11
|
function tsAnyFromSchema(schema, context) {
|
|
12
12
|
const typeName = "any";
|
|
13
|
-
const defaultValue = schema.
|
|
13
|
+
const defaultValue = schema.isNullable ? "null" : "undefined";
|
|
14
14
|
return {
|
|
15
15
|
typeName,
|
|
16
16
|
defaultValue,
|
|
@@ -92,16 +92,16 @@ function tsArrayFromSchema(schema, context) {
|
|
|
92
92
|
rpcGenerators: context.rpcGenerators
|
|
93
93
|
});
|
|
94
94
|
const typeName = `(${innerType.typeName})[]`;
|
|
95
|
-
const defaultValue = schema.
|
|
95
|
+
const defaultValue = schema.isNullable ? "null" : "[]";
|
|
96
96
|
return {
|
|
97
|
-
typeName: schema.
|
|
97
|
+
typeName: schema.isNullable ? `${typeName} | null` : typeName,
|
|
98
98
|
defaultValue,
|
|
99
99
|
validationTemplate(input) {
|
|
100
100
|
const mainPart = `Array.isArray(${input})
|
|
101
101
|
&& ${input}.every(
|
|
102
102
|
(_element) => ${innerType.validationTemplate("_element")}
|
|
103
103
|
)`;
|
|
104
|
-
if (schema.
|
|
104
|
+
if (schema.isNullable) {
|
|
105
105
|
return `((${mainPart}) ||
|
|
106
106
|
${input} === null)`;
|
|
107
107
|
}
|
|
@@ -122,7 +122,7 @@ function tsArrayFromSchema(schema, context) {
|
|
|
122
122
|
toJsonTemplate(input, target) {
|
|
123
123
|
const elVar = `_${codegenUtils.camelCase(validVarName(input.split(".").join("_")), { normalize: true })}El`;
|
|
124
124
|
const elKeyVar = `${elVar}Key`;
|
|
125
|
-
if (schema.
|
|
125
|
+
if (schema.isNullable) {
|
|
126
126
|
return `if (${input} !== null) {
|
|
127
127
|
${target} += '[';
|
|
128
128
|
for (let i = 0; i < ${input}.length; i++) {
|
|
@@ -153,12 +153,12 @@ function tsArrayFromSchema(schema, context) {
|
|
|
153
153
|
function tsObjectFromSchema(schema, context) {
|
|
154
154
|
const typeName = getTsTypeName(schema, context);
|
|
155
155
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
156
|
-
const defaultValue = schema.
|
|
156
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
|
|
157
157
|
const result = {
|
|
158
|
-
typeName: schema.
|
|
158
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
159
159
|
defaultValue,
|
|
160
160
|
validationTemplate(input) {
|
|
161
|
-
if (schema.
|
|
161
|
+
if (schema.isNullable) {
|
|
162
162
|
return `($$${prefixedTypeName}.validate(${input}) || ${input} === null)`;
|
|
163
163
|
}
|
|
164
164
|
return `$$${prefixedTypeName}.validate(${input})`;
|
|
@@ -171,7 +171,7 @@ function tsObjectFromSchema(schema, context) {
|
|
|
171
171
|
}`;
|
|
172
172
|
},
|
|
173
173
|
toJsonTemplate(input, target) {
|
|
174
|
-
if (schema.
|
|
174
|
+
if (schema.isNullable) {
|
|
175
175
|
return `if (${input} !== null) {
|
|
176
176
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
177
177
|
} else {
|
|
@@ -348,13 +348,13 @@ ${subContentParts.join("\n")}`;
|
|
|
348
348
|
function tsTaggedUnionFromSchema(schema, context) {
|
|
349
349
|
const typeName = getTsTypeName(schema, context);
|
|
350
350
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
351
|
-
const defaultValue = schema.
|
|
351
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
|
|
352
352
|
const result = {
|
|
353
|
-
typeName: schema.
|
|
353
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
354
354
|
defaultValue,
|
|
355
355
|
validationTemplate(input) {
|
|
356
356
|
const mainPart = `$$${prefixedTypeName}.validate(${input})`;
|
|
357
|
-
if (schema.
|
|
357
|
+
if (schema.isNullable) {
|
|
358
358
|
return `(${mainPart} || ${input} === null)`;
|
|
359
359
|
}
|
|
360
360
|
return mainPart;
|
|
@@ -367,7 +367,7 @@ function tsTaggedUnionFromSchema(schema, context) {
|
|
|
367
367
|
}`;
|
|
368
368
|
},
|
|
369
369
|
toJsonTemplate(input, target, _key) {
|
|
370
|
-
if (schema.
|
|
370
|
+
if (schema.isNullable) {
|
|
371
371
|
return `if (${input} != null) {
|
|
372
372
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
373
373
|
} else {
|
|
@@ -469,13 +469,13 @@ function tsEnumFromSchema(schema, context) {
|
|
|
469
469
|
);
|
|
470
470
|
const enumName = getTsTypeName(schema, context);
|
|
471
471
|
const prefixedEnumName = `${context.typePrefix}${enumName}`;
|
|
472
|
-
const typeName = schema.
|
|
473
|
-
const defaultValue = schema.
|
|
472
|
+
const typeName = schema.isNullable ? `${prefixedEnumName} | null` : prefixedEnumName;
|
|
473
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedEnumName}.new()`;
|
|
474
474
|
const result = {
|
|
475
475
|
typeName,
|
|
476
476
|
defaultValue,
|
|
477
477
|
validationTemplate(input) {
|
|
478
|
-
if (schema.
|
|
478
|
+
if (schema.isNullable) {
|
|
479
479
|
return `($$${prefixedEnumName}.validate(${input}) || ${input} === null)`;
|
|
480
480
|
}
|
|
481
481
|
return `$$${prefixedEnumName}.validate(${input})`;
|
|
@@ -488,7 +488,7 @@ function tsEnumFromSchema(schema, context) {
|
|
|
488
488
|
}`;
|
|
489
489
|
},
|
|
490
490
|
toJsonTemplate(input, target) {
|
|
491
|
-
if (schema.
|
|
491
|
+
if (schema.isNullable) {
|
|
492
492
|
return `if (typeof ${input} === 'string') {
|
|
493
493
|
${target} += \`"\${${input}}"\`;
|
|
494
494
|
} else {
|
|
@@ -539,13 +539,13 @@ export const $$${name}: ArriEnumValidator<${name}> = {
|
|
|
539
539
|
}
|
|
540
540
|
|
|
541
541
|
function tsStringFromSchema(schema, _context) {
|
|
542
|
-
const typeName = schema.
|
|
543
|
-
const defaultValue = schema.
|
|
542
|
+
const typeName = schema.isNullable ? "string | null" : "string";
|
|
543
|
+
const defaultValue = schema.isNullable ? "null" : '""';
|
|
544
544
|
return {
|
|
545
545
|
typeName,
|
|
546
546
|
defaultValue,
|
|
547
547
|
validationTemplate(input) {
|
|
548
|
-
if (schema.
|
|
548
|
+
if (schema.isNullable) {
|
|
549
549
|
return `(typeof ${input} === 'string' || ${input} === null)`;
|
|
550
550
|
}
|
|
551
551
|
return `typeof ${input} === 'string'`;
|
|
@@ -558,7 +558,7 @@ function tsStringFromSchema(schema, _context) {
|
|
|
558
558
|
}`;
|
|
559
559
|
},
|
|
560
560
|
toJsonTemplate(input, target) {
|
|
561
|
-
if (schema.
|
|
561
|
+
if (schema.isNullable) {
|
|
562
562
|
return `if (typeof ${input} === 'string') {
|
|
563
563
|
${target} += serializeString(${input});
|
|
564
564
|
} else {
|
|
@@ -574,13 +574,13 @@ function tsStringFromSchema(schema, _context) {
|
|
|
574
574
|
};
|
|
575
575
|
}
|
|
576
576
|
function tsBooleanFromSchema(schema, _context) {
|
|
577
|
-
const typeName = schema.
|
|
578
|
-
const defaultValue = schema.
|
|
577
|
+
const typeName = schema.isNullable ? `boolean | null` : "boolean";
|
|
578
|
+
const defaultValue = schema.isNullable ? `null` : "false";
|
|
579
579
|
return {
|
|
580
580
|
typeName,
|
|
581
581
|
defaultValue,
|
|
582
582
|
validationTemplate(input) {
|
|
583
|
-
if (schema.
|
|
583
|
+
if (schema.isNullable) {
|
|
584
584
|
return `(typeof ${input} === 'boolean' || ${input} === null)`;
|
|
585
585
|
}
|
|
586
586
|
return `typeof ${input} === 'boolean'`;
|
|
@@ -602,13 +602,13 @@ function tsBooleanFromSchema(schema, _context) {
|
|
|
602
602
|
};
|
|
603
603
|
}
|
|
604
604
|
function tsDateFromSchema(schema, _context) {
|
|
605
|
-
const typeName = schema.
|
|
606
|
-
const defaultValue = schema.
|
|
605
|
+
const typeName = schema.isNullable ? `Date | null` : "Date";
|
|
606
|
+
const defaultValue = schema.isNullable ? `null` : "new Date()";
|
|
607
607
|
return {
|
|
608
608
|
typeName,
|
|
609
609
|
defaultValue,
|
|
610
610
|
validationTemplate(input) {
|
|
611
|
-
if (schema.
|
|
611
|
+
if (schema.isNullable) {
|
|
612
612
|
return `(${input} instanceof Date || ${input} === null)`;
|
|
613
613
|
}
|
|
614
614
|
return `${input} instanceof Date`;
|
|
@@ -623,7 +623,7 @@ function tsDateFromSchema(schema, _context) {
|
|
|
623
623
|
}`;
|
|
624
624
|
},
|
|
625
625
|
toJsonTemplate(input, target) {
|
|
626
|
-
if (schema.
|
|
626
|
+
if (schema.isNullable) {
|
|
627
627
|
return `if (${input} instanceof Date) {
|
|
628
628
|
${target} += \`"\${${input}.toISOString()}"\`
|
|
629
629
|
} else {
|
|
@@ -633,7 +633,7 @@ function tsDateFromSchema(schema, _context) {
|
|
|
633
633
|
return `${target} += \`"\${${input}.toISOString()}"\``;
|
|
634
634
|
},
|
|
635
635
|
toQueryStringTemplate(input, target, key) {
|
|
636
|
-
if (schema.
|
|
636
|
+
if (schema.isNullable) {
|
|
637
637
|
return `${target}.push(\`${key}=\${${input}?.toISOString()}\`)`;
|
|
638
638
|
}
|
|
639
639
|
return `${target}.push(\`${key}=\${${input}.toISOString()}\`)`;
|
|
@@ -642,13 +642,13 @@ function tsDateFromSchema(schema, _context) {
|
|
|
642
642
|
};
|
|
643
643
|
}
|
|
644
644
|
function tsFloatFromSchema(schema, _context) {
|
|
645
|
-
const typeName = schema.
|
|
646
|
-
const defaultValue = schema.
|
|
645
|
+
const typeName = schema.isNullable ? "number | null" : "number";
|
|
646
|
+
const defaultValue = schema.isNullable ? "null" : "0";
|
|
647
647
|
return {
|
|
648
648
|
typeName,
|
|
649
649
|
defaultValue,
|
|
650
650
|
validationTemplate(input) {
|
|
651
|
-
if (schema.
|
|
651
|
+
if (schema.isNullable) {
|
|
652
652
|
return `(typeof ${input} === 'number' || ${input} === null)`;
|
|
653
653
|
}
|
|
654
654
|
return `typeof ${input} === 'number'`;
|
|
@@ -670,8 +670,8 @@ function tsFloatFromSchema(schema, _context) {
|
|
|
670
670
|
};
|
|
671
671
|
}
|
|
672
672
|
function tsIntFromSchema(schema, intType, _context) {
|
|
673
|
-
const typeName = schema.
|
|
674
|
-
const defaultValue = schema.
|
|
673
|
+
const typeName = schema.isNullable ? "number | null" : "number";
|
|
674
|
+
const defaultValue = schema.isNullable ? "null" : "0";
|
|
675
675
|
let min;
|
|
676
676
|
let max;
|
|
677
677
|
switch (intType) {
|
|
@@ -705,7 +705,7 @@ function tsIntFromSchema(schema, intType, _context) {
|
|
|
705
705
|
defaultValue,
|
|
706
706
|
validationTemplate(input) {
|
|
707
707
|
const mainPart = `typeof ${input} === 'number' && Number.isInteger(${input}) && ${input} >= ${min} && ${input} <= ${max}`;
|
|
708
|
-
if (schema.
|
|
708
|
+
if (schema.isNullable) {
|
|
709
709
|
return `((${mainPart}) || ${input} === null)`;
|
|
710
710
|
}
|
|
711
711
|
return mainPart;
|
|
@@ -732,14 +732,14 @@ function tsIntFromSchema(schema, intType, _context) {
|
|
|
732
732
|
};
|
|
733
733
|
}
|
|
734
734
|
function tsBigIntFromSchema(schema, isUnsigned, _context) {
|
|
735
|
-
const typeName = schema.
|
|
736
|
-
const defaultValue = schema.
|
|
735
|
+
const typeName = schema.isNullable ? `bigint | null` : `bigint`;
|
|
736
|
+
const defaultValue = schema.isNullable ? `null` : "BigInt(0)";
|
|
737
737
|
return {
|
|
738
738
|
typeName,
|
|
739
739
|
defaultValue,
|
|
740
740
|
validationTemplate(input) {
|
|
741
741
|
const mainPart = isUnsigned ? `typeof ${input} === 'bigint' && ${input} >= BigInt(0) && ${input} <= UINT64_MAX` : `typeof ${input} === 'bigint' && ${input} >= INT64_MIN && ${input} <= INT64_MAX`;
|
|
742
|
-
if (schema.
|
|
742
|
+
if (schema.isNullable) {
|
|
743
743
|
return `((${mainPart}) || ${input} === null)`;
|
|
744
744
|
}
|
|
745
745
|
return mainPart;
|
|
@@ -763,7 +763,7 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
|
|
|
763
763
|
}`;
|
|
764
764
|
},
|
|
765
765
|
toJsonTemplate(input, target) {
|
|
766
|
-
if (schema.
|
|
766
|
+
if (schema.isNullable) {
|
|
767
767
|
return `if (typeof ${input} === 'bigint') {
|
|
768
768
|
${target} += \`"\${${input}}"\`
|
|
769
769
|
} else {
|
|
@@ -794,15 +794,15 @@ function tsRecordFromSchema(schema, context) {
|
|
|
794
794
|
rpcGenerators: context.rpcGenerators
|
|
795
795
|
});
|
|
796
796
|
const typeName = `Record<string, ${innerType.typeName}>`;
|
|
797
|
-
const defaultValue = schema.
|
|
797
|
+
const defaultValue = schema.isNullable ? "null" : "{}";
|
|
798
798
|
return {
|
|
799
|
-
typeName: schema.
|
|
799
|
+
typeName: schema.isNullable ? `${typeName} | null` : typeName,
|
|
800
800
|
defaultValue,
|
|
801
801
|
validationTemplate(input) {
|
|
802
802
|
const mainPart = `isObject(${input}) && Object.values(${input}).every(
|
|
803
803
|
(_value) => ${innerType.validationTemplate("_value")},
|
|
804
804
|
)`;
|
|
805
|
-
if (schema.
|
|
805
|
+
if (schema.isNullable) {
|
|
806
806
|
return `((${mainPart}) || ${input} === null)`;
|
|
807
807
|
}
|
|
808
808
|
return mainPart;
|
|
@@ -821,7 +821,7 @@ function tsRecordFromSchema(schema, context) {
|
|
|
821
821
|
},
|
|
822
822
|
toJsonTemplate(input, target, key) {
|
|
823
823
|
const countVal = `_${validVarName(key)}PropertyCount`;
|
|
824
|
-
if (schema.
|
|
824
|
+
if (schema.isNullable) {
|
|
825
825
|
return `if (${input} !== null) {
|
|
826
826
|
${target} += '{';
|
|
827
827
|
let ${countVal} = 0;
|
|
@@ -861,12 +861,12 @@ function tsRecordFromSchema(schema, context) {
|
|
|
861
861
|
function tsRefFromSchema(schema, context) {
|
|
862
862
|
const typeName = codegenUtils.pascalCase(validVarName(schema.ref), { normalize: true });
|
|
863
863
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
864
|
-
const defaultValue = schema.
|
|
864
|
+
const defaultValue = schema.isNullable ? "null" : `${prefixedTypeName}.new()`;
|
|
865
865
|
return {
|
|
866
|
-
typeName: schema.
|
|
866
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
867
867
|
defaultValue,
|
|
868
868
|
validationTemplate(input) {
|
|
869
|
-
if (schema.
|
|
869
|
+
if (schema.isNullable) {
|
|
870
870
|
return `($$${prefixedTypeName}.validate(${input}) || ${input} === null)`;
|
|
871
871
|
}
|
|
872
872
|
return `$$${prefixedTypeName}.validate(${input})`;
|
|
@@ -879,7 +879,7 @@ function tsRefFromSchema(schema, context) {
|
|
|
879
879
|
}`;
|
|
880
880
|
},
|
|
881
881
|
toJsonTemplate(input, target, _key) {
|
|
882
|
-
if (schema.
|
|
882
|
+
if (schema.isNullable) {
|
|
883
883
|
return `if (${input} !== null) {
|
|
884
884
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
885
885
|
} else {
|
package/dist/index.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import prettier from 'prettier';
|
|
|
4
4
|
|
|
5
5
|
function tsAnyFromSchema(schema, context) {
|
|
6
6
|
const typeName = "any";
|
|
7
|
-
const defaultValue = schema.
|
|
7
|
+
const defaultValue = schema.isNullable ? "null" : "undefined";
|
|
8
8
|
return {
|
|
9
9
|
typeName,
|
|
10
10
|
defaultValue,
|
|
@@ -86,16 +86,16 @@ function tsArrayFromSchema(schema, context) {
|
|
|
86
86
|
rpcGenerators: context.rpcGenerators
|
|
87
87
|
});
|
|
88
88
|
const typeName = `(${innerType.typeName})[]`;
|
|
89
|
-
const defaultValue = schema.
|
|
89
|
+
const defaultValue = schema.isNullable ? "null" : "[]";
|
|
90
90
|
return {
|
|
91
|
-
typeName: schema.
|
|
91
|
+
typeName: schema.isNullable ? `${typeName} | null` : typeName,
|
|
92
92
|
defaultValue,
|
|
93
93
|
validationTemplate(input) {
|
|
94
94
|
const mainPart = `Array.isArray(${input})
|
|
95
95
|
&& ${input}.every(
|
|
96
96
|
(_element) => ${innerType.validationTemplate("_element")}
|
|
97
97
|
)`;
|
|
98
|
-
if (schema.
|
|
98
|
+
if (schema.isNullable) {
|
|
99
99
|
return `((${mainPart}) ||
|
|
100
100
|
${input} === null)`;
|
|
101
101
|
}
|
|
@@ -116,7 +116,7 @@ function tsArrayFromSchema(schema, context) {
|
|
|
116
116
|
toJsonTemplate(input, target) {
|
|
117
117
|
const elVar = `_${camelCase(validVarName(input.split(".").join("_")), { normalize: true })}El`;
|
|
118
118
|
const elKeyVar = `${elVar}Key`;
|
|
119
|
-
if (schema.
|
|
119
|
+
if (schema.isNullable) {
|
|
120
120
|
return `if (${input} !== null) {
|
|
121
121
|
${target} += '[';
|
|
122
122
|
for (let i = 0; i < ${input}.length; i++) {
|
|
@@ -147,12 +147,12 @@ function tsArrayFromSchema(schema, context) {
|
|
|
147
147
|
function tsObjectFromSchema(schema, context) {
|
|
148
148
|
const typeName = getTsTypeName(schema, context);
|
|
149
149
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
150
|
-
const defaultValue = schema.
|
|
150
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
|
|
151
151
|
const result = {
|
|
152
|
-
typeName: schema.
|
|
152
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
153
153
|
defaultValue,
|
|
154
154
|
validationTemplate(input) {
|
|
155
|
-
if (schema.
|
|
155
|
+
if (schema.isNullable) {
|
|
156
156
|
return `($$${prefixedTypeName}.validate(${input}) || ${input} === null)`;
|
|
157
157
|
}
|
|
158
158
|
return `$$${prefixedTypeName}.validate(${input})`;
|
|
@@ -165,7 +165,7 @@ function tsObjectFromSchema(schema, context) {
|
|
|
165
165
|
}`;
|
|
166
166
|
},
|
|
167
167
|
toJsonTemplate(input, target) {
|
|
168
|
-
if (schema.
|
|
168
|
+
if (schema.isNullable) {
|
|
169
169
|
return `if (${input} !== null) {
|
|
170
170
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
171
171
|
} else {
|
|
@@ -342,13 +342,13 @@ ${subContentParts.join("\n")}`;
|
|
|
342
342
|
function tsTaggedUnionFromSchema(schema, context) {
|
|
343
343
|
const typeName = getTsTypeName(schema, context);
|
|
344
344
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
345
|
-
const defaultValue = schema.
|
|
345
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
|
|
346
346
|
const result = {
|
|
347
|
-
typeName: schema.
|
|
347
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
348
348
|
defaultValue,
|
|
349
349
|
validationTemplate(input) {
|
|
350
350
|
const mainPart = `$$${prefixedTypeName}.validate(${input})`;
|
|
351
|
-
if (schema.
|
|
351
|
+
if (schema.isNullable) {
|
|
352
352
|
return `(${mainPart} || ${input} === null)`;
|
|
353
353
|
}
|
|
354
354
|
return mainPart;
|
|
@@ -361,7 +361,7 @@ function tsTaggedUnionFromSchema(schema, context) {
|
|
|
361
361
|
}`;
|
|
362
362
|
},
|
|
363
363
|
toJsonTemplate(input, target, _key) {
|
|
364
|
-
if (schema.
|
|
364
|
+
if (schema.isNullable) {
|
|
365
365
|
return `if (${input} != null) {
|
|
366
366
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
367
367
|
} else {
|
|
@@ -463,13 +463,13 @@ function tsEnumFromSchema(schema, context) {
|
|
|
463
463
|
);
|
|
464
464
|
const enumName = getTsTypeName(schema, context);
|
|
465
465
|
const prefixedEnumName = `${context.typePrefix}${enumName}`;
|
|
466
|
-
const typeName = schema.
|
|
467
|
-
const defaultValue = schema.
|
|
466
|
+
const typeName = schema.isNullable ? `${prefixedEnumName} | null` : prefixedEnumName;
|
|
467
|
+
const defaultValue = schema.isNullable ? "null" : `$$${prefixedEnumName}.new()`;
|
|
468
468
|
const result = {
|
|
469
469
|
typeName,
|
|
470
470
|
defaultValue,
|
|
471
471
|
validationTemplate(input) {
|
|
472
|
-
if (schema.
|
|
472
|
+
if (schema.isNullable) {
|
|
473
473
|
return `($$${prefixedEnumName}.validate(${input}) || ${input} === null)`;
|
|
474
474
|
}
|
|
475
475
|
return `$$${prefixedEnumName}.validate(${input})`;
|
|
@@ -482,7 +482,7 @@ function tsEnumFromSchema(schema, context) {
|
|
|
482
482
|
}`;
|
|
483
483
|
},
|
|
484
484
|
toJsonTemplate(input, target) {
|
|
485
|
-
if (schema.
|
|
485
|
+
if (schema.isNullable) {
|
|
486
486
|
return `if (typeof ${input} === 'string') {
|
|
487
487
|
${target} += \`"\${${input}}"\`;
|
|
488
488
|
} else {
|
|
@@ -533,13 +533,13 @@ export const $$${name}: ArriEnumValidator<${name}> = {
|
|
|
533
533
|
}
|
|
534
534
|
|
|
535
535
|
function tsStringFromSchema(schema, _context) {
|
|
536
|
-
const typeName = schema.
|
|
537
|
-
const defaultValue = schema.
|
|
536
|
+
const typeName = schema.isNullable ? "string | null" : "string";
|
|
537
|
+
const defaultValue = schema.isNullable ? "null" : '""';
|
|
538
538
|
return {
|
|
539
539
|
typeName,
|
|
540
540
|
defaultValue,
|
|
541
541
|
validationTemplate(input) {
|
|
542
|
-
if (schema.
|
|
542
|
+
if (schema.isNullable) {
|
|
543
543
|
return `(typeof ${input} === 'string' || ${input} === null)`;
|
|
544
544
|
}
|
|
545
545
|
return `typeof ${input} === 'string'`;
|
|
@@ -552,7 +552,7 @@ function tsStringFromSchema(schema, _context) {
|
|
|
552
552
|
}`;
|
|
553
553
|
},
|
|
554
554
|
toJsonTemplate(input, target) {
|
|
555
|
-
if (schema.
|
|
555
|
+
if (schema.isNullable) {
|
|
556
556
|
return `if (typeof ${input} === 'string') {
|
|
557
557
|
${target} += serializeString(${input});
|
|
558
558
|
} else {
|
|
@@ -568,13 +568,13 @@ function tsStringFromSchema(schema, _context) {
|
|
|
568
568
|
};
|
|
569
569
|
}
|
|
570
570
|
function tsBooleanFromSchema(schema, _context) {
|
|
571
|
-
const typeName = schema.
|
|
572
|
-
const defaultValue = schema.
|
|
571
|
+
const typeName = schema.isNullable ? `boolean | null` : "boolean";
|
|
572
|
+
const defaultValue = schema.isNullable ? `null` : "false";
|
|
573
573
|
return {
|
|
574
574
|
typeName,
|
|
575
575
|
defaultValue,
|
|
576
576
|
validationTemplate(input) {
|
|
577
|
-
if (schema.
|
|
577
|
+
if (schema.isNullable) {
|
|
578
578
|
return `(typeof ${input} === 'boolean' || ${input} === null)`;
|
|
579
579
|
}
|
|
580
580
|
return `typeof ${input} === 'boolean'`;
|
|
@@ -596,13 +596,13 @@ function tsBooleanFromSchema(schema, _context) {
|
|
|
596
596
|
};
|
|
597
597
|
}
|
|
598
598
|
function tsDateFromSchema(schema, _context) {
|
|
599
|
-
const typeName = schema.
|
|
600
|
-
const defaultValue = schema.
|
|
599
|
+
const typeName = schema.isNullable ? `Date | null` : "Date";
|
|
600
|
+
const defaultValue = schema.isNullable ? `null` : "new Date()";
|
|
601
601
|
return {
|
|
602
602
|
typeName,
|
|
603
603
|
defaultValue,
|
|
604
604
|
validationTemplate(input) {
|
|
605
|
-
if (schema.
|
|
605
|
+
if (schema.isNullable) {
|
|
606
606
|
return `(${input} instanceof Date || ${input} === null)`;
|
|
607
607
|
}
|
|
608
608
|
return `${input} instanceof Date`;
|
|
@@ -617,7 +617,7 @@ function tsDateFromSchema(schema, _context) {
|
|
|
617
617
|
}`;
|
|
618
618
|
},
|
|
619
619
|
toJsonTemplate(input, target) {
|
|
620
|
-
if (schema.
|
|
620
|
+
if (schema.isNullable) {
|
|
621
621
|
return `if (${input} instanceof Date) {
|
|
622
622
|
${target} += \`"\${${input}.toISOString()}"\`
|
|
623
623
|
} else {
|
|
@@ -627,7 +627,7 @@ function tsDateFromSchema(schema, _context) {
|
|
|
627
627
|
return `${target} += \`"\${${input}.toISOString()}"\``;
|
|
628
628
|
},
|
|
629
629
|
toQueryStringTemplate(input, target, key) {
|
|
630
|
-
if (schema.
|
|
630
|
+
if (schema.isNullable) {
|
|
631
631
|
return `${target}.push(\`${key}=\${${input}?.toISOString()}\`)`;
|
|
632
632
|
}
|
|
633
633
|
return `${target}.push(\`${key}=\${${input}.toISOString()}\`)`;
|
|
@@ -636,13 +636,13 @@ function tsDateFromSchema(schema, _context) {
|
|
|
636
636
|
};
|
|
637
637
|
}
|
|
638
638
|
function tsFloatFromSchema(schema, _context) {
|
|
639
|
-
const typeName = schema.
|
|
640
|
-
const defaultValue = schema.
|
|
639
|
+
const typeName = schema.isNullable ? "number | null" : "number";
|
|
640
|
+
const defaultValue = schema.isNullable ? "null" : "0";
|
|
641
641
|
return {
|
|
642
642
|
typeName,
|
|
643
643
|
defaultValue,
|
|
644
644
|
validationTemplate(input) {
|
|
645
|
-
if (schema.
|
|
645
|
+
if (schema.isNullable) {
|
|
646
646
|
return `(typeof ${input} === 'number' || ${input} === null)`;
|
|
647
647
|
}
|
|
648
648
|
return `typeof ${input} === 'number'`;
|
|
@@ -664,8 +664,8 @@ function tsFloatFromSchema(schema, _context) {
|
|
|
664
664
|
};
|
|
665
665
|
}
|
|
666
666
|
function tsIntFromSchema(schema, intType, _context) {
|
|
667
|
-
const typeName = schema.
|
|
668
|
-
const defaultValue = schema.
|
|
667
|
+
const typeName = schema.isNullable ? "number | null" : "number";
|
|
668
|
+
const defaultValue = schema.isNullable ? "null" : "0";
|
|
669
669
|
let min;
|
|
670
670
|
let max;
|
|
671
671
|
switch (intType) {
|
|
@@ -699,7 +699,7 @@ function tsIntFromSchema(schema, intType, _context) {
|
|
|
699
699
|
defaultValue,
|
|
700
700
|
validationTemplate(input) {
|
|
701
701
|
const mainPart = `typeof ${input} === 'number' && Number.isInteger(${input}) && ${input} >= ${min} && ${input} <= ${max}`;
|
|
702
|
-
if (schema.
|
|
702
|
+
if (schema.isNullable) {
|
|
703
703
|
return `((${mainPart}) || ${input} === null)`;
|
|
704
704
|
}
|
|
705
705
|
return mainPart;
|
|
@@ -726,14 +726,14 @@ function tsIntFromSchema(schema, intType, _context) {
|
|
|
726
726
|
};
|
|
727
727
|
}
|
|
728
728
|
function tsBigIntFromSchema(schema, isUnsigned, _context) {
|
|
729
|
-
const typeName = schema.
|
|
730
|
-
const defaultValue = schema.
|
|
729
|
+
const typeName = schema.isNullable ? `bigint | null` : `bigint`;
|
|
730
|
+
const defaultValue = schema.isNullable ? `null` : "BigInt(0)";
|
|
731
731
|
return {
|
|
732
732
|
typeName,
|
|
733
733
|
defaultValue,
|
|
734
734
|
validationTemplate(input) {
|
|
735
735
|
const mainPart = isUnsigned ? `typeof ${input} === 'bigint' && ${input} >= BigInt(0) && ${input} <= UINT64_MAX` : `typeof ${input} === 'bigint' && ${input} >= INT64_MIN && ${input} <= INT64_MAX`;
|
|
736
|
-
if (schema.
|
|
736
|
+
if (schema.isNullable) {
|
|
737
737
|
return `((${mainPart}) || ${input} === null)`;
|
|
738
738
|
}
|
|
739
739
|
return mainPart;
|
|
@@ -757,7 +757,7 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
|
|
|
757
757
|
}`;
|
|
758
758
|
},
|
|
759
759
|
toJsonTemplate(input, target) {
|
|
760
|
-
if (schema.
|
|
760
|
+
if (schema.isNullable) {
|
|
761
761
|
return `if (typeof ${input} === 'bigint') {
|
|
762
762
|
${target} += \`"\${${input}}"\`
|
|
763
763
|
} else {
|
|
@@ -788,15 +788,15 @@ function tsRecordFromSchema(schema, context) {
|
|
|
788
788
|
rpcGenerators: context.rpcGenerators
|
|
789
789
|
});
|
|
790
790
|
const typeName = `Record<string, ${innerType.typeName}>`;
|
|
791
|
-
const defaultValue = schema.
|
|
791
|
+
const defaultValue = schema.isNullable ? "null" : "{}";
|
|
792
792
|
return {
|
|
793
|
-
typeName: schema.
|
|
793
|
+
typeName: schema.isNullable ? `${typeName} | null` : typeName,
|
|
794
794
|
defaultValue,
|
|
795
795
|
validationTemplate(input) {
|
|
796
796
|
const mainPart = `isObject(${input}) && Object.values(${input}).every(
|
|
797
797
|
(_value) => ${innerType.validationTemplate("_value")},
|
|
798
798
|
)`;
|
|
799
|
-
if (schema.
|
|
799
|
+
if (schema.isNullable) {
|
|
800
800
|
return `((${mainPart}) || ${input} === null)`;
|
|
801
801
|
}
|
|
802
802
|
return mainPart;
|
|
@@ -815,7 +815,7 @@ function tsRecordFromSchema(schema, context) {
|
|
|
815
815
|
},
|
|
816
816
|
toJsonTemplate(input, target, key) {
|
|
817
817
|
const countVal = `_${validVarName(key)}PropertyCount`;
|
|
818
|
-
if (schema.
|
|
818
|
+
if (schema.isNullable) {
|
|
819
819
|
return `if (${input} !== null) {
|
|
820
820
|
${target} += '{';
|
|
821
821
|
let ${countVal} = 0;
|
|
@@ -855,12 +855,12 @@ function tsRecordFromSchema(schema, context) {
|
|
|
855
855
|
function tsRefFromSchema(schema, context) {
|
|
856
856
|
const typeName = pascalCase(validVarName(schema.ref), { normalize: true });
|
|
857
857
|
const prefixedTypeName = `${context.typePrefix}${typeName}`;
|
|
858
|
-
const defaultValue = schema.
|
|
858
|
+
const defaultValue = schema.isNullable ? "null" : `${prefixedTypeName}.new()`;
|
|
859
859
|
return {
|
|
860
|
-
typeName: schema.
|
|
860
|
+
typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
|
|
861
861
|
defaultValue,
|
|
862
862
|
validationTemplate(input) {
|
|
863
|
-
if (schema.
|
|
863
|
+
if (schema.isNullable) {
|
|
864
864
|
return `($$${prefixedTypeName}.validate(${input}) || ${input} === null)`;
|
|
865
865
|
}
|
|
866
866
|
return `$$${prefixedTypeName}.validate(${input})`;
|
|
@@ -873,7 +873,7 @@ function tsRefFromSchema(schema, context) {
|
|
|
873
873
|
}`;
|
|
874
874
|
},
|
|
875
875
|
toJsonTemplate(input, target, _key) {
|
|
876
|
-
if (schema.
|
|
876
|
+
if (schema.isNullable) {
|
|
877
877
|
return `if (${input} !== null) {
|
|
878
878
|
${target} += $$${prefixedTypeName}.toJsonString(${input});
|
|
879
879
|
} else {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arrirpc/codegen-ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.79.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -23,6 +23,6 @@
|
|
|
23
23
|
],
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"prettier": "^3.5.3",
|
|
26
|
-
"@arrirpc/codegen-utils": "0.
|
|
26
|
+
"@arrirpc/codegen-utils": "0.79.0"
|
|
27
27
|
}
|
|
28
28
|
}
|