@arrirpc/codegen-ts 0.81.3 → 0.82.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 CHANGED
@@ -507,8 +507,16 @@ function tsEnumFromSchema(schema, context) {
507
507
  }
508
508
  const name = `${context.typePrefix}${enumName}`;
509
509
  const valuesName = `$$${name}Values`;
510
- result.content = `${getJsDocComment(schema.metadata)}export type ${name} = (typeof ${valuesName})[number];
511
- const ${valuesName} = [${schema.enum.map((val) => `"${val}"`).join(", ")}] as const;
510
+ result.content = `${getJsDocComment(schema.metadata)}export type ${name} = ${schema.enum.map((val) => `'${val}'`).join(" | ")};
511
+ export const ${name} = {
512
+ ${schema.enum.map((val) => {
513
+ let str = " ";
514
+ str += `${codegenUtils.pascalCase(validVarName(val), { normalize: true })}:`;
515
+ str += ` '${val}',`;
516
+ return str;
517
+ }).join("\n")}
518
+ } as const;
519
+ const ${valuesName} = [${schema.enum.map((val) => `'${val}'`).join(", ")}] as const;
512
520
  export const $$${name}: ArriEnumValidator<${name}> = {
513
521
  new(): ${name} {
514
522
  return ${valuesName}[0];
@@ -551,11 +559,10 @@ function tsStringFromSchema(schema, _context) {
551
559
  return `typeof ${input} === 'string'`;
552
560
  },
553
561
  fromJsonTemplate(input, target) {
554
- return `if (typeof ${input} === 'string') {
555
- ${target} = ${input};
556
- } else {
557
- ${target} = ${defaultValue};
558
- }`;
562
+ if (schema.isNullable) {
563
+ return `${target} = parseNullableString(${input});`;
564
+ }
565
+ return `${target} = parseString(${input});`;
559
566
  },
560
567
  toJsonTemplate(input, target) {
561
568
  if (schema.isNullable) {
@@ -586,11 +593,10 @@ function tsBooleanFromSchema(schema, _context) {
586
593
  return `typeof ${input} === 'boolean'`;
587
594
  },
588
595
  fromJsonTemplate(input, target) {
589
- return `if (typeof ${input} === 'boolean') {
590
- ${target} = ${input};
591
- } else {
592
- ${target} = ${defaultValue};
593
- }`;
596
+ if (schema.isNullable) {
597
+ return `${target} = parseNullableBoolean(${input});`;
598
+ }
599
+ return `${target} = parseBoolean(${input});`;
594
600
  },
595
601
  toJsonTemplate(input, target) {
596
602
  return `${target} += \`\${${input}}\`;`;
@@ -614,13 +620,10 @@ function tsDateFromSchema(schema, _context) {
614
620
  return `${input} instanceof Date`;
615
621
  },
616
622
  fromJsonTemplate(input, target) {
617
- return `if (typeof ${input} === 'string') {
618
- ${target} = new Date(${input});
619
- } else if (${input} instanceof Date) {
620
- ${target} = ${input};
621
- } else {
622
- ${target} = ${defaultValue}
623
- }`;
623
+ if (schema.isNullable) {
624
+ return `${target} = parseNullableTimestamp(${input});`;
625
+ }
626
+ return `${target} = parseTimestamp(${input});`;
624
627
  },
625
628
  toJsonTemplate(input, target) {
626
629
  if (schema.isNullable) {
@@ -654,11 +657,10 @@ function tsFloatFromSchema(schema, _context) {
654
657
  return `typeof ${input} === 'number'`;
655
658
  },
656
659
  fromJsonTemplate(input, target) {
657
- return `if (typeof ${input} === 'number') {
658
- ${target} = ${input};
659
- } else {
660
- ${target} = ${defaultValue};
661
- }`;
660
+ if (schema.isNullable) {
661
+ return `${target} = parseNullableNumberFloat(${input});`;
662
+ }
663
+ return `${target} = parseNumberFloat(${input});`;
662
664
  },
663
665
  toJsonTemplate(input, target) {
664
666
  return `${target} += \`\${${input}}\``;
@@ -711,16 +713,10 @@ function tsIntFromSchema(schema, intType, _context) {
711
713
  return mainPart;
712
714
  },
713
715
  fromJsonTemplate(input, target) {
714
- return `if (
715
- typeof ${input} === 'number' &&
716
- Number.isInteger(${input}) &&
717
- ${input} >= ${min} &&
718
- ${input} <= ${max}
719
- ) {
720
- ${target} = ${input};
721
- } else {
722
- ${target} = ${defaultValue};
723
- }`;
716
+ if (schema.isNullable) {
717
+ return `${target} = parseNullableNumberInt(${input}, ${min}, ${max});`;
718
+ }
719
+ return `${target} = parseNumberInt(${input}, ${min}, ${max});`;
724
720
  },
725
721
  toJsonTemplate(input, target) {
726
722
  return `${target} += \`\${${input}}\``;
@@ -746,21 +742,15 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
746
742
  },
747
743
  fromJsonTemplate(input, target) {
748
744
  if (isUnsigned) {
749
- return `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
750
- ${target} = BigInt(${input});
751
- } else if (typeof ${input} === 'bigint' && ${input} >= BigInt(0)) {
752
- ${target} = ${input};
753
- } else {
754
- ${target} = ${defaultValue};
755
- }`;
745
+ if (schema.isNullable) {
746
+ return `${target} = parseNullableNumberUnsignedBigInt(${input});`;
747
+ }
748
+ return `${target} = parseNumberUnsignedBigInt(${input});`;
756
749
  }
757
- return `if (typeof ${input} === 'string') {
758
- ${target} = BigInt(${input});
759
- } else if (typeof ${input} === 'bigint') {
760
- ${target} = ${input};
761
- } else {
762
- ${target} = ${defaultValue};
763
- }`;
750
+ if (schema.isNullable) {
751
+ return `${target} = parseNullableNumberBigInt(${input});`;
752
+ }
753
+ return `${target} = parseNumberBigInt(${input});`;
764
754
  },
765
755
  toJsonTemplate(input, target) {
766
756
  if (schema.isNullable) {
@@ -1171,6 +1161,21 @@ import {
1171
1161
  INT64_MAX,
1172
1162
  INT64_MIN,
1173
1163
  isObject,
1164
+ parseString,
1165
+ parseBoolean,
1166
+ parseNumber,
1167
+ parseTimestamp,
1168
+ parseNumberInt,
1169
+ parseNumberBigInt,
1170
+ parseNumberUnsignedBigInt,
1171
+ parseNumberFloat,
1172
+ parseNullableString,
1173
+ parseNullableBoolean,
1174
+ parseNullableTimestamp,
1175
+ parseNullableNumberFloat,
1176
+ parseNullableNumberInt,
1177
+ parseNullableNumberBigInt,
1178
+ parseNullableNumberUnsignedBigInt,
1174
1179
  serializeString,
1175
1180
  ${context.usedFeatures.sse ? "type SseOptions," : ""}
1176
1181
  UINT8_MAX,
package/dist/index.d.cts CHANGED
@@ -72,6 +72,10 @@ interface TypescriptGeneratorOptions {
72
72
  * Override the default functions used for creating procedures
73
73
  */
74
74
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
75
+ /**
76
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
77
+ */
78
+ coerceBigInts?: boolean;
75
79
  }
76
80
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
81
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.d.mts CHANGED
@@ -72,6 +72,10 @@ interface TypescriptGeneratorOptions {
72
72
  * Override the default functions used for creating procedures
73
73
  */
74
74
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
75
+ /**
76
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
77
+ */
78
+ coerceBigInts?: boolean;
75
79
  }
76
80
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
81
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.d.ts CHANGED
@@ -72,6 +72,10 @@ interface TypescriptGeneratorOptions {
72
72
  * Override the default functions used for creating procedures
73
73
  */
74
74
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
75
+ /**
76
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
77
+ */
78
+ coerceBigInts?: boolean;
75
79
  }
76
80
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
81
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.mjs CHANGED
@@ -501,8 +501,16 @@ function tsEnumFromSchema(schema, context) {
501
501
  }
502
502
  const name = `${context.typePrefix}${enumName}`;
503
503
  const valuesName = `$$${name}Values`;
504
- result.content = `${getJsDocComment(schema.metadata)}export type ${name} = (typeof ${valuesName})[number];
505
- const ${valuesName} = [${schema.enum.map((val) => `"${val}"`).join(", ")}] as const;
504
+ result.content = `${getJsDocComment(schema.metadata)}export type ${name} = ${schema.enum.map((val) => `'${val}'`).join(" | ")};
505
+ export const ${name} = {
506
+ ${schema.enum.map((val) => {
507
+ let str = " ";
508
+ str += `${pascalCase(validVarName(val), { normalize: true })}:`;
509
+ str += ` '${val}',`;
510
+ return str;
511
+ }).join("\n")}
512
+ } as const;
513
+ const ${valuesName} = [${schema.enum.map((val) => `'${val}'`).join(", ")}] as const;
506
514
  export const $$${name}: ArriEnumValidator<${name}> = {
507
515
  new(): ${name} {
508
516
  return ${valuesName}[0];
@@ -545,11 +553,10 @@ function tsStringFromSchema(schema, _context) {
545
553
  return `typeof ${input} === 'string'`;
546
554
  },
547
555
  fromJsonTemplate(input, target) {
548
- return `if (typeof ${input} === 'string') {
549
- ${target} = ${input};
550
- } else {
551
- ${target} = ${defaultValue};
552
- }`;
556
+ if (schema.isNullable) {
557
+ return `${target} = parseNullableString(${input});`;
558
+ }
559
+ return `${target} = parseString(${input});`;
553
560
  },
554
561
  toJsonTemplate(input, target) {
555
562
  if (schema.isNullable) {
@@ -580,11 +587,10 @@ function tsBooleanFromSchema(schema, _context) {
580
587
  return `typeof ${input} === 'boolean'`;
581
588
  },
582
589
  fromJsonTemplate(input, target) {
583
- return `if (typeof ${input} === 'boolean') {
584
- ${target} = ${input};
585
- } else {
586
- ${target} = ${defaultValue};
587
- }`;
590
+ if (schema.isNullable) {
591
+ return `${target} = parseNullableBoolean(${input});`;
592
+ }
593
+ return `${target} = parseBoolean(${input});`;
588
594
  },
589
595
  toJsonTemplate(input, target) {
590
596
  return `${target} += \`\${${input}}\`;`;
@@ -608,13 +614,10 @@ function tsDateFromSchema(schema, _context) {
608
614
  return `${input} instanceof Date`;
609
615
  },
610
616
  fromJsonTemplate(input, target) {
611
- return `if (typeof ${input} === 'string') {
612
- ${target} = new Date(${input});
613
- } else if (${input} instanceof Date) {
614
- ${target} = ${input};
615
- } else {
616
- ${target} = ${defaultValue}
617
- }`;
617
+ if (schema.isNullable) {
618
+ return `${target} = parseNullableTimestamp(${input});`;
619
+ }
620
+ return `${target} = parseTimestamp(${input});`;
618
621
  },
619
622
  toJsonTemplate(input, target) {
620
623
  if (schema.isNullable) {
@@ -648,11 +651,10 @@ function tsFloatFromSchema(schema, _context) {
648
651
  return `typeof ${input} === 'number'`;
649
652
  },
650
653
  fromJsonTemplate(input, target) {
651
- return `if (typeof ${input} === 'number') {
652
- ${target} = ${input};
653
- } else {
654
- ${target} = ${defaultValue};
655
- }`;
654
+ if (schema.isNullable) {
655
+ return `${target} = parseNullableNumberFloat(${input});`;
656
+ }
657
+ return `${target} = parseNumberFloat(${input});`;
656
658
  },
657
659
  toJsonTemplate(input, target) {
658
660
  return `${target} += \`\${${input}}\``;
@@ -705,16 +707,10 @@ function tsIntFromSchema(schema, intType, _context) {
705
707
  return mainPart;
706
708
  },
707
709
  fromJsonTemplate(input, target) {
708
- return `if (
709
- typeof ${input} === 'number' &&
710
- Number.isInteger(${input}) &&
711
- ${input} >= ${min} &&
712
- ${input} <= ${max}
713
- ) {
714
- ${target} = ${input};
715
- } else {
716
- ${target} = ${defaultValue};
717
- }`;
710
+ if (schema.isNullable) {
711
+ return `${target} = parseNullableNumberInt(${input}, ${min}, ${max});`;
712
+ }
713
+ return `${target} = parseNumberInt(${input}, ${min}, ${max});`;
718
714
  },
719
715
  toJsonTemplate(input, target) {
720
716
  return `${target} += \`\${${input}}\``;
@@ -740,21 +736,15 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
740
736
  },
741
737
  fromJsonTemplate(input, target) {
742
738
  if (isUnsigned) {
743
- return `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
744
- ${target} = BigInt(${input});
745
- } else if (typeof ${input} === 'bigint' && ${input} >= BigInt(0)) {
746
- ${target} = ${input};
747
- } else {
748
- ${target} = ${defaultValue};
749
- }`;
739
+ if (schema.isNullable) {
740
+ return `${target} = parseNullableNumberUnsignedBigInt(${input});`;
741
+ }
742
+ return `${target} = parseNumberUnsignedBigInt(${input});`;
750
743
  }
751
- return `if (typeof ${input} === 'string') {
752
- ${target} = BigInt(${input});
753
- } else if (typeof ${input} === 'bigint') {
754
- ${target} = ${input};
755
- } else {
756
- ${target} = ${defaultValue};
757
- }`;
744
+ if (schema.isNullable) {
745
+ return `${target} = parseNullableNumberBigInt(${input});`;
746
+ }
747
+ return `${target} = parseNumberBigInt(${input});`;
758
748
  },
759
749
  toJsonTemplate(input, target) {
760
750
  if (schema.isNullable) {
@@ -1165,6 +1155,21 @@ import {
1165
1155
  INT64_MAX,
1166
1156
  INT64_MIN,
1167
1157
  isObject,
1158
+ parseString,
1159
+ parseBoolean,
1160
+ parseNumber,
1161
+ parseTimestamp,
1162
+ parseNumberInt,
1163
+ parseNumberBigInt,
1164
+ parseNumberUnsignedBigInt,
1165
+ parseNumberFloat,
1166
+ parseNullableString,
1167
+ parseNullableBoolean,
1168
+ parseNullableTimestamp,
1169
+ parseNullableNumberFloat,
1170
+ parseNullableNumberInt,
1171
+ parseNullableNumberBigInt,
1172
+ parseNullableNumberUnsignedBigInt,
1168
1173
  serializeString,
1169
1174
  ${context.usedFeatures.sse ? "type SseOptions," : ""}
1170
1175
  UINT8_MAX,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/codegen-ts",
3
- "version": "0.81.3",
3
+ "version": "0.82.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.8.1",
26
- "@arrirpc/codegen-utils": "0.81.3"
26
+ "@arrirpc/codegen-utils": "0.82.0"
27
27
  }
28
28
  }