@zayne-labs/callapi 1.7.4 → 1.7.5

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.
@@ -93,6 +93,30 @@ type PossibleHTTPError<TErrorData> = UnmaskType<{
93
93
  name: "HTTPError";
94
94
  }>;
95
95
 
96
+ type StreamProgressEvent = {
97
+ /**
98
+ * Current chunk of data being streamed
99
+ */
100
+ chunk: Uint8Array;
101
+ /**
102
+ * Progress in percentage
103
+ */
104
+ progress: number;
105
+ /**
106
+ * Total size of data in bytes
107
+ */
108
+ totalBytes: number;
109
+ /**
110
+ * Amount of data transferred so far
111
+ */
112
+ transferredBytes: number;
113
+ };
114
+ declare global {
115
+ interface ReadableStream<R> {
116
+ [Symbol.asyncIterator]: () => AsyncIterableIterator<R>;
117
+ }
118
+ }
119
+
96
120
  /**
97
121
  * The Standard Schema interface.
98
122
  * @see https://github.com/standard-schema/standard-schema
@@ -192,24 +216,6 @@ declare namespace StandardSchemaV1 {
192
216
  type InferOutput<Schema extends StandardSchemaV1> = NonNullable<Schema["~standard"]["types"]>["output"];
193
217
  }
194
218
 
195
- type Params = UnmaskType<Record<string, boolean | number | string> | Array<boolean | number | string>>;
196
- type Query = UnmaskType<Record<string, boolean | number | string>>;
197
- type InitURL = UnmaskType<string | URL>;
198
- interface UrlOptions<TSchemas extends CallApiSchemas> {
199
- /**
200
- * URL to be used in the request.
201
- */
202
- readonly initURL?: string;
203
- /**
204
- * Parameters to be appended to the URL (i.e: /:id)
205
- */
206
- params?: InferSchemaResult<TSchemas["params"], Params>;
207
- /**
208
- * Query parameters to append to the URL.
209
- */
210
- query?: InferSchemaResult<TSchemas["query"], Query>;
211
- }
212
-
213
219
  interface CallApiSchemas {
214
220
  /**
215
221
  * The schema to use for validating the request body.
@@ -261,56 +267,36 @@ interface CallApiValidators<TData = unknown, TErrorData = unknown> {
261
267
  }
262
268
  type InferSchemaResult<TSchema, TData> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : TData;
263
269
 
264
- type Parser = (responseString: string) => Awaitable<Record<string, unknown>>;
265
- declare const getResponseType: <TResponse>(response: Response, parser?: Parser) => {
266
- arrayBuffer: () => Promise<ArrayBuffer>;
267
- blob: () => Promise<Blob>;
268
- formData: () => Promise<FormData>;
269
- json: () => Promise<TResponse>;
270
- stream: () => ReadableStream<Uint8Array<ArrayBufferLike>> | null;
271
- text: () => Promise<string>;
272
- };
273
- type InitResponseTypeMap<TResponse = unknown> = ReturnType<typeof getResponseType<TResponse>>;
274
- type ResponseTypeUnion = keyof InitResponseTypeMap | undefined;
275
- type ResponseTypeMap<TResponse> = {
276
- [Key in keyof InitResponseTypeMap<TResponse>]: Awaited<ReturnType<InitResponseTypeMap<TResponse>[Key]>>;
277
- };
278
- type GetResponseType<TResponse, TResponseType extends ResponseTypeUnion, TComputedMap extends ResponseTypeMap<TResponse> = ResponseTypeMap<TResponse>> = undefined extends TResponseType ? TComputedMap["json"] : TResponseType extends NonNullable<ResponseTypeUnion> ? TComputedMap[TResponseType] : never;
279
-
280
- type StreamProgressEvent = {
281
- /**
282
- * Current chunk of data being streamed
283
- */
284
- chunk: Uint8Array;
270
+ type Params = UnmaskType<Record<string, boolean | number | string> | Array<boolean | number | string>>;
271
+ type Query = UnmaskType<Record<string, boolean | number | string>>;
272
+ type InitURL = UnmaskType<string | URL>;
273
+ interface UrlOptions<TSchemas extends CallApiSchemas> {
285
274
  /**
286
- * Progress in percentage
275
+ * URL to be used in the request.
287
276
  */
288
- progress: number;
277
+ readonly initURL?: string;
289
278
  /**
290
- * Total size of data in bytes
279
+ * Parameters to be appended to the URL (i.e: /:id)
291
280
  */
292
- totalBytes: number;
281
+ params?: InferSchemaResult<TSchemas["params"], Params>;
293
282
  /**
294
- * Amount of data transferred so far
283
+ * Query parameters to append to the URL.
295
284
  */
296
- transferredBytes: number;
297
- };
298
- declare global {
299
- interface ReadableStream<R> {
300
- [Symbol.asyncIterator]: () => AsyncIterableIterator<R>;
301
- }
285
+ query?: InferSchemaResult<TSchemas["query"], Query>;
302
286
  }
303
287
 
304
288
  type UnionToIntersection<TUnion> = (TUnion extends unknown ? (param: TUnion) => void : never) extends (param: infer TParam) => void ? TParam : never;
305
289
  type InferSchema<TResult> = TResult extends StandardSchemaV1 ? InferSchemaResult<TResult, NonNullable<unknown>> : TResult;
306
290
  type InferPluginOptions<TPluginArray extends CallApiPlugin[]> = UnionToIntersection<InferSchema<ReturnType<NonNullable<TPluginArray[number]["createExtraOptions"]>>>>;
307
- type PluginInitContext<TMoreOptions = DefaultMoreOptions> = Prettify<SharedHookContext & WithMoreOptions<TMoreOptions> & {
291
+ type PluginInitContext<TMoreOptions = DefaultMoreOptions> = Prettify<SharedHookContext<TMoreOptions> & {
308
292
  initURL: InitURL | undefined;
309
293
  }>;
310
294
  type PluginInitResult = Partial<Omit<PluginInitContext, "request"> & {
311
295
  request: CallApiRequestOptions;
312
296
  }>;
313
- interface CallApiPlugin<TData = never, TErrorData = never> {
297
+ type PluginHooksWithMoreOptions<TMoreOptions = DefaultMoreOptions> = HooksOrHooksArray<never, never, TMoreOptions>;
298
+ type PluginHooks<TData = never, TErrorData = never, TMoreOptions = DefaultMoreOptions> = HooksOrHooksArray<TData, TErrorData, TMoreOptions>;
299
+ interface CallApiPlugin {
314
300
  /**
315
301
  * Defines additional options that can be passed to callApi
316
302
  */
@@ -322,7 +308,7 @@ interface CallApiPlugin<TData = never, TErrorData = never> {
322
308
  /**
323
309
  * Hooks / Interceptors for the plugin
324
310
  */
325
- hooks?: hooksOrHooksArray<TData, TErrorData>;
311
+ hooks?: PluginHooks;
326
312
  /**
327
313
  * A unique id for the plugin
328
314
  */
@@ -390,10 +376,10 @@ type Hooks<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions =
390
376
  */
391
377
  onSuccess?: (context: Prettify<SuccessContext<TData> & WithMoreOptions<TMoreOptions>>) => Awaitable<unknown>;
392
378
  };
393
- type hooksOrHooksArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
379
+ type HooksOrHooksArray<TData = DefaultDataType, TErrorData = DefaultDataType, TMoreOptions = DefaultMoreOptions> = {
394
380
  [Key in keyof Hooks<TData, TErrorData, TMoreOptions>]: Hooks<TData, TErrorData, TMoreOptions>[Key] | Array<Hooks<TData, TErrorData, TMoreOptions>[Key]>;
395
381
  };
396
- type SharedHookContext = {
382
+ type SharedHookContext<TMoreOptions = DefaultMoreOptions> = {
397
383
  /**
398
384
  * Config object passed to createFetchClient
399
385
  */
@@ -406,7 +392,7 @@ type SharedHookContext = {
406
392
  * Merged options consisting of extra options from createFetchClient, the callApi instance and default options.
407
393
  *
408
394
  */
409
- options: CombinedCallApiExtraOptions;
395
+ options: CombinedCallApiExtraOptions & Partial<TMoreOptions>;
410
396
  /**
411
397
  * Merged request consisting of request options from createFetchClient, the callApi instance and default request options.
412
398
  */
@@ -450,6 +436,22 @@ type ResponseStreamContext = UnmaskType<Prettify<SharedHookContext & {
450
436
  response: Response;
451
437
  }>>;
452
438
 
439
+ type Parser = (responseString: string) => Awaitable<Record<string, unknown>>;
440
+ declare const getResponseType: <TResponse>(response: Response, parser?: Parser) => {
441
+ arrayBuffer: () => Promise<ArrayBuffer>;
442
+ blob: () => Promise<Blob>;
443
+ formData: () => Promise<FormData>;
444
+ json: () => Promise<TResponse>;
445
+ stream: () => ReadableStream<Uint8Array<ArrayBufferLike>> | null;
446
+ text: () => Promise<string>;
447
+ };
448
+ type InitResponseTypeMap<TResponse = unknown> = ReturnType<typeof getResponseType<TResponse>>;
449
+ type ResponseTypeUnion = keyof InitResponseTypeMap | null;
450
+ type ResponseTypeMap<TResponse> = {
451
+ [Key in keyof InitResponseTypeMap<TResponse>]: Awaited<ReturnType<InitResponseTypeMap<TResponse>[Key]>>;
452
+ };
453
+ type GetResponseType<TResponse, TResponseType extends ResponseTypeUnion, TComputedResponseTypeMap extends ResponseTypeMap<TResponse> = ResponseTypeMap<TResponse>> = null extends TResponseType ? TComputedResponseTypeMap["json"] : TResponseType extends NonNullable<ResponseTypeUnion> ? TComputedResponseTypeMap[TResponseType] : never;
454
+
453
455
  type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) => Awaitable<boolean>;
454
456
  interface RetryOptions<TErrorData> {
455
457
  /**
@@ -581,7 +583,7 @@ type ResultModeOption<TErrorData, TResultMode extends ResultModeUnion> = TErrorD
581
583
  resultMode: "onlySuccessWithException";
582
584
  } : TErrorData extends false | undefined ? {
583
585
  resultMode?: "onlySuccessWithException";
584
- } : undefined extends TResultMode ? {
586
+ } : null extends TResultMode ? {
585
587
  resultMode?: TResultMode;
586
588
  } : {
587
589
  resultMode: TResultMode;
@@ -695,7 +697,7 @@ type ExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResult
695
697
  * Custom validation functions for response validation
696
698
  */
697
699
  validators?: CallApiValidators<TData, TErrorData>;
698
- } & hooksOrHooksArray<TData, TErrorData> & Partial<InferPluginOptions<TPluginArray>> & MetaOption<TSchemas> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & UrlOptions<TSchemas>;
700
+ } & HooksOrHooksArray<TData, TErrorData, Partial<InferPluginOptions<TPluginArray>>> & Partial<InferPluginOptions<TPluginArray>> & MetaOption<TSchemas> & RetryOptions<TErrorData> & ResultModeOption<TErrorData, TResultMode> & UrlOptions<TSchemas>;
699
701
  type CallApiExtraOptions<TData = DefaultDataType, TErrorData = DefaultDataType, TResultMode extends ResultModeUnion = ResultModeUnion, TThrowOnError extends boolean = DefaultThrowOnError, TResponseType extends ResponseTypeUnion = ResponseTypeUnion, TPluginArray extends CallApiPlugin[] = DefaultPluginArray, TSchemas extends CallApiSchemas = DefaultMoreOptions> = ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas> & {
700
702
  plugins?: Plugins<TPluginArray> | ((context: {
701
703
  basePlugins: Plugins<TPluginArray>;
@@ -731,8 +733,8 @@ type BaseCallApiExtraOptions<TBaseData = DefaultDataType, TBaseErrorData = Defau
731
733
  skipAutoMergeFor?: "all" | "options" | "request";
732
734
  };
733
735
  type CombinedExtraOptionsWithoutHooks = Omit<BaseCallApiExtraOptions & CallApiExtraOptions, keyof Hooks>;
734
- type ResolvedHooks = Hooks;
735
- type CombinedCallApiExtraOptions = CombinedExtraOptionsWithoutHooks & ResolvedHooks;
736
+ interface CombinedCallApiExtraOptions extends CombinedExtraOptionsWithoutHooks, Hooks {
737
+ }
736
738
  type BaseCallApiConfig<TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions> = (CallApiRequestOptions<TBaseSchemas> & BaseCallApiExtraOptions<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray, TBaseSchemas>) | ((context: {
737
739
  initURL: string;
738
740
  options: CallApiExtraOptions;
@@ -767,8 +769,8 @@ type ResultModeMap<TData = DefaultDataType, TErrorData = DefaultDataType, TRespo
767
769
  onlySuccess: CallApiResultErrorVariant<TComputedErrorData>["data"] | CallApiResultSuccessVariant<TComputedData>["data"];
768
770
  onlySuccessWithException: CallApiResultSuccessVariant<TComputedData>["data"];
769
771
  }>;
770
- type ResultModeUnion = keyof ResultModeMap | undefined;
771
- type GetCallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = TErrorData extends false | undefined ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : ResultModeUnion | undefined extends TResultMode ? TThrowOnError extends true ? ResultModeMap<TData, TErrorData, TResponseType>["allWithException"] : ResultModeMap<TData, TErrorData, TResponseType>["all"] : TResultMode extends NonNullable<ResultModeUnion> ? TResultMode extends "onlySuccess" ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : TResultMode extends "onlyResponse" ? ResultModeMap<TData, TErrorData, TResponseType>["onlyResponseWithException"] : ResultModeMap<TData, TErrorData, TResponseType>[TResultMode] : never;
772
+ type ResultModeUnion = keyof ResultModeMap | null;
773
+ type GetCallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = TErrorData extends false | undefined ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : null extends TResultMode ? TThrowOnError extends true ? ResultModeMap<TData, TErrorData, TResponseType>["allWithException"] : ResultModeMap<TData, TErrorData, TResponseType>["all"] : TResultMode extends NonNullable<ResultModeUnion> ? TResultMode extends "onlySuccess" ? ResultModeMap<TData, TErrorData, TResponseType>["onlySuccessWithException"] : TResultMode extends "onlyResponse" ? ResultModeMap<TData, TErrorData, TResponseType>["onlyResponseWithException"] : ResultModeMap<TData, TErrorData, TResponseType>[TResultMode] : never;
772
774
  type CallApiResult<TData, TErrorData, TResultMode extends ResultModeUnion, TThrowOnError extends boolean, TResponseType extends ResponseTypeUnion> = Promise<GetCallApiResult<TData, TErrorData, TResultMode, TThrowOnError, TResponseType>>;
773
775
 
774
- export { type CallApiResultErrorVariant as A, type BaseCallApiConfig as B, type CallApiPlugin as C, type DefaultPluginArray as D, type ErrorContext as E, type CallApiResultSuccessVariant as F, type CombinedCallApiExtraOptions as G, HTTPError as H, type InferSchemaResult as I, type Register as J, type PluginInitContext as P, type ResultModeUnion as R, type SharedHookContext as S, type ResponseTypeUnion as a, type CallApiSchemas as b, type CallApiConfig as c, type CallApiResult as d, type DefaultDataType as e, type DefaultThrowOnError as f, type DefaultMoreOptions as g, type CallApiParameters as h, definePlugin as i, getDefaultOptions as j, type RetryOptions as k, type PossibleHTTPError as l, type PossibleJavaScriptError as m, type Hooks as n, type hooksOrHooksArray as o, type RequestContext as p, type RequestErrorContext as q, type RequestStreamContext as r, type ResponseContext as s, type ResponseErrorContext as t, type ResponseStreamContext as u, type SuccessContext as v, type BaseCallApiExtraOptions as w, type CallApiExtraOptions as x, type CallApiRequestOptions as y, type CallApiRequestOptionsForHooks as z };
776
+ export { type CallApiRequestOptions as A, type BaseCallApiConfig as B, type CallApiPlugin as C, type DefaultPluginArray as D, type ErrorContext as E, type CallApiRequestOptionsForHooks as F, type CallApiResultErrorVariant as G, HTTPError as H, type InferSchemaResult as I, type CallApiResultSuccessVariant as J, type CombinedCallApiExtraOptions as K, type Register as L, type PluginInitContext as P, type ResultModeUnion as R, type SharedHookContext as S, type ResponseTypeUnion as a, type CallApiSchemas as b, type CallApiConfig as c, type CallApiResult as d, type DefaultDataType as e, type DefaultThrowOnError as f, type DefaultMoreOptions as g, type CallApiParameters as h, definePlugin as i, type PluginHooks as j, type PluginHooksWithMoreOptions as k, getDefaultOptions as l, type RetryOptions as m, type PossibleHTTPError as n, type PossibleJavaScriptError as o, type Hooks as p, type HooksOrHooksArray as q, type RequestContext as r, type RequestErrorContext as s, type RequestStreamContext as t, type ResponseContext as u, type ResponseErrorContext as v, type ResponseStreamContext as w, type SuccessContext as x, type BaseCallApiExtraOptions as y, type CallApiExtraOptions as z };
@@ -629,6 +629,9 @@ var getResponseType = (response, parser) => ({
629
629
  });
630
630
  var resolveResponseData = async (response, responseType, parser) => {
631
631
  const RESPONSE_TYPE_LOOKUP = getResponseType(response, parser);
632
+ if (!responseType) {
633
+ return RESPONSE_TYPE_LOOKUP.json();
634
+ }
632
635
  if (!Object.hasOwn(RESPONSE_TYPE_LOOKUP, responseType)) {
633
636
  throw new Error(`Invalid response type: ${responseType}`);
634
637
  }