UNPKG

vexip-ui

Version:

A Vue 3 UI library, Highly customizability, full TypeScript, performance pretty good

53 lines (52 loc) 1.53 kB
import { noop as o, has as c, isDefined as l } from "@vexip-ui/utils"; function u(e, r) { const s = `fail to post ${e} ${r.status}'`, a = new Error(s); return a.response = d(r), a.url = e, a.status = r.status, a.method = "POST", a; } function d(e) { const r = e.responseText || e.response; if (!r) return r; try { return JSON.parse(r); } catch { return r; } } function h(e) { if (typeof XMLHttpRequest > "u") return o; const r = new XMLHttpRequest(), s = e.url; r.upload && e.onProgress && (r.upload.onprogress = (t) => { let n = 0; t.total > 0 && (n = t.loaded / t.total * 100), e.onProgress(n); }); const a = new FormData(); if (e.data) { const t = e.data; Object.keys(t).forEach((n) => { a.append(n, t[n]); }); } a.append(e.field || "file", e.file), e.file.path && a.append(e.pathField || "path", e.file.path), e.onError && (r.onerror = () => { e.onError(u(s, r)); }), r.onload = () => { var n; if (r.status < 200 || r.status >= 300) { e.onError && e.onError(u(s, r)); return; } const t = d(r); (n = e.onSuccess) == null || n.call(e, t); }, e.onAbort && (r.onabort = () => { e.onAbort(); }), r.open("POST", s, !0), e.withCredentials && "withCredentials" in r && (r.withCredentials = !0); const f = e.headers ?? {}; return Object.keys(f).forEach((t) => { c(f, t) && l(f[t]) && r.setRequestHeader(t, f[t]); }), r.send(a), () => r.abort(); } export { h as upload }; //# sourceMappingURL=request.mjs.map