Bug 33625: Pretty .js files for vue
[koha.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             })
38             .then(() => {
39                 if (mark_submitting) submitted();
40             });
41
42         if (error) throw Error(error);
43
44         return res;
45     }
46
47     get(params = {}) {
48         return this._fetchJSON(params.endpoint, params.headers, {
49             ...params.options,
50             method: "GET",
51         });
52     }
53
54     getAll(params = {}) {
55         let url =
56             params.endpoint +
57             "?" +
58             new URLSearchParams({
59                 _per_page: -1,
60                 ...(params.query && { q: JSON.stringify(params.query) }),
61             });
62         return this._fetchJSON(url, params.headers, {
63             ...params.options,
64             method: "GET",
65         });
66     }
67
68     post(params = {}) {
69         const body = params.body
70             ? typeof params.body === "string"
71                 ? params.body
72                 : JSON.stringify(params.body)
73             : undefined;
74         return this._fetchJSON(
75             params.endpoint,
76             params.headers,
77             {
78                 ...params.options,
79                 body,
80                 method: "POST",
81             },
82             false,
83             true
84         );
85     }
86
87     put(params = {}) {
88         const body = params.body
89             ? typeof params.body === "string"
90                 ? params.body
91                 : JSON.stringify(params.body)
92             : undefined;
93         return this._fetchJSON(
94             params.endpoint,
95             params.headers,
96             {
97                 ...params.options,
98                 body,
99                 method: "PUT",
100             },
101             false,
102             true
103         );
104     }
105
106     delete(params = {}) {
107         return this._fetchJSON(
108             params.endpoint,
109             params.headers,
110             {
111                 parseResponse: false,
112                 ...params.options,
113                 method: "DELETE",
114             },
115             true,
116             true
117         );
118     }
119
120     count(params = {}) {
121         let res;
122         return this._fetchJSON(params.endpoint, params.headers, {}, 1).then(
123             response => {
124                 if (response) {
125                     return response.headers.get("X-Total-Count");
126                 }
127             },
128             error => {
129                 setError(error.toString());
130             }
131         );
132     }
133
134     patch(params = {}) {
135         const body = params.body
136             ? typeof params.body === "string"
137                 ? params.body
138                 : JSON.stringify(params.body)
139             : undefined;
140         return this._fetchJSON(params.endpoint, params.headers, {
141             ...params.options,
142             body,
143             method: "PATCH",
144         });
145     }
146
147     checkError(response, return_response = 0) {
148         if (response.status >= 200 && response.status <= 299) {
149             return return_response ? response : response.json();
150         } else {
151             console.log("Server returned an error:");
152             console.log(response);
153             throw Error("%s (%s)".format(response.statusText, response.status));
154         }
155     }
156 }
157
158 export default HttpClient;