@ibm-cloud/cloudant
Version:
IBM Cloudant Node.js SDK
82 lines • 2.87 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BasePageIterator = void 0;
class BasePageIterator {
client;
pageSize;
_hasNext = true;
nextPageParams;
// The maximum and minimum limit values (i.e. page size)
static MAX_LIMIT = 200;
static MIN_LIMIT = 1;
constructor(client, params) {
this.client = client;
this.validate(params);
// Set the page size from the supplied params limit
this.pageSize = this.getPageSize(params);
// Clone the supplied params into the nextPageParams
this.nextPageParams = { ...params };
}
async next(...[value]) {
if (this._hasNext) {
const items = await this.nextRequest();
const readonlyItems = [...items];
return { done: false, value: readonlyItems };
}
return { done: true, value: undefined };
}
async nextRequest() {
const response = await this.nextRequestFunction().call(this.client, this.nextPageParams);
const { result } = response;
const items = this.getItems(result);
if (items.length < this.pageSize) {
this._hasNext = false;
}
else {
this.setNextPageParams(result);
}
return items;
}
getLimit(params) {
return params.limit;
}
getPageSize(params) {
return this.getLimit(params)
? this.getLimit(params)
: BasePageIterator.MAX_LIMIT;
}
[Symbol.asyncIterator]() {
return this;
}
hasNext() {
return this._hasNext;
}
validate(params) {
// filter out undefined and null values for limit:
if (params.limit !== undefined && params.limit !== null) {
this.validateLimit(params.limit);
}
}
validateParamsAbsent(params, paramNames) {
paramNames.forEach((paramName) => {
if (params[paramName] !== undefined) {
throw new Error(this.getValidateParamsAbsentErrorMessage(paramName));
}
});
}
getValidateParamsAbsentErrorMessage(paramName) {
return `The param '${paramName}' is invalid when using pagination.`;
}
validateLimit(limit) {
// If limit is set check it is within range
// Else it is unset and we will set the valid default value later
if (limit > BasePageIterator.MAX_LIMIT) {
throw new Error(`The provided limit ${limit} exceeds the maximum page size value of ${BasePageIterator.MAX_LIMIT}.`);
}
if (limit < BasePageIterator.MIN_LIMIT) {
throw new Error(`The provided limit ${limit} is lower than the minimum page size value of ${BasePageIterator.MIN_LIMIT}.`);
}
}
}
exports.BasePageIterator = BasePageIterator;
//# sourceMappingURL=basePageIterator.js.map
;