Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/types/index-global-layout.test-d.ts
107 views
1
import fastify from 'fastify'
2
import fastifyView, { FastifyViewOptions } from '..'
3
import { expectAssignable } from 'tsd'
4
import * as path from 'node:path'
5
6
interface Locals {
7
appVersion: string,
8
}
9
10
declare module 'fastify' {
11
interface FastifyReply {
12
locals: Partial<Locals> | undefined
13
}
14
}
15
const app = fastify()
16
17
app.register(fastifyView, {
18
engine: {
19
ejs: require('ejs'),
20
},
21
templates: 'templates',
22
includeViewExtension: true,
23
defaultContext: {
24
dev: true,
25
},
26
options: {},
27
charset: 'utf-8',
28
layout: 'layout-ts',
29
maxCache: 100,
30
production: false,
31
root: path.resolve(__dirname, '../templates'),
32
viewExt: 'ejs',
33
})
34
35
app.get('/', (_request, reply) => {
36
reply.view('/layout-ts-content-no-data')
37
})
38
39
app.get('/data', (_request, reply) => {
40
if (!reply.locals) {
41
reply.locals = {}
42
}
43
44
// reply.locals.appVersion = 1 // not a valid type
45
reply.locals.appVersion = '4.14.0'
46
reply.view('/layout-ts-content-with-data', { text: 'Sample data' })
47
})
48
49
app.get('/dataTyped', (_request, reply) => {
50
if (!reply.locals) {
51
reply.locals = {}
52
}
53
54
// reply.locals.appVersion = 1 // not a valid type
55
reply.locals.appVersion = '4.14.0'
56
reply.view<{ text: string; }>('/layout-ts-content-with-data', { text: 'Sample data' })
57
})
58
59
app.listen({ port: 3000 }, (err, address) => {
60
if (err) throw err
61
console.log(`server listening on ${address} ...`)
62
})
63
64
expectAssignable<FastifyViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })
65
66