Reduce type duplication

This commit is contained in:
Danielv123 committed 2025-04-13 18:10:17 +02:00
1 parent 25099c03ba
commit 2d62ff6689
2 files changed
+12 -39

No files matched your search

+6 -33
View File
@@ -6,43 +6,16 @@ import { integerModVersion, ModInfo, ModPack, ModRecord } from "./data";
import TypedEventEmitter from "./TypedEventEmitter"; import TypedEventEmitter from "./TypedEventEmitter";
import { safeOutputFile } from "./file_ops"; import { safeOutputFile } from "./file_ops";
import { Writable } from "stream"; import { Writable } from "stream";
import { ModPortalReleaseSchema, ModPortalDetailsSchema } from "./data/messages_mod";
export interface ModStoreEvents { export interface ModStoreEvents {
/** A stored mod was created, updated or deleted */ /** A stored mod was created, updated or deleted */
change: (mod: ModInfo) => void; 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<ModRelease>,
latest_release?: ModRelease,
}
interface ModsInfoResponse { interface ModsInfoResponse {
pagination: any, pagination: any,
results: Array<ModDetails>, results: Array<Static<typeof ModPortalDetailsSchema>>,
} }
/** /**
@@ -61,7 +34,7 @@ interface ModPortalPagination {
*/ */
interface ModPortalResponse { interface ModPortalResponse {
pagination: ModPortalPagination; pagination: ModPortalPagination;
results: ModDetails[]; results: Static<typeof ModPortalDetailsSchema>[];
} }
export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, ModStoreEvents> { export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, ModStoreEvents> {
@@ -182,7 +155,7 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
return new this(modsDirectory, files); return new this(modsDirectory, files);
} }
private async downloadModFromReleases(releases: Array<ModRelease>, version: string, private async downloadModFromReleases(releases: Array<Static<typeof ModPortalReleaseSchema>>, version: string,
username: string, token: string username: string, token: string
) { ) {
const selectedRelease = releases.find(release => release.version === version); const selectedRelease = releases.find(release => release.version === version);
@@ -325,10 +298,10 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
factorioVersion: string, factorioVersion: string,
pageSize: number, pageSize: number,
hide_deprecated: boolean = false hide_deprecated: boolean = false
): Promise<ModDetails[]> { ): Promise<Static<typeof ModPortalDetailsSchema>[]> {
logger.info(`Fetching all mods for Factorio version ${factorioVersion}...`); logger.info(`Fetching all mods for Factorio version ${factorioVersion}...`);
let currentPage = 1; let currentPage = 1;
let allMods: ModDetails[] = []; let allMods: Static<typeof ModPortalDetailsSchema>[] = [];
let hasMorePages = true; let hasMorePages = true;
while (hasMorePages) { while (hasMorePages) {
+6 -6
View File
@@ -198,7 +198,7 @@ export class ModSearchRequest {
} }
// Define the structure for the latest release info from the portal // Define the structure for the latest release info from the portal
const ModPortalReleaseSchema = Type.Object({ export const ModPortalReleaseSchema = Type.Object({
version: Type.String(), version: Type.String(),
// Match the structure from ModStore's ModRelease/ModDetails // Match the structure from ModStore's ModRelease/ModDetails
info_json: Type.Object({ factorio_version: Type.String() }), 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 // Define the structure for mod details returned by the portal API
// This should align with the ModDetails interface in ModStore.ts // This should align with the ModDetails interface in ModStore.ts
const ModPortalDetailsSchema = Type.Object({ export const ModPortalDetailsSchema = Type.Object({
name: Type.String(), name: Type.String(),
title: Type.String(), title: Type.String(),
summary: Type.String(), summary: Type.String(),
owner: Type.String(), owner: Type.String(),
downloads_count: Type.Integer(), downloads_count: Type.Integer(),
category: Type.Optional(Type.String()), // Add optional category category: Type.Optional(Type.String()),
score: Type.Optional(Type.Number()), // Add optional score score: Type.Optional(Type.Number()),
latest_release: Type.Optional(ModPortalReleaseSchema), 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 { export class ModPortalGetAllRequest {
@@ -227,7 +227,7 @@ export class ModPortalGetAllRequest {
static type = "request" as const; static type = "request" as const;
static src = "control" as const; static src = "control" as const;
static dst = "controller" 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 // Define allowed Factorio versions
static allowedVersions = [ static allowedVersions = [