Skip to content
0

katanakit-js / useReadModuleJson

Function: useReadModuleJson() ​

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

Defined in: src/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 ​

Type ParameterDefault typeDescription
TunknownExpected parsed (and validated) shape.

Parameters ​

ParameterTypeDescription
importMetaUrlstringYour module's import.meta.url.
relativePathstringPath relative to that module.
schema?ZodType<T, unknown, $ZodTypeInternals<T, unknown>>Optional Zod schema for runtime validation.

Returns ​

Promise<FilesystemResult<T>>

Safe Result with the parsed data.

Example ​

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

Released under the MIT License.