@kfijolek/boardgamegeekclient
Version:
Javascript client to interact with BoardGameGeek public XML API
78 lines • 4.18 kB
JavaScript
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 BggFamilyClient {
constructor(builder, fetcher, responseParser, dtoParser, paginator) {
this.resource = "https://www.boardgamegeek.com/xmlapi2/family";
this.builder = builder;
this.fetcher = fetcher;
this.responseParser = responseParser;
this.dtoParser = dtoParser;
this.paginator = paginator;
}
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
});
}
});
}
query(request) {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
if (Array.isArray(request.id) && request.id.length > 1) {
const pagination = this.paginator.paginate(request, 1);
let collection = [];
try {
for (var _b = __asyncValues(pagination.map(page => this.internalQuery(page.request))), _c; _c = yield _b.next(), !_c.done;) {
const data = _c.value;
collection.push(...data);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return collection;
}
return this.internalQuery(request);
});
}
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=BggFamilyClient.js.map