1 import { setError, submitting, submitted } from "../messages";
4 constructor(options = {}) {
5 this._baseURL = options.baseURL || "";
6 this._headers = options.headers || {
7 "Content-Type": "application/json;charset=utf-8",
15 return_response = false,
16 mark_submitting = false,
19 if ( mark_submitting) submitting()
20 await fetch(this._baseURL + endpoint, {
22 headers: { ...this._headers, ...headers },
24 .then((response) => this.checkError(response, return_response))
31 setError(err.toString());
38 if (mark_submitting) submitted()})
41 if (error) throw Error(error);
47 return this._fetchJSON(params.endpoint, params.headers, {
54 const body = params.body
55 ? typeof str === "string"
57 : JSON.stringify(params.body)
59 return this._fetchJSON(params.endpoint, params.headers, {
67 const body = params.body
68 ? typeof str === "string"
70 : JSON.stringify(params.body)
72 return this._fetchJSON(params.endpoint, params.headers, {
80 return this._fetchJSON(
94 return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
97 return response.headers.get("X-Total-Count");
101 setError(error.toString());
107 const body = params.body
108 ? typeof str === "string"
110 : JSON.stringify(params.body)
112 return this._fetchJSON(params.endpoint, params.headers, {
119 checkError(response, return_response = 0) {
120 if (response.status >= 200 && response.status <= 299) {
121 return return_response ? response : response.json();
123 console.log("Server returned an error:");
124 console.log(response);
125 throw Error("%s (%s)".format(response.statusText, response.status));
130 export default HttpClient;