Bug 32939: Use APIClient to fetch local eHoldings
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / vue / fetch / http-client.js
1 import { setError } from "../messages";
2
3 class HttpClient {
4     constructor(options = {}) {
5         this._baseURL = options.baseURL || "";
6         this._headers = options.headers || {
7             "Content-Type": "application/json;charset=utf-8",
8         };
9     }
10
11     async _fetchJSON(
12         endpoint,
13         headers = {},
14         options = {},
15         return_response = false
16     ) {
17         let res, error;
18         await fetch(this._baseURL + endpoint, {
19             ...options,
20             headers: { ...this._headers, ...headers },
21         })
22             .then((response) => this.checkError(response, return_response))
23             .then(
24                 (result) => {
25                     res = result;
26                 },
27                 (err) => {
28                     error = err;
29                     setError(err.toString());
30                 }
31             )
32             .catch((err) => {
33                 error = err;
34                 setError(err);
35             });
36
37         if (error) throw Error(error);
38
39         return res;
40     }
41
42     get(params = {}) {
43         return this._fetchJSON(params.endpoint, params.headers, {
44             ...params.options,
45             method: "GET",
46         });
47     }
48
49     post(params = {}) {
50         return this._fetchJSON(params.endpoint, params.headers, {
51             ...params.options,
52             body: params.body ? JSON.stringify(params.body) : undefined,
53             method: "POST",
54         });
55     }
56
57     put(params = {}) {
58         return this._fetchJSON(params.endpoint, params.headers, {
59             ...params.options,
60             body: params.body ? JSON.stringify(params.body) : undefined,
61             method: "PUT",
62         });
63     }
64
65     delete(params = {}) {
66         return this._fetchJSON(params.endpoint, params.headers, {
67             parseResponse: false,
68             ...params.options,
69             method: "DELETE",
70         }, true);
71     }
72
73     count(params = {}) {
74         let res;
75         return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
76             (response) => {
77                 if (response) {
78                     return response.headers.get("X-Total-Count");
79                 }
80             },
81             (error) => {
82                 setError(error.toString());
83             }
84         );
85     }
86
87     checkError(response, return_response = 0) {
88         if (response.status >= 200 && response.status <= 299) {
89             return return_response ? response : response.json();
90         } else {
91             console.log("Server returned an error:");
92             console.log(response);
93             throw Error("%s (%s)".format(response.statusText, response.status));
94         }
95     }
96 }
97
98 export default HttpClient;