UNPKG

@xmcl/modrinth

Version:

An implementation of modrinth API (https://docs.modrinth.com/api-spec)

316 lines (314 loc) 10.6 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => { __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); return value; }; // index.ts var ModerinthApiError = class extends Error { constructor(url, status, body) { super(`Fail to fetch modrinth api ${url}. Status=${status}. ${body}`); this.url = url; this.status = status; this.body = body; this.name = "ModerinthApiError"; } }; var ModrinthV2Client = class { constructor(options) { __publicField(this, "baseUrl"); __publicField(this, "fetch"); __publicField(this, "headers"); this.baseUrl = options?.baseUrl ?? "https://api.modrinth.com"; this.headers = options?.headers || {}; this.fetch = options?.fetch || ((...args) => fetch(...args)); } /** * @see https://docs.modrinth.com/#tag/projects/operation/searchProjects */ async searchProjects(options, signal) { const url = new URL(this.baseUrl + "/v2/search"); url.searchParams.append("query", options.query || ""); url.searchParams.append("filter", options.filter || ""); url.searchParams.append("index", options.index || (options.query ? "relevance" : "downloads")); url.searchParams.append("offset", options.offset?.toString() ?? "0"); url.searchParams.append("limit", options.limit?.toString() ?? "10"); if (options.facets) { url.searchParams.append("facets", options.facets); } const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/projects/operation/getProject */ async getProject(projectId, signal) { if (projectId.startsWith("local-")) { projectId = projectId.slice("local-".length); } const url = new URL(this.baseUrl + `/v2/project/${projectId}`); const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const project = await response.json(); return project; } /** * @see https://docs.modrinth.com/#tag/projects/operation/getProject */ async getProjects(projectIds, signal) { const url = new URL(this.baseUrl + "/v2/projects"); url.searchParams.append("ids", JSON.stringify(projectIds)); const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const project = await response.json(); return project; } /** * @see https://docs.modrinth.com/#tag/versions/operation/getProjectVersions */ async getProjectVersions(projectId, { loaders, gameVersions, featured } = {}, signal) { const url = new URL(this.baseUrl + `/v2/project/${projectId}/version`); if (loaders) { url.searchParams.append("loaders", JSON.stringify(loaders)); } if (gameVersions) { url.searchParams.append("game_versions", JSON.stringify(gameVersions)); } if (featured !== void 0) { url.searchParams.append("featured", featured ? "true" : "false"); } const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const versions = await response.json(); return versions; } /** * @see https://docs.modrinth.com/#tag/versions/operation/getVersion */ async getProjectVersion(versionId, signal) { const url = new URL(this.baseUrl + `/v2/version/${versionId}`); const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const version = await response.json(); return version; } /** * @see https://docs.modrinth.com/#tag/versions/operation/getVersions */ async getProjectVersionsById(ids, signal) { const url = new URL(this.baseUrl + "/v2/versions"); url.searchParams.append("ids", JSON.stringify(ids)); const response = await this.fetch(url, { signal, headers: this.headers }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const versions = await response.json(); return versions; } /** * @see https://docs.modrinth.com/#tag/version-files/operation/versionsFromHashes */ async getProjectVersionsByHash(hashes, algorithm = "sha1", signal) { const url = new URL(this.baseUrl + "/v2/version_files"); const response = await this.fetch(url, { method: "POST", body: JSON.stringify({ hashes, algorithm }), headers: { ...this.headers, "content-type": "application/json" }, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const versions = await response.json(); return versions; } /** * @see https://docs.modrinth.com/api-spec#tag/version-files/operation/getLatestVersionsFromHashes */ async getLatestVersionsFromHashes(hashes, { algorithm, loaders = [], gameVersions = [] } = {}, signal) { const url = new URL(this.baseUrl + "/v2/version_files/update"); const response = await this.fetch(url, { method: "POST", body: JSON.stringify({ hashes, algorithm, loaders, game_versions: gameVersions }), headers: { ...this.headers, "content-type": "application/json" }, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const versions = await response.json(); return versions; } /** * @see https://docs.modrinth.com/#tag/version-files/operation/getLatestVersionFromHash */ async getLatestProjectVersion(sha1, { algorithm, loaders = [], gameVersions = [] } = {}, signal) { const url = new URL(this.baseUrl + `/v2/version_file/${sha1}/update`); url.searchParams.append("algorithm", algorithm ?? "sha1"); const response = await this.fetch(url, { method: "POST", body: JSON.stringify({ loaders, game_versions: gameVersions }), headers: { ...this.headers, "content-type": "application/json" }, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const version = await response.json(); return version; } /** * @see https://docs.modrinth.com/#tag/tags/operation/licenseList */ async getLicenseTags(signal) { const url = new URL(this.baseUrl + "/v2/tag/license"); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/tags/operation/categoryList */ async getCategoryTags(signal) { const url = new URL(this.baseUrl + "/v2/tag/category"); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/tags/operation/versionList */ async getGameVersionTags(signal) { const url = new URL(this.baseUrl + "/v2/tag/game_version"); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/tags/operation/loaderList */ async getLoaderTags(signal) { const url = new URL(this.baseUrl + "/v2/tag/loader"); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/teams/operation/getProjectTeamMembers */ async getProjectTeamMembers(projectId, signal) { const url = new URL(this.baseUrl + `/v2/project/${projectId}/members`); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/users/operation/getUser */ async getUser(id, signal) { const url = new URL(this.baseUrl + `/v2/user/${id}`); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } /** * @see https://docs.modrinth.com/#tag/users/operation/getUserProjects */ async getUserProjects(id, signal) { const url = new URL(this.baseUrl + `/v2/user/${id}/projects`); const response = await this.fetch(url, { headers: this.headers, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } }; export { ModerinthApiError, ModrinthV2Client }; //# sourceMappingURL=index.browser.mjs.map