From 2d62ff668980ca2325e8916b7c4a1ab4cecca2fd Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sun, 13 Apr 2025 18:10:17 +0200 Subject: [PATCH] Reduce type duplication --- packages/lib/src/ModStore.ts | 39 +++++---------------------- packages/lib/src/data/messages_mod.ts | 12 ++++----- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/packages/lib/src/ModStore.ts b/packages/lib/src/ModStore.ts index 7b97e609..f7a6a3ca 100644 --- a/packages/lib/src/ModStore.ts +++ b/packages/lib/src/ModStore.ts @@ -6,43 +6,16 @@ import { integerModVersion, ModInfo, ModPack, ModRecord } from "./data"; import TypedEventEmitter from "./TypedEventEmitter"; import { safeOutputFile } from "./file_ops"; import { Writable } from "stream"; +import { ModPortalReleaseSchema, ModPortalDetailsSchema } from "./data/messages_mod"; export interface ModStoreEvents { /** A stored mod was created, updated or deleted */ change: (mod: ModInfo) => void; } -interface ModDownloadRequest { - name: string; - version?: string; - sha1?: string; -} - -interface ModRelease { - download_url: string, - file_name: string, - info_json: { factorio_version: string; }, - released_at: string, // ISO 8601 - version: string, - sha1: string; -} - - -interface ModDetails { - name: string, - title: string, - owner: string, - summary: string, - downloads_count: number, - category: string, - score?: number, - releases?: Array, - latest_release?: ModRelease, -} - interface ModsInfoResponse { pagination: any, - results: Array, + results: Array>, } /** @@ -61,7 +34,7 @@ interface ModPortalPagination { */ interface ModPortalResponse { pagination: ModPortalPagination; - results: ModDetails[]; + results: Static[]; } export default class ModStore extends TypedEventEmitter { @@ -182,7 +155,7 @@ export default class ModStore extends TypedEventEmitter, version: string, + private async downloadModFromReleases(releases: Array>, version: string, username: string, token: string ) { const selectedRelease = releases.find(release => release.version === version); @@ -325,10 +298,10 @@ export default class ModStore extends TypedEventEmitter { + ): Promise[]> { logger.info(`Fetching all mods for Factorio version ${factorioVersion}...`); let currentPage = 1; - let allMods: ModDetails[] = []; + let allMods: Static[] = []; let hasMorePages = true; while (hasMorePages) { diff --git a/packages/lib/src/data/messages_mod.ts b/packages/lib/src/data/messages_mod.ts index 929eb13e..b5a4d17a 100644 --- a/packages/lib/src/data/messages_mod.ts +++ b/packages/lib/src/data/messages_mod.ts @@ -198,7 +198,7 @@ export class ModSearchRequest { } // Define the structure for the latest release info from the portal -const ModPortalReleaseSchema = Type.Object({ +export const ModPortalReleaseSchema = Type.Object({ version: Type.String(), // Match the structure from ModStore's ModRelease/ModDetails info_json: Type.Object({ factorio_version: Type.String() }), @@ -210,16 +210,16 @@ const ModPortalReleaseSchema = Type.Object({ // Define the structure for mod details returned by the portal API // This should align with the ModDetails interface in ModStore.ts -const ModPortalDetailsSchema = Type.Object({ +export const ModPortalDetailsSchema = Type.Object({ name: Type.String(), title: Type.String(), summary: Type.String(), owner: Type.String(), downloads_count: Type.Integer(), - category: Type.Optional(Type.String()), // Add optional category - score: Type.Optional(Type.Number()), // Add optional score + category: Type.Optional(Type.String()), + score: Type.Optional(Type.Number()), latest_release: Type.Optional(ModPortalReleaseSchema), - // Add releases array if needed: releases: Type.Optional(Type.Array(ModPortalReleaseSchema)), + releases: Type.Optional(Type.Array(ModPortalReleaseSchema)), }); export class ModPortalGetAllRequest { @@ -227,7 +227,7 @@ export class ModPortalGetAllRequest { static type = "request" as const; static src = "control" as const; static dst = "controller" as const; - static permission = "core.mod.search_portal" as const; // Reuse search permission for now + static permission = "core.mod.search_portal" as const; // Define allowed Factorio versions static allowedVersions = [