UNPKG

@gaiot/apis

Version:

aiot apis

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