UNPKG

@xmcl/modrinth

Version:

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

429 lines (428 loc) 14.1 kB
// 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 { 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; } async getCollections(userId, signal) { const url = new URL(this.baseUrl + `/v3/user/${userId}/collections`); 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; } async uppdateCollectionIcon(collectionId, iconData, mimeType, signal) { const ext = mimeType.split("/")[1]; const url = new URL(this.baseUrl + `/v3/collection/${collectionId}/icon?ext=${ext}`); const response = await this.fetch(url, { method: "PATCH", headers: { ...this.headers, "content-type": mimeType }, body: iconData, signal }); if (response.status !== 200) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } } async createCollection(name, description, projectIds, signal) { const url = new URL(this.baseUrl + "/v3/collection"); const body = JSON.stringify({ name, description, projects: projectIds }, (key, value) => !value ? void 0 : value); const response = await this.fetch(url, { method: "POST", headers: { ...this.headers, "content-type": "application/json" }, body, signal }); if (!response.ok) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } const result = await response.json(); return result; } async updateCollection(collectionId, projectIds, signal) { const url = new URL(this.baseUrl + `/v3/collection/${collectionId}`); const response = await this.fetch(url, { method: "PATCH", headers: { ...this.headers, "content-type": "application/json" }, body: JSON.stringify({ new_projects: projectIds }), signal }); if (!response.ok) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } } async getAuthenticatedUser(signal) { const url = new URL(this.baseUrl + "/v2/user"); const response = await this.fetch(url, { 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/api/operations/followproject/#_top */ async followProject(id, signal) { const url = new URL(this.baseUrl + `/v2/project/${id}/follow`); const response = await this.fetch(url, { method: "POST", headers: this.headers, signal }); if (!response.ok) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } } /** * @see https://docs.modrinth.com/api/operations/unfollowproject/ */ async unfollowProject(id, signal) { const url = new URL(this.baseUrl + `/v2/project/${id}/follow`); const response = await this.fetch(url, { method: "DELETE", headers: this.headers, signal }); if (!response.ok) { throw new ModerinthApiError(url.toString(), response.status, await response.text()); } } /** * @see https://docs.modrinth.com/api/operations/getfollowedprojects/#_top */ async getUserFollowedProjects(userId, signal) { const url = new URL(this.baseUrl + `/v2/user/${userId}/follows`); 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.mjs.map