UNPKG

solid-fetch

Version:

Http request client promise-based supporting injectable request properties like: headers, parameters, query, body, etc.

165 lines (164 loc) 5.55 kB
var N = Object.defineProperty; var V = (s) => { throw TypeError(s); }; var T = (s, e, t) => e in s ? N(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t; var p = (s, e, t) => T(s, typeof e != "symbol" ? e + "" : e, t), C = (s, e, t) => e.has(s) || V("Cannot " + t); var m = (s, e, t) => (C(s, e, "read from private field"), t ? t.call(s) : e.get(s)), $ = (s, e, t) => e.has(s) ? V("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(s) : e.set(s, t); var v, R; class A { constructor({ initInjectables: e = {}, interceptedReq: t = [], interceptedRes: n = [], interceptedErr: l = [], globalQuery: c = {}, globalHeaders: f = {} }) { p(this, "injectables"); p(this, "interceptedReq"); p(this, "interceptedRes"); p(this, "interceptedErr"); p(this, "globalQuery"); p(this, "globalHeaders"); $(this, v, /\?$/); $(this, R, /\?.+$/); this.injectables = () => e, this.interceptedReq = t, this.interceptedRes = n, this.interceptedErr = l, this.globalQuery = c, this.globalHeaders = f; } setConfig(e) { Object.keys(e).forEach((n) => { this[n] = e[n]; }); } setInjectables(e) { const t = this.injectables(); this.injectables = () => ({ ...t, ...e }); } getInjectables() { const e = this.injectables(); return Object.keys(e).reduce((t, n) => typeof e[n] == "function" ? { ...t, [n]: e[n]() } : { ...t, [n]: e[n] }, {}); } resolveDynamic(e) { const t = this.getInjectables(); if (Array.isArray(e)) { let l = []; return e.length > 0 && (l = e.map((c) => typeof c == "function" ? c(t) : c)), l; } return Object.keys(e).reduce((l, c) => typeof e[c] == "function" ? { ...l, [c]: e[c](t) } : { ...l, [c]: e[c] }, {}); } generateRequest(e, ...t) { const n = this.resolveDynamic(t); return e.reduce((c, f, o) => { const q = `${f}${n[o] !== void 0 ? n[o] : ""}`; return `${c}${q}`; }, ""); } request(e, ...t) { const n = this.generateRequest(e, ...t); return ({ method: l = "GET", query: c = {}, headers: f = {}, body: o } = {}) => { const q = { ...this.globalQuery, ...typeof c == "function" ? c(this.getInjectables()) : c }, F = { ...this.globalHeaders, ...typeof f == "function" ? f(this.getInjectables()) : f }; let O = ""; m(this, v).test(n) ? O = "" : m(this, R).test(n) ? O = "&" : O = "?"; const E = this.resolveDynamic(q), I = Object.keys(E).reduce((r, i, d, a) => { const u = d + 1 < a.length, j = `${i}=${E[i]}`; return `${r}${j}${u ? "&" : ""}`; }, O), D = `${n}${Object.keys(q).length > 0 ? I : ""}`, x = this.resolveDynamic(F), y = { method: l, headers: { ...x } }; let h = o; typeof o == "function" ? h = o(this.getInjectables()) : o instanceof FormData || typeof o == "object" && (h = this.resolveDynamic(o)), h instanceof FormData ? y.body = h : typeof h == "object" ? (y.headers["Content-Type"] = "application/json", y.body = JSON.stringify(h)) : typeof h == "string" && (y.body = h); let b = D, g = { ...y }; if (this.interceptedReq.length > 0) { const r = { url: b, requestOptions: g }, i = this.interceptedReq.reduce((d, a) => { const u = a.action(d); return u !== null ? u : d; }, r); b = i.url, g = i.requestOptions; } return fetch(b, g).then(async (r) => { const i = Object.fromEntries(r.headers.entries()); if (r.status < 200 || r.status >= 300) { const a = new Error(JSON.stringify({ name: "NoSuccess", message: "request resulted in error", response: { bodyUsed: r.bodyUsed, headers: i, ok: r.ok, redirected: r.redirected, status: r.status, statusText: r.statusText, url: r.url }, request: { url: b, requestOptions: g } })); throw a.name = "NoSuccess", a; } const d = { request: { url: b, requestOptions: g }, headers: i, ok: r.ok, redirected: r.redirected, status: r.status, statusText: r.statusText, data: await (async () => { if (i != null && i["content-type"]) { if (i["content-type"].includes("application/json")) return r.json(); if (i["content-type"].includes("text/plain")) return r.text(); } return r.arrayBuffer(); })() }; return this.interceptedRes.length > 0 ? this.interceptedRes.reduce((u, j) => { const S = j.action(u); return S !== void 0 ? S : u; }, d) : d; }).catch((r) => { let i = r; throw r.name !== "NoSuccess" && (i = new Error(JSON.stringify({ name: r.name, message: r.message, request: { url: b, requestOptions: g } })), i.name = r.name), this.interceptedErr.length > 0 ? this.interceptedErr.reduce((a, u) => { const j = u.action(a); return j !== void 0 ? j : a; }, i) : i; }); }; } } v = new WeakMap(), R = new WeakMap(); export { A as default };