modrinth
Version:
JavaScript library for accessing the Modrinth API
99 lines (98 loc) • 3.88 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.User = void 0;
const object_1 = require("../object");
const Mod_1 = require("./Mod");
class User extends object_1.ModrinthObject {
static get(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return User.fromSource(modrinth, yield User.fetch(modrinth, id));
});
}
static getMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return (yield User.fetchMultiple(modrinth, ids)).map((user) => User.fromSource(modrinth, user));
});
}
static fetch(modrinth, id) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get(User.getObjectLocation(id));
});
}
static fetchMultiple(modrinth, ids) {
return __awaiter(this, void 0, void 0, function* () {
return modrinth.api.get("users", { query: { ids: JSON.stringify(ids) } });
});
}
static getObjectLocation(id) {
return `user/${id}`;
}
static getResourceLocation(id) {
return User.getObjectLocation(id);
}
static getCacheKey(id) {
return User.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.created)
json.created = object.created.toISOString();
return json;
}
static fromSource(modrinth, source) {
if (!modrinth.useCache)
return new User(modrinth, source);
const cacheKey = User.getCacheKey(source.id);
const cached = modrinth.cache.get(cacheKey);
if (cached)
return cached;
return new User(modrinth, source);
}
static current(modrinth) {
return __awaiter(this, void 0, void 0, function* () {
return User.fromSource(modrinth, yield modrinth.api.get("user"));
});
}
static update(modrinth, id, update) {
return __awaiter(this, void 0, void 0, function* () {
const json = JSON.stringify(User.toSource(update), null, 4);
return void (yield modrinth.api.patch(User.getObjectLocation(id), json, {
resultType: "status"
}));
});
}
mutate(source) {
super.mutate(source);
this.created = new Date(source.created);
return this;
}
update(update) {
return __awaiter(this, void 0, void 0, function* () {
yield User.update(this._modrinth, this.id, update);
return yield this.get();
});
}
mods() {
return __awaiter(this, void 0, void 0, function* () {
const ids = yield this._modrinth.api.get(`user/${this.id}/mods`);
return Mod_1.Mod.getMultiple(this._modrinth, ids);
});
}
}
exports.User = User;