@deltares/fews-web-oc-utils
Version:
Util Library for common Web OC functionality
73 lines (72 loc) • 1.97 kB
JavaScript
//#region src/restservice/requestOptions.ts
var e = class {
_mode = "cors";
_relativeUrl = !0;
get relativeUrl() {
return this._relativeUrl;
}
set relativeUrl(e) {
this._relativeUrl = e;
}
get mode() {
return this._mode;
}
set mode(e) {
this._mode = e;
}
}, t = class {
async parse(e) {
return await e.json();
}
}, n = class {
webserviceUrl;
transformRequest;
constructor(e, t) {
if (this.webserviceUrl = e, t === void 0) {
async function e(e) {
return e;
}
this.transformRequest = e;
} else this.transformRequest = t;
}
async getDataWithParser(e, t, n) {
let r = t.relativeUrl ? this.webserviceUrl + e : e, i = {}, a = {};
a.method = "GET";
let o = new Request(r, a), s = await fetch(await this.transformRequest(o));
if (!s.ok) throw Error("Fetch Error", { cause: s });
return await this.processResponse(i, s, r, n);
}
async postDataWithParser(e, t, n, r, i) {
let a = t.relativeUrl ? this.webserviceUrl + e : e, o = {}, s = {};
s.method = "POST", s.body = r, s.headers = i;
let c = new Request(a, s), l = await fetch(await this.transformRequest(c));
if (!l.ok) throw Error("Fetch Error", { cause: l });
return await this.processResponse(o, l, a, n);
}
async getData(n) {
let r = new e();
return r.relativeUrl = !n.startsWith("http"), this.getDataWithParser(n, r, new t());
}
async postData(n, r, i) {
let a = new e();
return a.relativeUrl = !n.startsWith("http"), this.postDataWithParser(n, a, new t(), r, {
"Content-Type": "application/json",
...i
});
}
async processResponse(e, t, n, r) {
e.responseCode = t.status, e.contentType = t.headers.get("content-type");
try {
e.data = await r.parse(t);
} catch (e) {
throw Error(`Parse Error for response ${n}.`, { cause: e });
}
return e;
}
}, r = class {
async parse(e) {
return await e.text();
}
};
//#endregion
export { t as DefaultParser, n as PiRestService, r as PlainTextParser, e as RequestOptions };