@zayne-labs/callapi 1.11.5 → 1.11.6

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.
@@ -1,12 +1,63 @@
1
- import { CallApiExtraOptions, CallApiResultErrorVariant, HTTPError, PossibleHTTPError, PossibleJavaScriptError, PossibleValidationError, ValidationError } from "../common-NVdD93VW.js";
2
-
3
- //#region src/utils/common.d.ts
1
+ import { A as CallApiResultErrorVariant, F as PossibleHTTPError, I as PossibleJavaScriptError, R as PossibleValidationError, X as HTTPError, Z as ValidationError, i as CallApiExtraOptions } from "../common-ClytspsT.js";
4
2
 
3
+ //#region src/utils/helpers.d.ts
5
4
  type ToQueryStringFn = {
6
- (params: CallApiExtraOptions["query"]): string | null;
7
- (params: Required<CallApiExtraOptions>["query"]): string;
5
+ (query: CallApiExtraOptions["query"]): string | null;
6
+ (query: Required<CallApiExtraOptions>["query"]): string;
8
7
  };
9
8
  declare const toQueryString: ToQueryStringFn;
9
+ type AllowedPrimitives = boolean | number | string | Blob;
10
+ type AllowedValues = AllowedPrimitives | AllowedPrimitives[] | Record<string, AllowedPrimitives>;
11
+ type ToFormDataFn = {
12
+ (data: Record<string, AllowedValues>): FormData;
13
+ <TData extends Record<string, AllowedValues>>(data: TData, options: {
14
+ returnType: "inputType";
15
+ }): TData;
16
+ };
17
+ /**
18
+ * @description Converts a plain object to FormData.
19
+ *
20
+ * Handles various data types:
21
+ * - **Primitives** (string, number, boolean): Converted to strings
22
+ * - **Blobs/Files**: Added directly to FormData
23
+ * - **Arrays**: Each item is appended (allows multiple values for same key)
24
+ * - **Objects**: JSON stringified before adding to FormData
25
+ *
26
+ * @example
27
+ * ```ts
28
+ * // Basic usage
29
+ * const formData = toFormData({
30
+ * name: "John",
31
+ * age: 30,
32
+ * active: true
33
+ * });
34
+ *
35
+ * // With arrays
36
+ * const formData = toFormData({
37
+ * tags: ["javascript", "typescript"],
38
+ * name: "John"
39
+ * });
40
+ *
41
+ * // With files
42
+ * const formData = toFormData({
43
+ * avatar: fileBlob,
44
+ * name: "John"
45
+ * });
46
+ *
47
+ * // With nested objects (one level only)
48
+ * const formData = toFormData({
49
+ * user: { name: "John", age: 30 },
50
+ * settings: { theme: "dark" }
51
+ * });
52
+ *
53
+ * // Type-preserving usage with Zod
54
+ * const schema = z.object({ name: z.string(), file: z.instanceof(Blob) });
55
+ * const data = schema.parse({ name: "John", file: blob });
56
+ * const typedFormData = toFormData(data, { returnType: "inputType" });
57
+ * // Type is { name: string; file: Blob }, runtime is FormData
58
+ * ```
59
+ */
60
+ declare const toFormData: ToFormDataFn;
10
61
  //#endregion
11
62
  //#region src/utils/guards.d.ts
12
63
  declare const isHTTPError: <TErrorData>(error: CallApiResultErrorVariant<TErrorData>["error"] | null) => error is PossibleHTTPError<TErrorData>;
@@ -15,5 +66,5 @@ declare const isValidationError: (error: CallApiResultErrorVariant<unknown>["err
15
66
  declare const isValidationErrorInstance: (error: unknown) => error is ValidationError;
16
67
  declare const isJavascriptError: (error: CallApiResultErrorVariant<unknown>["error"] | null) => error is PossibleJavaScriptError;
17
68
  //#endregion
18
- export { isHTTPError, isHTTPErrorInstance, isJavascriptError, isValidationError, isValidationErrorInstance, toQueryString };
69
+ export { isHTTPError, isHTTPErrorInstance, isJavascriptError, isValidationError, isValidationErrorInstance, toFormData, toQueryString };
19
70
  //# sourceMappingURL=index.d.ts.map
@@ -1,3 +1,3 @@
1
- import { isHTTPError, isHTTPErrorInstance, isJavascriptError, isValidationError, isValidationErrorInstance, toQueryString } from "../common-CfCB_X1k.js";
1
+ import { _ as isValidationError, c as isJavascriptError, n as toQueryString, o as isHTTPError, s as isHTTPErrorInstance, t as toFormData, v as isValidationErrorInstance } from "../utils-DOVvfarH.js";
2
2
 
3
- export { isHTTPError, isHTTPErrorInstance, isJavascriptError, isValidationError, isValidationErrorInstance, toQueryString };
3
+ export { isHTTPError, isHTTPErrorInstance, isJavascriptError, isValidationError, isValidationErrorInstance, toFormData, toQueryString };
@@ -0,0 +1,225 @@
1
+ //#region src/types/type-helpers.ts
2
+ const defineEnum = (value) => Object.freeze(value);
3
+
4
+ //#endregion
5
+ //#region src/constants/defaults.ts
6
+ const extraOptionDefaults = Object.freeze(defineEnum({
7
+ bodySerializer: JSON.stringify,
8
+ defaultHTTPErrorMessage: "HTTP request failed unexpectedly",
9
+ dedupeCacheScope: "local",
10
+ dedupeCacheScopeKey: "default",
11
+ dedupeStrategy: "cancel",
12
+ hooksExecutionMode: "parallel",
13
+ responseParser: JSON.parse,
14
+ responseType: "json",
15
+ resultMode: "all",
16
+ retryAttempts: 0,
17
+ retryCondition: () => true,
18
+ retryDelay: 1e3,
19
+ retryMaxDelay: 1e4,
20
+ retryMethods: ["GET", "POST"],
21
+ retryStatusCodes: [],
22
+ retryStrategy: "linear"
23
+ }));
24
+ const requestOptionDefaults = defineEnum({ method: "GET" });
25
+
26
+ //#endregion
27
+ //#region src/error.ts
28
+ const httpErrorSymbol = Symbol("HTTPError");
29
+ var HTTPError = class HTTPError extends Error {
30
+ errorData;
31
+ httpErrorSymbol = httpErrorSymbol;
32
+ name = "HTTPError";
33
+ response;
34
+ constructor(errorDetails, errorOptions) {
35
+ const { defaultHTTPErrorMessage, errorData, response } = errorDetails;
36
+ const selectedDefaultErrorMessage = (isString(defaultHTTPErrorMessage) ? defaultHTTPErrorMessage : defaultHTTPErrorMessage?.({
37
+ errorData,
38
+ response
39
+ })) ?? (response.statusText || extraOptionDefaults.defaultHTTPErrorMessage);
40
+ const message = errorData?.message ?? selectedDefaultErrorMessage;
41
+ super(message, errorOptions);
42
+ this.errorData = errorData;
43
+ this.response = response;
44
+ }
45
+ /**
46
+ * @description Checks if the given error is an instance of HTTPError
47
+ * @param error - The error to check
48
+ * @returns true if the error is an instance of HTTPError, false otherwise
49
+ */
50
+ static isError(error) {
51
+ if (!isObject(error)) return false;
52
+ if (error instanceof HTTPError) return true;
53
+ const actualError = error;
54
+ return actualError.httpErrorSymbol === httpErrorSymbol && actualError.name === "HTTPError";
55
+ }
56
+ };
57
+ const prettifyPath = (path) => {
58
+ if (!path || path.length === 0) return "";
59
+ return ` → at ${path.map((segment) => isObject(segment) ? segment.key : segment).join(".")}`;
60
+ };
61
+ const prettifyValidationIssues = (issues) => {
62
+ return issues.map((issue) => `✖ ${issue.message}${prettifyPath(issue.path)}`).join(" | ");
63
+ };
64
+ const validationErrorSymbol = Symbol("ValidationErrorSymbol");
65
+ var ValidationError = class ValidationError extends Error {
66
+ errorData;
67
+ issueCause;
68
+ name = "ValidationError";
69
+ response;
70
+ validationErrorSymbol = validationErrorSymbol;
71
+ constructor(details, errorOptions) {
72
+ const { issueCause, issues, response } = details;
73
+ const message = prettifyValidationIssues(issues);
74
+ super(message, errorOptions);
75
+ this.errorData = issues;
76
+ this.response = response;
77
+ this.issueCause = issueCause;
78
+ }
79
+ /**
80
+ * @description Checks if the given error is an instance of ValidationError
81
+ * @param error - The error to check
82
+ * @returns true if the error is an instance of ValidationError, false otherwise
83
+ */
84
+ static isError(error) {
85
+ if (!isObject(error)) return false;
86
+ if (error instanceof ValidationError) return true;
87
+ const actualError = error;
88
+ return actualError.validationErrorSymbol === validationErrorSymbol && actualError.name === "ValidationError";
89
+ }
90
+ };
91
+
92
+ //#endregion
93
+ //#region src/utils/guards.ts
94
+ const isHTTPError = (error) => {
95
+ return isObject(error) && error.name === "HTTPError";
96
+ };
97
+ const isHTTPErrorInstance = (error) => {
98
+ return HTTPError.isError(error);
99
+ };
100
+ const isValidationError = (error) => {
101
+ return isObject(error) && error.name === "ValidationError";
102
+ };
103
+ const isValidationErrorInstance = (error) => {
104
+ return ValidationError.isError(error);
105
+ };
106
+ const isJavascriptError = (error) => {
107
+ return isObject(error) && !isHTTPError(error) && !isValidationError(error);
108
+ };
109
+ const isArray = (value) => Array.isArray(value);
110
+ const isBoolean = (value) => typeof value === "boolean";
111
+ const isBlob = (value) => value instanceof Blob;
112
+ const isObject = (value) => {
113
+ return typeof value === "object" && value !== null;
114
+ };
115
+ const hasObjectPrototype = (value) => {
116
+ return Object.prototype.toString.call(value) === "[object Object]";
117
+ };
118
+ /**
119
+ * @description Copied from TanStack Query's isPlainObject
120
+ * @see https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts#L321
121
+ */
122
+ const isPlainObject = (value) => {
123
+ if (!hasObjectPrototype(value)) return false;
124
+ const constructor = value?.constructor;
125
+ if (constructor === void 0) return true;
126
+ const prototype = constructor.prototype;
127
+ if (!hasObjectPrototype(prototype)) return false;
128
+ if (!Object.hasOwn(prototype, "isPrototypeOf")) return false;
129
+ if (Object.getPrototypeOf(value) !== Object.prototype) return false;
130
+ return true;
131
+ };
132
+ const isValidJsonString = (value) => {
133
+ if (!isString(value)) return false;
134
+ try {
135
+ JSON.parse(value);
136
+ return true;
137
+ } catch {
138
+ return false;
139
+ }
140
+ };
141
+ const isSerializable = (value) => {
142
+ return isPlainObject(value) || isArray(value) || typeof value?.toJSON === "function";
143
+ };
144
+ const isFunction = (value) => typeof value === "function";
145
+ const isQueryString = (value) => isString(value) && value.includes("=");
146
+ const isString = (value) => typeof value === "string";
147
+ const isPromise = (value) => value instanceof Promise;
148
+ const isReadableStream = (value) => {
149
+ return value instanceof ReadableStream;
150
+ };
151
+
152
+ //#endregion
153
+ //#region src/utils/helpers.ts
154
+ const toQueryString = (query) => {
155
+ if (!query) {
156
+ console.error("toQueryString:", "No query params provided!");
157
+ return null;
158
+ }
159
+ return new URLSearchParams(query).toString();
160
+ };
161
+ const toBlobOrString = (value) => {
162
+ return isBlob(value) ? value : String(value);
163
+ };
164
+ /**
165
+ * @description Converts a plain object to FormData.
166
+ *
167
+ * Handles various data types:
168
+ * - **Primitives** (string, number, boolean): Converted to strings
169
+ * - **Blobs/Files**: Added directly to FormData
170
+ * - **Arrays**: Each item is appended (allows multiple values for same key)
171
+ * - **Objects**: JSON stringified before adding to FormData
172
+ *
173
+ * @example
174
+ * ```ts
175
+ * // Basic usage
176
+ * const formData = toFormData({
177
+ * name: "John",
178
+ * age: 30,
179
+ * active: true
180
+ * });
181
+ *
182
+ * // With arrays
183
+ * const formData = toFormData({
184
+ * tags: ["javascript", "typescript"],
185
+ * name: "John"
186
+ * });
187
+ *
188
+ * // With files
189
+ * const formData = toFormData({
190
+ * avatar: fileBlob,
191
+ * name: "John"
192
+ * });
193
+ *
194
+ * // With nested objects (one level only)
195
+ * const formData = toFormData({
196
+ * user: { name: "John", age: 30 },
197
+ * settings: { theme: "dark" }
198
+ * });
199
+ *
200
+ * // Type-preserving usage with Zod
201
+ * const schema = z.object({ name: z.string(), file: z.instanceof(Blob) });
202
+ * const data = schema.parse({ name: "John", file: blob });
203
+ * const typedFormData = toFormData(data, { returnType: "inputType" });
204
+ * // Type is { name: string; file: Blob }, runtime is FormData
205
+ * ```
206
+ */
207
+ const toFormData = (data) => {
208
+ const formData = new FormData();
209
+ for (const [key, value] of Object.entries(data)) {
210
+ if (isArray(value)) {
211
+ value.forEach((innerValue) => formData.append(key, toBlobOrString(innerValue)));
212
+ continue;
213
+ }
214
+ if (isObject(value) && !isBlob(value)) {
215
+ formData.set(key, JSON.stringify(value));
216
+ continue;
217
+ }
218
+ formData.set(key, toBlobOrString(value));
219
+ }
220
+ return formData;
221
+ };
222
+
223
+ //#endregion
224
+ export { defineEnum as C, requestOptionDefaults as S, isValidationError as _, isFunction as a, ValidationError as b, isJavascriptError as c, isPromise as d, isQueryString as f, isValidJsonString as g, isString as h, isBoolean as i, isObject as l, isSerializable as m, toQueryString as n, isHTTPError as o, isReadableStream as p, isArray as r, isHTTPErrorInstance as s, toFormData as t, isPlainObject as u, isValidationErrorInstance as v, extraOptionDefaults as x, HTTPError as y };
225
+ //# sourceMappingURL=utils-DOVvfarH.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils-DOVvfarH.js","names":["toQueryString: ToQueryStringFn","toFormData: ToFormDataFn"],"sources":["../../src/types/type-helpers.ts","../../src/constants/defaults.ts","../../src/error.ts","../../src/utils/guards.ts","../../src/utils/helpers.ts"],"sourcesContent":["// == 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 & NonNullable<unknown>;\nexport type AnyNumber = number & NonNullable<unknown>;\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 Prettify<TObject> = NonNullable<unknown> & { [Key in keyof TObject]: TObject[Key] };\n\nexport type WriteableLevel = \"deep\" | \"shallow\";\n\n/**\n * Makes all properties in an object type writeable (removes readonly modifiers).\n * Supports both shallow and deep modes, and handles special cases like arrays, tuples, and unions.\n * @template TObject - The object type to make writeable\n * @template TVariant - The level of writeable transformation (\"shallow\" | \"deep\")\n */\n\ntype ArrayOrObject = Record<number | string | symbol, unknown> | unknown[];\n\nexport type Writeable<TObject, TLevel extends WriteableLevel = \"shallow\"> =\n\tTObject extends readonly [...infer TTupleItems] ?\n\t\t[\n\t\t\t...{\n\t\t\t\t[Index in keyof TTupleItems]: TLevel extends \"deep\" ? Writeable<TTupleItems[Index], \"deep\">\n\t\t\t\t:\tTTupleItems[Index];\n\t\t\t},\n\t\t]\n\t: TObject extends ArrayOrObject ?\n\t\t{\n\t\t\t-readonly [Key in keyof TObject]: TLevel extends \"deep\" ? Writeable<TObject[Key], \"deep\">\n\t\t\t:\tTObject[Key];\n\t\t}\n\t:\tTObject;\n\nexport const defineEnum = <const TValue extends object>(value: TValue) =>\n\tObject.freeze(value) as Readonly<Writeable<TValue>>;\n\nexport type UnionToIntersection<TUnion> =\n\t(TUnion extends unknown ? (param: TUnion) => void : never) extends (param: infer TParam) => void ?\n\t\tTParam\n\t:\tnever;\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 RemovePrefix<TPrefix extends \"dedupe\" | \"retry\", TKey extends string> =\n\tTKey extends `${TPrefix}${infer TRest}` ? Uncapitalize<TRest> : TKey;\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 { CallApiConfig, CallApiExtraOptions } from \"../types/common\";\nimport { defineEnum } from \"../types/type-helpers\";\n\nexport const extraOptionDefaults = Object.freeze(\n\tdefineEnum({\n\t\t// Common defaults\n\t\tbodySerializer: JSON.stringify,\n\t\tdefaultHTTPErrorMessage: \"HTTP request failed unexpectedly\",\n\n\t\t// Dedupe defaults\n\t\t/* eslint-disable perfectionist/sort-objects -- Allow */\n\t\tdedupeCacheScope: \"local\",\n\t\tdedupeCacheScopeKey: \"default\",\n\t\tdedupeStrategy: \"cancel\",\n\t\t/* eslint-enable perfectionist/sort-objects -- Allow */\n\n\t\t// Hook defaults\n\t\thooksExecutionMode: \"parallel\",\n\n\t\t// Response defaults\n\t\tresponseParser: JSON.parse,\n\t\tresponseType: \"json\",\n\t\tresultMode: \"all\",\n\n\t\t// Retry Defaults\n\t\tretryAttempts: 0,\n\t\tretryCondition: () => true,\n\t\tretryDelay: 1000,\n\t\tretryMaxDelay: 10000,\n\t\tretryMethods: [\"GET\", \"POST\"],\n\t\tretryStatusCodes: [],\n\t\tretryStrategy: \"linear\",\n\t} satisfies CallApiExtraOptions)\n);\n\nexport const requestOptionDefaults = defineEnum({\n\tmethod: \"GET\",\n} satisfies CallApiConfig);\n","import { extraOptionDefaults } from \"./constants/defaults\";\nimport type { CallApiExtraOptions } from \"./types\";\nimport type { StandardSchemaV1 } from \"./types/standard-schema\";\nimport { isObject, isString } from \"./utils/guards\";\nimport type { CallApiSchema, CallApiSchemaConfig } from \"./validation\";\n\ntype HTTPErrorDetails<TErrorData> = Pick<CallApiExtraOptions, \"defaultHTTPErrorMessage\"> & {\n\terrorData: TErrorData;\n\tresponse: Response;\n};\n\nconst httpErrorSymbol = Symbol(\"HTTPError\");\n\nexport class HTTPError<TErrorData = Record<string, unknown>> extends Error {\n\terrorData: HTTPErrorDetails<TErrorData>[\"errorData\"];\n\n\treadonly httpErrorSymbol = httpErrorSymbol;\n\n\toverride name = \"HTTPError\" as const;\n\n\tresponse: HTTPErrorDetails<TErrorData>[\"response\"];\n\n\tconstructor(errorDetails: HTTPErrorDetails<TErrorData>, errorOptions?: ErrorOptions) {\n\t\tconst { defaultHTTPErrorMessage, errorData, response } = errorDetails;\n\n\t\tconst resolvedDefaultHTTPErrorMessage =\n\t\t\tisString(defaultHTTPErrorMessage) ? defaultHTTPErrorMessage : (\n\t\t\t\tdefaultHTTPErrorMessage?.({ errorData, response })\n\t\t\t);\n\n\t\tconst selectedDefaultErrorMessage =\n\t\t\tresolvedDefaultHTTPErrorMessage\n\t\t\t?? (response.statusText || extraOptionDefaults.defaultHTTPErrorMessage);\n\n\t\tconst message =\n\t\t\t(errorData as { message?: string } | undefined)?.message ?? selectedDefaultErrorMessage;\n\n\t\tsuper(message, errorOptions);\n\n\t\tthis.errorData = errorData;\n\t\tthis.response = response;\n\t}\n\n\t/**\n\t * @description Checks if the given error is an instance of HTTPError\n\t * @param error - The error to check\n\t * @returns true if the error is an instance of HTTPError, false otherwise\n\t */\n\tstatic override isError<TErrorData>(error: unknown): error is HTTPError<TErrorData> {\n\t\tif (!isObject<HTTPError>(error)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (error instanceof HTTPError) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst actualError = error as HTTPError;\n\n\t\treturn (\n\t\t\tactualError.httpErrorSymbol === httpErrorSymbol\n\t\t\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- Allow\n\t\t\t&& actualError.name === \"HTTPError\"\n\t\t);\n\t}\n}\n\nconst prettifyPath = (path: ValidationError[\"errorData\"][number][\"path\"]) => {\n\tif (!path || path.length === 0) {\n\t\treturn \"\";\n\t}\n\n\tconst pathString = path.map((segment) => (isObject(segment) ? segment.key : segment)).join(\".\");\n\n\treturn ` → at ${pathString}`;\n};\n\nconst prettifyValidationIssues = (issues: ValidationError[\"errorData\"]) => {\n\tconst issuesString = issues\n\t\t.map((issue) => `✖ ${issue.message}${prettifyPath(issue.path)}`)\n\t\t.join(\" | \");\n\n\treturn issuesString;\n};\n\ntype LockedExtract<TUnion, TKey extends TUnion> = Extract<TUnion, TKey>;\n\ntype ValidationErrorDetails = {\n\t/**\n\t * The cause of the validation error.\n\t *\n\t * It's either the name the schema for which validation failed, or the name of the schema config option that led to the validation error.\n\t */\n\tissueCause:\n\t\t| \"unknown\"\n\t\t| `schemaConfig-(${LockedExtract<keyof CallApiSchemaConfig, \"strict\">})`\n\t\t| keyof CallApiSchema;\n\n\t/**\n\t * The issues that caused the validation error.\n\t */\n\tissues: readonly StandardSchemaV1.Issue[];\n\n\t/**\n\t * The response from server, if any.\n\t */\n\tresponse: Response | null;\n};\n\nconst validationErrorSymbol = Symbol(\"ValidationErrorSymbol\");\n\nexport class ValidationError extends Error {\n\terrorData: ValidationErrorDetails[\"issues\"];\n\n\tissueCause: ValidationErrorDetails[\"issueCause\"];\n\n\toverride name = \"ValidationError\" as const;\n\n\tresponse: ValidationErrorDetails[\"response\"];\n\n\treadonly validationErrorSymbol = validationErrorSymbol;\n\n\tconstructor(details: ValidationErrorDetails, errorOptions?: ErrorOptions) {\n\t\tconst { issueCause, issues, response } = details;\n\n\t\tconst message = prettifyValidationIssues(issues);\n\n\t\tsuper(message, errorOptions);\n\n\t\tthis.errorData = issues;\n\t\tthis.response = response;\n\t\tthis.issueCause = issueCause;\n\t}\n\n\t/**\n\t * @description Checks if the given error is an instance of ValidationError\n\t * @param error - The error to check\n\t * @returns true if the error is an instance of ValidationError, false otherwise\n\t */\n\tstatic override isError(error: unknown): error is ValidationError {\n\t\tif (!isObject<ValidationError>(error)) {\n\t\t\treturn false;\n\t\t}\n\n\t\tif (error instanceof ValidationError) {\n\t\t\treturn true;\n\t\t}\n\n\t\tconst actualError = error as ValidationError;\n\n\t\treturn (\n\t\t\tactualError.validationErrorSymbol === validationErrorSymbol\n\t\t\t// eslint-disable-next-line ts-eslint/no-unnecessary-condition -- Allow\n\t\t\t&& actualError.name === \"ValidationError\"\n\t\t);\n\t}\n}\n","import { HTTPError, ValidationError } from \"../error\";\nimport type {\n\tCallApiResultErrorVariant,\n\tPossibleHTTPError,\n\tPossibleJavaScriptError,\n\tPossibleValidationError,\n} from \"../result\";\nimport type { AnyFunction } from \"../types/type-helpers\";\n\nexport const isHTTPError = <TErrorData>(\n\terror: CallApiResultErrorVariant<TErrorData>[\"error\"] | null\n): error is PossibleHTTPError<TErrorData> => {\n\treturn isObject(error) && error.name === \"HTTPError\";\n};\n\nexport const isHTTPErrorInstance = <TErrorData>(error: unknown) => {\n\treturn HTTPError.isError<TErrorData>(error);\n};\n\nexport const isValidationError = (\n\terror: CallApiResultErrorVariant<unknown>[\"error\"] | null\n): error is PossibleValidationError => {\n\treturn isObject(error) && error.name === \"ValidationError\";\n};\n\nexport const isValidationErrorInstance = (error: unknown): error is ValidationError => {\n\treturn ValidationError.isError(error);\n};\n\nexport const isJavascriptError = (\n\terror: CallApiResultErrorVariant<unknown>[\"error\"] | null\n): error is PossibleJavaScriptError => {\n\treturn isObject(error) && !isHTTPError(error) && !isValidationError(error);\n};\n\nexport const isArray = <TArrayItem>(value: unknown): value is TArrayItem[] => Array.isArray(value);\n\nexport const isBoolean = (value: unknown): value is boolean => typeof value === \"boolean\";\n\nexport const isBlob = (value: unknown): value is Blob => value instanceof Blob;\n\nexport const isObject = <TObject extends object>(value: unknown): value is TObject => {\n\treturn typeof value === \"object\" && value !== null;\n};\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 isValidJsonString = (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\nexport const isPromise = (value: unknown) => value instanceof Promise;\n\nexport const isReadableStream = (value: unknown): value is ReadableStream<unknown> => {\n\treturn value instanceof ReadableStream;\n};\n\n// https://github.com/unjs/ofetch/blob/main/src/utils.ts\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\tvalue?.constructor.name === \"Object\"\n\t\t|| typeof (value as { toJSON: () => unknown } | null)?.toJSON === \"function\"\n\t);\n};\n","import type { CallApiExtraOptions } from \"../types\";\nimport { isArray, isBlob, isObject } from \"./guards\";\n\ntype ToQueryStringFn = {\n\t(query: CallApiExtraOptions[\"query\"]): string | null;\n\t(query: Required<CallApiExtraOptions>[\"query\"]): string;\n};\n\nexport const toQueryString: ToQueryStringFn = (query) => {\n\tif (!query) {\n\t\tconsole.error(\"toQueryString:\", \"No query params provided!\");\n\n\t\treturn null as never;\n\t}\n\n\treturn new URLSearchParams(query as Record<string, string>).toString();\n};\n\ntype AllowedPrimitives = boolean | number | string | Blob;\n\ntype AllowedValues = AllowedPrimitives | AllowedPrimitives[] | Record<string, AllowedPrimitives>;\n\nconst toBlobOrString = (value: AllowedPrimitives): string | Blob => {\n\treturn isBlob(value) ? value : String(value);\n};\n\ntype ToFormDataFn = {\n\t(data: Record<string, AllowedValues>): FormData;\n\n\t<TData extends Record<string, AllowedValues>>(data: TData, options: { returnType: \"inputType\" }): TData;\n};\n\n/**\n * @description Converts a plain object to FormData.\n *\n * Handles various data types:\n * - **Primitives** (string, number, boolean): Converted to strings\n * - **Blobs/Files**: Added directly to FormData\n * - **Arrays**: Each item is appended (allows multiple values for same key)\n * - **Objects**: JSON stringified before adding to FormData\n *\n * @example\n * ```ts\n * // Basic usage\n * const formData = toFormData({\n * name: \"John\",\n * age: 30,\n * active: true\n * });\n *\n * // With arrays\n * const formData = toFormData({\n * tags: [\"javascript\", \"typescript\"],\n * name: \"John\"\n * });\n *\n * // With files\n * const formData = toFormData({\n * avatar: fileBlob,\n * name: \"John\"\n * });\n *\n * // With nested objects (one level only)\n * const formData = toFormData({\n * user: { name: \"John\", age: 30 },\n * settings: { theme: \"dark\" }\n * });\n *\n * // Type-preserving usage with Zod\n * const schema = z.object({ name: z.string(), file: z.instanceof(Blob) });\n * const data = schema.parse({ name: \"John\", file: blob });\n * const typedFormData = toFormData(data, { returnType: \"inputType\" });\n * // Type is { name: string; file: Blob }, runtime is FormData\n * ```\n */\nexport const toFormData: ToFormDataFn = (data: Record<string, AllowedValues>) => {\n\tconst formData = new FormData();\n\n\tfor (const [key, value] of Object.entries(data)) {\n\t\tif (isArray(value)) {\n\t\t\tvalue.forEach((innerValue) => formData.append(key, toBlobOrString(innerValue)));\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (isObject(value) && !isBlob(value)) {\n\t\t\tformData.set(key, JSON.stringify(value));\n\t\t\tcontinue;\n\t\t}\n\n\t\tformData.set(key, toBlobOrString(value));\n\t}\n\n\treturn formData;\n};\n"],"mappings":";AAuCA,MAAa,cAA2C,UACvD,OAAO,OAAO,MAAM;;;;ACrCrB,MAAa,sBAAsB,OAAO,OACzC,WAAW;CAEV,gBAAgB,KAAK;CACrB,yBAAyB;CAIzB,kBAAkB;CAClB,qBAAqB;CACrB,gBAAgB;CAIhB,oBAAoB;CAGpB,gBAAgB,KAAK;CACrB,cAAc;CACd,YAAY;CAGZ,eAAe;CACf,sBAAsB;CACtB,YAAY;CACZ,eAAe;CACf,cAAc,CAAC,OAAO,OAAO;CAC7B,kBAAkB,EAAE;CACpB,eAAe;CACf,CAA+B,CAChC;AAED,MAAa,wBAAwB,WAAW,EAC/C,QAAQ,OACR,CAAyB;;;;AC1B1B,MAAM,kBAAkB,OAAO,YAAY;AAE3C,IAAa,YAAb,MAAa,kBAAwD,MAAM;CAC1E;CAEA,AAAS,kBAAkB;CAE3B,AAAS,OAAO;CAEhB;CAEA,YAAY,cAA4C,cAA6B;EACpF,MAAM,EAAE,yBAAyB,WAAW,aAAa;EAOzD,MAAM,+BAJL,SAAS,wBAAwB,GAAG,0BACnC,0BAA0B;GAAE;GAAW;GAAU,CAAC,MAK/C,SAAS,cAAc,oBAAoB;EAEhD,MAAM,UACJ,WAA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BAAiB;;;;;AC7GzB,MAAaA,iBAAkC,UAAU;AACxD,KAAI,CAAC,OAAO;AACX,UAAQ,MAAM,kBAAkB,4BAA4B;AAE5D,SAAO;;AAGR,QAAO,IAAI,gBAAgB,MAAgC,CAAC,UAAU;;AAOvE,MAAM,kBAAkB,UAA4C;AACnE,QAAO,OAAO,MAAM,GAAG,QAAQ,OAAO,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoD7C,MAAaC,cAA4B,SAAwC;CAChF,MAAM,WAAW,IAAI,UAAU;AAE/B,MAAK,MAAM,CAAC,KAAK,UAAU,OAAO,QAAQ,KAAK,EAAE;AAChD,MAAI,QAAQ,MAAM,EAAE;AACnB,SAAM,SAAS,eAAe,SAAS,OAAO,KAAK,eAAe,WAAW,CAAC,CAAC;AAC/E;;AAGD,MAAI,SAAS,MAAM,IAAI,CAAC,OAAO,MAAM,EAAE;AACtC,YAAS,IAAI,KAAK,KAAK,UAAU,MAAM,CAAC;AACxC;;AAGD,WAAS,IAAI,KAAK,eAAe,MAAM,CAAC;;AAGzC,QAAO"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@zayne-labs/callapi",
3
3
  "type": "module",
4
- "version": "1.11.5",
4
+ "version": "1.11.6",
5
5
  "description": "A lightweight wrapper over fetch with quality of life improvements like built-in request cancellation, retries, interceptors and more",
6
6
  "author": "Ryan Zayne",
7
7
  "license": "MIT",
@@ -37,21 +37,21 @@
37
37
  "@arethetypeswrong/cli": "0.18.2",
38
38
  "@size-limit/esbuild-why": "11.2.0",
39
39
  "@size-limit/preset-small-lib": "11.2.0",
40
- "@testing-library/dom": "^10.4.1",
41
40
  "@total-typescript/ts-reset": "0.6.1",
42
- "@vitest/browser": "^3.2.4",
43
- "@vitest/coverage-v8": "3.2.4",
44
- "@zayne-labs/prettier-config": "^0.10.7",
45
- "@zayne-labs/tsconfig": "0.10.7",
41
+ "@vitest/browser": "4.0.1",
42
+ "@vitest/browser-playwright": "4.0.1",
43
+ "@vitest/coverage-v8": "4.0.1",
44
+ "@zayne-labs/prettier-config": "^0.11.4",
45
+ "@zayne-labs/tsconfig": "0.11.4",
46
46
  "concurrently": "^9.2.1",
47
47
  "cross-env": "^10.1.0",
48
- "playwright": "^1.55.1",
49
- "publint": "^0.3.13",
48
+ "playwright": "^1.56.1",
49
+ "publint": "^0.3.15",
50
50
  "size-limit": "11.2.0",
51
- "tsdown": "^0.15.7",
51
+ "tsdown": "^0.15.9",
52
52
  "typescript": "5.9.3",
53
- "vitest": "^3.2.4",
54
- "zod": "^4.1.11"
53
+ "vitest": "^4.0.1",
54
+ "zod": "^4.1.12"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public",