UNPKG

1.11 kBJavaScriptView Raw
1import OBJ from 'dot-prop';
2import fetch from 'omni-fetch';
3import { newValue } from './utils';
4
5/**
6 * Take an `Object` containing URLs to the ISTEX API, and returns results.
7 * @see ISTEXSearch
8 *
9 * @param {string} source
10 * @param {string} target
11 * @returns {Stream}
12 */
13function ISTEXScroll(data, feed) {
14 if (this.isLast()) {
15 return feed.close();
16 }
17
18 const source = this.getParam('source');
19 const target = this.getParam('target');
20 const handle = source ? OBJ.get(data, source) : data;
21
22
23 if (typeof handle !== 'string') {
24 return feed.send(data);
25 }
26 fetch(handle)
27 .then(response => response.json())
28 .then((json) => {
29 if (json.total === 0) {
30 return feed.send(new Error('No result.'));
31 }
32 if (json.total === undefined) {
33 return feed.send(new Error('Unexpected response.'));
34 }
35 feed.write(newValue(json, target, data));
36 return feed.end();
37 }).catch((err) => {
38 feed.send(err);
39 });
40}
41
42export default {
43 ISTEXScroll,
44};