-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpage.js
More file actions
86 lines (72 loc) · 1.94 KB
/
Copy pathpage.js
File metadata and controls
86 lines (72 loc) · 1.94 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
import getData from './utils/getData';
/**
* Page object constructor
* @param options
* @constructor
*/
class Page {
constructor({_embedded, page}, path, qs) {
const itemKey = Object.keys(_embedded)[0];
this.items = _embedded[itemKey];
this.page = page;
this.path = path;
this.qs = qs;
}
/**
* Method of Page object type
* Gets some page of results by it's number passed as param.
* @param n {number}
* @returns {Promise}
*/
getAt(n) {
const qs = Object.assign({}, this.qs, {page: n});
if (n > 0 && n <= this.page.totalPages) {
return getData({path: this.path, qs});
}
return Promise.reject({message: 'You should pass correct page number.', qs});
};
/**
* Method of Page object type
* (Iterator method) Gets next page of same type results
* @param step {number}
* @returns {Promise}
*/
getNext(step = 1) {
const n = this.page.number + step;
const qs = Object.assign({}, this.qs, {page: n});
if (n <= this.page.totalPages) {
return getData({path: this.path, qs});
}
return Promise.reject({message: 'No next page! You are on the last.', qs});
};
/**
* Method of Page object type
* (Iterator method) Gets previous page of same type results
* @param step {number}
* @returns {Promise}
*/
getPrev(step = 1) {
const n = this.page.number - step;
const qs = Object.assign({}, this.qs, {page: n});
if (n > 0) {
return getData({path: this.path, qs});
}
return Promise.reject({message: 'No previous page! You are on the first one.', qs});
};
/**
* Method of Page object type
* Checker if current result page is the last one
* @returns {boolean}
*/
isLast() {
return this.page.number === this.page.totalPages;
};
/**
* Method of Page object type
* @returns {number} quantity of all items of the same type
*/
count() {
return this.page.totalElements;
};
}
export default Page;