@kosmojs/fetch 0.0.11 → 0.0.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/package.json +2 -3
- package/pkg/index.js +28 -0
- package/pkg/index.js.map +2 -2
- package/pkg/src/index.d.ts +3 -1
- package/pkg/src/types.d.ts +5 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@kosmojs/fetch",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.20",
|
|
5
5
|
"author": "Slee Woo",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"publishConfig": {
|
|
@@ -20,8 +20,7 @@
|
|
|
20
20
|
"qs": "^6.14.0"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@types/qs": "^6.14.0"
|
|
24
|
-
"@kosmojs/config": "^0.0.11"
|
|
23
|
+
"@types/qs": "^6.14.0"
|
|
25
24
|
},
|
|
26
25
|
"scripts": {
|
|
27
26
|
"build": "esbuilder src/index.ts",
|
package/pkg/index.js
CHANGED
|
@@ -117,9 +117,37 @@ var stringify = (data) => {
|
|
|
117
117
|
encodeValuesOnly: true
|
|
118
118
|
});
|
|
119
119
|
};
|
|
120
|
+
var join = (...args) => {
|
|
121
|
+
for (const a of args) {
|
|
122
|
+
if (typeof a === "string" || typeof a === "number") {
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
throw new Error(
|
|
126
|
+
`The "path" argument must be of type string or number. Received type ${typeof a} (${JSON.stringify(a)})`
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
return args.join("/").replace(/\/+/g, "/");
|
|
130
|
+
};
|
|
131
|
+
var createHost = (host) => {
|
|
132
|
+
if (typeof host === "string") {
|
|
133
|
+
return host;
|
|
134
|
+
}
|
|
135
|
+
if (typeof host === "object") {
|
|
136
|
+
return [
|
|
137
|
+
host.secure ? "https://" : "http://",
|
|
138
|
+
host.hostname,
|
|
139
|
+
host.port ? `:${host.port}` : ""
|
|
140
|
+
].join("").replace(/\/+$/, "");
|
|
141
|
+
}
|
|
142
|
+
throw new Error(
|
|
143
|
+
"Expected host to be a string or an object like { hostname: string; port?: number; secure?: boolean }"
|
|
144
|
+
);
|
|
145
|
+
};
|
|
120
146
|
export {
|
|
147
|
+
createHost,
|
|
121
148
|
index_default as default,
|
|
122
149
|
defaults_default as defaults,
|
|
150
|
+
join,
|
|
123
151
|
stringify
|
|
124
152
|
};
|
|
125
153
|
//# sourceMappingURL=index.js.map
|
package/pkg/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/defaults.ts"],
|
|
4
|
-
"sourcesContent": ["import qs from \"qs\";\n\nimport defaults from \"./defaults\";\nimport type {\n FetchMapper,\n FetchMethod,\n HTTPError,\n HTTPMethod,\n Options,\n} from \"./types\";\n\nexport { defaults };\n\nexport * from \"./types\";\n\n// Supported data types for request body\ntype Data = Record<string, unknown> | FormData | ArrayBuffer | Blob | string;\n\n// Path can be a string, number, or array of these\ntype PathEntry = string | number;\n\n// HTTP methods that typically don't include a request body\nconst bodylessMethods = [\"GET\", \"DELETE\"];\n\n// Main factory function that creates a configured fetch client instance\nexport default (base: string | URL, opts?: Options): FetchMapper => {\n // Merge provided options with defaults, extracting specific properties\n const {\n headers: _headers,\n responseMode,\n stringify,\n errorHandler,\n ...fetchOpts // Remaining options passed directly to fetch\n } = {\n ...defaults,\n ...opts,\n };\n\n // Normalize headers to Headers instance for consistent API\n const headers = new Headers({\n ...(_headers instanceof Headers\n ? Object.fromEntries(_headers.entries()) // Convert Headers to plain object\n : _headers), // Use as-is if already a plain object\n });\n\n // Factory function that creates HTTP method implementations\n function wrapper(method: HTTPMethod): FetchMethod {\n // Function overloads for TypeScript type checking\n // No path, no data\n function _wrapper<T>(): Promise<T>;\n // Path without data\n function _wrapper<T>(path: PathEntry | Array<PathEntry>): Promise<T>;\n // Path with data\n function _wrapper<T>(\n path: PathEntry | Array<PathEntry>,\n data: Data,\n ): Promise<T>;\n\n // Main implementation function\n function _wrapper<T>(\n path?: PathEntry | Array<PathEntry>,\n data?: Data,\n ): Promise<T> {\n // Construct URL from base and path segments\n const url = [\n String(base),\n ...(Array.isArray(path)\n ? path // Use array as-is\n : [\"string\", \"number\"].includes(typeof path)\n ? [path] // Wrap single value in array\n : []), // No path provided\n ].join(\"/\");\n\n // Check if content-type was explicitly set in options\n const optedContentType =\n opts?.headers instanceof Headers\n ? opts.headers.get(\"Content-Type\")\n : opts?.headers?.[\"Content-Type\"];\n\n // Auto-set Content-Type header based on response mode if not explicitly set\n if (responseMode !== defaults.responseMode && !optedContentType) {\n const contentType = {\n text: \"text/plain\",\n blob: data instanceof Blob ? data.type : undefined,\n formData: null, // Let browser set multipart boundary\n arrayBuffer: null, // No content-type needed\n raw: undefined, // Don't modify\n }[responseMode];\n\n if (contentType === null) {\n headers.delete(\"Content-Type\");\n } else if (contentType) {\n headers.set(\"Content-Type\", contentType);\n }\n }\n\n let searchParams = \"\";\n\n // Default empty body\n let body: string | FormData | ArrayBuffer | Blob = JSON.stringify({});\n\n // Handle different data types for request body\n if (\n data instanceof Blob ||\n data instanceof FormData ||\n Object.prototype.toString.call(data) === \"[object ArrayBuffer]\"\n ) {\n // Use binary data as-is\n body = data as typeof body;\n } else if (typeof data === \"string\") {\n if (bodylessMethods.includes(method)) {\n // For GET/DELETE, add string data as query params\n searchParams = data;\n } else {\n // For other methods, use as body\n body = data;\n }\n } else if (typeof data === \"object\") {\n if (bodylessMethods.includes(method)) {\n // For GET/DELETE, serialize object to query string\n searchParams = stringify(data as Record<string, string>);\n } else {\n // For other methods, serialize as JSON\n body = JSON.stringify({ ...data });\n }\n }\n\n // Prepare fetch configuration\n const config: Options & { method: HTTPMethod; body?: typeof body } = {\n ...fetchOpts,\n method,\n headers,\n // Only include body for non-bodyless methods\n ...(bodylessMethods.includes(method) ? {} : { body }),\n };\n\n // Execute fetch request and process response\n return fetch([url, searchParams].join(\"?\"), config)\n .then((response) => {\n // Return both response and parsed data based on responseMode\n return Promise.all([\n response,\n responseMode === \"raw\"\n ? response // Return full response object\n : response[responseMode]().catch(() => null), // Parse response body\n ]);\n })\n .then(([response, data]) => {\n if (response.ok) {\n return data; // Return parsed data for successful responses\n }\n // Create enhanced error object for HTTP errors\n const error = new Error(\n data?.error || response.statusText,\n ) as HTTPError;\n error.response = response;\n error.body = data;\n errorHandler?.(error); // Call custom error handler if provided\n throw error;\n });\n }\n\n return _wrapper;\n }\n\n // Return object with HTTP method functions\n return {\n GET: wrapper(\"GET\"),\n POST: wrapper(\"POST\"),\n PUT: wrapper(\"PUT\"),\n PATCH: wrapper(\"PATCH\"),\n DELETE: wrapper(\"DELETE\"),\n };\n};\n\nexport const stringify = (data: Record<string, unknown>) => {\n return qs.stringify(data, {\n arrayFormat: \"brackets\",\n indices: false,\n encodeValuesOnly: true,\n });\n};\n", "export default {\n responseMode: \"json\",\n get headers() {\n return {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n };\n },\n stringify: (o) => new URLSearchParams(o as never).toString(),\n errorHandler: console.error,\n} satisfies import(\"./types\").Defaults;\n"],
|
|
5
|
-
"mappings": ";AAAA,OAAO,QAAQ;;;ACAf,IAAO,mBAAQ;AAAA,EACb,cAAc;AAAA,EACd,IAAI,UAAU;AACZ,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,gBAAgB;AAAA,IAClB;AAAA,EACF;AAAA,EACA,WAAW,CAAC,MAAM,IAAI,gBAAgB,CAAU,EAAE,SAAS;AAAA,EAC3D,cAAc,QAAQ;AACxB;;;
|
|
4
|
+
"sourcesContent": ["import qs from \"qs\";\n\nimport defaults from \"./defaults\";\nimport type {\n FetchMapper,\n FetchMethod,\n HostOpt,\n HTTPError,\n HTTPMethod,\n Options,\n} from \"./types\";\n\nexport { defaults };\n\nexport * from \"./types\";\n\n// Supported data types for request body\ntype Data = Record<string, unknown> | FormData | ArrayBuffer | Blob | string;\n\n// Path can be a string, number, or array of these\ntype PathEntry = string | number;\n\n// HTTP methods that typically don't include a request body\nconst bodylessMethods = [\"GET\", \"DELETE\"];\n\n// Main factory function that creates a configured fetch client instance\nexport default (base: string | URL, opts?: Options): FetchMapper => {\n // Merge provided options with defaults, extracting specific properties\n const {\n headers: _headers,\n responseMode,\n stringify,\n errorHandler,\n ...fetchOpts // Remaining options passed directly to fetch\n } = {\n ...defaults,\n ...opts,\n };\n\n // Normalize headers to Headers instance for consistent API\n const headers = new Headers({\n ...(_headers instanceof Headers\n ? Object.fromEntries(_headers.entries()) // Convert Headers to plain object\n : _headers), // Use as-is if already a plain object\n });\n\n // Factory function that creates HTTP method implementations\n function wrapper(method: HTTPMethod): FetchMethod {\n // Function overloads for TypeScript type checking\n // No path, no data\n function _wrapper<T>(): Promise<T>;\n // Path without data\n function _wrapper<T>(path: PathEntry | Array<PathEntry>): Promise<T>;\n // Path with data\n function _wrapper<T>(\n path: PathEntry | Array<PathEntry>,\n data: Data,\n ): Promise<T>;\n\n // Main implementation function\n function _wrapper<T>(\n path?: PathEntry | Array<PathEntry>,\n data?: Data,\n ): Promise<T> {\n // Construct URL from base and path segments\n const url = [\n String(base),\n ...(Array.isArray(path)\n ? path // Use array as-is\n : [\"string\", \"number\"].includes(typeof path)\n ? [path] // Wrap single value in array\n : []), // No path provided\n ].join(\"/\");\n\n // Check if content-type was explicitly set in options\n const optedContentType =\n opts?.headers instanceof Headers\n ? opts.headers.get(\"Content-Type\")\n : opts?.headers?.[\"Content-Type\"];\n\n // Auto-set Content-Type header based on response mode if not explicitly set\n if (responseMode !== defaults.responseMode && !optedContentType) {\n const contentType = {\n text: \"text/plain\",\n blob: data instanceof Blob ? data.type : undefined,\n formData: null, // Let browser set multipart boundary\n arrayBuffer: null, // No content-type needed\n raw: undefined, // Don't modify\n }[responseMode];\n\n if (contentType === null) {\n headers.delete(\"Content-Type\");\n } else if (contentType) {\n headers.set(\"Content-Type\", contentType);\n }\n }\n\n let searchParams = \"\";\n\n // Default empty body\n let body: string | FormData | ArrayBuffer | Blob = JSON.stringify({});\n\n // Handle different data types for request body\n if (\n data instanceof Blob ||\n data instanceof FormData ||\n Object.prototype.toString.call(data) === \"[object ArrayBuffer]\"\n ) {\n // Use binary data as-is\n body = data as typeof body;\n } else if (typeof data === \"string\") {\n if (bodylessMethods.includes(method)) {\n // For GET/DELETE, add string data as query params\n searchParams = data;\n } else {\n // For other methods, use as body\n body = data;\n }\n } else if (typeof data === \"object\") {\n if (bodylessMethods.includes(method)) {\n // For GET/DELETE, serialize object to query string\n searchParams = stringify(data as Record<string, string>);\n } else {\n // For other methods, serialize as JSON\n body = JSON.stringify({ ...data });\n }\n }\n\n // Prepare fetch configuration\n const config: Options & { method: HTTPMethod; body?: typeof body } = {\n ...fetchOpts,\n method,\n headers,\n // Only include body for non-bodyless methods\n ...(bodylessMethods.includes(method) ? {} : { body }),\n };\n\n // Execute fetch request and process response\n return fetch([url, searchParams].join(\"?\"), config)\n .then((response) => {\n // Return both response and parsed data based on responseMode\n return Promise.all([\n response,\n responseMode === \"raw\"\n ? response // Return full response object\n : response[responseMode]().catch(() => null), // Parse response body\n ]);\n })\n .then(([response, data]) => {\n if (response.ok) {\n return data; // Return parsed data for successful responses\n }\n // Create enhanced error object for HTTP errors\n const error = new Error(\n data?.error || response.statusText,\n ) as HTTPError;\n error.response = response;\n error.body = data;\n errorHandler?.(error); // Call custom error handler if provided\n throw error;\n });\n }\n\n return _wrapper;\n }\n\n // Return object with HTTP method functions\n return {\n GET: wrapper(\"GET\"),\n POST: wrapper(\"POST\"),\n PUT: wrapper(\"PUT\"),\n PATCH: wrapper(\"PATCH\"),\n DELETE: wrapper(\"DELETE\"),\n };\n};\n\nexport const stringify = (data: Record<string, unknown>) => {\n return qs.stringify(data, {\n arrayFormat: \"brackets\",\n indices: false,\n encodeValuesOnly: true,\n });\n};\n\nexport const join = (...args: Array<unknown>): string => {\n for (const a of args) {\n if (typeof a === \"string\" || typeof a === \"number\") {\n continue;\n }\n throw new Error(\n `The \"path\" argument must be of type string or number. Received type ${typeof a} (${JSON.stringify(a)})`,\n );\n }\n return args.join(\"/\").replace(/\\/+/g, \"/\");\n};\n\nexport const createHost = (host: HostOpt): string => {\n if (typeof host === \"string\") {\n return host;\n }\n\n if (typeof host === \"object\") {\n return [\n host.secure ? \"https://\" : \"http://\",\n host.hostname,\n host.port ? `:${host.port}` : \"\",\n ]\n .join(\"\")\n .replace(/\\/+$/, \"\");\n }\n\n throw new Error(\n \"Expected host to be a string or an object like { hostname: string; port?: number; secure?: boolean }\",\n );\n};\n", "export default {\n responseMode: \"json\",\n get headers() {\n return {\n Accept: \"application/json\",\n \"Content-Type\": \"application/json\",\n };\n },\n stringify: (o) => new URLSearchParams(o as never).toString(),\n errorHandler: console.error,\n} satisfies import(\"./types\").Defaults;\n"],
|
|
5
|
+
"mappings": ";AAAA,OAAO,QAAQ;;;ACAf,IAAO,mBAAQ;AAAA,EACb,cAAc;AAAA,EACd,IAAI,UAAU;AACZ,WAAO;AAAA,MACL,QAAQ;AAAA,MACR,gBAAgB;AAAA,IAClB;AAAA,EACF;AAAA,EACA,WAAW,CAAC,MAAM,IAAI,gBAAgB,CAAU,EAAE,SAAS;AAAA,EAC3D,cAAc,QAAQ;AACxB;;;ADaA,IAAM,kBAAkB,CAAC,OAAO,QAAQ;AAGxC,IAAO,gBAAQ,CAAC,MAAoB,SAAgC;AAElE,QAAM;AAAA,IACJ,SAAS;AAAA,IACT;AAAA,IACA,WAAAA;AAAA,IACA;AAAA,IACA,GAAG;AAAA;AAAA,EACL,IAAI;AAAA,IACF,GAAG;AAAA,IACH,GAAG;AAAA,EACL;AAGA,QAAM,UAAU,IAAI,QAAQ;AAAA,IAC1B,GAAI,oBAAoB,UACpB,OAAO,YAAY,SAAS,QAAQ,CAAC,IACrC;AAAA;AAAA,EACN,CAAC;AAGD,WAAS,QAAQ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,SAA0B;AACnD,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO;AAAA,EACT;AAEA,MAAI,OAAO,SAAS,UAAU;AAC5B,WAAO;AAAA,MACL,KAAK,SAAS,aAAa;AAAA,MAC3B,KAAK;AAAA,MACL,KAAK,OAAO,IAAI,KAAK,IAAI,KAAK;AAAA,IAChC,EACG,KAAK,EAAE,EACP,QAAQ,QAAQ,EAAE;AAAA,EACvB;AAEA,QAAM,IAAI;AAAA,IACR;AAAA,EACF;AACF;",
|
|
6
6
|
"names": ["stringify", "data"]
|
|
7
7
|
}
|
package/pkg/src/index.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import defaults from "./defaults";
|
|
2
|
-
import type { FetchMapper, Options } from "./types";
|
|
2
|
+
import type { FetchMapper, HostOpt, Options } from "./types";
|
|
3
3
|
export { defaults };
|
|
4
4
|
export * from "./types";
|
|
5
5
|
declare const _default: (base: string | URL, opts?: Options) => FetchMapper;
|
|
6
6
|
export default _default;
|
|
7
7
|
export declare const stringify: (data: Record<string, unknown>) => string;
|
|
8
|
+
export declare const join: (...args: Array<unknown>) => string;
|
|
9
|
+
export declare const createHost: (host: HostOpt) => string;
|