Skip to content

UseReadModuleJson

katanakit-js / useReadModuleJson

function useReadModuleJson<T>(
importMetaUrl,
relativePath,
schema?
): Promise<FilesystemResult<T>>;

Defined in: infrastructure/filesystem/filesystem.service.ts:557

Reads and parses a JSON file resolved from the caller’s module directory, optionally validating it with a Zod schema.

Type Parameter Default type Description
T unknown Expected parsed (and validated) shape.
Parameter Type Description
importMetaUrl string Your module’s import.meta.url.
relativePath string Path relative to that module.
schema? ZodType<T, unknown, $ZodTypeInternals<T, unknown>> Optional Zod schema for runtime validation.

Promise<FilesystemResult<T>>

Safe Result with the parsed data.

const result = await useReadModuleJson<Book[]>(import.meta.url, "../data/books.json");
if (result.ok) initBooks(result.data);