-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFetcher.js
More file actions
98 lines (93 loc) · 2.85 KB
/
Copy pathFetcher.js
File metadata and controls
98 lines (93 loc) · 2.85 KB
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
/**
* Base api fetcher.
*/
export default class Fetcher {
/**
* @param {Http} http
* @param {string} [territory = 'TW'] - ['TW', 'HK', 'SG', 'MY', 'JP'] The territory for the fetcher.
*/
constructor(http, territory = 'TW') {
/**
* @ignore
*/
this.http = http;
/**
* @ignore
*/
this.territory = territory;
}
/**
* Set the fetcher's territory.
* @param {string} [territory = 'TW'] - ['TW', 'HK', 'SG', 'MY', 'JP'] The territory for the fetcher.
* @return {Fetcher}
*/
setTerritory(territory) {
this.territory = territory;
return this;
}
/**
* Gets an object's nested property by path.
* @ignore
*/
getPropertyByPath(object, path) {
path = path.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
path = path.replace(/^\./, ''); // strip a leading dot
var keys = path.split('.');
for (var i = 0, n = keys.length; i < n; ++i) {
var key = keys[i];
if (key in object) {
object = object[key];
} else {
return;
}
}
return object;
}
/**
* Fetches next page of various paged APIs.
*
* @param {fulfillment} fulfillment - The fulfillment get from Promose's onFulfillment function
* @param {String} nextUriPath - The next uri's path. Defaults to 'data.paging.next',
* which means we will get the next uri path from 'fulfillment.data.paging.next'.
* The correct next uri path depends on respective api's response.
* @return {Promise}
* @example
* api.albumFetcher
* .setAlbumID('KmRKnW5qmUrTnGRuxF')
* .fetchTracks()
* .then(response => {
* api.albumFetcher.fetchNextPage(response));
* });
*/
fetchNextPage(fulfillment, nextUriPath = 'data.paging.next') {
var nextUri = this.getPropertyByPath(fulfillment, nextUriPath);
if (nextUri != null && nextUri !== undefined) {
return this.http.get(nextUri);
} else {
return new Promise((resolve, reject) => {
reject(new Error('Cannot fetch next page'));
});
}
}
/**
* Is next page available for various paged APIs.
* @param {fulfillment} fulfillment - The fulfillment get from Promose's onFulfillment function
* @param {String} nextUriPath - The next uri's path. Defaults to 'data.paging.next',
* which means we will get the next uri path from 'fulfillment.data.paging.next'.
* The correct next uri path depends on respective api's response.
* @return {Boolean}
* @example
* api.albumFetcher
* .setAlbumID('KmRKnW5qmUrTnGRuxF')
* .fetchTracks()
* .then(response => {
* if (api.albumFetcher.hasNextPage(response)) {
* // more data available
* }
* });
*/
hasNextPage(fulfillment, nextUriPath = 'data.paging.next') {
var nextUri = this.getPropertyByPath(fulfillment, nextUriPath);
return nextUri != null && nextUri !== undefined;
}
}