-
Notifications
You must be signed in to change notification settings - Fork 27
/
api.js
435 lines (374 loc) · 9.46 KB
/
api.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import _ from 'lodash';
import { create as createDom } from '@nymag/dom';
import { assertUri, uriToUrl, assertUrl } from '../utils/urls';
import store from './store';
import rest from '../utils/rest';
import {
getComponentName,
contentHeader,
contentJSON,
userHeader,
contentText,
refAttr,
componentRoute,
schemaRoute,
listsRoute,
hooksExt,
htmlExt,
editComponentExt,
layoutAttr,
loginRoute
} from '../utils/references';
// note: all api calls should be going through the queue, so they can handle
// the user dropping offline or having issues with their connection,
// as well as to guarantee the order of operations sent to the server
/**
* @param {object} obj
* @returns {object}
*/
function addJsonHeader(obj) {
_.assign(obj, {
headers: {
[contentHeader]: contentJSON,
[userHeader]: _.get(store, 'state.user.username')
}
});
return obj;
}
/**
* @param {object} obj
* @returns {object}
*/
function addTextHeader(obj) {
_.assign(obj, {
headers: {
[contentHeader]: contentText,
[userHeader]: _.get(store, 'state.user.username')
}
});
return obj;
}
/**
* add headers to all GET requests
* @param {string} url
* @returns {object}
*/
function addGetHeader(url) {
return {
method: 'GET',
url: url,
headers: {
[userHeader]: _.get(store, 'state.user.username')
}
};
}
/**
* add component hooks flag
* @param {string} uri
* @param {Boolean} hooks
* @returns {string}
*/
function addHooks(uri, hooks) {
// set `false` to disable component models from firing their save/render hooks
// note: this is true by default
return hooks === false ? uri + hooksExt : uri;
}
/**
* handle errors thrown by fetch itself, e.g. connection refused
* @param {string} method
* @return {object} with `statusText` for checkStatus to handle
*/
function checkError(method) {
return function apiError() {
return { statusText: `Cannot ${method === 'GET' ? 'get' : 'send'} data` };
};
}
/**
* check status of a request, passing through data on 2xx and 3xx
* and erroring on 4xx and 5xx
* @param {string} url
* @returns {object}
* @throws error on non-200/300 response status
*/
function checkStatus(url) {
return function (res) {
/* istanbul ignore else */
if (res.status && res.status >= 200 && res.status < 400) {
return res;
} else if (url !== res.url && _.includes(res.url, loginRoute)) {
// login redirect!
// this means we're trying to do an api call but we're not authenticated.
// reload the page to force a redirect to the login page
// (while also preserving the current page to redirect back to once they log in)
window.location.reload();
} else {
let error = new Error(res.statusText);
error.response = res;
throw error;
}
};
}
/**
* send api call!
* @param {string|object} options
* @returns {Promise}
*/
function send(options) {
let url = uriToUrl(options.url);
// add credentials. this tells fetch to pass along cookies, incl. auth
options.credentials = 'same-origin';
return rest.send(url, options)
.catch(checkError(options.method))
.then(checkStatus(url));
}
/**
* @param {object} res
* @returns {string}
*/
function expectTextResult(res) {
return res.text();
}
/**
* expect something to exist
* note: make sure you call this in the .then() AND .catch() of a promise
* @param {object} res
* @returns {boolean}
*/
function expectBooleanResult(res) {
if (_.isError(res)) {
return false;
} else {
return true;
}
}
/**
* @param {object} res
* @returns {object}
*/
function expectJSONResult(res) {
return res.json();
}
/**
* @param {string} uri The returned object probably won't have this because it would be referencing itself, so we need to remember it and add it.
* @returns {function}
*/
function expectHTMLResult(uri) {
return res => res.text().then((str) => {
// if we get a full document, return it as a full document.
// otherwise pass whatever we get into dom.create()
if (_.includes(str, '<html')) { // note: html tag might contain attributes
// what's up, doc?
const parser = new DOMParser();
return parser.parseFromString(str, 'text/html');
} else {
return createDom(str);
}
}).then((html) => {
if (html.nodeType === html.ELEMENT_NODE) {
// it's an element, add the uri
html.setAttribute(refAttr, uri);
} else if (html.nodeType === html.DOCUMENT_FRAGMENT_NODE) {
// it's a document fragment, add the uri to the first child
html.firstElementChild.setAttribute(refAttr, uri);
} else if (html.nodeType === html.DOCUMENT_NODE) {
const pageUri = _.get(store, 'state.page.uri');
html.documentElement.setAttribute(layoutAttr, uri);
html.documentElement.setAttribute(refAttr, pageUri);
}
return html;
});
}
/**
* @param {string} uri
* @returns {Promise}
*/
export function getSchema(uri) {
// get the prefix for _this specific uri_, regardless of others used on this page.
const prefix = uri.substr(0, uri.indexOf(componentRoute)) + componentRoute,
name = getComponentName(uri);
assertUri(uri);
return send(addGetHeader(prefix + name + schemaRoute)).then(expectJSONResult);
}
/**
* get data from the api
* @param {string} uri
* @returns {Promise}
*/
export function getObject(uri) {
assertUri(uri);
return send(addGetHeader(uri)).then(expectJSONResult);
}
/**
* get data from an external url
* @param {string} reqUrl
* @returns {Promise}
*/
export function getJSON(reqUrl) {
assertUrl(reqUrl);
return send(addGetHeader(reqUrl)).then(expectJSONResult);
}
/**
* @param {string} uri
* @returns {Promise}
*/
export function getText(uri) {
assertUri(uri);
return send(addGetHeader(uri)).then(expectTextResult);
}
/**
* a quick way to check if a resource exists
* @param {string} uri
* @returns {Promise}
*/
export function getHead(uri) {
assertUri(uri);
return send(addGetHeader(uri)).then(expectBooleanResult).catch(expectBooleanResult);
}
/**
* note: queries should start with '&'
* @param {string} uri
* @param {string} [queries] used by clay-space
* @returns {Promise}
*/
export function getHTML(uri, queries = '') {
assertUri(uri);
/* todo: Currently the second argument is ONLY used for the Space component because
* querying for article tags is limited. There is a pending update
* to our elastic indices that should make this unecessary. At that
* point the 'queries' argument should be removed.
*/
return send(addGetHeader(uri + htmlExt + editComponentExt + queries)).then(expectHTMLResult(uri));
}
/**
* @param {string} uri
* @param {object} data
* @param {boolean} [hooks] optional, true by default
* @returns {Promise}
*/
export function save(uri, data, hooks) {
assertUri(uri);
uri = addHooks(uri, hooks);
return send(addJsonHeader({
method: 'PUT',
url: uri,
body: JSON.stringify(data)
})).then(expectJSONResult);
}
/**
* @param {string} uri
* @param {object} data
* @returns {Promise}
*/
export function saveText(uri, data) {
assertUri(uri);
return send(addTextHeader({
method: 'PUT',
url: uri,
body: data
})).then(expectTextResult);
}
/**
* @param {string} uri
* @param {object} data
* @param {boolean} hooks
* @returns {Promise}
*/
export function create(uri, data, hooks) {
assertUri(uri);
uri = addHooks(uri, hooks);
return send(addJsonHeader({
method: 'POST',
url: uri,
body: JSON.stringify(data)
})).then(expectJSONResult);
}
/**
* @param {string} uri
* @returns {Promise}
*/
export function remove(uri) {
assertUri(uri);
return send(addJsonHeader({
method: 'DELETE',
url: uri
})).then(expectJSONResult);
}
/**
* @param {string} uri
* @returns {Promise}
*/
export function removeText(uri) {
assertUri(uri);
return send(addTextHeader({
method: 'DELETE',
url: uri
})).then(expectTextResult);
}
/**
* @param {string} uri
* @param {object} data
* @returns {Promise}
*/
export function postJSON(uri, data) {
assertUri(uri);
return send(addJsonHeader({
method: 'POST',
url: uri,
body: JSON.stringify(data)
})).then(expectJSONResult);
}
/**
* convenience function to get list data
* @param {string} prefix
* @param {string} listName
* @return {Promise}
*/
export function getList(prefix, listName) {
let uri = `${prefix}${listsRoute}${listName}`;
assertUri(uri);
return send(addGetHeader(uri)).then(expectJSONResult);
}
/**
* convenience function to save list data
* @param {string} prefix
* @param {string} listName
* @param {array} items
* @return {Promise}
*/
export function saveList(prefix, listName, items) {
let uri = `${prefix}${listsRoute}${listName}`;
assertUri(uri);
return send(addJsonHeader({
method: 'PUT',
url: uri,
body: JSON.stringify(items)
})).then(expectJSONResult);
}
/**
* convenience function to save list data
* @param {string} prefix
* @param {string} listName
* @param {array} items
* @return {Promise}
*/
export function patchList(prefix, listName, patch) {
let uri = `${prefix}${listsRoute}${listName}`;
assertUri(uri);
return send(addJsonHeader({
method: 'PATCH',
url: uri,
body: JSON.stringify(patch)
})).then(expectJSONResult);
}
export function getMeta(uri) {
assertUri(uri);
return send(addGetHeader(uri + '/meta')).then(expectJSONResult);
}
export function saveMeta(uri, data) {
assertUri(uri);
return send(addJsonHeader({
method: 'PATCH',
url: uri + '/meta',
body: JSON.stringify(data)
})).then(expectJSONResult);
}