UNPKG

@fastify/view

Version:
60 lines (50 loc) 1.39 kB
import fastify from 'fastify' import fastifyView, { type FastifyViewOptions } from '..' import { expect } from 'tstyche' import * as path from 'node:path' interface Locals { appVersion: string, } declare module 'fastify' { interface FastifyReply { locals: Partial<Locals> | undefined } } const app = fastify() app.register(fastifyView, { engine: { ejs: require('ejs'), }, templates: 'templates', includeViewExtension: true, defaultContext: { dev: true, }, options: {}, charset: 'utf-8', layout: 'layout-ts', maxCache: 100, production: false, root: path.resolve(__dirname, '../templates'), viewExt: 'ejs', }) app.get('/', (_request, reply) => { reply.view('/layout-ts-content-no-data') }) app.get('/data', (_request, reply) => { if (!reply.locals) { reply.locals = {} } // reply.locals.appVersion = 1 // not a valid type reply.locals.appVersion = '4.14.0' reply.view('/layout-ts-content-with-data', { text: 'Sample data' }) }) app.get('/dataTyped', (_request, reply) => { if (!reply.locals) { reply.locals = {} } // reply.locals.appVersion = 1 // not a valid type reply.locals.appVersion = '4.14.0' reply.view<{ text: string; }>('/layout-ts-content-with-data', { text: 'Sample data' }) }) expect<FastifyViewOptions>().type.toBeAssignableFrom({ engine: { twig: require('twig') }, propertyName: 'mobile' })