feat: add a function to fetch version manifest

This commit is contained in:
HerozDotExe 2025-09-28 14:28:17 +02:00
parent f944f1c5e4
commit dd548c5f26
5 changed files with 108 additions and 3 deletions

View file

@ -1 +1,2 @@
export * as java from "./java"
export * as java from "./java"
export * as version from "./version"

24
src/core/version.ts Normal file
View file

@ -0,0 +1,24 @@
import { fetchJson } from "../utils/fetch";
import { Versions, Version } from "../utils/types";
export async function getVersionManifest(versionString: string) {
const versions = await (
await fetchJson<Versions>(
"https://piston-meta.mojang.com/mc/game/version_manifest_v2.json",
)
).versions;
let versionManifestURL = "";
for (const v in versions) {
if (Object.prototype.hasOwnProperty.call(versions, v)) {
const version = versions[v];
if (version.id === versionString) {
versionManifestURL = version.url;
}
}
}
const versionManifest = await fetchJson<Version>(versionManifestURL);
return versionManifest;
}

View file

@ -57,6 +57,73 @@ export type FilesList = {
type: FileType;
};
};
}
};
export type PoolFile = { url: string; path: string };
export type PoolFile = { url: string; path: string };
export type Versions = {
latest: { release: string; snapshot: string };
versions: {
id: string;
type: "snapshot" | "release" | "old_beta" | "old_alpha";
url: string;
time: string;
releaseTime: string;
sha1: string;
complianceLevel: number;
}[];
};
export type Version = {
arguments: {
game: string[];
jvm:
| {
rules: {
action: string;
os: { name?: string; version?: string; arch?: string };
}[];
value: string[] | string;
}
| string[];
};
assetIndex: {
id: string;
sha1: string;
size: number;
totalSize: number;
url: string;
};
assets: string;
complianceLevel: number;
downloads: {
client: File;
client_mappings: File;
server: File;
server_mappings: File;
};
id: string;
javaVersion: {
component: RuntimeComponent;
majorVersion: number;
};
libraries: {
downloads: {
artifacts: {
path: string;
sha1: string;
size: number;
url: string;
};
name: string;
rules: { action: string; os: { name: string } }[];
};
}[];
logging: {
client: {
argument: string;
file: { id: string; sha1: string; size: number; url: string };
type: string;
};
};
};

1
tests/1.21.8.json Normal file

File diff suppressed because one or more lines are too long

12
tests/version.test.ts Normal file
View file

@ -0,0 +1,12 @@
import { expect, test } from "vitest";
import * as nlk from "../dist/index.js";
import fs from "fs/promises";
// ensure same formating
const manifest = JSON.parse(await fs.readFile("./tests/1.21.8.json", { encoding: "utf-8" }));
test("get 1.21.8 version manifest", async () => {
expect(
await nlk.core.version.getVersionManifest("1.21.8"),
).toStrictEqual(manifest);
});