-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathhttp.js
129 lines (107 loc) · 3.16 KB
/
http.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
117
118
119
120
121
122
123
124
125
126
127
128
129
define(function(require, exports) {
const ko = require('knockout');
const config = require('appConfig');
const sharedState = require('atlas-state');
const { Api:OHDSIApi, STATUS } = require('ohdsi-api');
const FHIR_JSON_RESPONSE_TYPE = 'application/fhir+json';
const TEXT_RESPONSE_TYPE = 'text/plain';
const EventBus = require('services/EventBus');
class Api extends OHDSIApi {
get headers() {
return {
'User-Language': ko.unwrap(sharedState.locale),
};
}
handleUnexpectedError() {
console.error('Oooops!.. Something went wrong :(');
}
handleNotFoundError(responseJson) {
if (responseJson !== undefined && responseJson.payload !== undefined) {
EventBus.errorMsg(responseJson.payload.message);
}
}
checkStatusError(response) {
const status = response.status;
if (status >= 200 && status < 300) {
return true;
}
switch (status) {
case STATUS.NOT_FOUND:
this.handleNotFoundError(response.json);
break;
case STATUS.UNAUTHORIZED:
this.handleUnauthorized(response.json);
break;
default:
this.handleUnexpectedError();
break;
}
return false;
}
isSecureUrl(url) {
var authProviders = config.authProviders.reduce(function(result, current) {
result[config.api.url + current.url] = current;
return result;
}, {});
return !authProviders[url] && url.startsWith(config.api.url);
}
getHeaders(requestUrl) {
if (this.isSecureUrl(requestUrl)) {
const headers = super.getHeaders();
headers['Action-Location'] = location;
return headers;
}
return {};
}
sendRequest(method, path, payload) {
const data = payload instanceof FormData
? payload
: ko.toJS(payload);
return super.sendRequest(method, path, data);
}
sendResult(res, parsedResponse) {
return {
...super.sendResult(res, parsedResponse),
data: parsedResponse,
headers: res.headers,
};
}
afterRequestHook(res) {
if (!this.checkStatusError(res)) {
throw res;
}
return res;
}
}
class PlainTextApi extends Api {
get headers() {
return {
'Accept': TEXT_RESPONSE_TYPE,
'User-Language': ko.unwrap(sharedState.locale),
};
}
parseResponse(text) {
return text;
}
}
class FhirApi extends Api {
getHeaders(requestUrl) {
return {
'Accept': FHIR_JSON_RESPONSE_TYPE,
};
}
parseResponse(json) {
return JSON.parse(json);
}
}
const singletonApi = new Api();
singletonApi.setAuthTokenHeader('Authorization');
const plainTextService = new PlainTextApi();
plainTextService.setAuthTokenHeader(singletonApi.AUTH_TOKEN_HEADER);
plainTextService.setUnauthorizedHandler(() => singletonApi.handleUnauthorized());
plainTextService.setUserTokenGetter(() => singletonApi.getUserToken());
const fhirService = new FhirApi();
singletonApi.plainTextService = plainTextService;
singletonApi.fhirService = fhirService;
return singletonApi;
});