-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (177 loc) · 4.7 KB
/
index.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// @ts-check
var URI = require('urijs');
export const STATUS = {
OK: 200,
UNAUTHORIZED: 401,
NOT_FOUND: 404,
};
const METHODS = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
};
const JSON_RESPONSE_TYPE = 'application/json';
/**
* Class for making AJAX calls
*/
export class Api {
constructor() {
/** @type string */
this.apiHost = '';
/** @type string */
this.AUTH_TOKEN_HEADER = 'Arachne-Auth-Token';
}
get headers() {
return {
Accept: JSON_RESPONSE_TYPE,
};
}
/**
* Sets the header to be sent in every AJAX call. This header will contain the token
* @param {string} header
*/
setAuthTokenHeader(header) {
this.AUTH_TOKEN_HEADER = header;
}
/**
* Sets the host for AJAX calls
* @param {string} url
*/
setApiHost(url) {
this.apiHost = url;
}
getUserToken() {
throw new Error('Replace this interface with implementation');
}
/**
*
* @param {function} getUserToken
*/
setUserTokenGetter(getUserToken) {
this.getUserToken = getUserToken;
return this;
}
/**
* Callback to be called when AJAX status is 401 Unauthorized
* @callback
*/
handleUnauthorized() {
throw new Error('Replace this interface with implementation');
}
/**
* Sets the unauthorized callback
* @param {function} handler
*/
setUnauthorizedHandler(handler) {
this.handleUnauthorized = handler;
return this;
}
handleUnexpectedError() {
alert('Oooops!.. Something went wrong :(');
}
getHeaders(requestUrl) {
const headers = Object.assign({}, this.headers);
const token = this.getUserToken();
if (token) {
headers[this.AUTH_TOKEN_HEADER] = token;
}
return headers;
}
/**
* Checks HTTP status for errors.
* @param { {[x: string]: any} } response
* @return {boolean}
*/
checkStatusError(response) {
const status = response.status;
if (status >= 200 && status < 300) {
return true;
}
switch (status) {
case STATUS.UNAUTHORIZED:
this.handleUnauthorized(response.json);
break;
default:
this.handleUnexpectedError();
break;
}
return false;
}
sendRequest(method, path, payload, callback) {
const params = {
method,
headers: this.getHeaders(path),
};
if (payload && payload instanceof FormData) {
params.body = payload;
// NOTE:
// Do not set 'Content-Type' - browser will automatically do this.
// Problem is in a 'boundary'.
// http://stackoverflow.com/questions/39280438/fetch-missing-boundary-in-multipart-form-data-post
} else if (payload) {
params.body = JSON.stringify(payload);
params.headers['Content-Type'] = JSON_RESPONSE_TYPE;
}
const fullpath = this.apiHost + path;
return fetch(fullpath, params)
.then(res => {
return res.text()
.then(text => this.parseResponse(text))
.then(data => this.sendResult(res, data))
})
.then(res => this.afterRequestHook(res, method, path, callback));
}
/**
* Performs initial sanitizing of the ajax response
* @param {string} text text returned by ajax
* @returns any
*/
parseResponse(text) {
// Protection from empty response
return text ? JSON.parse(text) : {};
}
/**
* Creates structured object to be used in callback
* @param {object} res response object returned by fetch
* @param {any} parsedResponse anything returned by parseResponse
*/
sendResult(res, parsedResponse) {
return { ok: res.ok, status: res.status, json: parsedResponse };
}
afterRequestHook(res, method, path, callback) {
if (this.checkStatusError(res)) {
if (typeof callback === 'function') {
callback(res.json);
}
}
return res.json;
}
doGet(path, payload = {}, callback) {
// Path with attached GET params
let pathWithParams;
// Callback, taking in account function overloads
let resolvedCb;
if (typeof payload === 'function') {
// If used function overload: doGet(path, callback)
pathWithParams = path;
resolvedCb = payload;
} else {
// If used full-version, with passed GET params
const uri = new URI(path);
uri.setSearch(payload);
pathWithParams = uri.toString();
resolvedCb = callback;
}
return this.sendRequest(METHODS.GET, pathWithParams, null, resolvedCb);
}
doPost(path, payload, callback) {
return this.sendRequest(METHODS.POST, path, payload, callback);
}
doPut(path, payload, callback) {
return this.sendRequest(METHODS.PUT, path, payload, callback);
}
doDelete(path, payload, callback) {
return this.sendRequest(METHODS.DELETE, path, payload, callback);
}
}