Source: pages/page.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
'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
/*
 * Nexmo Client SDK
 *
 * Copyright (c) Nexmo Inc.
*/
const nexmoClientError_1 = require("../nexmoClientError");
/** Page Class for Paginated Results
 *
 * @class Page
 * @param {number} page_size the number of resources returned in a single request list
 * @param {string} order 'asc' or 'desc' ordering of resources (usually based on creation time)
 * @param {string} cursor cursor parameter to access the next or previous page of a data set
 * @param {Application} application - the parent Application
 * @param {string} [event_type] the type of event used to filter event requests
 *
 * @private
*/
class Page {
    constructor(params = {}) {
        this.page_size = params.page_size;
        this.order = params.order;
        this.cursor = params.cursor;
        this.application = params.application;
        if (params.event_type && params.event_type.length > 0) {
            this.event_type = params.event_type;
        }
    }
    /**
     * Check if previous page exists
     * @returns {Boolean}
     * @example <caption>Check if previous page exists</caption>
     * // currentPage is the current Conversations or Events Page
     * currentPage.hasPrev() // true or false
    */
    hasPrev() {
        return this.cursor.prev ? this.cursor.prev.length > 0 : false;
    }
    /**
     * Check if next page exists
     * @returns {Boolean}
     * @example <caption>Check if next page exists</caption>
     * // currentPage is the current Conversations or Events Page
     * currentPage.hasNext() // true or false
    */
    hasNext() {
        return this.cursor.next ? this.cursor.next.length > 0 : false;
    }
    /**
      * Create config params for paginationRequest
      * @param {string} cursor cursor parameter to access the next or previous page of a data set
      * @returns {Object}
     * @private
    */
    _getConfig(cursor) {
        const config = {
            page_size: this.page_size,
            order: this.order,
            cursor,
            ...(this.event_type && { event_type: this.event_type })
        };
        return config;
    }
    /**
     * Create a nexmoClientError when page does not exist
     * @private
    */
    _getError() {
        return Promise.reject(new nexmoClientError_1.NexmoClientError('error:invalid-cursor'));
    }
}
exports.default = Page;
module.exports = Page;