You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Portfolio/src/utils/json.ts

24 lines
539 B

import { readJson } from "fs-extra";
import z from "zod";
import exists from "@utils/fileExists";
export const readAndParseJsonFile = async <T>(
location: string,
model: z.ZodType<T>
): Promise<T> => {
const fileExists = await exists(location);
if (!fileExists) {
throw new Error(`Could not find json file at: ${location}`);
}
const rawJson: unknown = await readJson(location);
const result = model.safeParse(rawJson);
if (!result.success) throw new Error(`Failed to parse json: ${result.error}`);
return result.data;
};