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());
39 if (mark_submitting) submitted();
42 if (error) throw Error(error);
48 return this._fetchJSON(params.endpoint, params.headers, {
60 ...(params.query && { q: JSON.stringify(params.query) }),
62 return this._fetchJSON(url, params.headers, {
69 const body = params.body
70 ? typeof params.body === "string"
72 : JSON.stringify(params.body)
74 return this._fetchJSON(
88 const body = params.body
89 ? typeof params.body === "string"
91 : JSON.stringify(params.body)
93 return this._fetchJSON(
106 delete(params = {}) {
107 return this._fetchJSON(
111 parseResponse: false,
122 return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
125 return response.headers.get("X-Total-Count");
129 setError(error.toString());
135 const body = params.body
136 ? typeof params.body === "string"
138 : JSON.stringify(params.body)
140 return this._fetchJSON(params.endpoint, params.headers, {
147 checkError(response, return_response = 0) {
148 if (response.status >= 200 && response.status <= 299) {
149 return return_response ? response : response.json();
151 console.log("Server returned an error:");
152 console.log(response);
153 throw Error("%s (%s)".format(response.statusText, response.status));
158 export default HttpClient;