-
Notifications
You must be signed in to change notification settings - Fork 17
/
RequestService.js
116 lines (106 loc) · 3.68 KB
/
RequestService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
* Used by Widget.WebService
* Service used to make authentificate HTTP requests (with a token)
* Wiki : https://github.com/MEPP-team/UD-Viz/wiki/Request-Service#request-service
*/
export function RequestService() {
// eslint-disable-next-line no-unused-expressions
this.authenticationService;
this.useAuthentication = false;
// eslint-disable-next-line func-names
this.initialize = function () {};
/**
* @param {string} method The HTTP method
* @param {string} url The requested URL
* @param {FormData | string} body The request body
* @param {boolean} authenticate Set to true if you want to authenticate
* @returns {Promise} The request promise
* @deprecated Prefer using `RequestService.request` instead.
* // eslint-disable-next-line valid-jsdoc
*/
// eslint-disable-next-line func-names
this.send = function (method, url, body = '', authenticate = true) {
return this.request(method, url, {
// eslint-disable-next-line object-shorthand
body: body,
// eslint-disable-next-line object-shorthand
authenticate: authenticate,
});
};
/**
* Performs an HTTP request.
*
* @async
* @param {string} method The HTTP method. Accepted methods include `GET`,
* `DELETE`, `POST` and `PUT`.
* @param {string} url The requested URL.
* @param {object} [options] A dictionary of optional parameters. These
* options include the following :
* @param {FormData | string} [options.body] The request body
* @param {boolean} [options.authenticate] Set to `false` if you don't want
* the request to use authentication.
* @param {string} [options.responseType] The expected
* response type.
* @param {Object<string, string>} [options.urlParameters] A dictionary of
* URL parameters.
* @returns {Promise<XMLHttpRequest>} Request promise
*/
this.request = (method, url, options = {}) => {
const args = options || {};
const body = args.body || '';
let authenticate =
args.authenticate !== null && args.authenticate !== undefined
? args.authenticate
: true;
if (authenticate === 'auto') {
authenticate = !!window.sessionStorage.getItem('user.token');
}
const responseType = args.responseType || null;
const urlParameters = args.urlParameters || null;
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
if (urlParameters) {
// eslint-disable-line no-extra-boolean-cast
url += '?';
for (const [paramKey, paramValue] of Object.entries(urlParameters)) {
url += `${encodeURIComponent(paramKey)}=${encodeURIComponent(
paramValue
)}&`;
}
}
req.open(method, url, true);
if (this.useAuthentication && authenticate) {
const token = window.sessionStorage.getItem('user.token');
if (token === null) {
reject(new AuthNeededError());
return;
}
req.setRequestHeader('Authorization', `Bearer ${token}`);
}
if (responseType) {
// eslint-disable-line no-extra-boolean-cast
req.responseType = responseType;
}
req.send(body);
req.onload = () => {
if (req.status >= 200 && req.status < 300) {
resolve(req);
} else {
reject(req.responseText);
}
};
});
};
// eslint-disable-next-line func-names
this.setAuthenticationService = function (authenticationService) {
this.authenticationService = authenticationService;
this.useAuthentication = true;
};
this.initialize();
}
export class AuthNeededError extends Error {
constructor() {
super('Login needed for this request');
this.name = 'AuthNeededError';
}
}