@akiyamka/extended-fetch
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,2 +0,2 @@ | ||
const m = "Network request timed out", g = globalThis, b = (t) => { | ||
const p = "Network request timed out", u = "AbortError", h = "AbortError", g = globalThis, b = (t) => { | ||
try { | ||
@@ -8,3 +8,3 @@ return t === "" && g.location.href ? g.location.href : t; | ||
}; | ||
function h(t) { | ||
function m(t) { | ||
const i = new Headers(); | ||
@@ -19,4 +19,4 @@ return t.replace(/\r?\n[\t ]+/g, " ").split("\r").map((l) => l.indexOf(` | ||
i.append(d, e); | ||
} catch (u) { | ||
u && typeof u == "object" && "message" in u ? console.warn("Response " + u.message) : console.warn("Response " + u); | ||
} catch (c) { | ||
c && typeof c == "object" && "message" in c ? console.warn("Response " + c.message) : console.warn("Response " + c); | ||
} | ||
@@ -26,3 +26,3 @@ } | ||
} | ||
const w = async (t) => t == null ? null : typeof t == "string" ? t : "getReader" in t ? await new Response(t).blob() : t, y = (t, i, n) => new Promise(async (l, a) => { | ||
const w = async (t) => t == null ? null : typeof t == "string" ? t : "getReader" in t ? await new Response(t).blob() : t, E = (t, i, n) => new Promise(async (l, a) => { | ||
const d = i == null ? void 0 : i.body, o = (() => { | ||
@@ -38,24 +38,24 @@ try { | ||
return a( | ||
new DOMException("The operation was aborted.", "AbortError") | ||
new DOMException("The operation was aborted.", u) | ||
); | ||
const e = new XMLHttpRequest(); | ||
if (e.onload = (s) => { | ||
if (e.onload = () => { | ||
try { | ||
const r = o.url.indexOf("file://") === 0 && (e.status < 200 || e.status > 599), c = { | ||
const s = o.url.indexOf("file://") === 0 && (e.status < 200 || e.status > 599), r = { | ||
statusText: e.statusText, | ||
headers: h(e.getAllResponseHeaders() || ""), | ||
status: r ? 200 : e.status | ||
headers: m(e.getAllResponseHeaders() || ""), | ||
status: s ? 200 : e.status | ||
}; | ||
c.url = "responseURL" in e ? e.responseURL : c.headers.get("X-Request-URL"); | ||
const p = "response" in e ? e.response : e.responseText; | ||
setTimeout(() => l(new Response(p, c)), 0); | ||
} catch (r) { | ||
console.log("CATCHED"), a(r); | ||
r.url = "responseURL" in e ? e.responseURL : r.headers.get("X-Request-URL"); | ||
const R = "response" in e ? e.response : e.responseText; | ||
setTimeout(() => l(new Response(R, r)), 0); | ||
} catch (s) { | ||
a(s); | ||
} | ||
}, e.onerror = (s) => { | ||
setTimeout(() => a(new TypeError("Network request failed")), 0); | ||
}, e.ontimeout = (s) => { | ||
setTimeout(() => a(new TypeError("Network request timed out")), 0); | ||
}, e.onabort = (s) => { | ||
setTimeout(() => a(new DOMException("Aborted", "AbortError")), 0); | ||
}, e.onerror = () => { | ||
setTimeout(() => a(new TypeError(h)), 0); | ||
}, e.ontimeout = () => { | ||
setTimeout(() => a(new TypeError(p)), 0); | ||
}, e.onabort = () => { | ||
setTimeout(() => a(new DOMException("Aborted", u)), 0); | ||
}, e.onprogress = (s) => { | ||
@@ -66,3 +66,3 @@ var r; | ||
var r; | ||
s.lengthComputable && ((r = n == null ? void 0 : n.onUploadProgress) == null || r.call(n, { progress: s.loaded / s.total, bytes: s.loaded })); | ||
s.lengthComputable ? (r = n == null ? void 0 : n.onUploadProgress) == null || r.call(n, { progress: s.loaded / s.total, bytes: s.loaded }) : console.debug("[Extended Fetch]: length not computable"); | ||
}), e.open(o.method, b(o.url), !0), o.credentials === "include" ? e.withCredentials = !0 : o.credentials === "omit" && (e.withCredentials = !1), "responseType" in e && (e.responseType = "blob"), o.headers.forEach( | ||
@@ -76,10 +76,10 @@ (s, r) => e.setRequestHeader(r, s) | ||
} | ||
const u = await w(d); | ||
e.send(u); | ||
}), E = (t) => typeof t == "object" && t !== null && "message" in t && t.message === m, R = (t) => typeof t == "object" && t !== null && "name" in t && // https://dom.spec.whatwg.org/#aborting-ongoing-activities-example | ||
t.name === "AbortError"; | ||
const c = await w(d); | ||
e.send(c); | ||
}), y = (t) => typeof t == "object" && t !== null && "message" in t && t.message === p, T = (t) => typeof t == "object" && t !== null && "name" in t && // https://dom.spec.whatwg.org/#aborting-ongoing-activities-example | ||
t.name === u; | ||
export { | ||
y as extendedFetch, | ||
R as isAbortError, | ||
E as isTimeoutError | ||
E as extendedFetch, | ||
T as isAbortError, | ||
y as isTimeoutError | ||
}; |
{ | ||
"name": "@akiyamka/extended-fetch", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Tiny window.fetch JavaScript implementation over XMLHttpRequest with additional features", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -7,3 +7,2 @@ # extended-fetch | ||
**This library allows you to cath Timeout Error without enforcing a time restriction** | ||
@@ -14,5 +13,5 @@ | ||
🧩 Does not patching existing fetch | ||
🔀 In most cases can be used as drop in replacement for fetch | ||
🔀 In most cases can be used as drop in replacement for fetch | ||
> ⚠️ It's *not* a fetch polyfill. It uses `Request` and `Response` objects from fetch implementation | ||
> ⚠️ It's _not_ a fetch polyfill. It uses `Request` and `Response` objects from fetch implementation | ||
@@ -50,2 +49,3 @@ ## Installation | ||
[Stackblitz live demo](https://stackblitz.com/edit/vitejs-vite-8t5mbf?file=src%2Fdemo.ts) | ||
@@ -56,7 +56,7 @@ ```js | ||
extendedFetch( | ||
'/users', | ||
'/upload', | ||
// Fetch configuration | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify({ foo: 'bar' }), | ||
body: File, | ||
}, | ||
@@ -66,6 +66,21 @@ // Additional settings | ||
onUploadProgress: (event) => { | ||
console.log(`Uploaded: ${event.progress}% (${event.bytes} bytes)`) | ||
console.log( | ||
`Uploaded: ${Math.floor(event.progress * 100)}% (${event.bytes} bytes)` | ||
) | ||
}, | ||
} | ||
) | ||
extendedFetch( | ||
'/download', | ||
// Fetch configuration | ||
{ | ||
method: 'GET', | ||
}, | ||
// Additional settings | ||
{ | ||
onDownloadProgress: (event) => { | ||
console.log(`Downloaded: ${event.progress}% (${event.bytes} bytes)`) | ||
console.log( | ||
`Downloaded: ${Math.floor(event.progress * 100)}% (${event.bytes} bytes)` | ||
) | ||
}, | ||
@@ -96,3 +111,3 @@ } | ||
## Motivation | ||
## Motivation | ||
@@ -102,2 +117,3 @@ Fetch has a pretty good api but doesn't cover some of the frequent occurrences of what an XHR query can give us | ||
### Fetch and timeout error | ||
Currently, there is no way to determine that the reason the request failed is due to the Timeout Error using the fetch API, but sometimes it needed, for example, for meaningful UI reaction. | ||
@@ -109,6 +125,6 @@ | ||
Using fetch we can get *download* progress information [using readable stream](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#consuming_a_fetch_as_a_stream) (if supported), but we still don't have a way to get *upload* progress using fetch api. | ||
Using fetch we can get _download_ progress information [using readable stream](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API/Using_readable_streams#consuming_a_fetch_as_a_stream) (if supported), but we still don't have a way to get _upload_ progress using fetch api. | ||
### Let's combine fetch api and XHR capabilities | ||
### Let's combine fetch api and XHR capabilities | ||
I wrapped XHR in a fetch api (taking some code from the [fetch polyfill](https://github.com/JakeChampion/fetch)) and added the missing functionality available from the XHR api |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13688
10
126
123