trifid-core
Version:
Trifid Core
43 lines (42 loc) • 1.42 kB
JavaScript
/**
* Render a specific template file.
*
* Configuration fields:
* - path (string, required): the path to the template file to load
* - context (object, optional): context to give to this specific template file (some variables)
* - options (object, optional): options to pass to the Trifid render function (change the title of the page, …)
*/
const factory = async (trifid) => {
const { config, render } = trifid;
const { path } = config;
if (typeof path !== 'string' || !path) {
throw new Error('configuration is missing \'path\' field');
}
const context = (config.context && typeof config.context === 'object')
? config.context
: {};
const options = (config.options && typeof config.options === 'object')
? config.options
: {};
return {
defaultConfiguration: async () => {
return {
methods: ['GET'],
};
},
routeHandler: async () => {
/**
* Route handler.
*
* @param request Request.
* @param reply Reply.
*/
const handler = async (request, reply) => {
reply.status(200).type('text/html').send(await render(request, path, { ...context }, options));
return reply;
};
return handler;
},
};
};
export default factory;