UNPKG

@glodon-aiot/apis

Version:

aiot apis

159 lines (158 loc) 4.38 kB
var x = Object.defineProperty; var l = Object.getOwnPropertySymbols; var g = Object.prototype.hasOwnProperty, y = Object.prototype.propertyIsEnumerable; var u = (o, s, t) => s in o ? x(o, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : o[s] = t, c = (o, s) => { for (var t in s || (s = {})) g.call(s, t) && u(o, t, s[t]); if (l) for (var t of l(s)) y.call(s, t) && u(o, t, s[t]); return o; }; var n = (o, s, t) => new Promise((e, r) => { var p = (a) => { try { h(t.next(a)); } catch (d) { r(d); } }, i = (a) => { try { h(t.throw(a)); } catch (d) { r(d); } }, h = (a) => a.done ? e(a.value) : Promise.resolve(a.value).then(p, i); h((t = t.apply(o, s)).next()); }); class R { constructor(s, t) { this.axios = s, this.errorHandler = t; } get(s, t, e) { return n(this, null, function* () { const r = this.axios.get(s, { params: t }); return this.handleResponse(r, e); }); } listPaged(s, t) { return n(this, null, function* () { const e = this.axios.get(s, { params: t }); return this.handleResponse(e); }); } list(s, t) { return n(this, null, function* () { const e = this.axios.get(s, { params: t }); return this.handleResponse(e); }); } delete(s, t) { return n(this, null, function* () { const e = this.axios.delete(s, { params: t }); return this.handleResponse(e); }); } post(s, t, e, r) { return n(this, null, function* () { const p = this.axios.post(s, t, c({ params: e }, r)); return this.handleResponse(p); }); } postForm(s, t, e) { return n(this, null, function* () { const r = this.axios.post(s, t, { params: e, headers: { "Content-Type": "multipart/form-data" } }); return this.handleResponse(r); }); } patch(s, t, e) { return n(this, null, function* () { const r = this.axios.patch(s, t, { params: e }); return this.handleResponse(r); }); } put(s, t) { return n(this, null, function* () { const e = this.axios.put(s, t); return this.handleResponse(e); }); } upload(s, t, e) { return n(this, null, function* () { const r = this.axios.post(s, t, c({}, e)); return this.handleResponse(r); }); } getNonStandard(s, t) { return n(this, null, function* () { const e = this.axios.get(s, t); try { const r = yield e; if (r.status === 200) return r; } catch (r) { throw this.handleError(r); } }); } postNonStandard(s, t, e, r) { return n(this, null, function* () { var h, a; const i = yield this.axios.post(s, t, c({ params: e }, r)); if (i.status === 200) if ((h = i.data) != null && h.code) (a = this.errorHandler) != null && a.onAppStatusError && this.errorHandler.onAppStatusError(i.data.code, i.data.message); else return i.data; }); } handleResponse(s, t) { return n(this, null, function* () { try { const e = yield s; return this.handleAppError(e.data, t).data; } catch (e) { throw this.handleError(e); } }); } handleAppError(s, t) { var e; if (s.code < 200 || s.code >= 300) throw (e = this.errorHandler) != null && e.onAppStatusError ? this.errorHandler.onAppStatusError(s.code, E(s.code, s.message), t) : console.error("未处理应用错误:", s), { isAxiosError: !1, message: s.message }; return s; } // https://github.com/axios/axios#handling-errors handleError(s) { var t, e; return s.response ? (t = this.errorHandler) != null && t.onHttpStatusError && (console.log("HTTP 错误", this.errorHandler), this.errorHandler.onHttpStatusError(s.response.status, "", s.response)) : s.request ? (console.log(s.request), (e = this.errorHandler) != null && e.onNetworkError && this.errorHandler.onNetworkError()) : console.error("axios request config error:", s), new Error(s.message); } } const f = (o) => o >= 10002 && o <= 10012, E = (o, s) => f(o) ? "数据操作错误" : s || "未知接口错误"; export { R as BaseApi, f as needPrettifyMsg, E as prettifyMsg };