1 import { setError } 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
18 await fetch(this._baseURL + endpoint, {
20 headers: { ...this._headers, ...headers },
22 .then((response) => this.checkError(response, return_response))
29 setError(err.toString());
37 if (error) throw Error(error);
43 return this._fetchJSON(params.endpoint, params.headers, {
50 return this._fetchJSON(params.endpoint, params.headers, {
52 body: params.body ? JSON.stringify(params.body) : undefined,
58 return this._fetchJSON(params.endpoint, params.headers, {
60 body: params.body ? JSON.stringify(params.body) : undefined,
66 return this._fetchJSON(params.endpoint, params.headers, {
75 return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
78 return response.headers.get("X-Total-Count");
82 setError(error.toString());
87 checkError(response, return_response = 0) {
88 if (response.status >= 200 && response.status <= 299) {
89 return return_response ? response : response.json();
91 console.log("Server returned an error:");
92 console.log(response);
93 throw Error("%s (%s)".format(response.statusText, response.status));
98 export default HttpClient;