UNPKG

@kfijolek/boardgamegeekclient

Version:

TypeScript client to interact with BoardGameGeek public XML API

86 lines 4.46 kB
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 __asyncValues = (this && this.__asyncValues) || function (o) { if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); var m = o[Symbol.asyncIterator], i; return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } }; export class BggThingClient { constructor(builder, fetcher, responseParser, dtoParser, paginator) { this.resource = 'https://boardgamegeek.com/xmlapi2/thing'; this.builder = builder; this.fetcher = fetcher; this.responseParser = responseParser; this.dtoParser = dtoParser; this.paginator = paginator; } query(request) { var _a, e_1, _b, _c; return __awaiter(this, void 0, void 0, function* () { if (Array.isArray(request.id) && request.id.length > 50) { const pagination = this.paginator.paginate(request, 50); let collection = []; try { for (var _d = true, _e = __asyncValues(pagination.map((page) => this.internalQuery(page.request))), _f; _f = yield _e.next(), _a = _f.done, !_a;) { _c = _f.value; _d = false; try { const data = _c; collection.push(...data); } finally { _d = true; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (!_d && !_a && (_b = _e.return)) yield _b.call(_e); } finally { if (e_1) throw e_1.error; } } return collection; } return this.internalQuery(request); }); } queryWithProgress(request, progressOptions, progressHandler) { var _a; return __awaiter(this, void 0, void 0, function* () { const pagination = this.paginator.paginate(request, progressOptions === null || progressOptions === void 0 ? void 0 : progressOptions.limit); for (const page of pagination) { const data = this.internalQuery(page.request); progressHandler === null || progressHandler === void 0 ? void 0 : progressHandler({ current: page.current, total: page.total, data: yield data, }); (_a = this.progressHandler) === null || _a === void 0 ? void 0 : _a.call(this, { current: page.current, total: page.total, data: yield data, }); } }); } internalQuery(request) { return __awaiter(this, void 0, void 0, function* () { const querystring = this.builder.build(request); const xml = yield this.fetcher.doFetch(`${this.resource}?${querystring}`); const jsonData = yield this.responseParser.parseResponse(xml); return yield this.dtoParser.jsonToDto(jsonData); }); } } //# sourceMappingURL=BggThingClient.js.map