UNPKG

@ibm-cloud/cloudant

Version:
69 lines 2.55 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.getPageSize(params)); // n+1 items per request } setNextKey(startKey) { this.nextPageParams.startKey = startKey; } setLimit(limit) { this.nextPageParams.limit = limit; } setNextPageParams(result) { const items = this.getItems(result); const lastItem = items[items.length - 1]; const nextKey = lastItem.key; const nextKeyId = lastItem.id; this.setNextKey(nextKey); this.setNextKeyId(nextKeyId); if (this.nextPageParams.skip) { delete this.nextPageParams.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