UNPKG

modrinth

Version:

JavaScript library for accessing the Modrinth API

138 lines (137 loc) 4.97 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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.Modrinth = void 0; const http_1 = require("@rubybb/http"); const deepmerge_1 = require("deepmerge"); const cache_1 = require("./cache"); const Package = __importStar(require("../package.json")); const Version_1 = require("./models/Version"); const User_1 = require("./models/User"); const Mod_1 = require("./models/Mod"); class Modrinth { constructor(options = {}) { this.options = {}; this.options = deepmerge_1.all([ Object.create(null), Modrinth.defaultOptions, options ]); if (typeof this.options.cache === "number" && this.options.cache > 0) { this.cache = new cache_1.Cache({ ttl: this.options.cache, capacity: 100, }); } this.api = http_1.HTTP.create({ baseURL: this.apiURL, debug: this.options.debug, resultType: "json", headers: { "content-type": "application/json", "user-agent": `Modrinth v${Modrinth.version} <https://modrinth.js.org>` } }); this.login(this.options.authorization); } static get defaultOptions() { return { url: "https://modrinth.com/", api: "https://api.modrinth.com/api/v1/", cdn: "https://cdn.modrinth.com/", authorization: (process.env["MODRINTH_TOKEN"]) || "", debug: false, cache: 1000 }; } get apiURL() { return this.options.api; } get cdnURL() { return this.options.cdn; } get siteURL() { return this.options.url; } get useCache() { return typeof this.options.cache === "number" && !!this.cache; } login(token) { if (token) this.api.mutate({ headers: { "authorization": token } }); } self() { return __awaiter(this, void 0, void 0, function* () { return User_1.User.current(this); }); } currentUser() { return __awaiter(this, void 0, void 0, function* () { return User_1.User.current(this); }); } user(id) { return __awaiter(this, void 0, void 0, function* () { return User_1.User.get(this, id); }); } users(...ids) { return __awaiter(this, void 0, void 0, function* () { return User_1.User.getMultiple(this, ([].concat(...ids))); }); } mod(id) { return __awaiter(this, void 0, void 0, function* () { return Mod_1.Mod.get(this, id); }); } mods(...ids) { return __awaiter(this, void 0, void 0, function* () { return Mod_1.Mod.getMultiple(this, ([].concat(...ids))); }); } version(id) { return __awaiter(this, void 0, void 0, function* () { return Version_1.Version.get(this, id); }); } versions(...ids) { return __awaiter(this, void 0, void 0, function* () { return Version_1.Version.getMultiple(this, ([].concat(...ids))); }); } createVersion(mod_id, data, files) { return __awaiter(this, void 0, void 0, function* () { return Version_1.Version.create(this, data, files); }); } } exports.Modrinth = Modrinth; Modrinth.version = Package.version;