UseReadModuleJson
katanakit-js / useReadModuleJson
Function: useReadModuleJson()
Section titled “Function: 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 Parameters
Section titled “Type Parameters”| Type Parameter | Default type | Description |
|---|---|---|
T |
unknown |
Expected parsed (and validated) shape. |
Parameters
Section titled “Parameters”| 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. |
Returns
Section titled “Returns”Promise<FilesystemResult<T>>
Safe Result with the parsed data.
Example
Section titled “Example”const result = await useReadModuleJson<Book[]>(import.meta.url, "../data/books.json");if (result.ok) initBooks(result.data);