UNPKG

curseforge-v2

Version:
212 lines (211 loc) 8.56 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CFV2Client = void 0; const axios_1 = require("axios"); const cfv2 = require("./curseforge-api-v2"); const DEFAULT_HTTP_TIMEOUT_MS = 30000; class CFV2Client { constructor(config) { if (typeof config.apiKey !== 'string' || config.apiKey.length === 0) { throw new Error('CFV2Client api key cannot be empty'); } this._apiKey = config.apiKey; this._apiUrl = typeof config.apiUrl === 'string' && config.apiUrl.length > 0 ? config.apiUrl : cfv2.BaseUrl; this._headers = config.headers; this._httpTimeoutMs = typeof config.httpTimeoutMs === 'number' && !isNaN(config.httpTimeoutMs) ? config.httpTimeoutMs : DEFAULT_HTTP_TIMEOUT_MS; } // GAMES // https://docs.curseforge.com/#get-games getGames(index, pageSize) { return __awaiter(this, void 0, void 0, function* () { const queryParams = new URLSearchParams(); if (typeof index === 'number' && !isNaN(index)) { queryParams.append('index', index.toString()); } if (typeof pageSize === 'number' && !isNaN(pageSize)) { queryParams.append('pageSize', pageSize.toString()); } const url = new URL(`${this._apiUrl}/v1/games?${queryParams.toString()}`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-game getGame(gameId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/games/${gameId}`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-versions getGameVersions(gameId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/games/${gameId}/versions`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-version-types getGameVersionTypes(gameId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/games/${gameId}/version-types`); return yield this.cfGet(url); }); } // MODS // https://docs.curseforge.com/#get-mod getMod(modId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/${modId}`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-mods getMods(req) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods`); return yield this.cfPost(url, req); }); } // https://docs.curseforge.com/#get-mod-description getModDescription(modId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/${modId}/description`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-featured-mods getFeaturedMods(req) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/featured`); return yield this.cfPost(url, req); }); } // https://docs.curseforge.com/#search-mods searchMods(params) { return __awaiter(this, void 0, void 0, function* () { const queryMap = {}; for (const [key, value] of Object.entries(params)) { if ((typeof value !== 'string' && isNaN(value)) || value === null || value === undefined) { continue; } queryMap[key] = value.toString(); } const queryParams = new URLSearchParams(queryMap); const url = new URL(`${this._apiUrl}/v1/mods/search?${queryParams.toString()}`); return yield this.cfGet(url); }); } // FILES // https://docs.curseforge.com/#get-mod-file getModFile(modId, fileId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/${modId}/files/${fileId}`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-mod-files getModFiles(req) { return __awaiter(this, void 0, void 0, function* () { const params = Object.assign({}, req); delete params.modId; const queryParams = new URLSearchParams(params); const url = new URL(`${this._apiUrl}/v1/mods/${req.modId}/files?${queryParams.toString()}`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-files getFiles(pRequest) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/files`); const req = Object.assign({}, pRequest); return yield this.cfPost(url, req); }); } // https://docs.curseforge.com/#get-mod-file-changelog getModFileChangelog(modId, fileId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/${modId}/files/${fileId}/changelog`); return yield this.cfGet(url); }); } // https://docs.curseforge.com/#get-mod-file-download-url getModFileDownloadUrl(modId, fileId) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/mods/${modId}/files/${fileId}/download-url`); return yield this.cfGet(url); }); } // FINGERPRINTS // https://docs.curseforge.com/#get-fingerprints-matches getFingerprintMatches(pRequest) { return __awaiter(this, void 0, void 0, function* () { const url = new URL(`${this._apiUrl}/v1/fingerprints`); const req = Object.assign({}, pRequest); return yield this.cfPost(url, req); }); } cfGet(url) { return __awaiter(this, void 0, void 0, function* () { return yield httpGet(url, { headers: this.getAuthHeaders(), timeout: this._httpTimeoutMs, }); }); } cfPost(url, data) { return __awaiter(this, void 0, void 0, function* () { return yield httpPost(url, data, { headers: this.getAuthHeaders(), timeout: this._httpTimeoutMs, }); }); } getAuthHeaders() { return Object.assign({ 'x-api-key': this._apiKey }, this._headers); } } exports.CFV2Client = CFV2Client; function httpGet(url, options) { return __awaiter(this, void 0, void 0, function* () { return yield httpSend(url, 'GET', undefined, options); }); } function httpPost(url, data, options) { return __awaiter(this, void 0, void 0, function* () { return yield httpSend(url, 'POST', data, options); }); } function httpSend(url, method, data, options) { return __awaiter(this, void 0, void 0, function* () { try { const config = Object.assign({ method, url: url.toString(), data }, options); const response = yield (0, axios_1.default)(config); return { data: response.data, message: response.statusText, statusCode: response.status, }; } catch (e) { if (typeof e.response === 'object') { return { data: e.response.data, message: e.response.statusText, statusCode: e.response.status, }; } throw e; } }); }