commit 605fe39c40d46ab8982977db929ce277fea0a3d3 Author: what Date: Thu Apr 4 11:47:06 2024 +0800 first commit diff --git a/dist/ff-request.js b/dist/ff-request.js new file mode 100644 index 0000000..eb7618d --- /dev/null +++ b/dist/ff-request.js @@ -0,0 +1,477 @@ +var ae = Object.defineProperty; +var ie = (t, e, r) => e in t ? ae(t, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : t[e] = r; +var A = (t, e, r) => (ie(t, typeof e != "symbol" ? e + "" : e, r), r), Z = (t, e, r) => { + if (!e.has(t)) + throw TypeError("Cannot " + r); +}; +var f = (t, e, r) => (Z(t, e, "read from private field"), r ? r.call(t) : e.get(t)), S = (t, e, r) => { + if (e.has(t)) + throw TypeError("Cannot add the same private member more than once"); + e instanceof WeakSet ? e.add(t) : e.set(t, r); +}, M = (t, e, r, n) => (Z(t, e, "write to private field"), n ? n.call(t, r) : e.set(t, r), r); +function te(t) { + return t && t.__esModule && Object.prototype.hasOwnProperty.call(t, "default") ? t.default : t; +} +function oe(t) { + if (t.length >= 255) + throw new TypeError("Alphabet too long"); + for (var e = new Uint8Array(256), r = 0; r < e.length; r++) + e[r] = 255; + for (var n = 0; n < t.length; n++) { + var h = t.charAt(n), v = h.charCodeAt(0); + if (e[v] !== 255) + throw new TypeError(h + " is ambiguous"); + e[v] = n; + } + var u = t.length, N = t.charAt(0), B = Math.log(u) / Math.log(256), j = Math.log(256) / Math.log(u); + function m(i) { + if (i instanceof Uint8Array || (ArrayBuffer.isView(i) ? i = new Uint8Array(i.buffer, i.byteOffset, i.byteLength) : Array.isArray(i) && (i = Uint8Array.from(i))), !(i instanceof Uint8Array)) + throw new TypeError("Expected Uint8Array"); + if (i.length === 0) + return ""; + for (var a = 0, c = 0, g = 0, o = i.length; g !== o && i[g] === 0; ) + g++, a++; + for (var l = (o - g) * j + 1 >>> 0, d = new Uint8Array(l); g !== o; ) { + for (var T = i[g], E = 0, w = l - 1; (T !== 0 || E < c) && w !== -1; w--, E++) + T += 256 * d[w] >>> 0, d[w] = T % u >>> 0, T = T / u >>> 0; + if (T !== 0) + throw new Error("Non-zero carry"); + c = E, g++; + } + for (var s = l - c; s !== l && d[s] === 0; ) + s++; + for (var y = N.repeat(a); s < l; ++s) + y += t.charAt(d[s]); + return y; + } + function _(i) { + if (typeof i != "string") + throw new TypeError("Expected String"); + if (i.length === 0) + return new Uint8Array(); + for (var a = 0, c = 0, g = 0; i[a] === N; ) + c++, a++; + for (var o = (i.length - a) * B + 1 >>> 0, l = new Uint8Array(o); i[a]; ) { + var d = e[i.charCodeAt(a)]; + if (d === 255) + return; + for (var T = 0, E = o - 1; (d !== 0 || T < g) && E !== -1; E--, T++) + d += u * l[E] >>> 0, l[E] = d % 256 >>> 0, d = d / 256 >>> 0; + if (d !== 0) + throw new Error("Non-zero carry"); + g = T, a++; + } + for (var w = o - g; w !== o && l[w] === 0; ) + w++; + for (var s = new Uint8Array(c + (o - w)), y = c; w !== o; ) + s[y++] = l[w++]; + return s; + } + function p(i) { + var a = _(i); + if (a) + return a; + throw new Error("Non-base" + u + " character"); + } + return { + encode: m, + decodeUnsafe: _, + decode: p + }; +} +var fe = oe; +const he = /* @__PURE__ */ te(fe); +var re = {}; +(function(t) { + (function(e) { + e([], function() { + var r = -1, n = -2, h = -3, v = -4, u = -5, N = function(a, c) { + c = c || {}; + var g = c.verbose || !1; + g && console.log("Normalize the JSON Object"), a = typeof a == "string" ? this.JSON.parse(a) : a, g && console.log("Creating a empty dictionary"); + var o = { + strings: [], + integers: [], + floats: [] + }; + g && console.log("Creating the AST"); + var l = function w(s) { + g && console.log("Calling recursiveAstBuilder with " + this.JSON.stringify(s)); + var y = typeof s; + if (s === null) + return { + type: "null", + index: h + }; + if (typeof s > "u") + return { + type: "undefined", + index: u + }; + if (s instanceof Array) { + var U = ["@"]; + for (var O in s) + s.hasOwnProperty(O) && U.push(w(s[O])); + return U; + } + if (y === "object") { + var U = ["$"]; + for (var R in s) + s.hasOwnProperty(R) && (U.push(w(R)), U.push(w(s[R]))); + return U; + } + if (s === "") + return { + type: "empty", + index: v + }; + if (y === "string") { + var P = i.call(o.strings, s); + return P == -1 && (o.strings.push(j(s)), P = o.strings.length - 1), { + type: "strings", + index: P + }; + } + if (y === "number" && s % 1 === 0) { + var P = i.call(o.integers, s); + return P == -1 && (o.integers.push(_(s)), P = o.integers.length - 1), { + type: "integers", + index: P + }; + } + if (y === "number") { + var P = i.call(o.floats, s); + return P == -1 && (o.floats.push(s), P = o.floats.length - 1), { + type: "floats", + index: P + }; + } + if (y === "boolean") + return { + type: "boolean", + index: s ? r : n + }; + throw new Error("Unexpected argument of type " + typeof s); + }(a), d = o.strings.length, T = o.integers.length; + o.floats.length, g && console.log("Parsing the dictionary"); + var E = o.strings.join("|"); + return E += "^" + o.integers.join("|"), E += "^" + o.floats.join("|"), g && console.log("Parsing the structure"), E += "^" + function w(s) { + if (g && console.log("Calling a recursiveParser with " + this.JSON.stringify(s)), s instanceof Array) { + var y = s.shift(); + for (var U in s) + s.hasOwnProperty(U) && (y += w(s[U]) + "|"); + return (y[y.length - 1] === "|" ? y.slice(0, -1) : y) + "]"; + } + var O = s.type, R = s.index; + if (O === "strings") + return _(R); + if (O === "integers") + return _(d + R); + if (O === "floats") + return _(d + T + R); + if (O === "boolean") + return s.index; + if (O === "null") + return h; + if (O === "undefined") + return u; + if (O === "empty") + return v; + throw new TypeError("The item is alien!"); + }(l), g && console.log("Ending parser"), c.debug ? { + dictionary: o, + ast: l, + packed: E + } : E; + }, B = function(a, c) { + c = c || {}; + var g = a.split("^"); + c.verbose && console.log("Building dictionary"); + var o = [], l = g[0]; + if (l !== "") { + l = l.split("|"), c.verbose && console.log("Parse the strings dictionary"); + for (var d = 0, T = l.length; d < T; d++) + o.push(m(l[d])); + } + if (l = g[1], l !== "") { + l = l.split("|"), c.verbose && console.log("Parse the integers dictionary"); + for (var d = 0, T = l.length; d < T; d++) + o.push(p(l[d])); + } + if (l = g[2], l !== "") { + l = l.split("|"), c.verbose && console.log("Parse the floats dictionary"); + for (var d = 0, T = l.length; d < T; d++) + o.push(parseFloat(l[d])); + } + l = null, c.verbose && console.log("Tokenizing the structure"); + for (var E = "", w = [], s = g[3].length, d = 0; d < s; d++) { + var y = g[3].charAt(d); + y === "|" || y === "$" || y === "@" || y === "]" ? (E && (w.push(p(E)), E = ""), y !== "|" && w.push(y)) : E += y; + } + var U = w.length, O = 0; + return c.verbose && console.log("Starting recursive parser"), function R() { + var P = w[O++]; + if (c.verbose && console.log("Reading collection type " + (P === "$" ? "object" : "Array")), P === "@") { + for (var b = []; O < U; O++) { + var C = w[O]; + if (c.verbose && console.log("Read " + C + " symbol"), C === "]") + return b; + if (C === "@" || C === "$") + b.push(R()); + else + switch (C) { + case r: + b.push(!0); + break; + case n: + b.push(!1); + break; + case h: + b.push(null); + break; + case u: + b.push(void 0); + break; + case v: + b.push(""); + break; + default: + b.push(o[C]); + } + } + return c.verbose && console.log("Parsed " + this.JSON.stringify(b)), b; + } + if (P === "$") { + for (var b = {}; O < U; O++) { + var x = w[O]; + if (x === "]") + return b; + x === v ? x = "" : x = o[x]; + var C = w[++O]; + if (C === "@" || C === "$") + b[x] = R(); + else + switch (C) { + case r: + b[x] = !0; + break; + case n: + b[x] = !1; + break; + case h: + b[x] = null; + break; + case u: + b[x] = void 0; + break; + case v: + b[x] = ""; + break; + default: + b[x] = o[C]; + } + } + return c.verbose && console.log("Parsed " + this.JSON.stringify(b)), b; + } + throw new TypeError("Bad token " + P + " isn't a type"); + }(); + }, j = function(a) { + return typeof a != "string" ? a : a.replace(/[\+ \|\^\%]/g, function(c) { + return { + " ": "+", + "+": "%2B", + "|": "%7C", + "^": "%5E", + "%": "%25" + }[c]; + }); + }, m = function(a) { + return typeof a != "string" ? a : a.replace(/\+|%2B|%7C|%5E|%25/g, function(c) { + return { + "+": " ", + "%2B": "+", + "%7C": "|", + "%5E": "^", + "%25": "%" + }[c]; + }); + }, _ = function(a) { + return Number.prototype.toString.call(a, 36).toUpperCase(); + }, p = function(a) { + return parseInt(a, 36); + }, i = Array.prototype.indexOf || function(a, c) { + for (var g = c || 0, o = this.length; g < o; g++) + if (this[g] === a) + return g; + return -1; + }; + return { + JSON, + pack: N, + unpack: B + }; + }); + })(function(e, r) { + var n = r(); + for (var h in n) + t[h] = n[h]; + }); +})(re); +const ee = /* @__PURE__ */ te(re); +var q, z, J, D, L, H, K, V, k; +class W { + constructor(e, r, n, h, v) { + A(this, "code", 0); + A(this, "message", "操作成功"); + A(this, "data", null); + A(this, "url", ""); + A(this, "res", ""); + // static [Symbol.hasInstance](self) { return self?.IS_ECHO_MSG !== undefined } + S(this, q, null); + // 判断返回 Response 对象, 还是 Response 中的 data + S(this, z, !0); + // 判罚是否已经给出错误提示 + S(this, J, !0); + S(this, D, { + get: (e, r) => f(this, L).call(this, r), + ownKeys: () => Object.keys(f(this, D).getPrototypeOf() || {}), + getPrototypeOf: () => f(this, z) ? this : this.data, + getOwnPropertyDescriptor: () => ({ + configurable: !0, + enumerable: !0, + writable: !0, + value: f(this, D).getPrototypeOf() + }) + }); + S(this, L, (e) => e === "$toast" ? f(this, K) : e === "msg" ? f(this, H) : e === "then" || e === "resp" ? (M(this, J, !0), M(this, z, e === "resp"), f(this, V)) : e === "catch" ? f(this, k) : e === "IS_ECHO_MSG" ? f(this, J) : Reflect.get(this, e)); + S(this, H, (e = (n) => n, r = !1) => (f(this, K).call(this), M(this, z, r), Promise.resolve(e == null ? void 0 : e(f(this, D).getPrototypeOf())))); + S(this, K, () => { + f(this, J) && (M(this, J, !1), we.onToast(this.code, this.message)); + }); + S(this, V, (e) => [0, 1].includes(this.code) ? Promise.resolve(e == null ? void 0 : e(f(this, D).getPrototypeOf())) : Promise.reject(f(this, q))); + S(this, k, (e) => Promise.resolve(e(f(this, q)))); + return this.code = e, this.message = r, this.data = n, this.url = h, this.res = v, M(this, q, new Proxy(this, f(this, D))); + } +} +q = new WeakMap(), z = new WeakMap(), J = new WeakMap(), D = new WeakMap(), L = new WeakMap(), H = new WeakMap(), K = new WeakMap(), V = new WeakMap(), k = new WeakMap(); +const Q = (t, e, r, n = "/", h = "") => new W(t, e, r, n, h); +var ne = [ + "utf8", + "utf-8", + "unicode-1-1-utf-8" +]; +function le(t) { + if (ne.indexOf(t) < 0 && typeof t < "u" && t != null) + throw new RangeError("Invalid encoding type. Only utf-8 is supported"); + this.encoding = "utf-8", this.encode = function(e) { + if (typeof e != "string") + throw new TypeError("passed argument must be of tye string"); + var r = unescape(encodeURIComponent(e)), n = new Uint8Array(r.length); + const h = r.split(""); + for (let v = 0; v < h.length; v++) + n[v] = h[v].charCodeAt(0); + return n; + }; +} +function ue(t) { + if (ne.indexOf(t) < 0 && typeof t < "u" && t != null) + throw new RangeError("Invalid encoding type. Only utf-8 is supported"); + this.encoding = "utf-8", this.decode = function(e, r) { + if (typeof e > "u") + return ""; + var n = typeof r < "u" && n in r ? r.stream : !1; + if (typeof n != "boolean") + throw new TypeError("stream option must be boolean"); + if (ArrayBuffer.isView(e)) { + var h = new Uint8Array(e.buffer, e.byteOffset, e.byteLength), v = new Array(h.length); + for (let u = 0; u < h.length; u++) + v[u] = String.fromCharCode(h[u]); + return decodeURIComponent(escape(v.join(""))); + } else + throw new TypeError("passed argument must be an array buffer view"); + }; +} +var se = { + TextEncoder: le, + TextDecoder: ue +}; +const ce = new se.TextEncoder("utf8"), ge = new se.TextDecoder("utf8"), de = (t) => { + let e = 5381; + for (let r = 0; r < t.length; r++) + e = e * 33 ^ t.charCodeAt(r); + return (e >>> 0).toString(16); +}, be = (t, e) => t.replace(new RegExp(`^${e}+|${e}+$`, "g"), ""), pe = (t) => ce.encode(t || ""), ve = (t) => ge.decode(new DataView(t), {}); +var F, G, Y, $, I; +class X { + constructor(e, r, n = ({}) => Promise.reject({ code: -1, msg: "请初始化HTTP请求方法主体" })) { + S(this, F, void 0); + S(this, G, void 0); + S(this, Y, void 0); + S(this, $, { + "#": /* @__PURE__ */ new Map(), + // 系统缓存 + $: /* @__PURE__ */ new Map() + // 普通缓存 + }); + S(this, I, /* @__PURE__ */ new Map()); + A(this, "request", ({ url: e = "/", method: r = "GET", ...n }, h = !1) => { + var j; + const v = r.toUpperCase(), u = de(JSON.stringify([e, v, n])), N = (j = e == null ? void 0 : e.includes) != null && j.call(e, "/_/") ? "#" : "$", B = new Promise((m, _) => { + !h && N !== "#" && f(this, $)[N].delete(u), f(this, $)[N].has(u) ? (console.log("缓存命中", e), m(f(this, $)[N].get(u))) : f(this, I).has(u) ? f(this, I).get(u).push([m, _]) : (f(this, I).set(u, [[m, _]]), f(this, F).call(this, { url: e, method: v, ...n }).then((p) => { + if (![0, 1].includes(p.code)) + throw Q(p.code, p.msg, p.data, e, p == null ? void 0 : p.res); + return [p.code, p.msg, p.data, e, p == null ? void 0 : p.res]; + }).then((p) => ((N == "#" || h && v === "GET") && f(this, $)[N].set(u, p), p)).then((p) => f(this, I).get(u).forEach(([i, a]) => i(p))).catch((p) => (f(this, $)[N].delete(u), f(this, I).get(u).forEach(([i, a]) => a(p instanceof W ? p : Q(-1, p, null, e))))).finally(() => f(this, I).delete(u))); + }); + return new Proxy(B, { + get: (m, _) => (...p) => m.then((i) => Q(...i)[_](...p)).catch((i) => { + if (!i instanceof W) + throw i; + return i[_](...p); + }) + }); + }); + A(this, "get", (e, r = {}) => this.request({ url: e, method: "GET", params: r })); + A(this, "post", (e, r = {}, n = {}) => this.request({ url: e, method: "POST", data: r, params: n })); + A(this, "put", (e, r = {}, n = {}) => this.request({ url: e, method: "PUT", data: r, params: n })); + A(this, "del", (e, r = {}, n = {}) => this.request({ url: e, method: "DELETE", data: r, params: n })); + A(this, "download", (e, r) => { + }); + A(this, "cache", (e, r = {}) => this.request({ url: e, method: "GET", params: r }, !0)); + A(this, "list", (e, r = {}) => this.get(`/api/${e}/${this.encode(r)}`)); + A(this, "getAppInfo", () => { + }); + A(this, "refreshCache", (e = !1) => { + e && f(this, $)["#"].clear(), f(this, $).$.clear(); + }); + A(this, "decode", (e = "", r = {}) => { + if (e === void 0 || e === "" || e === null) + return r; + try { + const n = f(this, G).decode(e).buffer; + return ee.unpack(ve(n)); + } catch { + return console.warn("core.decode", e), r; + } + }); + A(this, "encode", (e = {}) => { + const r = JSON.stringify(e, (n, h) => h === void 0 ? null : h); + return f(this, G).encode(pe(ee.pack(r))); + }); + M(this, Y, e), M(this, F, n), M(this, G, he(r)); + } +} +F = new WeakMap(), G = new WeakMap(), Y = new WeakMap(), $ = new WeakMap(), I = new WeakMap(); +const we = X; +X.onUnhandledRejection = (t) => { + var e, r, n, h, v, u; + t != null && t.IS_ECHO_MSG ? ((e = t == null ? void 0 : t.$toast) == null || e.call(t), (r = t == null ? void 0 : t.preventDefault) == null || r.call(t)) : (n = t.reason) != null && n.IS_ECHO_MSG && ((v = (h = t.reason) == null ? void 0 : h.$toast) == null || v.call(h), (u = t == null ? void 0 : t.preventDefault) == null || u.call(t)); +}; +X.onToast = (t, e) => [0, 1].includes(t) ? console.log(e) : console.warn(e); +export { + we as default, + de as signature, + pe as str2uint8array, + be as trim, + ve as uint8array2str +}; diff --git a/dist/ff-request.umd.cjs b/dist/ff-request.umd.cjs new file mode 100644 index 0000000..06d202a --- /dev/null +++ b/dist/ff-request.umd.cjs @@ -0,0 +1 @@ +(function(p,y){typeof exports=="object"&&typeof module<"u"?y(exports):typeof define=="function"&&define.amd?define(["exports"],y):(p=typeof globalThis<"u"?globalThis:p||self,y(p["ff-request"]={}))})(this,function(p){"use strict";var we=Object.defineProperty;var be=(p,y,_)=>y in p?we(p,y,{enumerable:!0,configurable:!0,writable:!0,value:_}):p[y]=_;var P=(p,y,_)=>(be(p,typeof y!="symbol"?y+"":y,_),_),le=(p,y,_)=>{if(!y.has(p))throw TypeError("Cannot "+_)};var f=(p,y,_)=>(le(p,y,"read from private field"),_?_.call(p):y.get(p)),U=(p,y,_)=>{if(y.has(p))throw TypeError("Cannot add the same private member more than once");y instanceof WeakSet?y.add(p):y.set(p,_)},q=(p,y,_,k)=>(le(p,y,"write to private field"),k?k.call(p,_):y.set(p,_),_);var G,B,z,J,Y,Q,L,W,X,H,K,Z,M,D;function y(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function _(t){if(t.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),r=0;r>>0,g=new Uint8Array(u);d!==o;){for(var A=a[d],T=0,b=u-1;(A!==0||T>>0,g[b]=A%l>>>0,A=A/l>>>0;if(A!==0)throw new Error("Non-zero carry");c=T,d++}for(var n=u-c;n!==u&&g[n]===0;)n++;for(var E=x.repeat(s);n>>0,u=new Uint8Array(o);a[s];){var g=e[a.charCodeAt(s)];if(g===255)return;for(var A=0,T=o-1;(g!==0||A>>0,u[T]=g%256>>>0,g=g/256>>>0;if(g!==0)throw new Error("Non-zero carry");d=A,s++}for(var b=o-d;b!==o&&u[b]===0;)b++;for(var n=new Uint8Array(c+(o-b)),E=c;b!==o;)n[E++]=u[b++];return n}function v(a){var s=C(a);if(s)return s;throw new Error("Non-base"+l+" character")}return{encode:j,decodeUnsafe:C,decode:v}}var k=_;const ce=y(k);var ne={};(function(t){(function(e){e([],function(){var r=-1,i=-2,h=-3,w=-4,l=-5,x=function(s,c){c=c||{};var d=c.verbose||!1;d&&console.log("Normalize the JSON Object"),s=typeof s=="string"?this.JSON.parse(s):s,d&&console.log("Creating a empty dictionary");var o={strings:[],integers:[],floats:[]};d&&console.log("Creating the AST");var u=function b(n){d&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(n));var E=typeof n;if(n===null)return{type:"null",index:h};if(typeof n>"u")return{type:"undefined",index:l};if(n instanceof Array){var m=["@"];for(var S in n)n.hasOwnProperty(S)&&m.push(b(n[S]));return m}if(E==="object"){var m=["$"];for(var I in n)n.hasOwnProperty(I)&&(m.push(b(I)),m.push(b(n[I])));return m}if(n==="")return{type:"empty",index:w};if(E==="string"){var N=a.call(o.strings,n);return N==-1&&(o.strings.push(F(n)),N=o.strings.length-1),{type:"strings",index:N}}if(E==="number"&&n%1===0){var N=a.call(o.integers,n);return N==-1&&(o.integers.push(C(n)),N=o.integers.length-1),{type:"integers",index:N}}if(E==="number"){var N=a.call(o.floats,n);return N==-1&&(o.floats.push(n),N=o.floats.length-1),{type:"floats",index:N}}if(E==="boolean")return{type:"boolean",index:n?r:i};throw new Error("Unexpected argument of type "+typeof n)}(s),g=o.strings.length,A=o.integers.length;o.floats.length,d&&console.log("Parsing the dictionary");var T=o.strings.join("|");return T+="^"+o.integers.join("|"),T+="^"+o.floats.join("|"),d&&console.log("Parsing the structure"),T+="^"+function b(n){if(d&&console.log("Calling a recursiveParser with "+this.JSON.stringify(n)),n instanceof Array){var E=n.shift();for(var m in n)n.hasOwnProperty(m)&&(E+=b(n[m])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var S=n.type,I=n.index;if(S==="strings")return C(I);if(S==="integers")return C(g+I);if(S==="floats")return C(g+A+I);if(S==="boolean")return n.index;if(S==="null")return h;if(S==="undefined")return l;if(S==="empty")return w;throw new TypeError("The item is alien!")}(u),d&&console.log("Ending parser"),c.debug?{dictionary:o,ast:u,packed:T}:T},V=function(s,c){c=c||{};var d=s.split("^");c.verbose&&console.log("Building dictionary");var o=[],u=d[0];if(u!==""){u=u.split("|"),c.verbose&&console.log("Parse the strings dictionary");for(var g=0,A=u.length;gf(this,Y).call(this,r),ownKeys:()=>Object.keys(f(this,J).getPrototypeOf()||{}),getPrototypeOf:()=>f(this,B)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:f(this,J).getPrototypeOf()})});U(this,Y,e=>e==="$toast"?f(this,L):e==="msg"?f(this,Q):e==="then"||e==="resp"?(q(this,z,!0),q(this,B,e==="resp"),f(this,W)):e==="catch"?f(this,X):e==="IS_ECHO_MSG"?f(this,z):Reflect.get(this,e));U(this,Q,(e=i=>i,r=!1)=>(f(this,L).call(this),q(this,B,r),Promise.resolve(e==null?void 0:e(f(this,J).getPrototypeOf()))));U(this,L,()=>{f(this,z)&&(q(this,z,!1),ue.onToast(this.code,this.message))});U(this,W,e=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(f(this,J).getPrototypeOf())):Promise.reject(f(this,G)));U(this,X,e=>Promise.resolve(e(f(this,G))));return this.code=e,this.message=r,this.data=i,this.url=h,this.res=w,q(this,G,new Proxy(this,f(this,J)))}}G=new WeakMap,B=new WeakMap,z=new WeakMap,J=new WeakMap,Y=new WeakMap,Q=new WeakMap,L=new WeakMap,W=new WeakMap,X=new WeakMap;const te=(t,e,r,i="/",h="")=>new ee(t,e,r,i,h);var se=["utf8","utf-8","unicode-1-1-utf-8"];function de(t){if(se.indexOf(t)<0&&typeof t<"u"&&t!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var r=unescape(encodeURIComponent(e)),i=new Uint8Array(r.length);const h=r.split("");for(let w=0;w"u")return"";var i=typeof r<"u"&&i in r?r.stream:!1;if(typeof i!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var h=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),w=new Array(h.length);for(let l=0;l{let e=5381;for(let r=0;r>>0).toString(16)},ye=(t,e)=>t.replace(new RegExp(`^${e}+|${e}+$`,"g"),""),fe=t=>pe.encode(t||""),he=t=>ve.decode(new DataView(t),{});class re{constructor(e,r,i=({})=>Promise.reject({code:-1,msg:"请初始化HTTP请求方法主体"})){U(this,H,void 0);U(this,K,void 0);U(this,Z,void 0);U(this,M,{"#":new Map,$:new Map});U(this,D,new Map);P(this,"request",({url:e="/",method:r="GET",...i},h=!1)=>{var F;const w=r.toUpperCase(),l=oe(JSON.stringify([e,w,i])),x=(F=e==null?void 0:e.includes)!=null&&F.call(e,"/_/")?"#":"$",V=new Promise((j,C)=>{!h&&x!=="#"&&f(this,M)[x].delete(l),f(this,M)[x].has(l)?(console.log("缓存命中",e),j(f(this,M)[x].get(l))):f(this,D).has(l)?f(this,D).get(l).push([j,C]):(f(this,D).set(l,[[j,C]]),f(this,H).call(this,{url:e,method:w,...i}).then(v=>{if(![0,1].includes(v.code))throw te(v.code,v.msg,v.data,e,v==null?void 0:v.res);return[v.code,v.msg,v.data,e,v==null?void 0:v.res]}).then(v=>((x=="#"||h&&w==="GET")&&f(this,M)[x].set(l,v),v)).then(v=>f(this,D).get(l).forEach(([a,s])=>a(v))).catch(v=>(f(this,M)[x].delete(l),f(this,D).get(l).forEach(([a,s])=>s(v instanceof ee?v:te(-1,v,null,e))))).finally(()=>f(this,D).delete(l)))});return new Proxy(V,{get:(j,C)=>(...v)=>j.then(a=>te(...a)[C](...v)).catch(a=>{if(!a instanceof ee)throw a;return a[C](...v)})})});P(this,"get",(e,r={})=>this.request({url:e,method:"GET",params:r}));P(this,"post",(e,r={},i={})=>this.request({url:e,method:"POST",data:r,params:i}));P(this,"put",(e,r={},i={})=>this.request({url:e,method:"PUT",data:r,params:i}));P(this,"del",(e,r={},i={})=>this.request({url:e,method:"DELETE",data:r,params:i}));P(this,"download",(e,r)=>{});P(this,"cache",(e,r={})=>this.request({url:e,method:"GET",params:r},!0));P(this,"list",(e,r={})=>this.get(`/api/${e}/${this.encode(r)}`));P(this,"getAppInfo",()=>{});P(this,"refreshCache",(e=!1)=>{e&&f(this,M)["#"].clear(),f(this,M).$.clear()});P(this,"decode",(e="",r={})=>{if(e===void 0||e===""||e===null)return r;try{const i=f(this,K).decode(e).buffer;return ie.unpack(he(i))}catch{return console.warn("core.decode",e),r}});P(this,"encode",(e={})=>{const r=JSON.stringify(e,(i,h)=>h===void 0?null:h);return f(this,K).encode(fe(ie.pack(r)))});q(this,Z,e),q(this,H,i),q(this,K,ce(r))}}H=new WeakMap,K=new WeakMap,Z=new WeakMap,M=new WeakMap,D=new WeakMap;const ue=re;re.onUnhandledRejection=t=>{var e,r,i,h,w,l;t!=null&&t.IS_ECHO_MSG?((e=t==null?void 0:t.$toast)==null||e.call(t),(r=t==null?void 0:t.preventDefault)==null||r.call(t)):(i=t.reason)!=null&&i.IS_ECHO_MSG&&((w=(h=t.reason)==null?void 0:h.$toast)==null||w.call(h),(l=t==null?void 0:t.preventDefault)==null||l.call(t))},re.onToast=(t,e)=>[0,1].includes(t)?console.log(e):console.warn(e),p.default=ue,p.signature=oe,p.str2uint8array=fe,p.trim=ye,p.uint8array2str=he,Object.defineProperties(p,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); diff --git a/package.json b/package.json new file mode 100644 index 0000000..3c2eddc --- /dev/null +++ b/package.json @@ -0,0 +1,28 @@ +{ + "name": "ff-request", + "private": true, + "version": "0.9.1", + "type": "module", + "files": [ + "dist" + ], + "main": "./dist/ff-request.umd.cjs", + "module": "./dist/ff-request.js", + "exports": { + "import": "./dist/ff-request.js", + "require": "./dist/ff-request.umd.cjs" + }, + "scripts": { + "dev": "vite", + "build": "vite build" + }, + "dependencies": { + "base-x": "^4.0.0", + "jsonpack": "^1.1.5", + "text-encoder": "^0.0.4" + }, + "devDependencies": { + "rollup-plugin-copy": "^3.5.0", + "vite": "^5.0.10" + } +}