forked from DuendeArchive/identity-model-oidc-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonService.js
More file actions
55 lines (44 loc) · 1.68 KB
/
JsonService.js
File metadata and controls
55 lines (44 loc) · 1.68 KB
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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import Log from './Log';
import Global from './Global';
export default class JsonService {
constructor(XMLHttpRequestCtor = Global.XMLHttpRequest) {
this._XMLHttpRequest = XMLHttpRequestCtor;
}
getJson(url, token) {
Log.debug("JsonService.getJson", url);
if (!url){
Log.error("No url passed");
throw new Error("url");
}
return new Promise((resolve, reject) => {
var req = new this._XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
Log.debug("HTTP response received, status", req.status);
if (req.status === 200) {
try {
resolve(JSON.parse(req.responseText));
}
catch (e) {
Log.error("Error parsing JSON response", e.message);
reject(e);
}
}
else {
reject(Error(req.statusText + " (" + req.status + ")"));
}
};
req.onerror = function() {
Log.error("network error");
reject(Error("Network Error"));
};
if (token) {
Log.debug("token passed, setting Authorization header");
req.setRequestHeader("Authorization", "Bearer " + token);
}
req.send();
});
}
}