Path: blob/main/types/index-global-layout.test-d.ts
107 views
import fastify from 'fastify'1import fastifyView, { FastifyViewOptions } from '..'2import { expectAssignable } from 'tsd'3import * as path from 'node:path'45interface Locals {6appVersion: string,7}89declare module 'fastify' {10interface FastifyReply {11locals: Partial<Locals> | undefined12}13}14const app = fastify()1516app.register(fastifyView, {17engine: {18ejs: require('ejs'),19},20templates: 'templates',21includeViewExtension: true,22defaultContext: {23dev: true,24},25options: {},26charset: 'utf-8',27layout: 'layout-ts',28maxCache: 100,29production: false,30root: path.resolve(__dirname, '../templates'),31viewExt: 'ejs',32})3334app.get('/', (_request, reply) => {35reply.view('/layout-ts-content-no-data')36})3738app.get('/data', (_request, reply) => {39if (!reply.locals) {40reply.locals = {}41}4243// reply.locals.appVersion = 1 // not a valid type44reply.locals.appVersion = '4.14.0'45reply.view('/layout-ts-content-with-data', { text: 'Sample data' })46})4748app.get('/dataTyped', (_request, reply) => {49if (!reply.locals) {50reply.locals = {}51}5253// reply.locals.appVersion = 1 // not a valid type54reply.locals.appVersion = '4.14.0'55reply.view<{ text: string; }>('/layout-ts-content-with-data', { text: 'Sample data' })56})5758app.listen({ port: 3000 }, (err, address) => {59if (err) throw err60console.log(`server listening on ${address} ...`)61})6263expectAssignable<FastifyViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })646566