UNPKG

@ibm-cloud/cloudant

Version:
75 lines 2.69 kB
"use strict"; /** * © Copyright IBM Corporation 2025. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.KeyPageIterator = void 0; const basePageIterator_1 = require("./basePageIterator"); class KeyPageIterator extends basePageIterator_1.BasePageIterator { boundaryFailure = null; constructor(client, params) { super(client, params); this.setLimit(this.nextPageParams, this.getPageSize(params)); // n+1 items per request } setNextKey(params, startKey) { params.startKey = startKey; } getNextKey(item) { return item.key; } getNextKeyId(item) { return item.id; } setLimit(params, limit) { params.limit = limit; } setNextPageParams(params, result) { const items = this.getItems(result); const lastItem = items[items.length - 1]; const nextKey = this.getNextKey(lastItem); const nextKeyId = this.getNextKeyId(lastItem); this.setNextKey(params, nextKey); this.setNextKeyId(params, nextKeyId); if (params.skip) { delete params.skip; } } async nextRequest() { // If the previous request had the duplicate boundary error // throw it now because we cannot safely get the next page. if (this.boundaryFailure) { throw new Error(this.boundaryFailure); } const items = await super.nextRequest(); if (this._hasNext) { const lastItem = items.pop(); if (items.length > 0) { const penultimateItem = items[items.length - 1]; // Defer a throw for a boundary failure to the next request this.boundaryFailure = this.checkBoundary(penultimateItem, lastItem); } } return items; } getPageSize(params) { return super.getPageSize(params) + 1; } validate(params) { super.validate(params); this.validateParamsAbsent(params, ['keys', 'key']); } } exports.KeyPageIterator = KeyPageIterator; //# sourceMappingURL=keyPageIterator.js.map