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))
28 setError(error.toString());
38 return this._fetchJSON(params.endpoint, params.headers, {
45 return this._fetchJSON(params.endpoint, params.headers, {
47 body: params.body ? JSON.stringify(params.body) : undefined,
53 return this._fetchJSON(params.endpoint, params.headers, {
55 body: params.body ? JSON.stringify(params.body) : undefined,
61 return this._fetchJSON(params.endpoint, params.headers, {
70 this._fetchJSON(params.endpoint, params.headers, 1).then(
73 res = response.headers.get("X-Total-Count");
77 setError(error.toString());
83 checkError(response, return_response = 0) {
84 if (response.status >= 200 && response.status <= 299) {
85 return return_response ? response : response.json();
87 console.log("Server returned an error:");
88 console.log(response);
89 throw Error("%s (%s)".format(response.statusText, response.status));
94 export default HttpClient;