UNPKG

@xmcl/modrinth

Version:

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

336 lines (334 loc) 11.4 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // index.ts var modrinth_exports = {}; __export(modrinth_exports, { ModerinthApiError: () => ModerinthApiError, ModrinthV2Client: () => ModrinthV2Client }); module.exports = __toCommonJS(modrinth_exports); 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 { baseUrl; fetch; headers; constructor(options) { this.baseUrl = (options == null ? void 0 : options.baseUrl) ?? "https://api.modrinth.com"; this.headers = (options == null ? void 0 : options.headers) || {}; this.fetch = (options == null ? void 0 : options.fetch) || ((...args) => fetch(...args)); } /** * @see https://docs.modrinth.com/#tag/projects/operation/searchProjects */ async searchProjects(options, signal) { var _a, _b; 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", ((_a = options.offset) == null ? void 0 : _a.toString()) ?? "0"); url.searchParams.append("limit", ((_b = options.limit) == null ? void 0 : _b.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; } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ModerinthApiError, ModrinthV2Client }); //# sourceMappingURL=index.js.map