UNPKG

my-fetch-plus

Version:
206 lines (205 loc) 5.81 kB
var c = Object.defineProperty; var b = (h, t, e) => t in h ? c(h, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : h[t] = e; var d = (h, t, e) => (b(h, typeof t != "symbol" ? t + "" : t, e), e); class y { constructor(t) { d(this, "credentials"); d(this, "timeout"); d(this, "baseURL"); d(this, "simplify"); const { baseURL: e = "", timeout: s = 0, withCredentials: a = !0, simplify: n = !1 } = t || {}; this.baseURL = e || "", this.timeout = s, typeof a == "boolean" ? this.credentials = a ? "include" : "omit" : this.credentials = "same-origin", this.simplify = n; } get all() { return "all"; } getAll() { return { request: this.sendRequest.bind(this), get: this.get.bind(this), post: this.post.bind(this), put: this.put.bind(this), patch: this.patch.bind(this), options: this.options.bind(this), head: this.head.bind(this) }; } sendRequest(t, e) { let s = ""; if (typeof t == "object") if (t.url) s = `${this.baseURL}${t.url}`, delete t.url, e = t; else throw new Error("can not find url"); if (s = `${this.baseURL}${t}`, e != null && e.params) { const r = this.handleParams(e.params); s += "?" + r; } const a = this.handleConfig(e), n = { status: -1, statusText: "", redirected: !1, data: "", headers: {}, config: a, mode: "cors" }; if (typeof window.AbortController == "function") { if (this.timeout > 0) { const r = new AbortController(), i = r.signal; setTimeout(() => r.abort(), this.timeout), a.signal = i; } return window.fetch(s, a).then((r) => { const i = (e == null ? void 0 : e.responseType) ?? "json"; Object.assign(n, { status: r.status, statusText: r.statusText, redirected: r.redirected, mode: r.type }); const o = {}; for (let [l, u] of r.headers.entries()) o[l] = u; switch (n.headers = o, i) { case "text": return r.text(); case "json": return r.json(); case "blob": return r.blob(); case "arrayBuffer": return r.arrayBuffer(); case "formData": return r.formData(); case "stream": return r.blob(); } }).then((r) => (n.data = r, [null, n])).catch((r) => [r, null]); } return new Promise((r, i) => (this.timeout > 0 && setTimeout(() => i(new Error("timeout")), this.timeout), window.fetch(s, a).then((o) => { const l = (e == null ? void 0 : e.responseType) ?? "json"; Object.assign(n, { status: o.status, statusText: o.statusText, redirected: o.redirected, config: e || {} }); const u = {}; for (let [m, p] of o.headers.entries()) u[m] = p; switch (n.headers = u, l) { case "text": return o.text(); case "json": return o.json(); case "blob": return o.blob(); case "arrayBuffer": return o.arrayBuffer(); case "formData": return o.formData(); case "stream": return o.blob(); } }).then((o) => (n.data = o, [null, n])).catch((o) => [o, null]))); } get(t, e) { return this.sendRequest(t, e); } post(t, e) { const s = e || { method: "POST" }; return (!s.method || s.method.toUpperCase() !== "POST") && (s.method = "POST"), this.sendRequest(t, e); } put(t, e) { const s = e || { method: "PUT" }; return (!s.method || s.method.toUpperCase() !== "PUT") && (s.method = "PUT"), this.sendRequest(t, e); } patch(t, e) { const s = e || { method: "PATCH" }; return (!s.method || s.method.toUpperCase() !== "PATCH") && (s.method = "PATCH"), this.sendRequest(t, e); } delete(t, e) { const s = e || { method: "DELETE" }; return (!s.method || s.method.toUpperCase() !== "DELETE") && (s.method = "DELETE"), this.sendRequest(t, e); } options(t, e) { const s = e || { method: "OPTIONS" }; return (!s.method || s.method.toUpperCase() !== "OPTIONS") && (s.method = "OPTIONS"), this.sendRequest(t, e); } head(t, e) { const s = e || { method: "HEAD" }; return (!s.method || s.method.toUpperCase() !== "HEAD") && (s.method = "HEAD"), this.sendRequest(t, e); } jsonp(t, e) { } handleParams(t) { if (!t) return !1; let e = ""; if (typeof t == "string") e = t; else { const s = new URLSearchParams(t); for (const [a, n] of s.entries()) e += `${encodeURIComponent(a)}=${encodeURIComponent(n)}`; } return e; } handleConfig(t) { const e = (t == null ? void 0 : t.headers) ?? {}, s = (t == null ? void 0 : t.responseType) ?? "json"; let a = ""; switch (s) { default: a = "application/json"; break; case "text": a = "text/plain"; break; case "json": a = "application/json"; break; case "blob": a = "application/octet-stream"; break; case "arrayBuffer": a = "application/octet-stream"; break; case "formData": a = "multipart/form-data"; break; case "stream": a = "application/octet-stream"; break; } e["Content-Type"] = a; const n = { method: (t == null ? void 0 : t.method) || "GET", headers: e, referrerPolicy: "no-referrer-when-downgrade", // integrity mode: "cors", body: void 0 }; return t != null && t.data && (n.body = t.data), n; } } export { y as FetchPlus };