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 },
26 return response.text().then(text => {
29 let json = JSON.parse(text);
32 json.errors.map(e => e.message).join("\n") ||
35 message = response.statusText;
37 throw new Error(message);
40 return return_response ? response : response.json();
50 if (mark_submitting) submitted();
53 if (error) throw Error(error);
59 return this._fetchJSON(params.endpoint, params.headers, {
71 ...(params.params && params.params),
72 ...(params.query && { q: JSON.stringify(params.query) }),
74 return this._fetchJSON(url, params.headers, {
81 const body = params.body
82 ? typeof params.body === "string"
84 : JSON.stringify(params.body)
86 return this._fetchJSON(
100 const body = params.body
101 ? typeof params.body === "string"
103 : JSON.stringify(params.body)
105 return this._fetchJSON(
118 delete(params = {}) {
119 return this._fetchJSON(
123 parseResponse: false,
134 return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
137 return response.headers.get("X-Total-Count");
145 const body = params.body
146 ? typeof params.body === "string"
148 : JSON.stringify(params.body)
150 return this._fetchJSON(params.endpoint, params.headers, {
158 export default HttpClient;