@zayne-labs/callapi 1.6.17 → 1.6.20
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/cjs/{error-D4EzEII0.d.cts → error-DIHsfUiJ.d.cts} +22 -5
- package/dist/cjs/index.cjs +53 -43
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +2 -2
- package/dist/cjs/utils/index.cjs +0 -37
- package/dist/cjs/utils/index.cjs.map +1 -1
- package/dist/cjs/utils/index.d.cts +1 -2
- package/dist/esm/{chunk-XF6F72SX.js → chunk-V7AOCZ3M.js} +20 -3
- package/dist/esm/chunk-V7AOCZ3M.js.map +1 -0
- package/dist/esm/{error-D4EzEII0.d.ts → error-DIHsfUiJ.d.ts} +22 -5
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +35 -46
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -2
- package/dist/esm/utils/index.js +1 -1
- package/package.json +1 -1
- package/dist/esm/chunk-XF6F72SX.js.map +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/utils/index.ts","../../../src/error.ts","../../../src/utils/type-guards.ts","../../../src/utils/type-helpers.ts","../../../src/utils/constants.ts","../../../src/utils/common.ts"],"sourcesContent":["export { toQueryString } from \"./common\";\nexport { isHTTPError, isHTTPErrorInstance } from \"./type-guards\";\nexport { defaultRetryMethods, defaultRetryStatusCodes } from \"./constants\";\n","import type {\n\tCallApiExtraOptions,\n\tCallApiResultErrorVariant,\n\tPossibleJavascriptErrorNames,\n\tResultModeMap,\n} from \"./types/common\";\nimport { omitKeys } from \"./utils/common\";\nimport { isHTTPErrorInstance, isObject } from \"./utils/type-guards\";\n\ntype ErrorInfo = {\n\tcloneResponse: CallApiExtraOptions[\"cloneResponse\"];\n\tdefaultErrorMessage: Required<CallApiExtraOptions>[\"defaultErrorMessage\"];\n\terror?: unknown;\n\tmessage?: string;\n\tresultMode: CallApiExtraOptions[\"resultMode\"];\n};\n\nexport const resolveErrorResult = <TCallApiResult = never>(info: ErrorInfo) => {\n\tconst { cloneResponse, defaultErrorMessage, error, message: customErrorMessage, resultMode } = info;\n\n\tlet apiDetails: CallApiResultErrorVariant<unknown> = {\n\t\tdata: null,\n\t\terror: {\n\t\t\terrorData: error as Error,\n\t\t\tmessage: customErrorMessage ?? (error as Error).message,\n\t\t\tname: (error as Error).name as PossibleJavascriptErrorNames,\n\t\t},\n\t\tresponse: null,\n\t};\n\n\tif (isHTTPErrorInstance<never>(error)) {\n\t\tconst { errorData, message = defaultErrorMessage, name, response } = error;\n\n\t\tapiDetails = {\n\t\t\tdata: null,\n\t\t\terror: {\n\t\t\t\terrorData,\n\t\t\t\tmessage,\n\t\t\t\tname,\n\t\t\t},\n\t\t\tresponse: cloneResponse ? response.clone() : response,\n\t\t};\n\t}\n\n\tconst resultModeMap = {\n\t\tall: apiDetails,\n\t\tallWithException: apiDetails as never,\n\t\tallWithoutResponse: omitKeys(apiDetails, [\"response\"]),\n\t\tonlyError: apiDetails.error,\n\t\tonlyResponse: apiDetails.response,\n\t\tonlyResponseWithException: apiDetails.response as never,\n\t\tonlySuccess: apiDetails.data,\n\t\tonlySuccessWithException: apiDetails.data,\n\t} satisfies ResultModeMap;\n\n\tconst getErrorResult = (customInfo?: Pick<ErrorInfo, \"message\">) => {\n\t\tconst errorResult = resultModeMap[resultMode ?? \"all\"] as TCallApiResult;\n\n\t\treturn isObject(customInfo) ? { ...errorResult, ...customInfo } : errorResult;\n\t};\n\n\treturn { apiDetails, getErrorResult };\n};\n\ntype ErrorDetails<TErrorResponse> = {\n\tdefaultErrorMessage: string;\n\terrorData: TErrorResponse;\n\tresponse: Response;\n};\n\ntype ErrorOptions = {\n\tcause?: unknown;\n};\n\nexport class HTTPError<TErrorResponse = Record<string, unknown>> extends Error {\n\terrorData: ErrorDetails<TErrorResponse>[\"errorData\"];\n\n\tisHTTPError = true;\n\n\toverride name = \"HTTPError\" as const;\n\n\tresponse: ErrorDetails<TErrorResponse>[\"response\"];\n\n\tconstructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions) {\n\t\tconst { defaultErrorMessage, errorData, response } = errorDetails;\n\n\t\tsuper((errorData as { message?: string } | undefined)?.message ?? defaultErrorMessage, errorOptions);\n\n\t\tthis.errorData = errorData;\n\t\tthis.response = response;\n\n\t\tError.captureStackTrace(this, this.constructor);\n\t}\n}\n","import { HTTPError } from \"@/error\";\nimport type { PossibleHTTPError, PossibleJavaScriptError } from \"@/types\";\nimport type { AnyFunction } from \"./type-helpers\";\n\ntype ErrorObjectUnion<TErrorData = unknown> = PossibleHTTPError<TErrorData> | PossibleJavaScriptError;\n\nexport const isHTTPError = <TErrorData>(\n\terror: ErrorObjectUnion<TErrorData> | null\n): error is PossibleHTTPError<TErrorData> => {\n\treturn isPlainObject(error) && error.name === \"HTTPError\";\n};\n\nexport const isHTTPErrorInstance = <TErrorResponse>(\n\terror: unknown\n): error is HTTPError<TErrorResponse> => {\n\treturn (\n\t\t// prettier-ignore\n\t\terror instanceof HTTPError|| (isPlainObject(error) && error.name === \"HTTPError\" && error.isHTTPError === true)\n\t);\n};\n\nexport const isArray = <TArrayItem>(value: unknown): value is TArrayItem[] => Array.isArray(value);\n\nexport const isObject = (value: unknown) => typeof value === \"object\" && value !== null;\n\nconst hasObjectPrototype = (value: unknown) => {\n\treturn Object.prototype.toString.call(value) === \"[object Object]\";\n};\n\n/**\n * @description Copied from TanStack Query's isPlainObject\n * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321\n */\nexport const isPlainObject = <TPlainObject extends Record<string, unknown>>(\n\tvalue: unknown\n): value is TPlainObject => {\n\tif (!hasObjectPrototype(value)) {\n\t\treturn false;\n\t}\n\n\t// If has no constructor\n\tconst constructor = (value as object | undefined)?.constructor;\n\tif (constructor === undefined) {\n\t\treturn true;\n\t}\n\n\t// If has modified prototype\n\tconst prototype = constructor.prototype as object;\n\tif (!hasObjectPrototype(prototype)) {\n\t\treturn false;\n\t}\n\n\t// If constructor does not have an Object-specific method\n\tif (!Object.hasOwn(prototype, \"isPrototypeOf\")) {\n\t\treturn false;\n\t}\n\n\t// Handles Objects created by Object.create(<arbitrary prototype>)\n\tif (Object.getPrototypeOf(value) !== Object.prototype) {\n\t\treturn false;\n\t}\n\n\t// It's probably a plain object at this point\n\treturn true;\n};\n\nexport const isJsonString = (value: unknown): value is string => {\n\tif (!isString(value)) {\n\t\treturn false;\n\t}\n\n\ttry {\n\t\tJSON.parse(value);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\nexport const isSerializable = (value: unknown) => {\n\treturn (\n\t\tisPlainObject(value)\n\t\t|| isArray(value)\n\t\t|| typeof (value as { toJSON: unknown } | undefined)?.toJSON === \"function\"\n\t);\n};\n\nexport const isFunction = <TFunction extends AnyFunction>(value: unknown): value is TFunction =>\n\ttypeof value === \"function\";\n\nexport const isQueryString = (value: unknown): value is string => isString(value) && value.includes(\"=\");\n\nexport const isString = (value: unknown) => typeof value === \"string\";\n\n// https://github.com/unjs/ofetch/blob/main/src/utils.ts\n// TODO Find a way to incorporate this function in checking when to apply the bodySerializer on the body and also whether to add the content type application/json\nexport const isJSONSerializable = (value: unknown) => {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- No time to make this more type-safe\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (isArray(value)) {\n\t\treturn true;\n\t}\n\tif ((value as Buffer | null)?.buffer) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\t(value?.constructor && value.constructor.name === \"Object\")\n\t\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\t\t|| typeof (value as { toJSON: () => unknown } | null)?.toJSON === \"function\"\n\t);\n};\n","// == These two types allows for adding arbitrary literal types, while still provided autocomplete for defaults.\n// == Usually intersection with \"{}\" or \"NonNullable<unknown>\" would make it work fine, but the placeholder with never type is added to make the AnyWhatever type appear last in a given union.\nexport type AnyString = string & { z_placeholder?: never };\nexport type AnyNumber = number & { z_placeholder?: never };\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is fine here\nexport type AnyObject = Record<keyof any, any>;\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport type AnyFunction<TResult = unknown> = (...args: any) => TResult;\n\nexport type CallbackFn<in TParams, out TResult = void> = (...params: TParams[]) => TResult;\n\nexport type Prettify<TObject> = NonNullable<unknown> & { [Key in keyof TObject]: TObject[Key] };\n\nexport type Writeable<TObject, TType extends \"deep\" | \"shallow\" = \"shallow\"> = {\n\t-readonly [key in keyof TObject]: TType extends \"shallow\"\n\t\t? TObject[key]\n\t\t: TType extends \"deep\"\n\t\t\t? TObject[key] extends object\n\t\t\t\t? Writeable<TObject[key], TType>\n\t\t\t\t: TObject[key]\n\t\t\t: never;\n};\n\nexport const defineEnum = <const TValue>(value: TValue) => value as Prettify<Writeable<TValue, \"deep\">>;\n\n// == Using this Immediately Indexed Mapped type helper to help show computed type of anything passed to it instead of just the type name\nexport type UnmaskType<TValue> = { _: TValue }[\"_\"];\n\nexport type Awaitable<TValue> = Promise<TValue> | TValue;\n\nexport type CommonRequestHeaders =\n\t| \"Access-Control-Allow-Credentials\"\n\t| \"Access-Control-Allow-Headers\"\n\t| \"Access-Control-Allow-Methods\"\n\t| \"Access-Control-Allow-Origin\"\n\t| \"Access-Control-Expose-Headers\"\n\t| \"Access-Control-Max-Age\"\n\t| \"Age\"\n\t| \"Allow\"\n\t| \"Cache-Control\"\n\t| \"Clear-Site-Data\"\n\t| \"Content-Disposition\"\n\t| \"Content-Encoding\"\n\t| \"Content-Language\"\n\t| \"Content-Length\"\n\t| \"Content-Location\"\n\t| \"Content-Range\"\n\t| \"Content-Security-Policy-Report-Only\"\n\t| \"Content-Security-Policy\"\n\t| \"Cookie\"\n\t| \"Cross-Origin-Embedder-Policy\"\n\t| \"Cross-Origin-Opener-Policy\"\n\t| \"Cross-Origin-Resource-Policy\"\n\t| \"Date\"\n\t| \"ETag\"\n\t| \"Expires\"\n\t| \"Last-Modified\"\n\t| \"Location\"\n\t| \"Permissions-Policy\"\n\t| \"Pragma\"\n\t| \"Retry-After\"\n\t| \"Save-Data\"\n\t| \"Sec-CH-Prefers-Color-Scheme\"\n\t| \"Sec-CH-Prefers-Reduced-Motion\"\n\t| \"Sec-CH-UA-Arch\"\n\t| \"Sec-CH-UA-Bitness\"\n\t| \"Sec-CH-UA-Form-Factor\"\n\t| \"Sec-CH-UA-Full-Version-List\"\n\t| \"Sec-CH-UA-Full-Version\"\n\t| \"Sec-CH-UA-Mobile\"\n\t| \"Sec-CH-UA-Model\"\n\t| \"Sec-CH-UA-Platform-Version\"\n\t| \"Sec-CH-UA-Platform\"\n\t| \"Sec-CH-UA-WoW64\"\n\t| \"Sec-CH-UA\"\n\t| \"Sec-Fetch-Dest\"\n\t| \"Sec-Fetch-Mode\"\n\t| \"Sec-Fetch-Site\"\n\t| \"Sec-Fetch-User\"\n\t| \"Sec-GPC\"\n\t| \"Server-Timing\"\n\t| \"Server\"\n\t| \"Service-Worker-Navigation-Preload\"\n\t| \"Set-Cookie\"\n\t| \"Strict-Transport-Security\"\n\t| \"Timing-Allow-Origin\"\n\t| \"Trailer\"\n\t| \"Transfer-Encoding\"\n\t| \"Upgrade\"\n\t| \"Vary\"\n\t| \"Warning\"\n\t| \"WWW-Authenticate\"\n\t| \"X-Content-Type-Options\"\n\t| \"X-DNS-Prefetch-Control\"\n\t| \"X-Frame-Options\"\n\t| \"X-Permitted-Cross-Domain-Policies\"\n\t| \"X-Powered-By\"\n\t| \"X-Robots-Tag\"\n\t| \"X-XSS-Protection\"\n\t| AnyString;\n\nexport type CommonAuthorizationHeaders = `${\"Basic\" | \"Bearer\" | \"Token\"} ${string}`;\n\nexport type CommonContentTypes =\n\t| \"application/epub+zip\"\n\t| \"application/gzip\"\n\t| \"application/json\"\n\t| \"application/ld+json\"\n\t| \"application/octet-stream\"\n\t| \"application/ogg\"\n\t| \"application/pdf\"\n\t| \"application/rtf\"\n\t| \"application/vnd.ms-fontobject\"\n\t| \"application/wasm\"\n\t| \"application/xhtml+xml\"\n\t| \"application/xml\"\n\t| \"application/zip\"\n\t| \"audio/aac\"\n\t| \"audio/mpeg\"\n\t| \"audio/ogg\"\n\t| \"audio/opus\"\n\t| \"audio/webm\"\n\t| \"audio/x-midi\"\n\t| \"font/otf\"\n\t| \"font/ttf\"\n\t| \"font/woff\"\n\t| \"font/woff2\"\n\t| \"image/avif\"\n\t| \"image/bmp\"\n\t| \"image/gif\"\n\t| \"image/jpeg\"\n\t| \"image/png\"\n\t| \"image/svg+xml\"\n\t| \"image/tiff\"\n\t| \"image/webp\"\n\t| \"image/x-icon\"\n\t| \"model/gltf-binary\"\n\t| \"model/gltf+json\"\n\t| \"text/calendar\"\n\t| \"text/css\"\n\t| \"text/csv\"\n\t| \"text/html\"\n\t| \"text/javascript\"\n\t| \"text/plain\"\n\t| \"video/3gpp\"\n\t| \"video/3gpp2\"\n\t| \"video/av1\"\n\t| \"video/mp2t\"\n\t| \"video/mp4\"\n\t| \"video/mpeg\"\n\t| \"video/ogg\"\n\t| \"video/webm\"\n\t| \"video/x-msvideo\"\n\t| AnyString;\n","import type { BaseCallApiExtraOptions } from \"../types/common\";\nimport { defineEnum } from \"./type-helpers\";\n\nexport const fetchSpecificKeys = defineEnum([\n\t\"body\",\n\t\"integrity\",\n\t\"method\",\n\t\"headers\",\n\t\"signal\",\n\t\"cache\",\n\t\"redirect\",\n\t\"window\",\n\t\"credentials\",\n\t\"keepalive\",\n\t\"referrer\",\n\t\"priority\",\n\t\"mode\",\n\t\"referrerPolicy\",\n] satisfies Array<keyof RequestInit>);\n\nconst retryStatusCodesLookup = defineEnum({\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t425: \"Too Early\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n});\n\nexport const defaultRetryMethods = [\"GET\", \"POST\"] satisfies BaseCallApiExtraOptions[\"retryMethods\"];\n\n// prettier-ignore\nexport const defaultRetryStatusCodes = Object.keys(retryStatusCodesLookup).map(Number) as Required<BaseCallApiExtraOptions>[\"retryStatusCodes\"];\n","import { getAuthHeader } from \"@/auth\";\nimport {\n\ttype BaseCallApiExtraOptions,\n\ttype CallApiExtraOptions,\n\ttype CallApiRequestOptions,\n\toptionsEnumToOmitFromBase,\n} from \"../types/common\";\nimport { fetchSpecificKeys } from \"./constants\";\nimport {\n\tisArray,\n\tisFunction,\n\tisJsonString,\n\tisPlainObject,\n\tisQueryString,\n\tisSerializable,\n} from \"./type-guards\";\nimport type { AnyFunction, Awaitable } from \"./type-helpers\";\n\nexport const omitKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TOmitArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToOmit: TOmitArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToOmitSet = new Set(keysToOmit);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (!keysToOmitSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Omit<TObject, TOmitArray[number]>;\n};\n\nexport const pickKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TPickArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToPick: TPickArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToPickSet = new Set(keysToPick);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (keysToPickSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Pick<TObject, TPickArray[number]>;\n};\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitBaseConfig = (baseConfig: Record<string, any>) =>\n\t[\n\t\tpickKeys(baseConfig, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(baseConfig, [\n\t\t\t...fetchSpecificKeys,\n\t\t\t...optionsEnumToOmitFromBase,\n\t\t]) as BaseCallApiExtraOptions,\n\t] as const;\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitConfig = (config: Record<string, any>) =>\n\t[\n\t\tpickKeys(config, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(config, fetchSpecificKeys) as CallApiExtraOptions,\n\t] as const;\n\ntype ToQueryStringFn = {\n\t(params: CallApiExtraOptions[\"query\"]): string | null;\n\t(params: Required<CallApiExtraOptions>[\"query\"]): string;\n};\n\nexport const toQueryString: ToQueryStringFn = (params) => {\n\tif (!params) {\n\t\tconsole.error(\"toQueryString:\", \"No query params provided!\");\n\n\t\treturn null as never;\n\t}\n\n\treturn new URLSearchParams(params as Record<string, string>).toString();\n};\n\nexport const objectifyHeaders = (headers: CallApiRequestOptions[\"headers\"]) => {\n\tif (!headers || isPlainObject(headers)) {\n\t\treturn headers;\n\t}\n\n\treturn Object.fromEntries(headers);\n};\n\ntype MergeAndResolveHeadersOptions = {\n\tauth: CallApiExtraOptions[\"auth\"];\n\tbaseHeaders?: CallApiRequestOptions[\"headers\"];\n\tbody: CallApiRequestOptions[\"body\"];\n\theaders: CallApiRequestOptions[\"headers\"];\n};\n\nexport const mergeAndResolveHeaders = (options: MergeAndResolveHeadersOptions) => {\n\tconst { auth, baseHeaders, body, headers } = options;\n\n\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\tconst shouldResolveHeaders = Boolean(baseHeaders || headers || body || auth);\n\n\t// == Return early if any of the following conditions are not met (so that native fetch would auto set the correct headers):\n\t// == - headers are provided\n\t// == - The body is an object\n\t// == - The auth option is provided\n\tif (!shouldResolveHeaders) return;\n\n\tconst headersObject: Record<string, string | undefined> = {\n\t\t...getAuthHeader(auth),\n\t\t...objectifyHeaders(baseHeaders),\n\t\t...objectifyHeaders(headers),\n\t};\n\n\tif (isQueryString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/x-www-form-urlencoded\";\n\n\t\treturn headersObject;\n\t}\n\n\tif (isSerializable(body) || isJsonString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/json\";\n\t\theadersObject.Accept = \"application/json\";\n\t}\n\n\treturn headersObject;\n};\n\nexport const combineHooks = <TInterceptor extends AnyFunction | Array<AnyFunction | undefined>>(\n\tbaseInterceptor: TInterceptor | undefined,\n\tinterceptor: TInterceptor | undefined\n) => {\n\tif (isArray(baseInterceptor)) {\n\t\treturn [baseInterceptor, interceptor].flat() as TInterceptor;\n\t}\n\n\treturn interceptor ?? baseInterceptor;\n};\n\nexport const getFetchImpl = (customFetchImpl: CallApiExtraOptions[\"customFetchImpl\"]) => {\n\tif (customFetchImpl) {\n\t\treturn customFetchImpl;\n\t}\n\n\tif (typeof globalThis !== \"undefined\" && isFunction(globalThis.fetch)) {\n\t\treturn globalThis.fetch;\n\t}\n\n\tthrow new Error(\"No fetch implementation found\");\n};\n\nexport const executeHooks = <TInterceptor extends Awaitable<unknown>>(...interceptors: TInterceptor[]) =>\n\tPromise.all(interceptors);\n\nconst PromiseWithResolvers = () => {\n\tlet reject!: (reason?: unknown) => void;\n\tlet resolve!: (value: unknown) => void;\n\n\tconst promise = new Promise((res, rej) => {\n\t\tresolve = res;\n\t\treject = rej;\n\t});\n\n\treturn { promise, reject, resolve };\n};\n\nexport const waitUntil = (delay: number) => {\n\tif (delay === 0) return;\n\n\tconst { promise, resolve } = PromiseWithResolvers();\n\n\tsetTimeout(resolve, delay);\n\n\treturn promise;\n};\n"],"mappings":";;;;;;;;;;;;;;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ACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD,CAAoC;AAEpC,IAAM,yBAAyB,WAAW;AAAA,EACzC,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN,CAAC;AAEM,IAAM,sBAAsB,CAAC,OAAO,MAAM;AAG1C,IAAM,0BAA0B,OAAO,KAAK,sBAAsB,EAAE,IAAI,MAAM;;;AC8C9E,IAAM,gBAAiC,CAAC,WAAW;AACzD,MAAI,CAAC,QAAQ;AACZ,YAAQ,MAAM,kBAAkB,2BAA2B;AAE3D,WAAO;AAAA,EACR;AAEA,SAAO,IAAI,gBAAgB,MAAgC,EAAE,SAAS;AACvE;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/utils/index.ts","../../../src/error.ts","../../../src/utils/type-guards.ts","../../../src/utils/common.ts"],"sourcesContent":["export { toQueryString } from \"./common\";\nexport { isHTTPError, isHTTPErrorInstance } from \"./type-guards\";\n","import type {\n\tCallApiExtraOptions,\n\tCallApiResultErrorVariant,\n\tPossibleJavascriptErrorNames,\n\tResultModeMap,\n} from \"./types/common\";\nimport { omitKeys } from \"./utils/common\";\nimport { isHTTPErrorInstance, isObject } from \"./utils/type-guards\";\n\ntype ErrorInfo = {\n\tcloneResponse: CallApiExtraOptions[\"cloneResponse\"];\n\tdefaultErrorMessage: Required<CallApiExtraOptions>[\"defaultErrorMessage\"];\n\terror?: unknown;\n\tmessage?: string;\n\tresultMode: CallApiExtraOptions[\"resultMode\"];\n};\n\nexport const resolveErrorResult = <TCallApiResult = never>(info: ErrorInfo) => {\n\tconst { cloneResponse, defaultErrorMessage, error, message: customErrorMessage, resultMode } = info;\n\n\tlet apiDetails: CallApiResultErrorVariant<unknown> = {\n\t\tdata: null,\n\t\terror: {\n\t\t\terrorData: error as Error,\n\t\t\tmessage: customErrorMessage ?? (error as Error).message,\n\t\t\tname: (error as Error).name as PossibleJavascriptErrorNames,\n\t\t},\n\t\tresponse: null,\n\t};\n\n\tif (isHTTPErrorInstance<never>(error)) {\n\t\tconst { errorData, message = defaultErrorMessage, name, response } = error;\n\n\t\tapiDetails = {\n\t\t\tdata: null,\n\t\t\terror: {\n\t\t\t\terrorData,\n\t\t\t\tmessage,\n\t\t\t\tname,\n\t\t\t},\n\t\t\tresponse: cloneResponse ? response.clone() : response,\n\t\t};\n\t}\n\n\tconst resultModeMap = {\n\t\tall: apiDetails,\n\t\tallWithException: apiDetails as never,\n\t\tallWithoutResponse: omitKeys(apiDetails, [\"response\"]),\n\t\tonlyError: apiDetails.error,\n\t\tonlyResponse: apiDetails.response,\n\t\tonlyResponseWithException: apiDetails.response as never,\n\t\tonlySuccess: apiDetails.data,\n\t\tonlySuccessWithException: apiDetails.data,\n\t} satisfies ResultModeMap;\n\n\tconst getErrorResult = (customInfo?: Pick<ErrorInfo, \"message\">) => {\n\t\tconst errorResult = resultModeMap[resultMode ?? \"all\"] as TCallApiResult;\n\n\t\treturn isObject(customInfo) ? { ...errorResult, ...customInfo } : errorResult;\n\t};\n\n\treturn { apiDetails, getErrorResult };\n};\n\ntype ErrorDetails<TErrorResponse> = {\n\tdefaultErrorMessage: string;\n\terrorData: TErrorResponse;\n\tresponse: Response;\n};\n\ntype ErrorOptions = {\n\tcause?: unknown;\n};\n\nexport class HTTPError<TErrorResponse = Record<string, unknown>> extends Error {\n\terrorData: ErrorDetails<TErrorResponse>[\"errorData\"];\n\n\tisHTTPError = true;\n\n\toverride name = \"HTTPError\" as const;\n\n\tresponse: ErrorDetails<TErrorResponse>[\"response\"];\n\n\tconstructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions) {\n\t\tconst { defaultErrorMessage, errorData, response } = errorDetails;\n\n\t\tsuper((errorData as { message?: string } | undefined)?.message ?? defaultErrorMessage, errorOptions);\n\n\t\tthis.errorData = errorData;\n\t\tthis.response = response;\n\n\t\tError.captureStackTrace(this, this.constructor);\n\t}\n}\n","import { HTTPError } from \"@/error\";\nimport type { PossibleHTTPError, PossibleJavaScriptError } from \"@/types\";\nimport type { AnyFunction } from \"./type-helpers\";\n\ntype ErrorObjectUnion<TErrorData = unknown> = PossibleHTTPError<TErrorData> | PossibleJavaScriptError;\n\nexport const isHTTPError = <TErrorData>(\n\terror: ErrorObjectUnion<TErrorData> | null\n): error is PossibleHTTPError<TErrorData> => {\n\treturn isPlainObject(error) && error.name === \"HTTPError\";\n};\n\nexport const isHTTPErrorInstance = <TErrorResponse>(\n\terror: unknown\n): error is HTTPError<TErrorResponse> => {\n\treturn (\n\t\t// prettier-ignore\n\t\terror instanceof HTTPError|| (isPlainObject(error) && error.name === \"HTTPError\" && error.isHTTPError === true)\n\t);\n};\n\nexport const isArray = <TArrayItem>(value: unknown): value is TArrayItem[] => Array.isArray(value);\n\nexport const isObject = (value: unknown) => typeof value === \"object\" && value !== null;\n\nconst hasObjectPrototype = (value: unknown) => {\n\treturn Object.prototype.toString.call(value) === \"[object Object]\";\n};\n\n/**\n * @description Copied from TanStack Query's isPlainObject\n * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321\n */\nexport const isPlainObject = <TPlainObject extends Record<string, unknown>>(\n\tvalue: unknown\n): value is TPlainObject => {\n\tif (!hasObjectPrototype(value)) {\n\t\treturn false;\n\t}\n\n\t// If has no constructor\n\tconst constructor = (value as object | undefined)?.constructor;\n\tif (constructor === undefined) {\n\t\treturn true;\n\t}\n\n\t// If has modified prototype\n\tconst prototype = constructor.prototype as object;\n\tif (!hasObjectPrototype(prototype)) {\n\t\treturn false;\n\t}\n\n\t// If constructor does not have an Object-specific method\n\tif (!Object.hasOwn(prototype, \"isPrototypeOf\")) {\n\t\treturn false;\n\t}\n\n\t// Handles Objects created by Object.create(<arbitrary prototype>)\n\tif (Object.getPrototypeOf(value) !== Object.prototype) {\n\t\treturn false;\n\t}\n\n\t// It's probably a plain object at this point\n\treturn true;\n};\n\nexport const isJsonString = (value: unknown): value is string => {\n\tif (!isString(value)) {\n\t\treturn false;\n\t}\n\n\ttry {\n\t\tJSON.parse(value);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\nexport const isSerializable = (value: unknown) => {\n\treturn (\n\t\tisPlainObject(value)\n\t\t|| isArray(value)\n\t\t|| typeof (value as { toJSON: unknown } | undefined)?.toJSON === \"function\"\n\t);\n};\n\nexport const isFunction = <TFunction extends AnyFunction>(value: unknown): value is TFunction =>\n\ttypeof value === \"function\";\n\nexport const isQueryString = (value: unknown): value is string => isString(value) && value.includes(\"=\");\n\nexport const isString = (value: unknown) => typeof value === \"string\";\n\n// https://github.com/unjs/ofetch/blob/main/src/utils.ts\n// TODO Find a way to incorporate this function in checking when to apply the bodySerializer on the body and also whether to add the content type application/json\nexport const isJSONSerializable = (value: unknown) => {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- No time to make this more type-safe\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (isArray(value)) {\n\t\treturn true;\n\t}\n\tif ((value as Buffer | null)?.buffer) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\t(value?.constructor && value.constructor.name === \"Object\")\n\t\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\t\t|| typeof (value as { toJSON: () => unknown } | null)?.toJSON === \"function\"\n\t);\n};\n","import { getAuthHeader } from \"@/auth\";\nimport {\n\ttype BaseCallApiExtraOptions,\n\ttype CallApiExtraOptions,\n\ttype CallApiRequestOptions,\n\toptionsEnumToOmitFromBase,\n} from \"../types/common\";\nimport { fetchSpecificKeys } from \"./constants\";\nimport {\n\tisArray,\n\tisFunction,\n\tisJsonString,\n\tisPlainObject,\n\tisQueryString,\n\tisSerializable,\n} from \"./type-guards\";\nimport type { AnyFunction, Awaitable } from \"./type-helpers\";\n\nexport const omitKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TOmitArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToOmit: TOmitArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToOmitSet = new Set(keysToOmit);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (!keysToOmitSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Omit<TObject, TOmitArray[number]>;\n};\n\nexport const pickKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TPickArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToPick: TPickArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToPickSet = new Set(keysToPick);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (keysToPickSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Pick<TObject, TPickArray[number]>;\n};\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitBaseConfig = (baseConfig: Record<string, any>) =>\n\t[\n\t\tpickKeys(baseConfig, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(baseConfig, [\n\t\t\t...fetchSpecificKeys,\n\t\t\t...optionsEnumToOmitFromBase,\n\t\t]) as BaseCallApiExtraOptions,\n\t] as const;\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitConfig = (config: Record<string, any>) =>\n\t[\n\t\tpickKeys(config, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(config, fetchSpecificKeys) as CallApiExtraOptions,\n\t] as const;\n\ntype ToQueryStringFn = {\n\t(params: CallApiExtraOptions[\"query\"]): string | null;\n\t(params: Required<CallApiExtraOptions>[\"query\"]): string;\n};\n\nexport const toQueryString: ToQueryStringFn = (params) => {\n\tif (!params) {\n\t\tconsole.error(\"toQueryString:\", \"No query params provided!\");\n\n\t\treturn null as never;\n\t}\n\n\treturn new URLSearchParams(params as Record<string, string>).toString();\n};\n\nexport const objectifyHeaders = (headers: CallApiRequestOptions[\"headers\"]) => {\n\tif (!headers || isPlainObject(headers)) {\n\t\treturn headers;\n\t}\n\n\treturn Object.fromEntries(headers);\n};\n\ntype MergeAndResolveHeadersOptions = {\n\tauth: CallApiExtraOptions[\"auth\"];\n\tbaseHeaders?: CallApiRequestOptions[\"headers\"];\n\tbody: CallApiRequestOptions[\"body\"];\n\theaders: CallApiRequestOptions[\"headers\"];\n};\n\nexport const mergeAndResolveHeaders = (options: MergeAndResolveHeadersOptions) => {\n\tconst { auth, baseHeaders, body, headers } = options;\n\n\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\tconst shouldResolveHeaders = Boolean(baseHeaders || headers || body || auth);\n\n\t// == Return early if any of the following conditions are not met (so that native fetch would auto set the correct headers):\n\t// == - headers are provided\n\t// == - The body is an object\n\t// == - The auth option is provided\n\tif (!shouldResolveHeaders) return;\n\n\tconst headersObject: Record<string, string | undefined> = {\n\t\t...getAuthHeader(auth),\n\t\t...objectifyHeaders(baseHeaders),\n\t\t...objectifyHeaders(headers),\n\t};\n\n\tif (isQueryString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/x-www-form-urlencoded\";\n\n\t\treturn headersObject;\n\t}\n\n\tif (isSerializable(body) || isJsonString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/json\";\n\t\theadersObject.Accept = \"application/json\";\n\t}\n\n\treturn headersObject;\n};\n\nexport const combineHooks = <TInterceptor extends AnyFunction | Array<AnyFunction | undefined>>(\n\tbaseInterceptor: TInterceptor | undefined,\n\tinterceptor: TInterceptor | undefined\n) => {\n\tif (isArray(baseInterceptor)) {\n\t\treturn [baseInterceptor, interceptor].flat() as TInterceptor;\n\t}\n\n\treturn interceptor ?? baseInterceptor;\n};\n\nexport const getFetchImpl = (customFetchImpl: CallApiExtraOptions[\"customFetchImpl\"]) => {\n\tif (customFetchImpl) {\n\t\treturn customFetchImpl;\n\t}\n\n\tif (typeof globalThis !== \"undefined\" && isFunction(globalThis.fetch)) {\n\t\treturn globalThis.fetch;\n\t}\n\n\tthrow new Error(\"No fetch implementation found\");\n};\n\nexport const executeHooks = <TInterceptor extends Awaitable<unknown>>(...interceptors: TInterceptor[]) =>\n\tPromise.all(interceptors);\n\nconst PromiseWithResolvers = () => {\n\tlet reject!: (reason?: unknown) => void;\n\tlet resolve!: (value: unknown) => void;\n\n\tconst promise = new Promise((res, rej) => {\n\t\tresolve = res;\n\t\treject = rej;\n\t});\n\n\treturn { promise, reject, resolve };\n};\n\nexport const waitUntil = (delay: number) => {\n\tif (delay === 0) return;\n\n\tconst { promise, resolve } = PromiseWithResolvers();\n\n\tsetTimeout(resolve, delay);\n\n\treturn promise;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;AC0EO,IAAM,YAAN,cAAkE,MAAM;AAAA,EAC9E;AAAA,EAEA,cAAc;AAAA,EAEL,OAAO;AAAA,EAEhB;AAAA,EAEA,YAAY,cAA4C,cAA6B;AACpF,UAAM,EAAE,qBAAqB,WAAW,SAAS,IAAI;AAErD,UAAO,WAAgD,WAAW,qBAAqB,YAAY;AAEnG,SAAK,YAAY;AACjB,SAAK,WAAW;AAEhB,UAAM,kBAAkB,MAAM,KAAK,WAAW;AAAA,EAC/C;AACD;;;ACvFO,IAAM,cAAc,CAC1B,UAC4C;AAC5C,SAAO,cAAc,KAAK,KAAK,MAAM,SAAS;AAC/C;AAEO,IAAM,sBAAsB,CAClC,UACwC;AACxC;AAAA;AAAA,IAEC,iBAAiB,aAAa,cAAc,KAAK,KAAK,MAAM,SAAS,eAAe,MAAM,gBAAgB;AAAA;AAE5G;AAMA,IAAM,qBAAqB,CAAC,UAAmB;AAC9C,SAAO,OAAO,UAAU,SAAS,KAAK,KAAK,MAAM;AAClD;AAMO,IAAM,gBAAgB,CAC5B,UAC2B;AAC3B,MAAI,CAAC,mBAAmB,KAAK,GAAG;AAC/B,WAAO;AAAA,EACR;AAGA,QAAM,cAAe,OAA8B;AACnD,MAAI,gBAAgB,QAAW;AAC9B,WAAO;AAAA,EACR;AAGA,QAAM,YAAY,YAAY;AAC9B,MAAI,CAAC,mBAAmB,SAAS,GAAG;AACnC,WAAO;AAAA,EACR;AAGA,MAAI,CAAC,OAAO,OAAO,WAAW,eAAe,GAAG;AAC/C,WAAO;AAAA,EACR;AAGA,MAAI,OAAO,eAAe,KAAK,MAAM,OAAO,WAAW;AACtD,WAAO;AAAA,EACR;AAGA,SAAO;AACR;;;ACgBO,IAAM,gBAAiC,CAAC,WAAW;AACzD,MAAI,CAAC,QAAQ;AACZ,YAAQ,MAAM,kBAAkB,2BAA2B;AAE3D,WAAO;AAAA,EACR;AAEA,SAAO,IAAI,gBAAgB,MAAgC,EAAE,SAAS;AACvE;","names":[]}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { A as defaultRetryMethods, F as defaultRetryStatusCodes } from '../error-D4EzEII0.cjs';
|
|
1
|
+
import { m as CallApiExtraOptions, o as PossibleHTTPError, n as PossibleJavaScriptError, H as HTTPError } from '../error-DIHsfUiJ.cjs';
|
|
3
2
|
import '@standard-schema/spec';
|
|
4
3
|
|
|
5
4
|
type ToQueryStringFn = {
|
|
@@ -175,6 +175,23 @@ var retryStatusCodesLookup = defineEnum({
|
|
|
175
175
|
});
|
|
176
176
|
var defaultRetryMethods = ["GET", "POST"];
|
|
177
177
|
var defaultRetryStatusCodes = Object.keys(retryStatusCodesLookup).map(Number);
|
|
178
|
+
var defaultExtraOptions = {
|
|
179
|
+
baseURL: "",
|
|
180
|
+
bodySerializer: JSON.stringify,
|
|
181
|
+
dedupeStrategy: "cancel",
|
|
182
|
+
defaultErrorMessage: "Failed to fetch data from server!",
|
|
183
|
+
mergedHooksExecutionMode: "parallel",
|
|
184
|
+
mergedHooksExecutionOrder: "mainHooksAfterPlugins",
|
|
185
|
+
responseType: "json",
|
|
186
|
+
resultMode: "all",
|
|
187
|
+
retryAttempts: 0,
|
|
188
|
+
retryDelay: 1e3,
|
|
189
|
+
retryMaxDelay: 1e4,
|
|
190
|
+
retryMethods: defaultRetryMethods,
|
|
191
|
+
retryStatusCodes: defaultRetryStatusCodes,
|
|
192
|
+
retryStrategy: "linear"
|
|
193
|
+
};
|
|
194
|
+
var getDefaultOptions = () => defaultExtraOptions;
|
|
178
195
|
|
|
179
196
|
// src/utils/common.ts
|
|
180
197
|
var omitKeys = (initialObject, keysToOmit) => {
|
|
@@ -272,6 +289,6 @@ var waitUntil = (delay) => {
|
|
|
272
289
|
return promise;
|
|
273
290
|
};
|
|
274
291
|
|
|
275
|
-
export { HTTPError, combineHooks,
|
|
276
|
-
//# sourceMappingURL=chunk-
|
|
277
|
-
//# sourceMappingURL=chunk-
|
|
292
|
+
export { HTTPError, combineHooks, defaultExtraOptions, executeHooks, getDefaultOptions, getFetchImpl, isArray, isFunction, isHTTPError, isHTTPErrorInstance, isPlainObject, isString, mergeAndResolveHeaders, omitKeys, resolveErrorResult, splitBaseConfig, splitConfig, toQueryString, waitUntil };
|
|
293
|
+
//# sourceMappingURL=chunk-V7AOCZ3M.js.map
|
|
294
|
+
//# sourceMappingURL=chunk-V7AOCZ3M.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/error.ts","../../src/utils/type-guards.ts","../../src/auth.ts","../../src/utils/type-helpers.ts","../../src/types/common.ts","../../src/utils/constants.ts","../../src/utils/common.ts"],"names":[],"mappings":";AAiBa,IAAA,kBAAA,GAAqB,CAAyB,IAAoB,KAAA;AAC9E,EAAA,MAAM,EAAE,a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UAAU,CAAG,EAAA;AAEjB,EAAA,MAAM,EAAE,OAAA,EAAS,OAAQ,EAAA,GAAI,oBAAqB,EAAA;AAElD,EAAA,UAAA,CAAW,SAAS,KAAK,CAAA;AAEzB,EAAO,OAAA,OAAA;AACR","file":"chunk-V7AOCZ3M.js","sourcesContent":["import type {\n\tCallApiExtraOptions,\n\tCallApiResultErrorVariant,\n\tPossibleJavascriptErrorNames,\n\tResultModeMap,\n} from \"./types/common\";\nimport { omitKeys } from \"./utils/common\";\nimport { isHTTPErrorInstance, isObject } from \"./utils/type-guards\";\n\ntype ErrorInfo = {\n\tcloneResponse: CallApiExtraOptions[\"cloneResponse\"];\n\tdefaultErrorMessage: Required<CallApiExtraOptions>[\"defaultErrorMessage\"];\n\terror?: unknown;\n\tmessage?: string;\n\tresultMode: CallApiExtraOptions[\"resultMode\"];\n};\n\nexport const resolveErrorResult = <TCallApiResult = never>(info: ErrorInfo) => {\n\tconst { cloneResponse, defaultErrorMessage, error, message: customErrorMessage, resultMode } = info;\n\n\tlet apiDetails: CallApiResultErrorVariant<unknown> = {\n\t\tdata: null,\n\t\terror: {\n\t\t\terrorData: error as Error,\n\t\t\tmessage: customErrorMessage ?? (error as Error).message,\n\t\t\tname: (error as Error).name as PossibleJavascriptErrorNames,\n\t\t},\n\t\tresponse: null,\n\t};\n\n\tif (isHTTPErrorInstance<never>(error)) {\n\t\tconst { errorData, message = defaultErrorMessage, name, response } = error;\n\n\t\tapiDetails = {\n\t\t\tdata: null,\n\t\t\terror: {\n\t\t\t\terrorData,\n\t\t\t\tmessage,\n\t\t\t\tname,\n\t\t\t},\n\t\t\tresponse: cloneResponse ? response.clone() : response,\n\t\t};\n\t}\n\n\tconst resultModeMap = {\n\t\tall: apiDetails,\n\t\tallWithException: apiDetails as never,\n\t\tallWithoutResponse: omitKeys(apiDetails, [\"response\"]),\n\t\tonlyError: apiDetails.error,\n\t\tonlyResponse: apiDetails.response,\n\t\tonlyResponseWithException: apiDetails.response as never,\n\t\tonlySuccess: apiDetails.data,\n\t\tonlySuccessWithException: apiDetails.data,\n\t} satisfies ResultModeMap;\n\n\tconst getErrorResult = (customInfo?: Pick<ErrorInfo, \"message\">) => {\n\t\tconst errorResult = resultModeMap[resultMode ?? \"all\"] as TCallApiResult;\n\n\t\treturn isObject(customInfo) ? { ...errorResult, ...customInfo } : errorResult;\n\t};\n\n\treturn { apiDetails, getErrorResult };\n};\n\ntype ErrorDetails<TErrorResponse> = {\n\tdefaultErrorMessage: string;\n\terrorData: TErrorResponse;\n\tresponse: Response;\n};\n\ntype ErrorOptions = {\n\tcause?: unknown;\n};\n\nexport class HTTPError<TErrorResponse = Record<string, unknown>> extends Error {\n\terrorData: ErrorDetails<TErrorResponse>[\"errorData\"];\n\n\tisHTTPError = true;\n\n\toverride name = \"HTTPError\" as const;\n\n\tresponse: ErrorDetails<TErrorResponse>[\"response\"];\n\n\tconstructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions) {\n\t\tconst { defaultErrorMessage, errorData, response } = errorDetails;\n\n\t\tsuper((errorData as { message?: string } | undefined)?.message ?? defaultErrorMessage, errorOptions);\n\n\t\tthis.errorData = errorData;\n\t\tthis.response = response;\n\n\t\tError.captureStackTrace(this, this.constructor);\n\t}\n}\n","import { HTTPError } from \"@/error\";\nimport type { PossibleHTTPError, PossibleJavaScriptError } from \"@/types\";\nimport type { AnyFunction } from \"./type-helpers\";\n\ntype ErrorObjectUnion<TErrorData = unknown> = PossibleHTTPError<TErrorData> | PossibleJavaScriptError;\n\nexport const isHTTPError = <TErrorData>(\n\terror: ErrorObjectUnion<TErrorData> | null\n): error is PossibleHTTPError<TErrorData> => {\n\treturn isPlainObject(error) && error.name === \"HTTPError\";\n};\n\nexport const isHTTPErrorInstance = <TErrorResponse>(\n\terror: unknown\n): error is HTTPError<TErrorResponse> => {\n\treturn (\n\t\t// prettier-ignore\n\t\terror instanceof HTTPError|| (isPlainObject(error) && error.name === \"HTTPError\" && error.isHTTPError === true)\n\t);\n};\n\nexport const isArray = <TArrayItem>(value: unknown): value is TArrayItem[] => Array.isArray(value);\n\nexport const isObject = (value: unknown) => typeof value === \"object\" && value !== null;\n\nconst hasObjectPrototype = (value: unknown) => {\n\treturn Object.prototype.toString.call(value) === \"[object Object]\";\n};\n\n/**\n * @description Copied from TanStack Query's isPlainObject\n * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321\n */\nexport const isPlainObject = <TPlainObject extends Record<string, unknown>>(\n\tvalue: unknown\n): value is TPlainObject => {\n\tif (!hasObjectPrototype(value)) {\n\t\treturn false;\n\t}\n\n\t// If has no constructor\n\tconst constructor = (value as object | undefined)?.constructor;\n\tif (constructor === undefined) {\n\t\treturn true;\n\t}\n\n\t// If has modified prototype\n\tconst prototype = constructor.prototype as object;\n\tif (!hasObjectPrototype(prototype)) {\n\t\treturn false;\n\t}\n\n\t// If constructor does not have an Object-specific method\n\tif (!Object.hasOwn(prototype, \"isPrototypeOf\")) {\n\t\treturn false;\n\t}\n\n\t// Handles Objects created by Object.create(<arbitrary prototype>)\n\tif (Object.getPrototypeOf(value) !== Object.prototype) {\n\t\treturn false;\n\t}\n\n\t// It's probably a plain object at this point\n\treturn true;\n};\n\nexport const isJsonString = (value: unknown): value is string => {\n\tif (!isString(value)) {\n\t\treturn false;\n\t}\n\n\ttry {\n\t\tJSON.parse(value);\n\t\treturn true;\n\t} catch {\n\t\treturn false;\n\t}\n};\n\nexport const isSerializable = (value: unknown) => {\n\treturn (\n\t\tisPlainObject(value)\n\t\t|| isArray(value)\n\t\t|| typeof (value as { toJSON: unknown } | undefined)?.toJSON === \"function\"\n\t);\n};\n\nexport const isFunction = <TFunction extends AnyFunction>(value: unknown): value is TFunction =>\n\ttypeof value === \"function\";\n\nexport const isQueryString = (value: unknown): value is string => isString(value) && value.includes(\"=\");\n\nexport const isString = (value: unknown) => typeof value === \"string\";\n\n// https://github.com/unjs/ofetch/blob/main/src/utils.ts\n// TODO Find a way to incorporate this function in checking when to apply the bodySerializer on the body and also whether to add the content type application/json\nexport const isJSONSerializable = (value: unknown) => {\n\tif (value === undefined) {\n\t\treturn false;\n\t}\n\tconst t = typeof value;\n\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- No time to make this more type-safe\n\tif (t === \"string\" || t === \"number\" || t === \"boolean\" || t === null) {\n\t\treturn true;\n\t}\n\tif (t !== \"object\") {\n\t\treturn false;\n\t}\n\tif (isArray(value)) {\n\t\treturn true;\n\t}\n\tif ((value as Buffer | null)?.buffer) {\n\t\treturn false;\n\t}\n\n\treturn (\n\t\t(value?.constructor && value.constructor.name === \"Object\")\n\t\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\t\t|| typeof (value as { toJSON: () => unknown } | null)?.toJSON === \"function\"\n\t);\n};\n","/* eslint-disable perfectionist/sort-object-types -- Avoid Sorting for now */\n\nimport type { ExtraOptions } from \"./types/common\";\nimport { isFunction, isString } from \"./utils/type-guards\";\n\ntype ValueOrFunctionResult<TValue> = TValue | (() => TValue);\n\n/**\n * Bearer Or Token authentication\n *\n * The value of `bearer` will be added to a header as\n * `auth: Bearer some-auth-token`,\n *\n * The value of `token` will be added to a header as\n * `auth: Token some-auth-token`,\n */\nexport type BearerOrTokenAuth =\n\t| {\n\t\t\ttype?: \"Bearer\";\n\t\t\tbearer?: ValueOrFunctionResult<string | null>;\n\t\t\ttoken?: never;\n\t }\n\t| {\n\t\t\ttype?: \"Token\";\n\t\t\tbearer?: never;\n\t\t\ttoken?: ValueOrFunctionResult<string | null>;\n\t };\n\n/**\n * Basic auth\n */\nexport type BasicAuth = {\n\ttype: \"Basic\";\n\tusername: ValueOrFunctionResult<string | null | undefined>;\n\tpassword: ValueOrFunctionResult<string | null | undefined>;\n};\n\n/**\n * Custom auth\n *\n * @param prefix - prefix of the header\n * @param authValue - value of the header\n *\n * @example\n * ```ts\n * {\n * type: \"Custom\",\n * prefix: \"Token\",\n * authValue: \"token\"\n * }\n * ```\n */\nexport type CustomAuth = {\n\ttype: \"Custom\";\n\tprefix: ValueOrFunctionResult<string | null | undefined>;\n\tvalue: ValueOrFunctionResult<string | null | undefined>;\n};\n\n// eslint-disable-next-line perfectionist/sort-union-types -- Let the first one be first\nexport type Auth = BearerOrTokenAuth | BasicAuth | CustomAuth;\n\nconst getValue = (value: ValueOrFunctionResult<string | null | undefined>) => {\n\treturn isFunction(value) ? value() : value;\n};\n\ntype AuthorizationHeader = {\n\tAuthorization: string;\n};\n\nexport const getAuthHeader = (auth: ExtraOptions[\"auth\"]): false | AuthorizationHeader | undefined => {\n\tif (auth === undefined) return;\n\n\tif (isString(auth) || auth === null) {\n\t\treturn { Authorization: `Bearer ${auth}` };\n\t}\n\n\tswitch (auth.type) {\n\t\tcase \"Basic\": {\n\t\t\tconst username = getValue(auth.username);\n\t\t\tconst password = getValue(auth.password);\n\n\t\t\tif (username === undefined || password === undefined) return;\n\n\t\t\treturn {\n\t\t\t\tAuthorization: `Basic ${globalThis.btoa(`${username}:${password}`)}`,\n\t\t\t};\n\t\t}\n\n\t\tcase \"Custom\": {\n\t\t\tconst value = getValue(auth.value);\n\n\t\t\tif (value === undefined) return;\n\n\t\t\tconst prefix = getValue(auth.prefix);\n\n\t\t\treturn {\n\t\t\t\tAuthorization: `${prefix} ${value}`,\n\t\t\t};\n\t\t}\n\n\t\tdefault: {\n\t\t\tconst bearer = getValue(auth.bearer);\n\t\t\tconst token = getValue(auth.token);\n\n\t\t\tif (\"token\" in auth && token !== undefined) {\n\t\t\t\treturn { Authorization: `Token ${token}` };\n\t\t\t}\n\n\t\t\treturn bearer !== undefined && { Authorization: `Bearer ${bearer}` };\n\t\t}\n\t}\n};\n","// == These two types allows for adding arbitrary literal types, while still provided autocomplete for defaults.\n// == Usually intersection with \"{}\" or \"NonNullable<unknown>\" would make it work fine, but the placeholder with never type is added to make the AnyWhatever type appear last in a given union.\nexport type AnyString = string & { z_placeholder?: never };\nexport type AnyNumber = number & { z_placeholder?: never };\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is fine here\nexport type AnyObject = Record<keyof any, any>;\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport type AnyFunction<TResult = unknown> = (...args: any) => TResult;\n\nexport type CallbackFn<in TParams, out TResult = void> = (...params: TParams[]) => TResult;\n\nexport type Prettify<TObject> = NonNullable<unknown> & { [Key in keyof TObject]: TObject[Key] };\n\nexport type Writeable<TObject, TType extends \"deep\" | \"shallow\" = \"shallow\"> = {\n\t-readonly [key in keyof TObject]: TType extends \"shallow\"\n\t\t? TObject[key]\n\t\t: TType extends \"deep\"\n\t\t\t? TObject[key] extends object\n\t\t\t\t? Writeable<TObject[key], TType>\n\t\t\t\t: TObject[key]\n\t\t\t: never;\n};\n\nexport const defineEnum = <const TValue>(value: TValue) => value as Prettify<Writeable<TValue, \"deep\">>;\n\n// == Using this Immediately Indexed Mapped type helper to help show computed type of anything passed to it instead of just the type name\nexport type UnmaskType<TValue> = { _: TValue }[\"_\"];\n\nexport type Awaitable<TValue> = Promise<TValue> | TValue;\n\nexport type CommonRequestHeaders =\n\t| \"Access-Control-Allow-Credentials\"\n\t| \"Access-Control-Allow-Headers\"\n\t| \"Access-Control-Allow-Methods\"\n\t| \"Access-Control-Allow-Origin\"\n\t| \"Access-Control-Expose-Headers\"\n\t| \"Access-Control-Max-Age\"\n\t| \"Age\"\n\t| \"Allow\"\n\t| \"Cache-Control\"\n\t| \"Clear-Site-Data\"\n\t| \"Content-Disposition\"\n\t| \"Content-Encoding\"\n\t| \"Content-Language\"\n\t| \"Content-Length\"\n\t| \"Content-Location\"\n\t| \"Content-Range\"\n\t| \"Content-Security-Policy-Report-Only\"\n\t| \"Content-Security-Policy\"\n\t| \"Cookie\"\n\t| \"Cross-Origin-Embedder-Policy\"\n\t| \"Cross-Origin-Opener-Policy\"\n\t| \"Cross-Origin-Resource-Policy\"\n\t| \"Date\"\n\t| \"ETag\"\n\t| \"Expires\"\n\t| \"Last-Modified\"\n\t| \"Location\"\n\t| \"Permissions-Policy\"\n\t| \"Pragma\"\n\t| \"Retry-After\"\n\t| \"Save-Data\"\n\t| \"Sec-CH-Prefers-Color-Scheme\"\n\t| \"Sec-CH-Prefers-Reduced-Motion\"\n\t| \"Sec-CH-UA-Arch\"\n\t| \"Sec-CH-UA-Bitness\"\n\t| \"Sec-CH-UA-Form-Factor\"\n\t| \"Sec-CH-UA-Full-Version-List\"\n\t| \"Sec-CH-UA-Full-Version\"\n\t| \"Sec-CH-UA-Mobile\"\n\t| \"Sec-CH-UA-Model\"\n\t| \"Sec-CH-UA-Platform-Version\"\n\t| \"Sec-CH-UA-Platform\"\n\t| \"Sec-CH-UA-WoW64\"\n\t| \"Sec-CH-UA\"\n\t| \"Sec-Fetch-Dest\"\n\t| \"Sec-Fetch-Mode\"\n\t| \"Sec-Fetch-Site\"\n\t| \"Sec-Fetch-User\"\n\t| \"Sec-GPC\"\n\t| \"Server-Timing\"\n\t| \"Server\"\n\t| \"Service-Worker-Navigation-Preload\"\n\t| \"Set-Cookie\"\n\t| \"Strict-Transport-Security\"\n\t| \"Timing-Allow-Origin\"\n\t| \"Trailer\"\n\t| \"Transfer-Encoding\"\n\t| \"Upgrade\"\n\t| \"Vary\"\n\t| \"Warning\"\n\t| \"WWW-Authenticate\"\n\t| \"X-Content-Type-Options\"\n\t| \"X-DNS-Prefetch-Control\"\n\t| \"X-Frame-Options\"\n\t| \"X-Permitted-Cross-Domain-Policies\"\n\t| \"X-Powered-By\"\n\t| \"X-Robots-Tag\"\n\t| \"X-XSS-Protection\"\n\t| AnyString;\n\nexport type CommonAuthorizationHeaders = `${\"Basic\" | \"Bearer\" | \"Token\"} ${string}`;\n\nexport type CommonContentTypes =\n\t| \"application/epub+zip\"\n\t| \"application/gzip\"\n\t| \"application/json\"\n\t| \"application/ld+json\"\n\t| \"application/octet-stream\"\n\t| \"application/ogg\"\n\t| \"application/pdf\"\n\t| \"application/rtf\"\n\t| \"application/vnd.ms-fontobject\"\n\t| \"application/wasm\"\n\t| \"application/xhtml+xml\"\n\t| \"application/xml\"\n\t| \"application/zip\"\n\t| \"audio/aac\"\n\t| \"audio/mpeg\"\n\t| \"audio/ogg\"\n\t| \"audio/opus\"\n\t| \"audio/webm\"\n\t| \"audio/x-midi\"\n\t| \"font/otf\"\n\t| \"font/ttf\"\n\t| \"font/woff\"\n\t| \"font/woff2\"\n\t| \"image/avif\"\n\t| \"image/bmp\"\n\t| \"image/gif\"\n\t| \"image/jpeg\"\n\t| \"image/png\"\n\t| \"image/svg+xml\"\n\t| \"image/tiff\"\n\t| \"image/webp\"\n\t| \"image/x-icon\"\n\t| \"model/gltf-binary\"\n\t| \"model/gltf+json\"\n\t| \"text/calendar\"\n\t| \"text/css\"\n\t| \"text/csv\"\n\t| \"text/html\"\n\t| \"text/javascript\"\n\t| \"text/plain\"\n\t| \"video/3gpp\"\n\t| \"video/3gpp2\"\n\t| \"video/av1\"\n\t| \"video/mp2t\"\n\t| \"video/mp4\"\n\t| \"video/mpeg\"\n\t| \"video/ogg\"\n\t| \"video/webm\"\n\t| \"video/x-msvideo\"\n\t| AnyString;\n","/* eslint-disable ts-eslint/consistent-type-definitions -- I need to use interfaces for the sake of user overrides */\nimport type { Auth } from \"../auth\";\nimport type { CallApiPlugin, InferPluginOptions, Plugins } from \"../plugins\";\nimport type { GetResponseType, ResponseTypeUnion } from \"../response\";\nimport type { RetryOptions } from \"../retry\";\nimport type { InitURL, UrlOptions } from \"../url\";\nimport type { fetchSpecificKeys } from \"../utils/constants\";\nimport { type Awaitable, type UnmaskType, defineEnum } from \"../utils/type-helpers\";\nimport type { CallApiSchemas, CallApiValidators, InferSchemaResult } from \"../validation\";\nimport type {\n\tBodyOption,\n\tHeadersOption,\n\tMetaOption,\n\tMethodOption,\n\tResultModeOption,\n} from \"./conditional-types\";\nimport type {\n\tDefaultDataType,\n\tDefaultMoreOptions,\n\tDefaultPluginArray,\n\tDefaultThrowOnError,\n} from \"./default-types\";\n\ntype FetchSpecificKeysUnion = Exclude<(typeof fetchSpecificKeys)[number], \"body\" | \"headers\" | \"method\">;\n\nexport type CallApiRequestOptions<TSchemas extends CallApiSchemas = DefaultMoreOptions> =\n\tBodyOption<TSchemas>\n\t\t& HeadersOption<TSchemas>\n\t\t& MethodOption<TSchemas>\n\t\t& Pick<RequestInit, FetchSpecificKeysUnion>;\n\nexport type CallApiRequestOptionsForHooks<TSchemas extends CallApiSchemas = DefaultMoreOptions> = Omit<\n\tCallApiRequestOptions<TSchemas>,\n\t\"headers\"\n> & {\n\theaders?: Record<string, string | undefined>;\n};\n\nexport type WithMoreOptions<TMoreOptions = DefaultMoreOptions> = {\n\toptions: CombinedCallApiExtraOptions & Partial<TMoreOptions>;\n};\n\nexport interface Interceptors<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTMoreOptions = DefaultMoreOptions,\n> {\n\t/**\n\t * Interceptor that will be called when any error occurs within the request/response lifecycle, regardless of whether the error is from the api or not.\n\t * It is basically a combination of `onRequestError` and `onResponseError` interceptors\n\t */\n\tonError?: (context: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;\n\n\t/**\n\t * Interceptor that will be called just before the request is made, allowing for modifications or additional operations.\n\t */\n\tonRequest?: (context: RequestContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;\n\n\t/**\n\t * Interceptor that will be called when an error occurs during the fetch request.\n\t */\n\tonRequestError?: (context: RequestErrorContext & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;\n\n\t/**\n\t * Interceptor that will be called when any response is received from the api, whether successful or not\n\t */\n\tonResponse?: (\n\t\tcontext: ResponseContext<TData, TErrorData> & WithMoreOptions<TMoreOptions>\n\t) => Awaitable<unknown>;\n\n\t/**\n\t * Interceptor that will be called when an error response is received from the api.\n\t */\n\tonResponseError?: (\n\t\tcontext: ResponseErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>\n\t) => Awaitable<unknown>;\n\n\t/**\n\t * Interceptor that will be called when a request is retried.\n\t */\n\tonRetry?: (response: ErrorContext<TErrorData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;\n\t/**\n\t * Interceptor that will be called when a successful response is received from the api.\n\t */\n\tonSuccess?: (context: SuccessContext<TData> & WithMoreOptions<TMoreOptions>) => Awaitable<unknown>;\n}\n\n/* eslint-disable perfectionist/sort-union-types -- I need arrays to be last */\nexport type InterceptorsOrInterceptorArray<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTMoreOptions = DefaultMoreOptions,\n> = {\n\t[Key in keyof Interceptors<TData, TErrorData, TMoreOptions>]:\n\t\t| Interceptors<TData, TErrorData, TMoreOptions>[Key]\n\t\t| Array<Interceptors<TData, TErrorData, TMoreOptions>[Key]>;\n};\n/* eslint-enable perfectionist/sort-union-types -- I need arrays to be last */\n\ntype FetchImpl = UnmaskType<(input: string | Request | URL, init?: RequestInit) => Promise<Response>>;\n\nexport type ExtraOptions<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTResultMode extends ResultModeUnion = ResultModeUnion,\n\tTThrowOnError extends boolean = DefaultThrowOnError,\n\tTResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTPluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTSchemas extends CallApiSchemas = DefaultMoreOptions,\n> = {\n\t/**\n\t * Authorization header value.\n\t */\n\tauth?: string | Auth | null;\n\t/**\n\t * Base URL to be prepended to all request URLs\n\t */\n\tbaseURL?: string;\n\n\t/**\n\t * Custom function to serialize the body object into a string.\n\t */\n\tbodySerializer?: (bodyData: Record<string, unknown>) => string;\n\n\t/**\n\t * Whether or not to clone the response, so response.json() and the like, can be read again else where.\n\t * @see https://developer.mozilla.org/en-US/docs/Web/API/Response/clone\n\t * @default false\n\t */\n\tcloneResponse?: boolean;\n\n\t/**\n\t * Custom fetch implementation\n\t */\n\tcustomFetchImpl?: FetchImpl;\n\n\t/**\n\t * Custom request key to be used to identify a request in the fetch deduplication strategy.\n\t * @default the full request url + string formed from the request options\n\t */\n\tdedupeKey?: string;\n\n\t/**\n\t * Defines the deduplication strategy for the request, can be set to \"none\" | \"defer\" | \"cancel\".\n\t * - If set to \"cancel\", the previous pending request with the same request key will be cancelled and lets the new request through.\n\t * - If set to \"defer\", all new request with the same request key will be share the same response, until the previous one is completed.\n\t * - If set to \"none\", deduplication is disabled.\n\t * @default \"cancel\"\n\t */\n\tdedupeStrategy?: \"cancel\" | \"defer\" | \"none\";\n\n\t/**\n\t * Default error message to use if none is provided from a response.\n\t * @default \"Failed to fetch data from server!\"\n\t */\n\tdefaultErrorMessage?: string;\n\n\t/**\n\t * Resolved request URL\n\t */\n\treadonly fullURL?: string;\n\n\t/**\n\t * Defines the mode in which the merged hooks are executed, can be set to \"parallel\" | \"sequential\".\n\t * - If set to \"parallel\", main and plugin hooks will be executed in parallel.\n\t * - If set to \"sequential\", the plugin hooks will be executed first, followed by the main hook.\n\t * @default \"parallel\"\n\t */\n\tmergedHooksExecutionMode?: \"parallel\" | \"sequential\";\n\n\t/**\n\t * - Controls what order in which the merged hooks execute\n\t * @default \"mainHooksLast\"\n\t */\n\tmergedHooksExecutionOrder?: \"mainHooksAfterPlugins\" | \"mainHooksBeforePlugins\";\n\n\t/**\n\t * An array of CallApi plugins. It allows you to extend the behavior of the library.\n\t */\n\tplugins?: Plugins<TPluginArray>;\n\n\t/**\n\t * Custom function to parse the response string into a object.\n\t */\n\tresponseParser?: (responseString: string) => Awaitable<Record<string, unknown>>;\n\n\t/**\n\t * Expected response type, affects how response is parsed\n\t * @default \"json\"\n\t */\n\tresponseType?: TResponseType;\n\n\t/**\n\t * Mode of the result, can influence how results are handled or returned.\n\t * Can be set to \"all\" | \"onlySuccess\" | \"onlyError\" | \"onlyResponse\".\n\t * @default \"all\"\n\t */\n\tresultMode?: TResultMode;\n\n\t/**\n\t * Type-safe schemas for the response validation.\n\t */\n\tschemas?: TSchemas;\n\n\t/**\n\t * If true or the function returns true, throws errors instead of returning them\n\t * The function is passed the error object and can be used to conditionally throw the error\n\t * @default false\n\t */\n\tthrowOnError?: TThrowOnError | ((context: ErrorContext<TErrorData>) => TThrowOnError);\n\n\t/**\n\t * Request timeout in milliseconds\n\t */\n\ttimeout?: number;\n\n\t/**\n\t * Custom validation functions for response validation\n\t */\n\tvalidators?: CallApiValidators<TData, TErrorData>;\n\t/* eslint-disable perfectionist/sort-intersection-types -- Allow these to be last for the sake of docs */\n} & InterceptorsOrInterceptorArray<TData, TErrorData>\n\t& Partial<InferPluginOptions<TPluginArray>>\n\t& MetaOption<TSchemas>\n\t& RetryOptions<TErrorData>\n\t& ResultModeOption<TErrorData, TResultMode>\n\t& UrlOptions<TSchemas>;\n/* eslint-enable perfectionist/sort-intersection-types -- Allow these to be last for the sake of docs */\n\nexport const optionsEnumToExtendFromBase = defineEnum([\"plugins\", \"validators\", \"schemas\"] satisfies Array<\n\tkeyof ExtraOptions\n>);\n\nexport type CallApiExtraOptions<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTResultMode extends ResultModeUnion = ResultModeUnion,\n\tTThrowOnError extends boolean = DefaultThrowOnError,\n\tTResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTPluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTSchemas extends CallApiSchemas = DefaultMoreOptions,\n> = ExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas> & {\n\t/**\n\t * Options that should extend the base options.\n\t */\n\textend?: Pick<\n\t\tExtraOptions<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas>,\n\t\t(typeof optionsEnumToExtendFromBase)[number]\n\t>;\n};\n\nexport const optionsEnumToOmitFromBase = defineEnum([\"extend\", \"dedupeKey\"] satisfies Array<\n\tkeyof CallApiExtraOptions\n>);\n\nexport type BaseCallApiExtraOptions<\n\tTBaseData = DefaultDataType,\n\tTBaseErrorData = DefaultDataType,\n\tTBaseResultMode extends ResultModeUnion = ResultModeUnion,\n\tTBaseThrowOnError extends boolean = DefaultThrowOnError,\n\tTBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTBasePluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTBaseSchemas extends CallApiSchemas = DefaultMoreOptions,\n> = Omit<\n\tPartial<\n\t\tCallApiExtraOptions<\n\t\t\tTBaseData,\n\t\t\tTBaseErrorData,\n\t\t\tTBaseResultMode,\n\t\t\tTBaseThrowOnError,\n\t\t\tTBaseResponseType,\n\t\t\tTBasePluginArray,\n\t\t\tTBaseSchemas\n\t\t>\n\t>,\n\t(typeof optionsEnumToOmitFromBase)[number]\n>;\n\nexport type CombinedCallApiExtraOptions = BaseCallApiExtraOptions & CallApiExtraOptions;\n\nexport type BaseCallApiConfig<\n\tTBaseData = DefaultDataType,\n\tTBaseErrorData = DefaultDataType,\n\tTBaseResultMode extends ResultModeUnion = ResultModeUnion,\n\tTBaseThrowOnError extends boolean = DefaultThrowOnError,\n\tTBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTBasePluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTBaseSchemas extends CallApiSchemas = DefaultMoreOptions,\n> =\n\t| (CallApiRequestOptions<TBaseSchemas> // eslint-disable-next-line perfectionist/sort-intersection-types -- Allow\n\t\t\t& BaseCallApiExtraOptions<\n\t\t\t\tTBaseData,\n\t\t\t\tTBaseErrorData,\n\t\t\t\tTBaseResultMode,\n\t\t\t\tTBaseThrowOnError,\n\t\t\t\tTBaseResponseType,\n\t\t\t\tTBasePluginArray,\n\t\t\t\tTBaseSchemas\n\t\t\t>)\n\t| ((context: {\n\t\t\tinitURL: string;\n\t\t\toptions: CallApiExtraOptions;\n\t\t\trequest: CallApiRequestOptions;\n\t }) => CallApiRequestOptions<TBaseSchemas> // eslint-disable-next-line perfectionist/sort-intersection-types -- Allow\n\t\t\t& BaseCallApiExtraOptions<\n\t\t\t\tTBaseData,\n\t\t\t\tTBaseErrorData,\n\t\t\t\tTBaseResultMode,\n\t\t\t\tTBaseThrowOnError,\n\t\t\t\tTBaseResponseType,\n\t\t\t\tTBasePluginArray,\n\t\t\t\tTBaseSchemas\n\t\t\t>);\n\nexport type CallApiConfig<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTResultMode extends ResultModeUnion = ResultModeUnion,\n\tTThrowOnError extends boolean = DefaultThrowOnError,\n\tTResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTPluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTSchemas extends CallApiSchemas = DefaultMoreOptions,\n> = CallApiRequestOptions<TSchemas> // eslint-disable-next-line perfectionist/sort-intersection-types -- Allow these to be last for the sake of docs\n\t& CallApiExtraOptions<\n\t\tTData,\n\t\tTErrorData,\n\t\tTResultMode,\n\t\tTThrowOnError,\n\t\tTResponseType,\n\t\tTPluginArray,\n\t\tTSchemas\n\t>;\n\nexport type CallApiParameters<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTResultMode extends ResultModeUnion = ResultModeUnion,\n\tTThrowOnError extends boolean = DefaultThrowOnError,\n\tTResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTPluginArray extends CallApiPlugin[] = DefaultPluginArray,\n\tTSchemas extends CallApiSchemas = DefaultMoreOptions,\n> = [\n\tinitURL: InferSchemaResult<TSchemas[\"initURL\"], InitURL>,\n\tconfig?: CallApiConfig<\n\t\tTData,\n\t\tTErrorData,\n\t\tTResultMode,\n\t\tTThrowOnError,\n\t\tTResponseType,\n\t\tTPluginArray,\n\t\tTSchemas\n\t>,\n];\n\nexport type RequestContext = UnmaskType<{\n\toptions: CombinedCallApiExtraOptions;\n\trequest: CallApiRequestOptionsForHooks;\n}>;\n\nexport type ResponseContext<TData, TErrorData> = UnmaskType<\n\t| {\n\t\t\tdata: TData;\n\t\t\terror: null;\n\t\t\toptions: CombinedCallApiExtraOptions;\n\t\t\trequest: CallApiRequestOptionsForHooks;\n\t\t\tresponse: Response;\n\t }\n\t// eslint-disable-next-line perfectionist/sort-union-types -- I need the first one to be first\n\t| {\n\t\t\tdata: null;\n\t\t\terror: PossibleHTTPError<TErrorData>;\n\t\t\toptions: CombinedCallApiExtraOptions;\n\t\t\trequest: CallApiRequestOptionsForHooks;\n\t\t\tresponse: Response;\n\t }\n>;\n\nexport type SuccessContext<TData> = UnmaskType<{\n\tdata: TData;\n\toptions: CombinedCallApiExtraOptions;\n\trequest: CallApiRequestOptionsForHooks;\n\tresponse: Response;\n}>;\n\nexport type PossibleJavascriptErrorNames =\n\t| \"AbortError\"\n\t| \"Error\"\n\t| \"SyntaxError\"\n\t| \"TimeoutError\"\n\t| \"TypeError\"\n\t| (`${string}Error` & DefaultMoreOptions);\n\nexport type PossibleJavaScriptError = UnmaskType<{\n\terrorData: DOMException | Error | SyntaxError | TypeError;\n\tmessage: string;\n\tname: PossibleJavascriptErrorNames;\n}>;\n\nexport type PossibleHTTPError<TErrorData> = UnmaskType<{\n\terrorData: TErrorData;\n\tmessage: string;\n\tname: \"HTTPError\";\n}>;\n\nexport type RequestErrorContext = UnmaskType<{\n\terror: PossibleJavaScriptError;\n\toptions: CombinedCallApiExtraOptions;\n\trequest: CallApiRequestOptionsForHooks;\n\tresponse: null;\n}>;\n\nexport type ResponseErrorContext<TErrorData> = UnmaskType<{\n\terror: PossibleHTTPError<TErrorData>;\n\toptions: CombinedCallApiExtraOptions;\n\trequest: CallApiRequestOptionsForHooks;\n\tresponse: Response;\n}>;\n\nexport type ErrorContext<TErrorData> = UnmaskType<\n\t| {\n\t\t\terror: PossibleHTTPError<TErrorData>;\n\t\t\toptions: CombinedCallApiExtraOptions;\n\t\t\trequest: CallApiRequestOptionsForHooks;\n\t\t\tresponse: Response;\n\t }\n\t| {\n\t\t\terror: PossibleJavaScriptError;\n\t\t\toptions: CombinedCallApiExtraOptions;\n\t\t\trequest: CallApiRequestOptionsForHooks;\n\t\t\tresponse: null;\n\t }\n>;\n\nexport type CallApiResultSuccessVariant<TData> = {\n\tdata: TData;\n\terror: null;\n\tresponse: Response;\n};\n\nexport type CallApiResultErrorVariant<TErrorData> =\n\t| {\n\t\t\tdata: null;\n\t\t\terror: PossibleHTTPError<TErrorData>;\n\t\t\tresponse: Response;\n\t }\n\t| {\n\t\t\tdata: null;\n\t\t\terror: PossibleJavaScriptError;\n\t\t\tresponse: null;\n\t };\n\nexport type ResultModeMap<\n\tTData = DefaultDataType,\n\tTErrorData = DefaultDataType,\n\tTResponseType extends ResponseTypeUnion = ResponseTypeUnion,\n\tTComputedData = GetResponseType<TData, TResponseType>,\n\tTComputedErrorData = GetResponseType<TErrorData, TResponseType>,\n> = UnmaskType<{\n\t/* eslint-disable perfectionist/sort-union-types -- I need the first one to be first */\n\tall: CallApiResultSuccessVariant<TComputedData> | CallApiResultErrorVariant<TComputedErrorData>;\n\n\tallWithException: CallApiResultSuccessVariant<TComputedData>;\n\n\tallWithoutResponse:\n\t\t| CallApiResultSuccessVariant<TComputedData>[\"data\" | \"error\"]\n\t\t| CallApiResultErrorVariant<TComputedErrorData>[\"data\" | \"error\"];\n\n\tonlyError:\n\t\t| CallApiResultSuccessVariant<TComputedData>[\"error\"]\n\t\t| CallApiResultErrorVariant<TComputedErrorData>[\"error\"];\n\n\tonlyResponse:\n\t\t| CallApiResultErrorVariant<TComputedErrorData>[\"response\"]\n\t\t| CallApiResultSuccessVariant<TComputedData>[\"response\"];\n\n\tonlyResponseWithException: CallApiResultSuccessVariant<TComputedErrorData>[\"response\"];\n\n\tonlySuccess:\n\t\t| CallApiResultErrorVariant<TComputedErrorData>[\"data\"]\n\t\t| CallApiResultSuccessVariant<TComputedData>[\"data\"];\n\n\tonlySuccessWithException: CallApiResultSuccessVariant<TComputedData>[\"data\"];\n\t/* eslint-enable perfectionist/sort-union-types -- I need the first one to be first */\n}>;\n\nexport type ResultModeUnion = keyof ResultModeMap | undefined;\n\nexport type GetCallApiResult<\n\tTData,\n\tTErrorData,\n\tTResultMode extends ResultModeUnion,\n\tTThrowOnError extends boolean,\n\tTResponseType extends ResponseTypeUnion,\n> = TErrorData extends false | undefined\n\t? ResultModeMap<TData, TErrorData, TResponseType>[\"onlySuccessWithException\"]\n\t: ResultModeUnion | undefined extends TResultMode\n\t\t? TThrowOnError extends true\n\t\t\t? ResultModeMap<TData, TErrorData, TResponseType>[\"allWithException\"]\n\t\t\t: ResultModeMap<TData, TErrorData, TResponseType>[\"all\"]\n\t\t: TResultMode extends NonNullable<ResultModeUnion>\n\t\t\t? TResultMode extends \"onlySuccess\"\n\t\t\t\t? ResultModeMap<TData, TErrorData, TResponseType>[\"onlySuccessWithException\"]\n\t\t\t\t: TResultMode extends \"onlyResponse\"\n\t\t\t\t\t? ResultModeMap<TData, TErrorData, TResponseType>[\"onlyResponseWithException\"]\n\t\t\t\t\t: ResultModeMap<TData, TErrorData, TResponseType>[TResultMode]\n\t\t\t: never;\n\nexport type CallApiResult<\n\tTData,\n\tTErrorData,\n\tTResultMode extends ResultModeUnion,\n\tTThrowOnError extends boolean,\n\tTResponseType extends ResponseTypeUnion,\n> = Promise<GetCallApiResult<TData, TErrorData, TResultMode, TThrowOnError, TResponseType>>;\n","import type { BaseCallApiExtraOptions, CombinedCallApiExtraOptions } from \"../types/common\";\nimport { defineEnum } from \"./type-helpers\";\n\nexport const fetchSpecificKeys = defineEnum([\n\t\"body\",\n\t\"integrity\",\n\t\"method\",\n\t\"headers\",\n\t\"signal\",\n\t\"cache\",\n\t\"redirect\",\n\t\"window\",\n\t\"credentials\",\n\t\"keepalive\",\n\t\"referrer\",\n\t\"priority\",\n\t\"mode\",\n\t\"referrerPolicy\",\n] satisfies Array<keyof RequestInit>);\n\nconst retryStatusCodesLookup = defineEnum({\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t425: \"Too Early\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n});\n\nexport const defaultRetryMethods = [\"GET\", \"POST\"] satisfies BaseCallApiExtraOptions[\"retryMethods\"];\n\n// prettier-ignore\nexport const defaultRetryStatusCodes = Object.keys(retryStatusCodesLookup).map(Number) as Required<BaseCallApiExtraOptions>[\"retryStatusCodes\"];\n\nexport const defaultExtraOptions = {\n\tbaseURL: \"\",\n\tbodySerializer: JSON.stringify,\n\tdedupeStrategy: \"cancel\",\n\tdefaultErrorMessage: \"Failed to fetch data from server!\",\n\tmergedHooksExecutionMode: \"parallel\",\n\tmergedHooksExecutionOrder: \"mainHooksAfterPlugins\",\n\tresponseType: \"json\",\n\tresultMode: \"all\",\n\tretryAttempts: 0,\n\tretryDelay: 1000,\n\tretryMaxDelay: 10000,\n\tretryMethods: defaultRetryMethods,\n\tretryStatusCodes: defaultRetryStatusCodes,\n\tretryStrategy: \"linear\",\n} satisfies CombinedCallApiExtraOptions;\n\nexport const getDefaultOptions = () => defaultExtraOptions;\n","import { getAuthHeader } from \"@/auth\";\nimport {\n\ttype BaseCallApiExtraOptions,\n\ttype CallApiExtraOptions,\n\ttype CallApiRequestOptions,\n\toptionsEnumToOmitFromBase,\n} from \"../types/common\";\nimport { fetchSpecificKeys } from \"./constants\";\nimport {\n\tisArray,\n\tisFunction,\n\tisJsonString,\n\tisPlainObject,\n\tisQueryString,\n\tisSerializable,\n} from \"./type-guards\";\nimport type { AnyFunction, Awaitable } from \"./type-helpers\";\n\nexport const omitKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TOmitArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToOmit: TOmitArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToOmitSet = new Set(keysToOmit);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (!keysToOmitSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Omit<TObject, TOmitArray[number]>;\n};\n\nexport const pickKeys = <\n\tTObject extends Record<string, unknown>,\n\tconst TPickArray extends Array<keyof TObject>,\n>(\n\tinitialObject: TObject,\n\tkeysToPick: TPickArray\n) => {\n\tconst updatedObject = {} as Record<string, unknown>;\n\n\tconst keysToPickSet = new Set(keysToPick);\n\n\tfor (const [key, value] of Object.entries(initialObject)) {\n\t\tif (keysToPickSet.has(key)) {\n\t\t\tupdatedObject[key] = value;\n\t\t}\n\t}\n\n\treturn updatedObject as Pick<TObject, TPickArray[number]>;\n};\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitBaseConfig = (baseConfig: Record<string, any>) =>\n\t[\n\t\tpickKeys(baseConfig, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(baseConfig, [\n\t\t\t...fetchSpecificKeys,\n\t\t\t...optionsEnumToOmitFromBase,\n\t\t]) as BaseCallApiExtraOptions,\n\t] as const;\n\n// eslint-disable-next-line ts-eslint/no-explicit-any -- Any is required here so that one can pass custom function type without type errors\nexport const splitConfig = (config: Record<string, any>) =>\n\t[\n\t\tpickKeys(config, fetchSpecificKeys) as CallApiRequestOptions,\n\t\tomitKeys(config, fetchSpecificKeys) as CallApiExtraOptions,\n\t] as const;\n\ntype ToQueryStringFn = {\n\t(params: CallApiExtraOptions[\"query\"]): string | null;\n\t(params: Required<CallApiExtraOptions>[\"query\"]): string;\n};\n\nexport const toQueryString: ToQueryStringFn = (params) => {\n\tif (!params) {\n\t\tconsole.error(\"toQueryString:\", \"No query params provided!\");\n\n\t\treturn null as never;\n\t}\n\n\treturn new URLSearchParams(params as Record<string, string>).toString();\n};\n\nexport const objectifyHeaders = (headers: CallApiRequestOptions[\"headers\"]) => {\n\tif (!headers || isPlainObject(headers)) {\n\t\treturn headers;\n\t}\n\n\treturn Object.fromEntries(headers);\n};\n\ntype MergeAndResolveHeadersOptions = {\n\tauth: CallApiExtraOptions[\"auth\"];\n\tbaseHeaders?: CallApiRequestOptions[\"headers\"];\n\tbody: CallApiRequestOptions[\"body\"];\n\theaders: CallApiRequestOptions[\"headers\"];\n};\n\nexport const mergeAndResolveHeaders = (options: MergeAndResolveHeadersOptions) => {\n\tconst { auth, baseHeaders, body, headers } = options;\n\n\t// eslint-disable-next-line ts-eslint/prefer-nullish-coalescing -- Nullish coalescing makes no sense in this boolean context\n\tconst shouldResolveHeaders = Boolean(baseHeaders || headers || body || auth);\n\n\t// == Return early if any of the following conditions are not met (so that native fetch would auto set the correct headers):\n\t// == - headers are provided\n\t// == - The body is an object\n\t// == - The auth option is provided\n\tif (!shouldResolveHeaders) return;\n\n\tconst headersObject: Record<string, string | undefined> = {\n\t\t...getAuthHeader(auth),\n\t\t...objectifyHeaders(baseHeaders),\n\t\t...objectifyHeaders(headers),\n\t};\n\n\tif (isQueryString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/x-www-form-urlencoded\";\n\n\t\treturn headersObject;\n\t}\n\n\tif (isSerializable(body) || isJsonString(body)) {\n\t\theadersObject[\"Content-Type\"] = \"application/json\";\n\t\theadersObject.Accept = \"application/json\";\n\t}\n\n\treturn headersObject;\n};\n\nexport const combineHooks = <TInterceptor extends AnyFunction | Array<AnyFunction | undefined>>(\n\tbaseInterceptor: TInterceptor | undefined,\n\tinterceptor: TInterceptor | undefined\n) => {\n\tif (isArray(baseInterceptor)) {\n\t\treturn [baseInterceptor, interceptor].flat() as TInterceptor;\n\t}\n\n\treturn interceptor ?? baseInterceptor;\n};\n\nexport const getFetchImpl = (customFetchImpl: CallApiExtraOptions[\"customFetchImpl\"]) => {\n\tif (customFetchImpl) {\n\t\treturn customFetchImpl;\n\t}\n\n\tif (typeof globalThis !== \"undefined\" && isFunction(globalThis.fetch)) {\n\t\treturn globalThis.fetch;\n\t}\n\n\tthrow new Error(\"No fetch implementation found\");\n};\n\nexport const executeHooks = <TInterceptor extends Awaitable<unknown>>(...interceptors: TInterceptor[]) =>\n\tPromise.all(interceptors);\n\nconst PromiseWithResolvers = () => {\n\tlet reject!: (reason?: unknown) => void;\n\tlet resolve!: (value: unknown) => void;\n\n\tconst promise = new Promise((res, rej) => {\n\t\tresolve = res;\n\t\treject = rej;\n\t});\n\n\treturn { promise, reject, resolve };\n};\n\nexport const waitUntil = (delay: number) => {\n\tif (delay === 0) return;\n\n\tconst { promise, resolve } = PromiseWithResolvers();\n\n\tsetTimeout(resolve, delay);\n\n\treturn promise;\n};\n"]}
|
|
@@ -102,7 +102,7 @@ interface CallApiSchemas {
|
|
|
102
102
|
*/
|
|
103
103
|
query?: StandardSchemaV1<Query>;
|
|
104
104
|
}
|
|
105
|
-
interface CallApiValidators<TData =
|
|
105
|
+
interface CallApiValidators<TData = unknown, TErrorData = unknown> {
|
|
106
106
|
/**
|
|
107
107
|
* Custom function to validate the response data.
|
|
108
108
|
*/
|
|
@@ -133,7 +133,7 @@ interface UrlOptions<TSchemas extends CallApiSchemas> {
|
|
|
133
133
|
query?: InferSchemaResult<TSchemas["query"], Query>;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) =>
|
|
136
|
+
type RetryCondition<TErrorData> = (context: ErrorContext<TErrorData>) => Awaitable<boolean>;
|
|
137
137
|
interface RetryOptions<TErrorData> {
|
|
138
138
|
/**
|
|
139
139
|
* Keeps track of the number of times the request has already been retried
|
|
@@ -244,8 +244,25 @@ declare const definePlugin: <TPlugin extends CallApiPlugin | AnyFunction<CallApi
|
|
|
244
244
|
type Plugins<TPluginArray extends CallApiPlugin[]> = TPluginArray;
|
|
245
245
|
|
|
246
246
|
declare const fetchSpecificKeys: ("body" | "cache" | "credentials" | "headers" | "integrity" | "keepalive" | "method" | "mode" | "priority" | "redirect" | "referrer" | "referrerPolicy" | "signal" | "window")[];
|
|
247
|
-
declare const
|
|
248
|
-
|
|
247
|
+
declare const getDefaultOptions: () => {
|
|
248
|
+
baseURL: string;
|
|
249
|
+
bodySerializer: {
|
|
250
|
+
(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string;
|
|
251
|
+
(value: any, replacer?: (number | string)[] | null, space?: string | number): string;
|
|
252
|
+
};
|
|
253
|
+
dedupeStrategy: "cancel";
|
|
254
|
+
defaultErrorMessage: string;
|
|
255
|
+
mergedHooksExecutionMode: "parallel";
|
|
256
|
+
mergedHooksExecutionOrder: "mainHooksAfterPlugins";
|
|
257
|
+
responseType: "json";
|
|
258
|
+
resultMode: "all";
|
|
259
|
+
retryAttempts: number;
|
|
260
|
+
retryDelay: number;
|
|
261
|
+
retryMaxDelay: number;
|
|
262
|
+
retryMethods: ("GET" | "POST")[];
|
|
263
|
+
retryStatusCodes: (AnyNumber | 409 | 425 | 429 | 500 | 502 | 503 | 504)[];
|
|
264
|
+
retryStrategy: "linear";
|
|
265
|
+
};
|
|
249
266
|
|
|
250
267
|
/**
|
|
251
268
|
* @description Makes a type required if TSchema type is undefined or if the output type of TSchema contains undefined, otherwise keeps it as is
|
|
@@ -576,4 +593,4 @@ declare class HTTPError<TErrorResponse = Record<string, unknown>> extends Error
|
|
|
576
593
|
constructor(errorDetails: ErrorDetails<TErrorResponse>, errorOptions?: ErrorOptions);
|
|
577
594
|
}
|
|
578
595
|
|
|
579
|
-
export {
|
|
596
|
+
export { type ResponseContext as A, type BaseCallApiConfig as B, type CallApiPlugin as C, type DefaultPluginArray as D, type ErrorContext as E, type ResponseErrorContext as F, HTTPError as H, type InferSchemaResult as I, type PluginInitContext as P, type ResultModeUnion as R, type SuccessContext 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 BaseCallApiExtraOptions as l, type CallApiExtraOptions as m, type PossibleJavaScriptError as n, type PossibleHTTPError as o, type CallApiRequestOptions as p, type CallApiRequestOptionsForHooks as q, type CallApiResultErrorVariant as r, type CallApiResultSuccessVariant as s, type CombinedCallApiExtraOptions as t, type Interceptors as u, type InterceptorsOrInterceptorArray as v, type PossibleJavascriptErrorNames as w, type Register as x, type RequestContext as y, type RequestErrorContext as z };
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { R as ResultModeUnion, a as ResponseTypeUnion, C as CallApiPlugin, D as DefaultPluginArray, b as CallApiSchemas, I as InferSchemaResult, c as CallApiConfig, d as CallApiResult, e as DefaultDataType, f as DefaultThrowOnError, g as DefaultMoreOptions, B as BaseCallApiConfig, h as CallApiParameters } from './error-
|
|
2
|
-
export {
|
|
1
|
+
import { R as ResultModeUnion, a as ResponseTypeUnion, C as CallApiPlugin, D as DefaultPluginArray, b as CallApiSchemas, I as InferSchemaResult, c as CallApiConfig, d as CallApiResult, e as DefaultDataType, f as DefaultThrowOnError, g as DefaultMoreOptions, B as BaseCallApiConfig, h as CallApiParameters } from './error-DIHsfUiJ.js';
|
|
2
|
+
export { l as BaseCallApiExtraOptions, m as CallApiExtraOptions, p as CallApiRequestOptions, q as CallApiRequestOptionsForHooks, r as CallApiResultErrorVariant, s as CallApiResultSuccessVariant, t as CombinedCallApiExtraOptions, E as ErrorContext, H as HTTPError, u as Interceptors, v as InterceptorsOrInterceptorArray, P as PluginInitContext, o as PossibleHTTPError, n as PossibleJavaScriptError, w as PossibleJavascriptErrorNames, x as Register, y as RequestContext, z as RequestErrorContext, A as ResponseContext, F as ResponseErrorContext, k as RetryOptions, S as SuccessContext, i as definePlugin, j as getDefaultOptions } from './error-DIHsfUiJ.js';
|
|
3
3
|
import '@standard-schema/spec';
|
|
4
4
|
|
|
5
5
|
declare const createFetchClient: <TBaseData = DefaultDataType, TBaseErrorData = DefaultDataType, TBaseResultMode extends ResultModeUnion = ResultModeUnion, TBaseThrowOnError extends boolean = DefaultThrowOnError, TBaseResponseType extends ResponseTypeUnion = ResponseTypeUnion, TBasePluginArray extends CallApiPlugin[] = DefaultPluginArray, TBaseSchemas extends CallApiSchemas = DefaultMoreOptions>(baseConfig?: BaseCallApiConfig<TBaseData, TBaseErrorData, TBaseResultMode, TBaseThrowOnError, TBaseResponseType, TBasePluginArray, TBaseSchemas>) => <TData = InferSchemaResult<TBaseSchemas["data"], TBaseData>, TErrorData = InferSchemaResult<TBaseSchemas["errorData"], TBaseErrorData>, TResultMode extends ResultModeUnion = TBaseResultMode, TThrowOnError extends boolean = TBaseThrowOnError, TResponseType extends ResponseTypeUnion = TBaseResponseType, TPluginArray extends CallApiPlugin[] = TBasePluginArray, TSchemas extends CallApiSchemas = TBaseSchemas>(initURL: InferSchemaResult<TSchemas["initURL"], string | URL>, config?: CallApiConfig<TData, TErrorData, TResultMode, TThrowOnError, TResponseType, TPluginArray, TSchemas> | undefined) => CallApiResult<InferSchemaResult<TSchemas["data"], TData>, InferSchemaResult<TSchemas["errorData"], TErrorData>, TResultMode, TThrowOnError, TResponseType>;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { splitConfig, isFunction, splitBaseConfig, combineHooks, mergeAndResolveHeaders, isPlainObject, executeHooks, HTTPError, resolveErrorResult,
|
|
2
|
-
export { HTTPError } from './chunk-
|
|
1
|
+
import { splitConfig, isFunction, splitBaseConfig, combineHooks, defaultExtraOptions, mergeAndResolveHeaders, isPlainObject, executeHooks, HTTPError, resolveErrorResult, isHTTPErrorInstance, waitUntil, omitKeys, isString, isArray, toQueryString, getFetchImpl } from './chunk-V7AOCZ3M.js';
|
|
2
|
+
export { HTTPError, getDefaultOptions } from './chunk-V7AOCZ3M.js';
|
|
3
3
|
|
|
4
4
|
// src/dedupe.ts
|
|
5
5
|
var createDedupeStrategy = async (context) => {
|
|
@@ -189,28 +189,28 @@ var getExponentialDelay = (currentAttemptCount, options) => {
|
|
|
189
189
|
const exponentialDelay = (options.retryDelay ?? 1e3) * 2 ** currentAttemptCount;
|
|
190
190
|
return Math.min(exponentialDelay, maxDelay);
|
|
191
191
|
};
|
|
192
|
-
var createRetryStrategy = (
|
|
193
|
-
const currentRetryCount = options["~retryCount"] ?? 0;
|
|
192
|
+
var createRetryStrategy = (ctx) => {
|
|
193
|
+
const currentRetryCount = ctx.options["~retryCount"] ?? 0;
|
|
194
194
|
const getDelay = () => {
|
|
195
|
-
if (options.retryStrategy === "exponential") {
|
|
196
|
-
return getExponentialDelay(currentRetryCount, options);
|
|
195
|
+
if (ctx.options.retryStrategy === "exponential") {
|
|
196
|
+
return getExponentialDelay(currentRetryCount, ctx.options);
|
|
197
197
|
}
|
|
198
|
-
return getLinearDelay(options);
|
|
198
|
+
return getLinearDelay(ctx.options);
|
|
199
199
|
};
|
|
200
200
|
const shouldAttemptRetry = async () => {
|
|
201
|
-
const customRetryCondition = await options.retryCondition?.(ctx) ?? true;
|
|
202
|
-
const maxRetryAttempts = options.retryAttempts ?? 0;
|
|
201
|
+
const customRetryCondition = await ctx.options.retryCondition?.(ctx) ?? true;
|
|
202
|
+
const maxRetryAttempts = ctx.options.retryAttempts ?? 0;
|
|
203
203
|
const baseRetryCondition = maxRetryAttempts > currentRetryCount && customRetryCondition;
|
|
204
204
|
if (ctx.error.name !== "HTTPError") {
|
|
205
205
|
return baseRetryCondition;
|
|
206
206
|
}
|
|
207
207
|
const includesMethod = (
|
|
208
208
|
// eslint-disable-next-line no-implicit-coercion -- Boolean doesn't narrow
|
|
209
|
-
!!ctx.request.method && options.retryMethods?.includes(ctx.request.method)
|
|
209
|
+
!!ctx.request.method && ctx.options.retryMethods?.includes(ctx.request.method)
|
|
210
210
|
);
|
|
211
211
|
const includesCodes = (
|
|
212
212
|
// eslint-disable-next-line no-implicit-coercion -- Boolean doesn't narrow
|
|
213
|
-
!!ctx.response?.status && options.retryStatusCodes?.includes(ctx.response.status)
|
|
213
|
+
!!ctx.response?.status && ctx.options.retryStatusCodes?.includes(ctx.response.status)
|
|
214
214
|
);
|
|
215
215
|
return includesCodes && includesMethod && baseRetryCondition;
|
|
216
216
|
};
|
|
@@ -294,36 +294,24 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
294
294
|
const callApi2 = async (...parameters) => {
|
|
295
295
|
const [initURL, config = {}] = parameters;
|
|
296
296
|
const [fetchOptions, extraOptions] = splitConfig(config);
|
|
297
|
-
const resolvedBaseConfig = isFunction(baseConfig) ? baseConfig({
|
|
297
|
+
const resolvedBaseConfig = isFunction(baseConfig) ? baseConfig({
|
|
298
|
+
initURL: initURL.toString(),
|
|
299
|
+
options: extraOptions,
|
|
300
|
+
request: fetchOptions
|
|
301
|
+
}) : baseConfig;
|
|
298
302
|
const [baseFetchOptions, baseExtraOptions] = splitBaseConfig(resolvedBaseConfig);
|
|
299
|
-
const initCombinedHooks = {};
|
|
300
303
|
for (const key of Object.keys(hooksEnum)) {
|
|
301
|
-
|
|
304
|
+
combineHooks(
|
|
302
305
|
baseExtraOptions[key],
|
|
303
306
|
extraOptions[key]
|
|
304
307
|
);
|
|
305
|
-
initCombinedHooks[key] = combinedHook;
|
|
306
308
|
}
|
|
307
|
-
const
|
|
308
|
-
|
|
309
|
-
bodySerializer: JSON.stringify,
|
|
310
|
-
dedupeStrategy: "cancel",
|
|
311
|
-
defaultErrorMessage: "Failed to fetch data from server!",
|
|
312
|
-
mergedHooksExecutionMode: "parallel",
|
|
313
|
-
mergedHooksExecutionOrder: "mainHooksAfterPlugins",
|
|
314
|
-
responseType: "json",
|
|
315
|
-
resultMode: "all",
|
|
316
|
-
retryAttempts: 0,
|
|
317
|
-
retryDelay: 1e3,
|
|
318
|
-
retryMaxDelay: 1e4,
|
|
319
|
-
retryMethods: defaultRetryMethods,
|
|
320
|
-
retryStatusCodes: defaultRetryStatusCodes,
|
|
321
|
-
retryStrategy: "linear",
|
|
309
|
+
const mergedExtraOptions = {
|
|
310
|
+
...defaultExtraOptions,
|
|
322
311
|
...baseExtraOptions,
|
|
323
|
-
...extraOptions
|
|
324
|
-
...initCombinedHooks
|
|
312
|
+
...extraOptions
|
|
325
313
|
};
|
|
326
|
-
const
|
|
314
|
+
const mergedRequestOptions = {
|
|
327
315
|
...baseFetchOptions,
|
|
328
316
|
...fetchOptions
|
|
329
317
|
};
|
|
@@ -331,8 +319,8 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
331
319
|
baseConfig: resolvedBaseConfig,
|
|
332
320
|
config,
|
|
333
321
|
initURL,
|
|
334
|
-
options:
|
|
335
|
-
request:
|
|
322
|
+
options: mergedExtraOptions,
|
|
323
|
+
request: mergedRequestOptions
|
|
336
324
|
});
|
|
337
325
|
const fullURL = `${resolvedOptions.baseURL}${mergeUrlWithParamsAndQuery(url, resolvedOptions.params, resolvedOptions.query)}`;
|
|
338
326
|
const options = {
|
|
@@ -393,11 +381,7 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
393
381
|
options.responseType,
|
|
394
382
|
options.responseParser
|
|
395
383
|
);
|
|
396
|
-
const validSuccessData = await handleValidation(
|
|
397
|
-
successData,
|
|
398
|
-
schemas?.data,
|
|
399
|
-
validators?.data
|
|
400
|
-
);
|
|
384
|
+
const validSuccessData = await handleValidation(successData, schemas?.data, validators?.data);
|
|
401
385
|
const successContext = {
|
|
402
386
|
data: validSuccessData,
|
|
403
387
|
options,
|
|
@@ -426,9 +410,11 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
426
410
|
request,
|
|
427
411
|
response: apiDetails.response
|
|
428
412
|
};
|
|
429
|
-
const
|
|
430
|
-
const
|
|
431
|
-
|
|
413
|
+
const shouldThrowOnError = isFunction(options.throwOnError) ? options.throwOnError(errorContext) : options.throwOnError;
|
|
414
|
+
const handleRetry = async () => {
|
|
415
|
+
const { getDelay, shouldAttemptRetry } = createRetryStrategy(errorContext);
|
|
416
|
+
const shouldRetry = !combinedSignal.aborted && await shouldAttemptRetry();
|
|
417
|
+
if (!shouldRetry) return;
|
|
432
418
|
await executeHooks(options.onRetry(errorContext));
|
|
433
419
|
const delay = getDelay();
|
|
434
420
|
await waitUntil(delay);
|
|
@@ -436,9 +422,8 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
436
422
|
...config,
|
|
437
423
|
"~retryCount": (options["~retryCount"] ?? 0) + 1
|
|
438
424
|
};
|
|
439
|
-
return
|
|
440
|
-
}
|
|
441
|
-
const shouldThrowOnError = isFunction(options.throwOnError) ? options.throwOnError(errorContext) : options.throwOnError;
|
|
425
|
+
return callApi2(initURL, updatedOptions);
|
|
426
|
+
};
|
|
442
427
|
const handleThrowOnError = () => {
|
|
443
428
|
if (!shouldThrowOnError) return;
|
|
444
429
|
throw apiDetails.error;
|
|
@@ -449,18 +434,21 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
449
434
|
options.onError(errorContext),
|
|
450
435
|
options.onResponse({ ...errorContext, data: null })
|
|
451
436
|
);
|
|
437
|
+
await handleRetry();
|
|
452
438
|
handleThrowOnError();
|
|
453
439
|
return getErrorResult();
|
|
454
440
|
}
|
|
455
441
|
if (error instanceof DOMException && error.name === "AbortError") {
|
|
456
442
|
const { message, name } = error;
|
|
457
443
|
console.error(`${name}:`, message);
|
|
444
|
+
await handleRetry();
|
|
458
445
|
handleThrowOnError();
|
|
459
446
|
return getErrorResult();
|
|
460
447
|
}
|
|
461
448
|
if (error instanceof DOMException && error.name === "TimeoutError") {
|
|
462
449
|
const message = `Request timed out after ${options.timeout}ms`;
|
|
463
450
|
console.error(`${error.name}:`, message);
|
|
451
|
+
await handleRetry();
|
|
464
452
|
handleThrowOnError();
|
|
465
453
|
return getErrorResult({ message });
|
|
466
454
|
}
|
|
@@ -470,6 +458,7 @@ var createFetchClient = (baseConfig = {}) => {
|
|
|
470
458
|
// == Also call the onError interceptor
|
|
471
459
|
options.onError(errorContext)
|
|
472
460
|
);
|
|
461
|
+
await handleRetry();
|
|
473
462
|
handleThrowOnError();
|
|
474
463
|
return getErrorResult();
|
|
475
464
|
} finally {
|