Bug 32925: Rename isSubmitting with submitting
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / js / vue / fetch / http-client.js
1 import { setError, submitting, submitted } 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         mark_submitting = false,
17     ) {
18         let res, error;
19         if ( mark_submitting) submitting()
20         await fetch(this._baseURL + endpoint, {
21             ...options,
22             headers: { ...this._headers, ...headers },
23         })
24             .then((response) => this.checkError(response, return_response))
25             .then(
26                 (result) => {
27                     res = result;
28                 },
29                 (err) => {
30                     error = err;
31                     setError(err.toString());
32                 }
33             )
34             .catch((err) => {
35                 error = err;
36                 setError(err);
37             }).then(() => {
38               if (mark_submitting) submitted()})
39             ;
40
41         if (error) throw Error(error);
42
43         return res;
44     }
45
46     get(params = {}) {
47         return this._fetchJSON(params.endpoint, params.headers, {
48             ...params.options,
49             method: "GET",
50         });
51     }
52
53     post(params = {}) {
54         const body = params.body
55             ? typeof str === "string"
56                 ? params.body
57                 : JSON.stringify(params.body)
58             : undefined;
59         return this._fetchJSON(params.endpoint, params.headers, {
60             ...params.options,
61             body,
62             method: "POST",
63         }, false, true);
64     }
65
66     put(params = {}) {
67         const body = params.body
68             ? typeof str === "string"
69                 ? params.body
70                 : JSON.stringify(params.body)
71             : undefined;
72         return this._fetchJSON(params.endpoint, params.headers, {
73             ...params.options,
74             body,
75             method: "PUT",
76         }, false, true);
77     }
78
79     delete(params = {}) {
80         return this._fetchJSON(
81             params.endpoint,
82             params.headers,
83             {
84                 parseResponse: false,
85                 ...params.options,
86                 method: "DELETE",
87             },
88             true, true
89         );
90     }
91
92     count(params = {}) {
93         let res;
94         return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
95             (response) => {
96                 if (response) {
97                     return response.headers.get("X-Total-Count");
98                 }
99             },
100             (error) => {
101                 setError(error.toString());
102             }
103         );
104     }
105
106     patch(params = {}) {
107         const body = params.body
108             ? typeof str === "string"
109                 ? params.body
110                 : JSON.stringify(params.body)
111             : undefined;
112         return this._fetchJSON(params.endpoint, params.headers, {
113             ...params.options,
114             body,
115             method: "PATCH",
116         });
117     }
118
119     checkError(response, return_response = 0) {
120         if (response.status >= 200 && response.status <= 299) {
121             return return_response ? response : response.json();
122         } else {
123             console.log("Server returned an error:");
124             console.log(response);
125             throw Error("%s (%s)".format(response.statusText, response.status));
126         }
127     }
128 }
129
130 export default HttpClient;