modrinth
Version:
JavaScript library for accessing the Modrinth API
101 lines (100 loc) • 4.05 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Mod = void 0;
const object_1 = require("../object");
const Version_1 = require("./Version");
class Mod extends object_1.ModrinthObject {
static get(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return Mod.fromSource(modrinth, yield Mod.fetch(modrinth, id));
});
}
static getMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return (yield Mod.fetchMultiple(modrinth, ids)).map((mod) => Mod.fromSource(modrinth, mod));
});
}
static fetch(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get(Mod.getObjectLocation(id), { headers: { pragma: "no-cache" } });
});
}
static fetchMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get("mods", { query: { ids: JSON.stringify(ids) } });
});
}
static getObjectLocation(id) {
return `mod/${id}`;
}
static getResourceLocation(id) {
return Mod.getObjectLocation(id);
}
static getCacheKey(id) {
return Mod.getObjectLocation(id);
}
static toSource(object) {
// if (object._source) return object._source;
object = Object.assign(Object.create(null), object);
Object.keys(object).forEach((key) => {
const value = object[key];
if (key.startsWith("_") || typeof value === "function")
delete object[key];
});
const json = Object.assign({}, object);
if (object.published)
json.published = object.published.toISOString();
if (object.updated)
json.updated = object.updated.toISOString();
return json;
}
static fromSource(modrinth, source) {
if (!modrinth.useCache)
return new Mod(modrinth, source);
const cacheKey = Mod.getCacheKey(source.id);
const cached = modrinth.cache.get(cacheKey);
if (cached)
return cached;
return new Mod(modrinth, source);
}
static update(modrinth, id, update) {
return __awaiter(this, void 0, void 0, function* () {
const json = JSON.stringify(Mod.toSource(update), null, 4);
return void (yield modrinth.api.patch(Mod.getObjectLocation(id), json, {
resultType: "status"
}));
});
}
mutate(source) {
super.mutate(source);
this.published = new Date(source.published);
this.updated = new Date(source.updated);
return this;
}
versions() {
return __awaiter(this, void 0, void 0, function* () {
return Version_1.Version.getMultiple(this._modrinth, this._source.versions);
});
}
update(update) {
return __awaiter(this, void 0, void 0, function* () {
yield Mod.update(this._modrinth, this.id, update);
return yield this.get();
});
}
createVersion(body, file) {
return __awaiter(this, void 0, void 0, function* () {
return Version_1.Version.create(this._modrinth, Object.assign(Object.assign({}, body), { mod_id: this.id }), file);
});
}
}
exports.Mod = Mod;