@arrirpc/codegen-ts 0.77.1 → 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/README.md CHANGED
@@ -50,19 +50,35 @@ pnpm i --save @arrirpc/client
50
50
  import { MyClient } from './myClient.g';
51
51
 
52
52
  const client = new MyClient({
53
+ // only required field
53
54
  baseUrl: 'https://example.com',
55
+
56
+ // everything below is optional
54
57
  headers: () => {
55
58
  return {
56
59
  Authorization: '<some-token>',
57
60
  };
58
61
  },
59
- // optional
60
- onError: (err) => {
61
- // do something
62
+ onError: (err) => {},
63
+ options: {
64
+ retry: 4,
65
+ retryDelay: 200,
66
+ retryStatusCodes: [400, 403, 500, 501],
67
+ onRequest: (ctx) => {},
68
+ onRequestError: (ctx) => {},
69
+ onResponse: (ctx) => {},
70
+ onResponseError: (ctx) => {},
71
+ signal: undefined, // abortcontroller signal
72
+ timeout: 200,
62
73
  },
63
74
  });
64
75
 
65
- await client.myProcedure();
76
+ await client.myProcedure({ foo: 'foo' });
77
+
78
+ // individual procedures can also override the global request options
79
+ await client.myProcedure({ foo: 'foo' }, { timeout: 400 });
80
+ // be aware that these options are not merged with the global request options so you will have to
81
+ // re-specify every hook if you only want to change one thing
66
82
  ```
67
83
 
68
84
  The root client will be a class containing all of the services and procedures in a single class. If you only need a particular service. You can also import just that service.
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.nullable ? "null" : "undefined";
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.nullable ? "null" : "[]";
95
+ const defaultValue = schema.isNullable ? "null" : "[]";
96
96
  return {
97
- typeName: schema.nullable ? `${typeName} | null` : typeName,
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.nullable) {
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.nullable) {
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.nullable ? "null" : `$$${prefixedTypeName}.new()`;
156
+ const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
157
157
  const result = {
158
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
158
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
159
159
  defaultValue,
160
160
  validationTemplate(input) {
161
- if (schema.nullable) {
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.nullable) {
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.nullable ? "null" : `$$${prefixedTypeName}.new()`;
351
+ const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
352
352
  const result = {
353
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
353
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
354
354
  defaultValue,
355
355
  validationTemplate(input) {
356
356
  const mainPart = `$$${prefixedTypeName}.validate(${input})`;
357
- if (schema.nullable) {
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.nullable) {
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.nullable ? `${prefixedEnumName} | null` : prefixedEnumName;
473
- const defaultValue = schema.nullable ? "null" : `$$${prefixedEnumName}.new()`;
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.nullable) {
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.nullable) {
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.nullable ? "string | null" : "string";
543
- const defaultValue = schema.nullable ? "null" : '""';
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.nullable) {
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.nullable) {
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.nullable ? `boolean | null` : "boolean";
578
- const defaultValue = schema.nullable ? `null` : "false";
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.nullable) {
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.nullable ? `Date | null` : "Date";
606
- const defaultValue = schema.nullable ? `null` : "new Date()";
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.nullable) {
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.nullable) {
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.nullable) {
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.nullable ? "number | null" : "number";
646
- const defaultValue = schema.nullable ? "null" : "0";
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.nullable) {
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.nullable ? "number | null" : "number";
674
- const defaultValue = schema.nullable ? "null" : "0";
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.nullable) {
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.nullable ? `bigint | null` : `bigint`;
736
- const defaultValue = schema.nullable ? `null` : "BigInt(0)";
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.nullable) {
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.nullable) {
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.nullable ? "null" : "{}";
797
+ const defaultValue = schema.isNullable ? "null" : "{}";
798
798
  return {
799
- typeName: schema.nullable ? `${typeName} | null` : typeName,
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.nullable) {
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.nullable) {
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.nullable ? "null" : `${prefixedTypeName}.new()`;
864
+ const defaultValue = schema.isNullable ? "null" : `${prefixedTypeName}.new()`;
865
865
  return {
866
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
866
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
867
867
  defaultValue,
868
868
  validationTemplate(input) {
869
- if (schema.nullable) {
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.nullable) {
882
+ if (schema.isNullable) {
883
883
  return `if (${input} !== null) {
884
884
  ${target} += $$${prefixedTypeName}.toJsonString(${input});
885
885
  } else {
@@ -944,7 +944,7 @@ function httpRpcFromDefinition(def, context) {
944
944
  return `${getJsDocComment({
945
945
  description: def.description,
946
946
  isDeprecated: def.isDeprecated
947
- })} async ${key}(${params ? `params: ${params}` : ""}): Promise<${response ?? "undefined"}> {
947
+ })} async ${key}(${params ? `params: ${params}, ` : ""}options?: ArriRequestOptions): Promise<${response ?? "undefined"}> {
948
948
  return arriRequest<${response ?? "undefined"}, ${params ?? "undefined"}>({
949
949
  url: \`\${this._baseUrl}${def.path}\`,
950
950
  method: "${def.method.toLowerCase()}",
@@ -956,6 +956,7 @@ function httpRpcFromDefinition(def, context) {
956
956
  responseFromString: ${response ? `$$${response}.fromJsonString` : "() => {}"},
957
957
  serializer: ${params ? `$$${params}.${serializerMethod}` : "() => {}"},
958
958
  clientVersion: "${context.versionNumber}",
959
+ options: options ?? this._options,
959
960
  });
960
961
  }`;
961
962
  }
@@ -1048,23 +1049,26 @@ function tsServiceFromDefinition(def, context) {
1048
1049
  private readonly _baseUrl: string;
1049
1050
  private readonly _fetch?: $Fetch;
1050
1051
  private readonly _headers: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
1051
- private readonly _onError?: (err: unknown) => void;
1052
+ private readonly _onError?: (err: unknown) => void;
1053
+ private readonly _options?: ArriRequestOptions;
1052
1054
  ${subServices.map((service) => ` ${service.key}: ${service.name};`).join("\n")}
1053
1055
  constructor(
1054
- options: {
1056
+ config: {
1055
1057
  baseUrl?: string;
1056
1058
  fetch?: Fetch;
1057
1059
  headers?: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
1058
1060
  onError?: (err: unknown) => void;
1061
+ options?: ArriRequestOptions;
1059
1062
  } = {},
1060
1063
  ) {
1061
- this._baseUrl = options.baseUrl ?? "";
1062
- if (options.fetch) {
1063
- this._fetch = createFetch({ fetch: options.fetch });
1064
+ this._baseUrl = config.baseUrl ?? "";
1065
+ if (config.fetch) {
1066
+ this._fetch = createFetch({ fetch: config.fetch });
1064
1067
  }
1065
- this._headers = options.headers ?? {};
1066
- this._onError = options.onError;
1067
- ${subServices.map((service) => ` this.${service.key} = new ${service.name}(options);`).join("\n")}
1068
+ this._headers = config.headers ?? {};
1069
+ this._onError = config.onError;
1070
+ this._options = config.options;
1071
+ ${subServices.map((service) => ` this.${service.key} = new ${service.name}(config);`).join("\n")}
1068
1072
  }
1069
1073
  ${rpcParts.map((rpc) => ` ${rpc}`).join("\n")}
1070
1074
  }
@@ -1153,6 +1157,7 @@ async function createTypescriptClient(def, options) {
1153
1157
  import {
1154
1158
  ArriEnumValidator,
1155
1159
  ArriModelValidator,
1160
+ type ArriRequestOptions,
1156
1161
  arriRequest,
1157
1162
  ${context.usedFeatures.sse ? "arriSseRequest," : ""}
1158
1163
  ${context.usedFeatures.ws ? "arriWsRequest," : ""}
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.nullable ? "null" : "undefined";
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.nullable ? "null" : "[]";
89
+ const defaultValue = schema.isNullable ? "null" : "[]";
90
90
  return {
91
- typeName: schema.nullable ? `${typeName} | null` : typeName,
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.nullable) {
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.nullable) {
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.nullable ? "null" : `$$${prefixedTypeName}.new()`;
150
+ const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
151
151
  const result = {
152
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
152
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
153
153
  defaultValue,
154
154
  validationTemplate(input) {
155
- if (schema.nullable) {
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.nullable) {
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.nullable ? "null" : `$$${prefixedTypeName}.new()`;
345
+ const defaultValue = schema.isNullable ? "null" : `$$${prefixedTypeName}.new()`;
346
346
  const result = {
347
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
347
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
348
348
  defaultValue,
349
349
  validationTemplate(input) {
350
350
  const mainPart = `$$${prefixedTypeName}.validate(${input})`;
351
- if (schema.nullable) {
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.nullable) {
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.nullable ? `${prefixedEnumName} | null` : prefixedEnumName;
467
- const defaultValue = schema.nullable ? "null" : `$$${prefixedEnumName}.new()`;
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.nullable) {
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.nullable) {
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.nullable ? "string | null" : "string";
537
- const defaultValue = schema.nullable ? "null" : '""';
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.nullable) {
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.nullable) {
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.nullable ? `boolean | null` : "boolean";
572
- const defaultValue = schema.nullable ? `null` : "false";
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.nullable) {
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.nullable ? `Date | null` : "Date";
600
- const defaultValue = schema.nullable ? `null` : "new Date()";
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.nullable) {
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.nullable) {
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.nullable) {
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.nullable ? "number | null" : "number";
640
- const defaultValue = schema.nullable ? "null" : "0";
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.nullable) {
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.nullable ? "number | null" : "number";
668
- const defaultValue = schema.nullable ? "null" : "0";
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.nullable) {
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.nullable ? `bigint | null` : `bigint`;
730
- const defaultValue = schema.nullable ? `null` : "BigInt(0)";
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.nullable) {
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.nullable) {
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.nullable ? "null" : "{}";
791
+ const defaultValue = schema.isNullable ? "null" : "{}";
792
792
  return {
793
- typeName: schema.nullable ? `${typeName} | null` : typeName,
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.nullable) {
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.nullable) {
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.nullable ? "null" : `${prefixedTypeName}.new()`;
858
+ const defaultValue = schema.isNullable ? "null" : `${prefixedTypeName}.new()`;
859
859
  return {
860
- typeName: schema.nullable ? `${prefixedTypeName} | null` : prefixedTypeName,
860
+ typeName: schema.isNullable ? `${prefixedTypeName} | null` : prefixedTypeName,
861
861
  defaultValue,
862
862
  validationTemplate(input) {
863
- if (schema.nullable) {
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.nullable) {
876
+ if (schema.isNullable) {
877
877
  return `if (${input} !== null) {
878
878
  ${target} += $$${prefixedTypeName}.toJsonString(${input});
879
879
  } else {
@@ -938,7 +938,7 @@ function httpRpcFromDefinition(def, context) {
938
938
  return `${getJsDocComment({
939
939
  description: def.description,
940
940
  isDeprecated: def.isDeprecated
941
- })} async ${key}(${params ? `params: ${params}` : ""}): Promise<${response ?? "undefined"}> {
941
+ })} async ${key}(${params ? `params: ${params}, ` : ""}options?: ArriRequestOptions): Promise<${response ?? "undefined"}> {
942
942
  return arriRequest<${response ?? "undefined"}, ${params ?? "undefined"}>({
943
943
  url: \`\${this._baseUrl}${def.path}\`,
944
944
  method: "${def.method.toLowerCase()}",
@@ -950,6 +950,7 @@ function httpRpcFromDefinition(def, context) {
950
950
  responseFromString: ${response ? `$$${response}.fromJsonString` : "() => {}"},
951
951
  serializer: ${params ? `$$${params}.${serializerMethod}` : "() => {}"},
952
952
  clientVersion: "${context.versionNumber}",
953
+ options: options ?? this._options,
953
954
  });
954
955
  }`;
955
956
  }
@@ -1042,23 +1043,26 @@ function tsServiceFromDefinition(def, context) {
1042
1043
  private readonly _baseUrl: string;
1043
1044
  private readonly _fetch?: $Fetch;
1044
1045
  private readonly _headers: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
1045
- private readonly _onError?: (err: unknown) => void;
1046
+ private readonly _onError?: (err: unknown) => void;
1047
+ private readonly _options?: ArriRequestOptions;
1046
1048
  ${subServices.map((service) => ` ${service.key}: ${service.name};`).join("\n")}
1047
1049
  constructor(
1048
- options: {
1050
+ config: {
1049
1051
  baseUrl?: string;
1050
1052
  fetch?: Fetch;
1051
1053
  headers?: HeaderMap | (() => HeaderMap | Promise<HeaderMap>);
1052
1054
  onError?: (err: unknown) => void;
1055
+ options?: ArriRequestOptions;
1053
1056
  } = {},
1054
1057
  ) {
1055
- this._baseUrl = options.baseUrl ?? "";
1056
- if (options.fetch) {
1057
- this._fetch = createFetch({ fetch: options.fetch });
1058
+ this._baseUrl = config.baseUrl ?? "";
1059
+ if (config.fetch) {
1060
+ this._fetch = createFetch({ fetch: config.fetch });
1058
1061
  }
1059
- this._headers = options.headers ?? {};
1060
- this._onError = options.onError;
1061
- ${subServices.map((service) => ` this.${service.key} = new ${service.name}(options);`).join("\n")}
1062
+ this._headers = config.headers ?? {};
1063
+ this._onError = config.onError;
1064
+ this._options = config.options;
1065
+ ${subServices.map((service) => ` this.${service.key} = new ${service.name}(config);`).join("\n")}
1062
1066
  }
1063
1067
  ${rpcParts.map((rpc) => ` ${rpc}`).join("\n")}
1064
1068
  }
@@ -1147,6 +1151,7 @@ async function createTypescriptClient(def, options) {
1147
1151
  import {
1148
1152
  ArriEnumValidator,
1149
1153
  ArriModelValidator,
1154
+ type ArriRequestOptions,
1150
1155
  arriRequest,
1151
1156
  ${context.usedFeatures.sse ? "arriSseRequest," : ""}
1152
1157
  ${context.usedFeatures.ws ? "arriWsRequest," : ""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arrirpc/codegen-ts",
3
- "version": "0.77.1",
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.77.1"
26
+ "@arrirpc/codegen-utils": "0.79.0"
27
27
  }
28
28
  }