PluginProbe
seQura / 3.2.2
seQura v3.2.2
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / assets / js / src / core / AjaxService.js

AjaxService.js in seQura 3.2.2, at assets/js/src/core/AjaxService.js

158 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 if (!window.SequraFE) {
2 window.SequraFE = {};
3 }
4 if (!window.SequraFE.customHeader) {
5 window.SequraFE.customHeader = {};
6 }
7
8 /**
9 * @typedef AjaxServiceType
10 * @property {(url:string, errorCallback?: (error?: Record<string, any>) => Promise<any> | any, fallthrough?:
11 * boolean) => Promise<any>} get
12 * @property {(url:string, data?: any, customHeader?: Record<string, string>, errorCallback: (error?:
13 * Record<string, any>) => Promise<void>) => Promise<any>} post
14 * @property {(url:string, data?: any, customHeader?: Record<string, string>, errorCallback: (error?:
15 * Record<string, any>) => Promise<void>) => Promise<any>} put
16 * @property {(url:string, data?: any, errorCallback?: (error?: Record<string, any>) => Promise<void>) =>
17 * Promise<any>} delete
18 */
19 (function () {
20 /**
21 * Ajax/API service.
22 *
23 * @returns {AjaxServiceType}
24 */
25 const AjaxService = () => {
26 /**
27 * Handles the server response.
28 * @param {Response} response
29 * @param {(error: Record<string, any>) => Promise<void>?} errorCallback
30 * @returns {Record<string, any>}
31 */
32 const handleResponse = (response, errorCallback) => {
33 if (!errorCallback) {
34 errorCallback = SequraFE.responseService.errorHandler;
35 }
36
37 try {
38 if (response.ok) {
39 return response.json();
40 }
41
42 if (response.status === 401 || response.status === 403) {
43 return response.json().then(SequraFE.responseService.unauthorizedHandler);
44 }
45
46 if (response.status === 400) {
47 return response.json().then(errorCallback);
48 }
49 } catch (e) { }
50
51 return errorCallback({ status: response.status, error: response.statusMessage });
52 };
53
54 /**
55 * Performs GET ajax request.
56 *
57 * @param {string} url The URL to call.
58 * @param {(error: Record<string, any>) => Promise<void>?} errorCallback
59 * @param {Record<string, string>?} customHeader
60 */
61 const get = (url, errorCallback, customHeader = {}) => call('GET', url, null, errorCallback, customHeader);
62
63 /**
64 * Performs POST ajax request.
65 *
66 * @param {string} url The URL to call.
67 * @param {Record<string, any>?} data
68 * @param {Record<string, string>?} customHeader
69 * @param {(error: Record<string, any>) => Promise<void>?} errorCallback
70 */
71 const post = (url, data, customHeader, errorCallback) => call('POST', url, data, errorCallback, customHeader);
72
73 /**
74 * Performs PUT ajax request.
75 *
76 * @param {string} url The URL to call.
77 * @param {Record<string, any>} data
78 * @param {Record<string, string>?} customHeader
79 * @param {(error: Record<string, any>) => Promise<void>?} errorCallback
80 */
81 const put = (url, data, customHeader, errorCallback) => call('PUT', url, data, errorCallback, customHeader);
82
83 /**
84 * Performs DELETE ajax request.
85 *
86 * @param {string} url The URL to call.
87 * @param {Record<string, any>?} data
88 * @param {Record<string, string>?} customHeader
89 * @param {(error: Record<string, any>) => Promise<void>?} errorCallback
90 */
91 const del = (url, data, customHeader, errorCallback) => call('DELETE', url, data, errorCallback, customHeader);
92
93 /**
94 * Performs ajax call.
95 *
96 * @param {'GET' | 'POST' | 'PUT' | 'DELETE'} method The HTTP method.
97 * @param {string} url The URL to call.
98 * @param {Record<string, any>?} data The data to send.
99 * @param {(error: Record<string, any>) => Promise<any>?} errorCallback An error callback. If not set, the
100 * default one will be used.
101 * @param {Record<string, string>?} customHeader
102 * @returns {Promise<Record<string, any>>}
103 */
104 const call = (method, url, data, errorCallback, customHeader) => {
105 const callUUID = SequraFE.StateUUIDService.getStateUUID();
106
107 return new Promise((resolve, reject) => {
108 url = url.replace('https:', '');
109 url = url.replace('http:', '');
110
111 let authHeader = {};
112 if (SequraFE?.integration?.authToken) {
113 authHeader = {
114 'Authorization': `Bearer ${SequraFE.integration.authToken}`
115 }
116 }
117
118 const headers = {
119 'Content-Type': 'application/json',
120 ...authHeader,
121 ...(customHeader || {})
122 };
123
124 if (headers['Content-Type'] === 'multipart/form-data') {
125 delete headers['Content-Type'];
126 }
127
128 const body = data
129 ? headers['Content-Type'] === 'application/json'
130 ? JSON.stringify(data)
131 : data
132 : undefined;
133
134 fetch(url, { method, headers, body }).then((response) => {
135 if (callUUID !== SequraFE.StateUUIDService.getStateUUID()) {
136 // Obsolete request. The app has changed the original state (page) that issued the call.
137 // console.log('cancelling an obsolete request', url);
138 return;
139 }
140
141 handleResponse(response, errorCallback).then(resolve).catch(reject);
142 }).catch(reject);
143 });
144 };
145
146 return {
147 get,
148 post,
149 put,
150 delete: del,
151 call,
152 handleResponse
153 };
154 };
155
156 SequraFE.ajaxService = AjaxService();
157 })();
158