@volverjs/data
Version:
Repository pattern implementation with a tiny HttpClient based on Fetch API.
107 lines (106 loc) • 3.35 kB
JavaScript
var d = Object.defineProperty;
var m = (c, t, r) => t in c ? d(c, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : c[t] = r;
var s = (c, t, r) => m(c, typeof t != "symbol" ? t + "" : t, r);
import U from "ky";
import { HTTPError as P, TimeoutError as B } from "ky";
import { UrlBuilder as a } from "./UrlBuilder.js";
const l = class l {
constructor(t = {}) {
s(this, "_client");
s(this, "_urlBuilder");
s(this, "_prefixUrl");
s(this, "get", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.get(this.buildUrl(t, e), i);
});
s(this, "post", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.post(
this.buildUrl(t, e),
i
);
});
s(this, "put", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.put(this.buildUrl(t, e), i);
});
s(this, "delete", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.delete(
this.buildUrl(t, e),
i
);
});
s(this, "patch", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.patch(
this.buildUrl(t, e),
i
);
});
s(this, "head", (t, r = {}) => {
const { searchParams: e, ...i } = r;
return this._client.head(
this.buildUrl(t, e),
i
);
});
s(this, "request", (t, r, e = {}) => {
const { abortController: i, ...n } = e, { controller: u, signal: o } = l.createAbortController(i);
return {
responsePromise: this[t](r, { signal: o, ...n }),
abort: (p) => u.abort(p),
signal: o
};
});
s(this, "fetch", (t) => this._client(t));
s(this, "extend", (t = {}) => {
const { searchParams: r, ...e } = t;
this._client = this._client.extend(e), this._prefixUrl = e.prefixUrl ?? this._prefixUrl, this._urlBuilder.extend(r ?? {});
});
s(this, "clone", (t = {}) => {
const { searchParams: r, ...e } = t;
return new l({
client: this._client.extend(e),
urlBuilder: this._urlBuilder.clone(r)
});
});
s(this, "setBearerToken", (t, { headerName: r = "Authorization", prefix: e = "Bearer" } = {}) => {
this.extend({
headers: {
[r]: t ? `${e} ${t}` : void 0
}
});
});
const { client: r, urlBuilder: e, searchParams: i, ...n } = t;
this._client = r ?? U.create(n), this._urlBuilder = e ?? new a(i), this._prefixUrl = n.prefixUrl;
}
get stop() {
return this._client.stop;
}
buildUrl(t, r) {
const e = l.buildUrl(t, r, this._urlBuilder);
return this._prefixUrl && typeof e == "string" && (e != null && e.startsWith("/")) ? e.slice(1) : e;
}
static isUrlTemplate(t) {
return typeof t == "object" && t !== null && ((r) => ["template", "params"].every(
(e) => r.includes(e)
))(Object.keys(t));
}
};
s(l, "buildUrl", (t, r, e) => {
if (l.isUrlTemplate(t)) {
const { template: i, params: n } = t;
return e ? e.build(i, n, r) : a.build(i, n, r);
}
return t;
}), s(l, "createAbortController", (t) => {
const r = t ?? new AbortController(), { signal: e } = r;
return { controller: r, signal: e };
});
let h = l;
export {
P as HTTPError,
h as HttpClient,
B as TimeoutError
};