@volverjs/data
Version:
Repository pattern implementation with a tiny HttpClient based on Fetch API.
235 lines (234 loc) • 6.77 kB
JavaScript
var B = Object.defineProperty;
var D = (e, a, t) => a in e ? B(e, a, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[a] = t;
var A = (e, a, t) => D(e, typeof a != "symbol" ? a + "" : a, t);
import { unref as i, ref as c, readonly as q, computed as G } from "vue";
import { HttpClient as I } from "../HttpClient.js";
import { RepositoryHttp as M } from "../RepositoryHttp.js";
var Y = /* @__PURE__ */ ((e) => (e.loading = "loading", e.error = "error", e.success = "success", e.idle = "idle", e))(Y || {});
function y() {
const e = c(
"idle"
/* idle */
), a = G(
() => e.value === "loading"
/* loading */
), t = G(
() => e.value === "error"
/* error */
), r = G(
() => e.value === "success"
/* success */
);
return {
status: e,
isLoading: a,
isError: t,
isSuccess: r
};
}
const S = /* @__PURE__ */ new Map(), O = "global";
class k extends I {
constructor(t) {
super(t);
A(this, "_scope");
this._scope = t.scope;
}
get scope() {
return this._scope;
}
install(t, { globalName: r = "vvHttp" } = {}) {
if (t.config.globalProperties[`$${r}`])
throw new Error(`globalName already exist: ${r}`);
t.config.globalProperties[`$${r}`] = this;
}
}
function R({
scope: e = O,
...a
} = {}) {
if (S.has(e))
throw new Error(`httpClient with scope ${e} already exist`);
const t = new k({ ...a, scope: e });
return S.set(e, t), t;
}
function T(e) {
if (e === O)
throw new Error("You cannot remove httpClient global instance");
if (!S.has(e))
throw new Error(`httpClient with scope ${e} not exist`);
return S.delete(e);
}
function z(e = O) {
const a = S.get(e);
if (!a)
throw new Error("HttpClient instance not found");
const t = (r, d, _ = {}) => {
const { status: H, isLoading: j, isError: E, isSuccess: f } = y(), o = i(_).immediate ?? !0, s = c(), g = c(), w = c(), b = (v = i(d), u = i(_)) => {
H.value = "loading", s.value = void 0, g.value = void 0;
const { responsePromise: l, abort: p, signal: m } = a.request(
r,
v,
u
);
return l.then((n) => (w.value = n, n.json())).then((n) => {
g.value = n, H.value = "success";
}).catch((n) => {
if (!m.aborted) {
s.value = n, H.value = "error";
return;
}
H.value = "idle";
}), { responsePromise: l, abort: p, signal: m };
};
return {
execute: b,
isLoading: j,
isSuccess: f,
isError: E,
error: q(s),
data: g,
response: w,
...o ? b() : {}
};
};
return {
client: a,
request: t,
requestGet: (r, d = {}) => t("get", r, d),
requestPost: (r, d = {}) => t("post", r, d),
requestPut: (r, d = {}) => t("put", r, d),
requestDelete: (r, d = {}) => t("delete", r, d),
requestHead: (r, d = {}) => t("head", r, d),
requestPatch: (r, d = {}) => t("patch", r, d)
};
}
function U(e, a) {
const { client: t } = z(a == null ? void 0 : a.httpClientScope), r = new M(
t,
e,
a
);
return {
repository: r,
read: (E, f = {}) => {
const { status: o, isLoading: s, isError: g, isSuccess: w } = y(), b = i(f).immediate ?? !0, v = c(), u = c(), l = c(), p = c(), m = (n = i(E), P = i(f)) => {
o.value = "loading", v.value = void 0, l.value = void 0, u.value = void 0;
const { abort: L, responsePromise: x } = r.read(
n,
P
);
return x.then((C) => {
if (u.value = C.data, l.value = C.item, p.value = C.metadata, C.aborted) {
o.value = "idle";
return;
}
o.value = "success";
}).catch((C) => {
v.value = C, o.value = "error";
}), { abort: L, responsePromise: x };
};
return {
execute: m,
isLoading: s,
isSuccess: w,
isError: g,
error: q(v),
data: u,
item: l,
metadata: p,
...b ? m() : {}
};
},
create: (E, f = {}, o = {}) => {
const { status: s, isLoading: g, isError: w, isSuccess: b } = y(), v = i(o).immediate ?? !0, u = c(), l = c(), p = c(), m = c(), n = (P = i(E), L = i(f), x = i(o)) => {
s.value = "loading", u.value = void 0, p.value = void 0, l.value = void 0;
const { abort: C, responsePromise: $ } = r.create(
P,
L,
x
);
return $.then((h) => {
if (l.value = h.data, p.value = h.item, m.value = h.metadata, h.aborted) {
s.value = "idle";
return;
}
s.value = "success";
}).catch((h) => {
u.value = h, s.value = "error";
}), { abort: C, responsePromise: $ };
};
return {
execute: n,
isLoading: g,
isSuccess: b,
isError: w,
error: q(u),
data: l,
metadata: m,
...v ? n() : {}
};
},
update: (E, f = {}, o = {}) => {
const { status: s, isLoading: g, isError: w, isSuccess: b } = y(), v = i(o).immediate ?? !0, u = c(), l = c(), p = c(), m = c(), n = (P = i(E), L = i(f), x = i(o)) => {
s.value = "loading", u.value = void 0, p.value = void 0, l.value = void 0;
const { abort: C, responsePromise: $ } = r.update(
P,
L,
x
);
return $.then((h) => {
if (l.value = h.data, p.value = h.item, m.value = h.metadata, h.aborted) {
s.value = "idle";
return;
}
s.value = "success";
}).catch((h) => {
u.value = h, s.value = "error";
}), { abort: C, responsePromise: $ };
};
return {
execute: n,
isLoading: g,
isSuccess: b,
isError: w,
error: q(u),
data: l,
metadata: m,
...v ? n() : {}
};
},
remove: (E, f = {}) => {
const { status: o, isLoading: s, isError: g, isSuccess: w } = y(), b = i(f).immediate ?? !0, v = c(), u = (l = i(E), p = i(f)) => {
o.value = "loading", v.value = void 0;
const { abort: m, responsePromise: n } = r.remove(
l,
p
);
return n.then((P) => {
if (P.aborted) {
o.value = "idle";
return;
}
o.value = "success";
}).catch((P) => {
v.value = P, o.value = "error";
}), { abort: m, responsePromise: n };
};
return {
execute: u,
isLoading: s,
isSuccess: w,
isError: g,
error: q(v),
...b ? u() : {}
};
}
};
}
export {
Y as HttpRequestStatus,
R as createHttpClient,
T as removeHttpClient,
z as useHttpClient,
U as useRepositoryHttp
};