WaspW
Wasp13mo ago
genyus

Accessing static files

I need my app to access a collection of static JSON Schema files, but it doesn't seem straightforward to do so. The files are currently positioned in the server/utils/schemas directory, but this directory isn't included in .wasp/out/sdk/wasp/dist/ext-src. It is included in .wasp/out/sdk/ext-src, which is where I'm currently having to reference them from, but I suspect this location won't be valid for the deployed application.

As the __directory and __file magic variables aren't available, here's my current, hacky, implementation:

export function getSchema(model: string): any {
    const schemaPath = path.join(dirname(fileURLToPath(import.meta.url)), '../../sdk/wasp/ext-src/server/utils/schemas', `${model}.schema.json`);

    try {
        return JSON.parse(fs.readFileSync(schemaPath, 'utf8'));
    } catch (error) {
        throw new Error(`Error reading or parsing schema file: ${error}`);
    }
}


What's the accepted best practise to accomplish this?
Was this page helpful?