1 | "use strict";
|
2 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4 | return new (P || (P = Promise))(function (resolve, reject) {
|
5 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9 | });
|
10 | };
|
11 | Object.defineProperty(exports, "__esModule", { value: true });
|
12 | exports.XKCDAPI = void 0;
|
13 | class XKCDAPI {
|
14 | constructor(apiClient, options) {
|
15 | this.apiClient = apiClient;
|
16 | this.options = options;
|
17 | this.lowestIndex = 1;
|
18 | this.JSON_INFO_FILE = 'info.0.json';
|
19 | }
|
20 | getByIndex(index, options = {}) {
|
21 | return __awaiter(this, void 0, void 0, function* () {
|
22 | if (index < this.lowestIndex) {
|
23 | throw new Error(`Index is lower than the lowest index of ${this.lowestIndex}.`);
|
24 | }
|
25 | const { data: metaData } = yield this.apiClient.get(`/${index}/${this.JSON_INFO_FILE}`);
|
26 | if (options.withData === true) {
|
27 | const imageData = yield this.getImage(metaData.img);
|
28 | return Object.assign(Object.assign({}, metaData), { data: imageData });
|
29 | }
|
30 | return metaData;
|
31 | });
|
32 | }
|
33 | getLatest(options = {}) {
|
34 | return __awaiter(this, void 0, void 0, function* () {
|
35 | const { data: metaData } = yield this.apiClient.get(`/${this.JSON_INFO_FILE}`);
|
36 | if (options.withData) {
|
37 | const imageData = yield this.getImage(metaData.img);
|
38 | return Object.assign(Object.assign({}, metaData), { data: imageData });
|
39 | }
|
40 | return metaData;
|
41 | });
|
42 | }
|
43 | getRandom(options = {}) {
|
44 | return __awaiter(this, void 0, void 0, function* () {
|
45 | const latest = yield this.getLatest();
|
46 | const randomIndex = Math.floor(Math.random() * (latest.num - this.lowestIndex + 1)) + this.lowestIndex;
|
47 | const metaData = yield this.getByIndex(randomIndex);
|
48 | if (options.withData === true) {
|
49 | const imageData = yield this.getImage(metaData.img);
|
50 | return Object.assign(Object.assign({}, metaData), { data: imageData });
|
51 | }
|
52 | return metaData;
|
53 | });
|
54 | }
|
55 | getImage(imageUrl) {
|
56 | return __awaiter(this, void 0, void 0, function* () {
|
57 | const { data, headers } = yield this.apiClient.request({
|
58 | responseType: 'arraybuffer',
|
59 | url: imageUrl,
|
60 | });
|
61 | const contentType = headers['content-type'];
|
62 | return {
|
63 | data,
|
64 | mimeType: contentType ? String(contentType) : undefined,
|
65 | };
|
66 | });
|
67 | }
|
68 | }
|
69 | exports.XKCDAPI = XKCDAPI;
|
70 |
|
\ | No newline at end of file |