UNPKG

react-box-tools

Version:

Box tools react components, utils and hooks

137 lines (136 loc) 4.07 kB
var m = Object.defineProperty; var w = (n, e, t) => e in n ? m(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; var i = (n, e, t) => w(n, typeof e != "symbol" ? e + "" : e, t); class p { constructor(e = { headers: {} }) { i(this, "onReq", (e) => e); i(this, "onRes", (e) => e); i(this, "interceptors", { onRequest: this.onReq, onResponse: this.onRes }); i(this, "baseUrl", ""); i(this, "config", { headers: {} }); this.config = e; } create(e) { return new p(e); } processError(e, t) { const s = { 400: "Bad Request", 401: "Unauthorized", 403: "Forbidden", 404: "Not Found", 405: "Method Not Allowed", 413: "Payload Too Large", 500: "Internal Server Error", 502: "Bad Gateway", 503: "Service Unavailable", 504: "Gateway Timeout", ERR_NETWORK: "Network Error", ERR_TIMEOUT: "Timeout Error", ERR_UNKNOWN: "Unknown Error" }; let c; t && (t instanceof TypeError && t.message === "Failed to fetch" ? e.code = "ERR_NETWORK" : t instanceof Error && t.message === "Timed out" ? e.code = "ERR_TIMEOUT" : typeof t == "object" && t.response && t.response.status ? c = `Error ${t.response.status}: ${t.response.statusText}` : c = `Error ${t}`), e.message = s[e.code] || c || `An error code ${e.code} has occurred`; } async request(e, t, s, c) { let h = { method: t, ...s, headers: { ...this.config.headers, ...s == null ? void 0 : s.headers } }, o = { baseUrl: this.baseUrl, url: e, method: t, code: "ERR_UNKNOWN", message: "" }; await this.interceptors.onRequest(h); const E = fetch( this.baseUrl + e, h ), l = (s == null ? void 0 : s.timeout) || this.config.timeout || 3e4, R = new Promise((r, a) => { setTimeout(async () => { o.code = "ERR_TIMEOUT", this.processError(o), a(async () => { this.interceptors.onError ? await this.interceptors.onError(o) : new Error(o.message); }); }, l); }); try { const r = await Promise.race([E, R]); let a; var u = r.status; if (r.ok) switch (c) { case "text": a = await r.text(); break; case "blob": a = await r.blob(); break; case "arrayBuffer": a = await r.arrayBuffer(); break; case "formData": a = await r.formData(); break; case "unknown": a = r; break; default: a = await r.json(); break; } else u >= 400 && u <= 599 && (o.code = u, o.message = r.statusText, this.processError(o), this.interceptors.onError && await this.interceptors.onError(o)); const d = { //@ts-ignore data: a ?? {}, req: h, res: { headers: Object.fromEntries(r.headers), status: r.status, statusText: r.statusText, redirected: r.redirected, ok: r.ok, type: r.type, url: r.url } }; return await this.interceptors.onResponse(d), d; } catch (r) { throw this.processError(o, r), this.interceptors.onError && await this.interceptors.onError(o), o.message; } } get(e, t, s) { return this.request(e, "get", t, s); } post(e, t, s) { return this.request(e, "post", t, s); } put(e, t, s) { return this.request(e, "put", t, s); } delete(e, t, s) { return this.request(e, "delete", t, s); } patch(e, t, s) { return this.request(e, "patch", t, s); } head(e, t, s) { return this.request(e, "head", t, s); } options(e, t, s) { return this.request(e, "options", t, s); } cleanup() { this.interceptors.onRequest = this.onReq, this.interceptors.onResponse = this.onRes, this.interceptors.onError = void 0, this.baseUrl = "", this.config = { headers: {} }; } } const T = new p(); export { T as fetchApi };