refactor providers

This commit is contained in:
cswimr 2025-02-08 09:01:17 -06:00
parent 7e1f186d62
commit 109155631d
Signed by: cswimr
GPG key ID: 0EC431A8DA8F8087
2 changed files with 10 additions and 24 deletions

View file

@ -1,10 +1,9 @@
import * as toml from "toml"; import * as toml from "toml";
import Resource from "./resource"; import Resource from "./resource";
import { import {
Provider, UrlProvider,
ModrinthProvider, ModrinthProvider,
CurseForgeProvider, CurseForgeProvider,
UrlProvider,
GitHubProvider, GitHubProvider,
} from "./provider"; } from "./provider";
@ -12,7 +11,7 @@ export interface Metafile {
name: string; name: string;
filename: string; filename: string;
side: "server" | "client" | "both"; side: "server" | "client" | "both";
provider: Provider; provider: UrlProvider;
} }
/** /**
@ -52,7 +51,7 @@ export class IndexFileEntry {
return metafile; return metafile;
} }
private parseProvider(parsed: any): Provider { private parseProvider(parsed: any): UrlProvider {
if (parsed.update?.modrinth) { if (parsed.update?.modrinth) {
return new ModrinthProvider( return new ModrinthProvider(
parsed.download.hash, parsed.download.hash,

View file

@ -1,22 +1,11 @@
import Resource from "./resource"; import Resource from "./resource";
export abstract class Provider { export class UrlProvider {
constructor(
public hash: string,
public hashFormat: string,
) {}
abstract get downloadUrl(): Resource;
}
export class UrlProvider extends Provider {
constructor( constructor(
public hash: string, public hash: string,
public hashFormat: string, public hashFormat: string,
private url: string, private url: string,
) { ) {}
super(hash, hashFormat);
}
get downloadUrl(): Resource { get downloadUrl(): Resource {
return new Resource(this.url); return new Resource(this.url);
@ -49,7 +38,7 @@ export class GitHubProvider extends UrlProvider {
} }
} }
export class CurseForgeProvider extends Provider { export class CurseForgeProvider extends UrlProvider {
constructor( constructor(
hash: string, hash: string,
hashFormat: string, hashFormat: string,
@ -57,12 +46,10 @@ export class CurseForgeProvider extends Provider {
public fileId: number, public fileId: number,
public projectId: number, public projectId: number,
) { ) {
super(hash, hashFormat); super(
} hash,
hashFormat,
get downloadUrl(): Resource { `https://www.curseforge.com/api/v1/mods/${projectId}/files/${fileId}/download`,
return new Resource(
`https://www.curseforge.com/api/v1/mods/${this.projectId}/files/${this.fileId}/download`,
); );
} }
} }