@arrirpc/codegen-ts 0.81.3 → 0.81.4

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
@@ -89,7 +89,8 @@ function tsArrayFromSchema(schema, context) {
89
89
  discriminatorValue: "",
90
90
  versionNumber: context.versionNumber,
91
91
  usedFeatures: context.usedFeatures,
92
- rpcGenerators: context.rpcGenerators
92
+ rpcGenerators: context.rpcGenerators,
93
+ coerceBigInts: context.coerceBigInts
93
94
  });
94
95
  const typeName = `(${innerType.typeName})[]`;
95
96
  const defaultValue = schema.isNullable ? "null" : "[]";
@@ -227,7 +228,8 @@ function tsObjectFromSchema(schema, context) {
227
228
  discriminatorValue: "",
228
229
  versionNumber: context.versionNumber,
229
230
  usedFeatures: context.usedFeatures,
230
- rpcGenerators: context.rpcGenerators
231
+ rpcGenerators: context.rpcGenerators,
232
+ coerceBigInts: context.coerceBigInts
231
233
  });
232
234
  if (prop.content) subContentParts.push(prop.content);
233
235
  const fieldName = validVarName(codegenUtils.camelCase(key, { normalize: true }));
@@ -270,7 +272,8 @@ function tsObjectFromSchema(schema, context) {
270
272
  discriminatorValue: "",
271
273
  versionNumber: context.versionNumber,
272
274
  usedFeatures: context.usedFeatures,
273
- rpcGenerators: context.rpcGenerators
275
+ rpcGenerators: context.rpcGenerators,
276
+ coerceBigInts: context.coerceBigInts
274
277
  });
275
278
  if (prop.content) subContentParts.push(prop.content);
276
279
  const fieldName = validVarName(codegenUtils.camelCase(key, { normalize: true }));
@@ -397,7 +400,8 @@ function tsTaggedUnionFromSchema(schema, context) {
397
400
  discriminatorValue: key,
398
401
  versionNumber: context.versionNumber,
399
402
  usedFeatures: context.usedFeatures,
400
- rpcGenerators: context.rpcGenerators
403
+ rpcGenerators: context.rpcGenerators,
404
+ coerceBigInts: context.coerceBigInts
401
405
  });
402
406
  subTypes.push({ value: key, data: subType });
403
407
  }
@@ -731,7 +735,7 @@ function tsIntFromSchema(schema, intType, _context) {
731
735
  content: ""
732
736
  };
733
737
  }
734
- function tsBigIntFromSchema(schema, isUnsigned, _context) {
738
+ function tsBigIntFromSchema(schema, isUnsigned, context) {
735
739
  const typeName = schema.isNullable ? `bigint | null` : `bigint`;
736
740
  const defaultValue = schema.isNullable ? `null` : "BigInt(0)";
737
741
  return {
@@ -746,21 +750,37 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
746
750
  },
747
751
  fromJsonTemplate(input, target) {
748
752
  if (isUnsigned) {
749
- return `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
753
+ let content2 = `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
750
754
  ${target} = BigInt(${input});
751
755
  } else if (typeof ${input} === 'bigint' && ${input} >= BigInt(0)) {
752
756
  ${target} = ${input};
753
- } else {
757
+ }`;
758
+ if (context.coerceBigInts) {
759
+ content2 += ` else if (typeof ${input} === 'number' && ${input} >= BigInt(0)) {
760
+ console.warn("[arri] Expected a BigInt string at ${context.instancePath} but got a raw number. This could lead to precision loss.")
761
+ ${target} = BigInt(${input});
762
+ }`;
763
+ }
764
+ content2 += ` else {
754
765
  ${target} = ${defaultValue};
755
766
  }`;
767
+ return content2;
756
768
  }
757
- return `if (typeof ${input} === 'string') {
769
+ let content = `if (typeof ${input} === 'string') {
758
770
  ${target} = BigInt(${input});
759
771
  } else if (typeof ${input} === 'bigint') {
760
772
  ${target} = ${input};
761
- } else {
773
+ }`;
774
+ if (context.coerceBigInts) {
775
+ content += ` else if (typeof ${input} === 'number') {
776
+ console.warn("[arri] Expected a BigInt string at ${context.instancePath} but got a raw number. This could lead to precision loss.")
777
+ ${target} = BigInt(${input});
778
+ } `;
779
+ }
780
+ content += ` else {
762
781
  ${target} = ${defaultValue};
763
782
  }`;
783
+ return content;
764
784
  },
765
785
  toJsonTemplate(input, target) {
766
786
  if (schema.isNullable) {
@@ -791,7 +811,8 @@ function tsRecordFromSchema(schema, context) {
791
811
  discriminatorValue: "",
792
812
  versionNumber: context.versionNumber,
793
813
  usedFeatures: context.usedFeatures,
794
- rpcGenerators: context.rpcGenerators
814
+ rpcGenerators: context.rpcGenerators,
815
+ coerceBigInts: context.coerceBigInts
795
816
  });
796
817
  const typeName = `Record<string, ${innerType.typeName}>`;
797
818
  const defaultValue = schema.isNullable ? "null" : "{}";
@@ -1005,7 +1026,8 @@ function tsServiceFromDefinition(def, context) {
1005
1026
  schemaPath: `${context.instancePath}.${key2}`,
1006
1027
  versionNumber: context.versionNumber,
1007
1028
  usedFeatures: context.usedFeatures,
1008
- rpcGenerators: context.rpcGenerators
1029
+ rpcGenerators: context.rpcGenerators,
1030
+ coerceBigInts: context.coerceBigInts
1009
1031
  });
1010
1032
  if (subService.content) {
1011
1033
  subServices.push(subService);
@@ -1024,7 +1046,8 @@ function tsServiceFromDefinition(def, context) {
1024
1046
  discriminatorValue: "",
1025
1047
  versionNumber: context.versionNumber,
1026
1048
  usedFeatures: context.usedFeatures,
1027
- rpcGenerators: context.rpcGenerators
1049
+ rpcGenerators: context.rpcGenerators,
1050
+ coerceBigInts: context.coerceBigInts
1028
1051
  });
1029
1052
  if (rpc) {
1030
1053
  rpcParts.push(rpc);
@@ -1123,7 +1146,8 @@ async function createTypescriptClient(def, options) {
1123
1146
  sse: false,
1124
1147
  ws: false
1125
1148
  },
1126
- rpcGenerators: options.rpcGenerators ?? {}
1149
+ rpcGenerators: options.rpcGenerators ?? {},
1150
+ coerceBigInts: options.coerceBigInts ?? false
1127
1151
  };
1128
1152
  const serviceDefinitions = codegenUtils.unflattenProcedures(
1129
1153
  def.procedures,
@@ -1143,7 +1167,8 @@ async function createTypescriptClient(def, options) {
1143
1167
  discriminatorValue: "",
1144
1168
  versionNumber: context.versionNumber,
1145
1169
  usedFeatures: context.usedFeatures,
1146
- rpcGenerators: context.rpcGenerators
1170
+ rpcGenerators: context.rpcGenerators,
1171
+ coerceBigInts: context.coerceBigInts
1147
1172
  });
1148
1173
  if (result2.content) {
1149
1174
  types.push(result2.content);
@@ -1228,9 +1253,9 @@ function tsTypeFromSchema(schema, context) {
1228
1253
  case "uint32":
1229
1254
  return tsIntFromSchema(schema, "uint32");
1230
1255
  case "int64":
1231
- return tsBigIntFromSchema(schema, false);
1256
+ return tsBigIntFromSchema(schema, false, context);
1232
1257
  case "uint64":
1233
- return tsBigIntFromSchema(schema, true);
1258
+ return tsBigIntFromSchema(schema, true, context);
1234
1259
  }
1235
1260
  }
1236
1261
  if (codegenUtils.isSchemaFormEnum(schema)) {
package/dist/index.d.cts CHANGED
@@ -33,6 +33,7 @@ interface CodegenContext {
33
33
  ws: boolean;
34
34
  };
35
35
  rpcGenerators: Partial<Record<string, RpcGenerator>>;
36
+ coerceBigInts: boolean;
36
37
  }
37
38
  declare function getJsDocComment(metadata: Schema['metadata']): string;
38
39
  declare function validVarName(name: string): string;
@@ -72,6 +73,10 @@ interface TypescriptGeneratorOptions {
72
73
  * Override the default functions used for creating procedures
73
74
  */
74
75
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
76
+ /**
77
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
78
+ */
79
+ coerceBigInts?: boolean;
75
80
  }
76
81
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
82
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.d.mts CHANGED
@@ -33,6 +33,7 @@ interface CodegenContext {
33
33
  ws: boolean;
34
34
  };
35
35
  rpcGenerators: Partial<Record<string, RpcGenerator>>;
36
+ coerceBigInts: boolean;
36
37
  }
37
38
  declare function getJsDocComment(metadata: Schema['metadata']): string;
38
39
  declare function validVarName(name: string): string;
@@ -72,6 +73,10 @@ interface TypescriptGeneratorOptions {
72
73
  * Override the default functions used for creating procedures
73
74
  */
74
75
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
76
+ /**
77
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
78
+ */
79
+ coerceBigInts?: boolean;
75
80
  }
76
81
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
82
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.d.ts CHANGED
@@ -33,6 +33,7 @@ interface CodegenContext {
33
33
  ws: boolean;
34
34
  };
35
35
  rpcGenerators: Partial<Record<string, RpcGenerator>>;
36
+ coerceBigInts: boolean;
36
37
  }
37
38
  declare function getJsDocComment(metadata: Schema['metadata']): string;
38
39
  declare function validVarName(name: string): string;
@@ -72,6 +73,10 @@ interface TypescriptGeneratorOptions {
72
73
  * Override the default functions used for creating procedures
73
74
  */
74
75
  rpcGenerators?: Partial<Record<RpcDefinition['transport'], RpcGenerator>>;
76
+ /**
77
+ * Coerces JSON numbers into BigInts when parsing. Can lead to precision loss.
78
+ */
79
+ coerceBigInts?: boolean;
75
80
  }
76
81
  declare const typescriptClientGenerator: _arrirpc_codegen_utils.GeneratorPlugin<TypescriptGeneratorOptions>;
77
82
  declare function createTypescriptClient(def: AppDefinition, options: TypescriptGeneratorOptions): Promise<string>;
package/dist/index.mjs CHANGED
@@ -83,7 +83,8 @@ function tsArrayFromSchema(schema, context) {
83
83
  discriminatorValue: "",
84
84
  versionNumber: context.versionNumber,
85
85
  usedFeatures: context.usedFeatures,
86
- rpcGenerators: context.rpcGenerators
86
+ rpcGenerators: context.rpcGenerators,
87
+ coerceBigInts: context.coerceBigInts
87
88
  });
88
89
  const typeName = `(${innerType.typeName})[]`;
89
90
  const defaultValue = schema.isNullable ? "null" : "[]";
@@ -221,7 +222,8 @@ function tsObjectFromSchema(schema, context) {
221
222
  discriminatorValue: "",
222
223
  versionNumber: context.versionNumber,
223
224
  usedFeatures: context.usedFeatures,
224
- rpcGenerators: context.rpcGenerators
225
+ rpcGenerators: context.rpcGenerators,
226
+ coerceBigInts: context.coerceBigInts
225
227
  });
226
228
  if (prop.content) subContentParts.push(prop.content);
227
229
  const fieldName = validVarName(camelCase(key, { normalize: true }));
@@ -264,7 +266,8 @@ function tsObjectFromSchema(schema, context) {
264
266
  discriminatorValue: "",
265
267
  versionNumber: context.versionNumber,
266
268
  usedFeatures: context.usedFeatures,
267
- rpcGenerators: context.rpcGenerators
269
+ rpcGenerators: context.rpcGenerators,
270
+ coerceBigInts: context.coerceBigInts
268
271
  });
269
272
  if (prop.content) subContentParts.push(prop.content);
270
273
  const fieldName = validVarName(camelCase(key, { normalize: true }));
@@ -391,7 +394,8 @@ function tsTaggedUnionFromSchema(schema, context) {
391
394
  discriminatorValue: key,
392
395
  versionNumber: context.versionNumber,
393
396
  usedFeatures: context.usedFeatures,
394
- rpcGenerators: context.rpcGenerators
397
+ rpcGenerators: context.rpcGenerators,
398
+ coerceBigInts: context.coerceBigInts
395
399
  });
396
400
  subTypes.push({ value: key, data: subType });
397
401
  }
@@ -725,7 +729,7 @@ function tsIntFromSchema(schema, intType, _context) {
725
729
  content: ""
726
730
  };
727
731
  }
728
- function tsBigIntFromSchema(schema, isUnsigned, _context) {
732
+ function tsBigIntFromSchema(schema, isUnsigned, context) {
729
733
  const typeName = schema.isNullable ? `bigint | null` : `bigint`;
730
734
  const defaultValue = schema.isNullable ? `null` : "BigInt(0)";
731
735
  return {
@@ -740,21 +744,37 @@ function tsBigIntFromSchema(schema, isUnsigned, _context) {
740
744
  },
741
745
  fromJsonTemplate(input, target) {
742
746
  if (isUnsigned) {
743
- return `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
747
+ let content2 = `if (typeof ${input} === 'string' && BigInt(${input}) >= BigInt(0)) {
744
748
  ${target} = BigInt(${input});
745
749
  } else if (typeof ${input} === 'bigint' && ${input} >= BigInt(0)) {
746
750
  ${target} = ${input};
747
- } else {
751
+ }`;
752
+ if (context.coerceBigInts) {
753
+ content2 += ` else if (typeof ${input} === 'number' && ${input} >= BigInt(0)) {
754
+ console.warn("[arri] Expected a BigInt string at ${context.instancePath} but got a raw number. This could lead to precision loss.")
755
+ ${target} = BigInt(${input});
756
+ }`;
757
+ }
758
+ content2 += ` else {
748
759
  ${target} = ${defaultValue};
749
760
  }`;
761
+ return content2;
750
762
  }
751
- return `if (typeof ${input} === 'string') {
763
+ let content = `if (typeof ${input} === 'string') {
752
764
  ${target} = BigInt(${input});
753
765
  } else if (typeof ${input} === 'bigint') {
754
766
  ${target} = ${input};
755
- } else {
767
+ }`;
768
+ if (context.coerceBigInts) {
769
+ content += ` else if (typeof ${input} === 'number') {
770
+ console.warn("[arri] Expected a BigInt string at ${context.instancePath} but got a raw number. This could lead to precision loss.")
771
+ ${target} = BigInt(${input});
772
+ } `;
773
+ }
774
+ content += ` else {
756
775
  ${target} = ${defaultValue};
757
776
  }`;
777
+ return content;
758
778
  },
759
779
  toJsonTemplate(input, target) {
760
780
  if (schema.isNullable) {
@@ -785,7 +805,8 @@ function tsRecordFromSchema(schema, context) {
785
805
  discriminatorValue: "",
786
806
  versionNumber: context.versionNumber,
787
807
  usedFeatures: context.usedFeatures,
788
- rpcGenerators: context.rpcGenerators
808
+ rpcGenerators: context.rpcGenerators,
809
+ coerceBigInts: context.coerceBigInts
789
810
  });
790
811
  const typeName = `Record<string, ${innerType.typeName}>`;
791
812
  const defaultValue = schema.isNullable ? "null" : "{}";
@@ -999,7 +1020,8 @@ function tsServiceFromDefinition(def, context) {
999
1020
  schemaPath: `${context.instancePath}.${key2}`,
1000
1021
  versionNumber: context.versionNumber,
1001
1022
  usedFeatures: context.usedFeatures,
1002
- rpcGenerators: context.rpcGenerators
1023
+ rpcGenerators: context.rpcGenerators,
1024
+ coerceBigInts: context.coerceBigInts
1003
1025
  });
1004
1026
  if (subService.content) {
1005
1027
  subServices.push(subService);
@@ -1018,7 +1040,8 @@ function tsServiceFromDefinition(def, context) {
1018
1040
  discriminatorValue: "",
1019
1041
  versionNumber: context.versionNumber,
1020
1042
  usedFeatures: context.usedFeatures,
1021
- rpcGenerators: context.rpcGenerators
1043
+ rpcGenerators: context.rpcGenerators,
1044
+ coerceBigInts: context.coerceBigInts
1022
1045
  });
1023
1046
  if (rpc) {
1024
1047
  rpcParts.push(rpc);
@@ -1117,7 +1140,8 @@ async function createTypescriptClient(def, options) {
1117
1140
  sse: false,
1118
1141
  ws: false
1119
1142
  },
1120
- rpcGenerators: options.rpcGenerators ?? {}
1143
+ rpcGenerators: options.rpcGenerators ?? {},
1144
+ coerceBigInts: options.coerceBigInts ?? false
1121
1145
  };
1122
1146
  const serviceDefinitions = unflattenProcedures(
1123
1147
  def.procedures,
@@ -1137,7 +1161,8 @@ async function createTypescriptClient(def, options) {
1137
1161
  discriminatorValue: "",
1138
1162
  versionNumber: context.versionNumber,
1139
1163
  usedFeatures: context.usedFeatures,
1140
- rpcGenerators: context.rpcGenerators
1164
+ rpcGenerators: context.rpcGenerators,
1165
+ coerceBigInts: context.coerceBigInts
1141
1166
  });
1142
1167
  if (result2.content) {
1143
1168
  types.push(result2.content);
@@ -1222,9 +1247,9 @@ function tsTypeFromSchema(schema, context) {
1222
1247
  case "uint32":
1223
1248
  return tsIntFromSchema(schema, "uint32");
1224
1249
  case "int64":
1225
- return tsBigIntFromSchema(schema, false);
1250
+ return tsBigIntFromSchema(schema, false, context);
1226
1251
  case "uint64":
1227
- return tsBigIntFromSchema(schema, true);
1252
+ return tsBigIntFromSchema(schema, true, context);
1228
1253
  }
1229
1254
  }
1230
1255
  if (isSchemaFormEnum(schema)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/codegen-ts",
3
- "version": "0.81.3",
3
+ "version": "0.81.4",
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.81.4"
27
27
  }
28
28
  }