@wedevelop/mindbody-client
Version:
A MindBody v6 API Client
99 lines (76 loc) • 3.02 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
class ResultsPage {
constructor(mbClient, requestConfig, response) {
this[Symbol.asyncIterator] = this.pagesGenerator;
this._mbClient = mbClient;
this._requestConfig = requestConfig;
this._response = response;
this._paginationResponse = response.PaginationResponse;
}
get response() {
if (typeof this._requestConfig.responseMapper === 'function') {
return this._requestConfig.responseMapper(this._response);
}
return this._response;
}
get pageSize() {
var _this$_paginationResp, _this$_paginationResp2;
return (_this$_paginationResp = (_this$_paginationResp2 = this._paginationResponse) === null || _this$_paginationResp2 === void 0 ? void 0 : _this$_paginationResp2.PageSize) !== null && _this$_paginationResp !== void 0 ? _this$_paginationResp : 0;
}
get totalResults() {
var _this$_paginationResp3, _this$_paginationResp4;
return (_this$_paginationResp3 = (_this$_paginationResp4 = this._paginationResponse) === null || _this$_paginationResp4 === void 0 ? void 0 : _this$_paginationResp4.TotalResults) !== null && _this$_paginationResp3 !== void 0 ? _this$_paginationResp3 : 0;
}
get requestedOffset() {
var _this$_paginationResp5, _this$_paginationResp6;
return (_this$_paginationResp5 = (_this$_paginationResp6 = this._paginationResponse) === null || _this$_paginationResp6 === void 0 ? void 0 : _this$_paginationResp6.RequestedOffset) !== null && _this$_paginationResp5 !== void 0 ? _this$_paginationResp5 : 0;
}
get requestedLimit() {
var _this$_paginationResp7, _this$_paginationResp8;
return (_this$_paginationResp7 = (_this$_paginationResp8 = this._paginationResponse) === null || _this$_paginationResp8 === void 0 ? void 0 : _this$_paginationResp8.RequestedLimit) !== null && _this$_paginationResp7 !== void 0 ? _this$_paginationResp7 : 0;
}
get nextOffset() {
return this.requestedOffset + this.pageSize;
}
get previousOffset() {
return this.requestedOffset - this.requestedLimit;
}
isEmpty() {
return this.pageSize === 0;
}
async *pagesGenerator() {
let current = this;
while (current && !current.isEmpty()) {
yield current.response;
current = await current.getNextPage();
}
}
hasNextPage() {
return this.nextOffset < this.totalResults;
}
hasPreviousPage() {
return this.requestedOffset > 0;
}
getNextPage() {
if (!this.hasNextPage()) return null;
return this._mbClient.doRequest({ ...this._requestConfig,
params: { ...(this._requestConfig.params || {}),
Offset: this.nextOffset
}
});
}
getPreviousPage() {
if (!this.hasPreviousPage()) return null;
return this._mbClient.doRequest({ ...this._requestConfig,
params: { ...(this._requestConfig.params || {}),
Offset: this.previousOffset
}
});
}
}
var _default = ResultsPage;
exports.default = _default;