Mostly looking at the types, is there a better approach to this?
import { Constituency } from "@/server/types";
const TAGS = [
"shapes",
"constituency-parties",
"constituency-to-party-candidates",
];
const fileCache: Record<string, object> = {};
type Tags = typeof TAGS;
type Tag = Tags[number];
type PoliticalParty = string;
type Candidate = {
name: string,
twitter: string,
} | {
name: null,
twitter: null,
}
interface Candidates {
conservative: Candidate | null,
labour: Candidate | null,
lib_dem: Candidate | null,
snp: Candidate | null,
}
async function get(name: Tag): Promise<Record<Constituency, unknown>>;
async function get(name: "shapes"): Promise<Record<Constituency, [number, number][]>>;
async function get(name: "constituency-parties"): Promise<Record<Constituency, PoliticalParty>>;
async function get(name: "constituency-to-party-candidates"): Promise<Record<Constituency, Candidates>>;
async function get(name: Tag) {
const file = Bun.file(`public/json/${name}.json`);
const payload = await file.json();
fileCache[name] = payload;
return payload;
}
(Bun.file can be mentally replaced with fsPromises.read for clarity's sake)