UNPKG

@threadmc/minecraft-api

Version:

An advanced Minecraft API client for interacting with piston-meta, minecraft servers, and more

196 lines 8.37 kB
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()); }); }; import { PISTON, MOJANG_API } from "./constants.js"; /** * Low-level REST client for interacting with Mojang and piston-meta APIs. * Handles fetching version manifests, version details, assets, libraries, and user data. */ export class Rest { constructor() { this.VERSION_MANIFEST_DATA = null; this.initialized = this.initialize(); } initialize() { return __awaiter(this, void 0, void 0, function* () { if (!this.VERSION_MANIFEST_DATA) { try { this.VERSION_MANIFEST_DATA = yield this.fetchVersionManifest(); } catch (error) { console.error("Error initializing version manifest data:", error); throw error; } } }); } fetchVersion(version) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield this.initialized; const versionData = (_b = (_a = this.VERSION_MANIFEST_DATA) === null || _a === void 0 ? void 0 : _a.versions) === null || _b === void 0 ? void 0 : _b.find((v) => v.id === version); if (!versionData) { throw new Error(`Version ${version} not found in manifest`); } const response = yield fetch(versionData.url); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return yield response.json(); }); } fetchVersions(type) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield this.initialized; const versions = (_b = (_a = this.VERSION_MANIFEST_DATA) === null || _a === void 0 ? void 0 : _a.versions) === null || _b === void 0 ? void 0 : _b.filter((v) => v.type === type); if (!versions || versions.length === 0) { throw new Error(`No versions found for type ${type}`); } return versions; }); } fetchLatestVersion(type) { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield this.initialized; const versions = (_b = (_a = this.VERSION_MANIFEST_DATA) === null || _a === void 0 ? void 0 : _a.versions) === null || _b === void 0 ? void 0 : _b.filter((v) => v.type === type); if (!versions || versions.length === 0) { throw new Error(`No versions found for type ${type}`); } const latestVersion = versions.reduce((latest, current) => { return new Date(latest.releaseTime) > new Date(current.releaseTime) ? latest : current; }); return latestVersion; }); } /** * Fetch the Minecraft version manifest from piston-meta. * @returns The version manifest JSON. */ fetchVersionManifest() { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(PISTON.VERSION_MANIFEST); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return yield response.json(); }); } /** * Fetch the asset index for a specific version. * @param version The version ID. * @returns The asset index JSON. */ fetchAssetIndex(version) { return __awaiter(this, void 0, void 0, function* () { var _a; const details = yield this.fetchVersion(version); if (!((_a = details.assetIndex) === null || _a === void 0 ? void 0 : _a.url)) throw new Error("No asset index for version " + version); const response = yield fetch(details.assetIndex.url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.json(); }); } /** * Download a Minecraft library JAR file as an ArrayBuffer. * @param libraryPath The path to the library. * @returns The library file as an ArrayBuffer. */ fetchLibrary(libraryPath) { return __awaiter(this, void 0, void 0, function* () { const url = PISTON.LIBRARIES_BASE + libraryPath; const response = yield fetch(url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.arrayBuffer(); }); } /** * Fetch a Mojang user profile by username. * @param username The Minecraft username. * @returns The user profile JSON. */ fetchUserProfile(username) { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(MOJANG_API.USER_PROFILE + encodeURIComponent(username)); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.json(); }); } /** * Fetch the name history for a given UUID. * @param uuid The user's UUID. * @returns The name history array. */ fetchNameHistory(uuid) { return __awaiter(this, void 0, void 0, function* () { const url = MOJANG_API.NAME_HISTORY.replace("{uuid}", uuid.replace(/-/g, "")); const response = yield fetch(url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.json(); }); } /** * Fetch the skin and/or cape textures for a user by UUID. * @param uuid The user's UUID (with or without dashes). * @returns The textures object containing skin/cape URLs, or null if not found. */ fetchUserTextures(uuid) { return __awaiter(this, void 0, void 0, function* () { var _a; const url = `https://sessionserver.mojang.com/session/minecraft/profile/${uuid.replace(/-/g, "")}`; const response = yield fetch(url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); const data = yield response.json(); const texturesProperty = (_a = data.properties) === null || _a === void 0 ? void 0 : _a.find((p) => p.name === "textures"); if (!texturesProperty) return null; const decoded = JSON.parse(atob(texturesProperty.value)); return decoded.textures || null; }); } } /** * Fetch the Minecraft version manifest from piston-meta. * @returns The version manifest JSON. */ export function fetchVersionManifest() { return __awaiter(this, void 0, void 0, function* () { const response = yield fetch(PISTON.VERSION_MANIFEST); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.json(); }); } /** * Fetch detailed information about a specific Minecraft version. * @param version The version ID. * @returns The version details JSON. */ export function fetchVersionDetails(version) { return __awaiter(this, void 0, void 0, function* () { const manifest = yield fetchVersionManifest(); const versionData = manifest.versions.find((v) => v.id === version); if (!versionData) throw new Error(`Version ${version} not found`); const response = yield fetch(versionData.url); if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); return yield response.json(); }); } //# sourceMappingURL=rest.js.map