rxjs-http-client
Version:
A simple to use http client built on ES6 fetch and RxJS
155 lines (154 loc) • 4.39 kB
JavaScript
var l = Object.defineProperty;
var b = (s, e, t) => e in s ? l(s, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : s[e] = t;
var r = (s, e, t) => b(s, typeof e != "symbol" ? e + "" : e, t);
import { mergeMap as w, of as x, throwError as y, from as o, map as p } from "rxjs";
var h = /* @__PURE__ */ ((s) => (s.POST = "POST", s.GET = "GET", s.PUT = "PUT", s.PATCH = "PATCH", s.DELETE = "DELETE", s))(h || {});
class u {
static for(e, t) {
return {
cache: e.cache,
mode: e.mode,
credentials: e.credentials,
headers: e.headers,
redirect: e.redirect,
referrer: e.referrer,
method: t,
body: this.formatBody(e, t)
};
}
static formatBody(e, t) {
var c;
return t === h.GET || !e.body ? null : ((c = e.body) == null ? void 0 : c.constructor) === Object ? JSON.stringify(e.body) : e.body;
}
}
function f() {
return function(s) {
return s.pipe(
w((e) => e.ok ? x(e) : y(() => e))
);
};
}
class _ {
constructor(e) {
r(this, "_interceptors");
this._interceptors = e;
}
execute(e) {
return this._interceptors.length > 0 ? this._interceptors.reduce((t, c) => c.intercept(t), e) : e;
}
}
const T = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;
class d {
constructor(e, t) {
r(this, "url");
r(this, "headers");
r(this, "mode");
r(this, "cache");
r(this, "credentials");
r(this, "redirect");
r(this, "referrer");
r(this, "body");
this.url = new RegExp(T).test(e) ? e : `${window.location.origin}${e}`, this.mode = t.mode || void 0, this.cache = t.cache || void 0, this.credentials = t.credentials || void 0, this.headers = t.headers || {}, this.redirect = t.redirect || void 0, this.referrer = t.referrer || void 0, this.body = t.body || void 0;
}
clone() {
return new d(this.url, {
body: this.body,
cache: this.cache,
mode: this.mode,
credentials: this.credentials,
headers: this.headers,
redirect: this.redirect,
referrer: this.referrer
});
}
}
class a {
constructor(e) {
r(this, "headers");
r(this, "ok");
r(this, "redirected");
r(this, "status");
r(this, "statusText");
r(this, "type");
r(this, "url");
r(this, "_response");
this._response = e, this.headers = e.headers, this.ok = e.ok, this.redirected = e.redirected, this.status = e.status, this.statusText = e.statusText, this.type = e.type, this.url = e.url;
}
arrayBuffer() {
return o(this._response.arrayBuffer());
}
blob() {
return o(this._response.blob());
}
formData() {
return o(this._response.formData());
}
json() {
return o(this._response.json());
}
text() {
return o(this._response.text());
}
clone() {
return new a(this._response.clone());
}
}
class E {
constructor(e = [], t = []) {
r(this, "_reqInterceptors");
r(this, "_resInterceptors");
this._reqInterceptors = new _(e), this._resInterceptors = new _(t);
}
get(e, t = {}) {
const c = this._reqInterceptors.execute(new d(e, t)), i = u.for(c, h.GET);
return o(fetch(e, i)).pipe(
p((n) => this._resInterceptors.execute(new a(n))),
f()
);
}
post(e, t) {
const c = this._reqInterceptors.execute(new d(e, t)), i = u.for(c, h.POST);
return o(fetch(e, i)).pipe(
p((n) => this._resInterceptors.execute(new a(n))),
f()
);
}
put(e, t) {
const c = this._reqInterceptors.execute(new d(e, t)), i = u.for(c, h.PUT);
return o(fetch(e, i)).pipe(
p((n) => this._resInterceptors.execute(new a(n))),
f()
);
}
patch(e, t) {
const c = this._reqInterceptors.execute(new d(e, t)), i = u.for(c, h.PATCH);
return o(fetch(e, i)).pipe(
p((n) => this._resInterceptors.execute(new a(n))),
f()
);
}
delete(e, t) {
const c = this._reqInterceptors.execute(new d(e, t)), i = u.for(c, h.DELETE);
return o(fetch(e, i)).pipe(
p((n) => this._resInterceptors.execute(new a(n))),
f()
);
}
}
class O {
constructor() {
r(this, "mode");
r(this, "cache");
r(this, "credentials");
r(this, "headers");
r(this, "redirect");
r(this, "referrer");
r(this, "body");
}
}
export {
d as HttpRequest,
O as HttpRequestConfig,
a as HttpResponse,
E as RxJSHttpClient
};