From 0dd215e0d7237d722b66e1915d8d600d7cf7100f Mon Sep 17 00:00:00 2001 From: Kir_Antipov Date: Tue, 2 May 2023 09:34:10 +0000 Subject: [PATCH] Moved `modrinth` module --- src/platforms/modrinth/index.ts | 47 ++++++++++++++ src/utils/modrinth/index.ts | 109 -------------------------------- 2 files changed, 47 insertions(+), 109 deletions(-) create mode 100644 src/platforms/modrinth/index.ts delete mode 100644 src/utils/modrinth/index.ts diff --git a/src/platforms/modrinth/index.ts b/src/platforms/modrinth/index.ts new file mode 100644 index 0000000..52ce241 --- /dev/null +++ b/src/platforms/modrinth/index.ts @@ -0,0 +1,47 @@ +export { + ModrinthApiClient, + ModrinthApiOptions, + + MODRINTH_API_URL, + MODRINTH_STAGING_API_URL, +} from "./modrinth-api-client"; + +export { + ModrinthDependencyType, +} from "./modrinth-dependency-type"; + +export { + ModrinthDependency, +} from "./modrinth-dependency"; + +export { + ModrinthGameVersion, +} from "./modrinth-game-version"; + +export { + ModrinthLoader, +} from "./modrinth-loader"; + +export { + ModrinthProject, + ModrinthProjectPatch, +} from "./modrinth-project"; + +export { + ModrinthUnfeatureMode, +} from "./modrinth-unfeature-mode"; + +export { + ModrinthVersion, + ModrinthVersionInit, + ModrinthVersionPatch, + ModrinthVersionSearchTemplate, + UnfeaturableModrinthVersion, +} from "./modrinth-version"; + +export { + ModrinthUploader, + ModrinthUploaderOptions, + ModrinthUploadRequest, + ModrinthUploadReport, +} from "./modrinth-uploader"; diff --git a/src/utils/modrinth/index.ts b/src/utils/modrinth/index.ts deleted file mode 100644 index 9200d3c..0000000 --- a/src/utils/modrinth/index.ts +++ /dev/null @@ -1,109 +0,0 @@ -import FormData from "form-data"; -import fetch, { Response } from "node-fetch"; -import { URLSearchParams } from "url"; -import File from "../io/file"; -import SoftError from "../soft-error"; - -const baseUrl = "https://api.modrinth.com/v2"; - -interface ModrinthProject { - id: string; - slug: string; -} - -interface ModrinthVersion { - id: string; - loaders: string[]; - game_versions: string[]; - featured: boolean; -} - -export function createVersion(modId: string, data: Record, files: File[], token: string): Promise { - data = { - featured: true, - dependencies: [], - ...data, - project_id: modId, - primary_file: files.length ? "0" : undefined, - file_parts: files.map((_, i) => i.toString()) - }; - - const form = new FormData(); - form.append("data", JSON.stringify(data)); - for (let i = 0; i < files.length; ++i) { - const file = files[i]; - form.append(i.toString(), file.getStream(), file.name); - } - - const response = fetch(`${baseUrl}/version`, { - method: "POST", - headers: form.getHeaders({ - Authorization: token, - }), - body: form - }); - - return processResponse(response, undefined, (x, msg) => new SoftError(x, `Failed to upload file: ${msg}`)); -} - -export function getProject(idOrSlug: string): Promise { - return processResponse(fetch(`${baseUrl}/project/${idOrSlug}`), { 404: () => null }); -} - -export function getVersions(idOrSlug: string, loaders?: string[], gameVersions?: string[], featured?: boolean, token?: string): Promise { - const urlParams = new URLSearchParams(); - if (loaders) { - urlParams.append("loaders", JSON.stringify(loaders)); - } - if (gameVersions) { - urlParams.append("game_versions", JSON.stringify(gameVersions)); - } - if (typeof featured === "boolean") { - urlParams.append("featured", String(featured)); - } - - const response = fetch(`${baseUrl}/project/${idOrSlug}/version?${urlParams}`, token ? { - headers: { Authorization: token } - } : undefined); - return processResponse(response, { 404: () => [] }); -} - -export async function modifyVersion(id: string, version: Partial, token: string): Promise { - const response = await fetch(`${baseUrl}/version/${id}`, { - method: "PATCH", - headers: { - "Authorization": token, - "Content-Type": "application/json", - }, - body: JSON.stringify(version) - }); - - return response.ok; -} - -async function processResponse(response: Response | Promise, mappers?: Record T | Promise>, errorFactory?: (isServerError: boolean, message: string, response: Response) => Error | Promise): Promise { - response = await response; - if (response.ok) { - return await response.json(); - } - - const mapper = mappers?.[response.status]; - if (mapper) { - const mapped = await mapper(response); - if (mapped !== undefined) { - return mapped; - } - } - - let errorText = response.statusText; - try { - errorText += `, ${await response.text()}`; - } catch { } - errorText = `${response.status} (${errorText})`; - const isSoftError = response.status === 429 || response.status >= 500; - if (errorFactory) { - throw errorFactory(isSoftError, errorText, response); - } else { - throw new SoftError(isSoftError, errorText); - } -}