modrinth
Version:
JavaScript library for accessing the Modrinth API
113 lines (112 loc) • 4.54 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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Version = void 0;
const form_data_1 = __importDefault(require("form-data"));
const object_1 = require("../object");
const Mod_1 = require("./Mod");
const User_1 = require("./User");
class Version extends object_1.ModrinthObject {
static get(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return Version.fromSource(modrinth, yield Version.fetch(modrinth, id));
});
}
static getMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return (yield Version.fetchMultiple(modrinth, ids)).map((version) => Version.fromSource(modrinth, version));
});
}
static fetch(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get(Version.getObjectLocation(id));
});
}
static fetchMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get("versions", { query: { ids: JSON.stringify(ids) } });
});
}
static getObjectLocation(id) {
return `version/${id}`;
}
static getResourceLocation(id) {
throw new Error("Version doesn't have static resource location");
}
static getCacheKey(id) {
return Version.getObjectLocation(id);
}
static toSource(object) {
if (object._source)
return object._source;
}
static fromSource(modrinth, source) {
if (!modrinth.useCache)
return new Version(modrinth, source);
const cacheKey = Version.getCacheKey(source.id);
const cached = modrinth.cache.get(cacheKey);
if (cached)
return cached;
return new Version(modrinth, source);
}
static create(modrinth, body, files) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const form = new form_data_1.default();
const data = {
mod_id: body.mod_id,
version_title: body.title || body.number,
version_number: body.number,
version_body: body.body,
loaders: body.loaders,
game_versions: body.game_versions,
release_channel: body.type,
featured: (_a = body.featured) !== null && _a !== void 0 ? _a : false,
dependencies: body.dependencies || [],
file_parts: files.map((_, index) => `${index}`)
};
form.append("data", JSON.stringify(data));
files.map((file, index) => {
form.append(`${index}`, file.data, {
filename: file.name,
});
});
return Version.fromSource(modrinth, yield modrinth.api.post("version", form, {
headers: form.getHeaders()
}));
});
}
mutate(source) {
const _source = Object.assign({}, source);
delete _source["version_type"];
super.mutate(_source);
this.type = source.version_type;
this.date_published = new Date(source.date_published);
return this;
}
getResourceLocation() {
return `${Mod_1.Mod.getResourceLocation(this.mod_id)}/version/${this.id}`;
}
getMod() {
return __awaiter(this, void 0, void 0, function* () {
return Mod_1.Mod.get(this._modrinth, this.mod_id);
});
}
getAuthor() {
return __awaiter(this, void 0, void 0, function* () {
return User_1.User.get(this._modrinth, this.author_id);
});
}
}
exports.Version = Version;