UNPKG

@tangany/waas

Version:

node.js SDK for Tangany Wallet as a Service API

191 lines 10.5 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); } var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var g = generator.apply(thisArg, _arguments || []), i, q = []; return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i; function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; } function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } } function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); } function fulfill(value) { resume("next", value); } function reject(value) { resume("throw", value); } function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ResourceIterable = void 0; /** * Base class to provide the results of API search requests as an asynchronous iterable that returns a single object per iteration rather than the complete results page. * Nevertheless, each search result page is not fetched for each iteration, but only once. * As soon as all items of the result list of the currently stored page have been returned, the next result page is fetched automatically. * This way, the iterable hides the API-side pagination from the user such that it looks like an ordinary list of single objects that are actually located across pages. * @template TApiResponse - Type of an entire search result page returned by the API * @template TIteratorValue - Type of the item returned per iteration, usually created by converting single API search result items */ var ResourceIterable = /** @class */ (function () { function ResourceIterable(waas, initialRequest) { this.waas = waas; this.initialRequest = initialRequest; } Object.defineProperty(ResourceIterable.prototype, "hits", { /** * Returns statistics on how many resources met the search criteria. * This getter returns a Promise because the initial API search request is performed if that has not yet been done and the first page is therefore not in memory. * If the first search result page is already stored, e.g. because the iterator has been used, the value is read from memory without any further HTTP request. */ get: function () { return this.getFirstPage().then(function (x) { return x.hits; }); }, enumerable: false, configurable: true }); /** * Returns the asynchronous iterator to traverse the list of items. */ ResourceIterable.prototype[Symbol.asyncIterator] = function () { return __asyncGenerator(this, arguments, function _a() { var currentPage, allItemsYielded, _b, _c, item, e_1_1, nextPageUrl; var e_1, _d; return __generator(this, function (_e) { switch (_e.label) { case 0: return [4 /*yield*/, __await(this.getFirstPage())]; case 1: currentPage = _e.sent(); allItemsYielded = false; _e.label = 2; case 2: if (!!allItemsYielded) return [3 /*break*/, 15]; _e.label = 3; case 3: _e.trys.push([3, 9, 10, 11]); _b = (e_1 = void 0, __values(currentPage.list)), _c = _b.next(); _e.label = 4; case 4: if (!!_c.done) return [3 /*break*/, 8]; item = _c.value; return [4 /*yield*/, __await(this.convertResponseItem(item))]; case 5: return [4 /*yield*/, _e.sent()]; case 6: _e.sent(); _e.label = 7; case 7: _c = _b.next(); return [3 /*break*/, 4]; case 8: return [3 /*break*/, 11]; case 9: e_1_1 = _e.sent(); e_1 = { error: e_1_1 }; return [3 /*break*/, 11]; case 10: try { if (_c && !_c.done && (_d = _b.return)) _d.call(_b); } finally { if (e_1) throw e_1.error; } return [7 /*endfinally*/]; case 11: nextPageUrl = currentPage.links.next; if (!nextPageUrl) return [3 /*break*/, 13]; return [4 /*yield*/, __await(this.fetchPage(nextPageUrl))]; case 12: // Do not use the query params as the first call is already done and the pagination links contain all the query params currentPage = _e.sent(); return [3 /*break*/, 14]; case 13: allItemsYielded = true; _e.label = 14; case 14: return [3 /*break*/, 2]; case 15: return [2 /*return*/]; } }); }); }; /** * Reads the first search result page from memory if it has previously been fetched, or performs the initial HTTP request otherwise. * Note that in the latter case the result is stored in the corresponding private property. */ ResourceIterable.prototype.getFirstPage = function () { return __awaiter(this, void 0, void 0, function () { var _a, url, params, _b; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!!this.firstPage) return [3 /*break*/, 2]; _a = this.initialRequest, url = _a.url, params = _a.params; _b = this; return [4 /*yield*/, this.fetchPage(url, params)]; case 1: _b.firstPage = _c.sent(); _c.label = 2; case 2: return [2 /*return*/, this.firstPage]; } }); }); }; /** * Queries another search results page based on the passed arguments. * @param url - URL to be used for the HTTP GET request * @param [params] - Query parameters to set the search criteria (usually only necessary to fetch the first page) */ ResourceIterable.prototype.fetchPage = function (url, params) { return __awaiter(this, void 0, void 0, function () { var config; var _this = this; return __generator(this, function (_a) { config = {}; if (params) { config.params = params; } return [2 /*return*/, this.waas.wrap(function () { return _this.waas.instance.get(url, config); })]; }); }); }; return ResourceIterable; }()); exports.ResourceIterable = ResourceIterable; //# sourceMappingURL=resource-iterable.js.map