1 | import OBJ from 'dot-prop';
|
2 | import fetch from 'omni-fetch';
|
3 | import { newValue } from './utils';
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function 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 |
|
42 | export default {
|
43 | ISTEXScroll,
|
44 | };
|