Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
fastify
GitHub Repository: fastify/point-of-view
Path: blob/main/types/index.test-d.ts
107 views
1
import fastify from 'fastify'
2
import fastifyView, { PointOfViewOptions, FastifyViewOptions } from '..'
3
import { expectAssignable, expectNotAssignable, expectDeprecated, expectType } 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
maxCache: 100,
29
production: false,
30
root: path.resolve(__dirname, '../templates'),
31
viewExt: 'ejs',
32
})
33
34
app.get('/', (_request, reply) => {
35
reply.view('/index-with-no-data')
36
})
37
38
app.get('/data', (_request, reply) => {
39
if (!reply.locals) {
40
reply.locals = {}
41
}
42
43
// reply.locals.appVersion = 1 // not a valid type
44
expectNotAssignable<NonNullable<(typeof reply)['locals']>['appVersion']>(1)
45
46
reply.locals.appVersion = '4.14.0'
47
reply.view('/index', { text: 'Sample data' })
48
})
49
50
app.get('/dataTyped', (_request, reply) => {
51
if (!reply.locals) {
52
reply.locals = {}
53
}
54
55
// reply.locals.appVersion = 1 // not a valid type
56
expectNotAssignable<NonNullable<(typeof reply)['locals']>['appVersion']>(1)
57
58
reply.locals.appVersion = '4.14.0'
59
reply.view<{ text: string; }>('/index', { text: 'Sample data' })
60
})
61
62
app.get('/use-layout', (_request, reply) => {
63
reply.view('/layout-ts-content-with-data', { text: 'Using a layout' }, { layout: '/layout-ts' })
64
})
65
66
app.get('/view-async', async (_request, reply) => {
67
expectAssignable<NonNullable<(typeof reply)['locals']>['appVersion']>('4.14.0')
68
expectNotAssignable<NonNullable<(typeof reply)['locals']>['appVersion']>(1)
69
70
type ViewAsyncDataParamType = Parameters<typeof reply.viewAsync>[1]
71
expectAssignable<ViewAsyncDataParamType>({ text: 'Sample data' })
72
expectAssignable<ViewAsyncDataParamType>({ notText: 'Sample data ' })
73
74
const html = await reply.viewAsync('/index', { text: 'Sample data' })
75
expectType<string>(html)
76
return html
77
})
78
79
app.get('/view-async-generic-provided', async (_request, reply) => {
80
type ViewAsyncDataParamType = Parameters<typeof reply.viewAsync<{ text: string; }>>[1]
81
expectAssignable<ViewAsyncDataParamType>({ text: 'Sample data' })
82
expectNotAssignable<ViewAsyncDataParamType>({ notText: 'Sample data ' })
83
84
const html = reply.viewAsync<{ text: string; }>('/index', { text: 'Sample data' })
85
expectType<Promise<string>>(html)
86
return html
87
})
88
89
app.listen({ port: 3000 }, (err, address) => {
90
if (err) throw err
91
console.log(`server listening on ${address} ...`)
92
})
93
94
expectType<Promise<string>>(app.view('/index', {}, { layout: '/layout-ts' }))
95
96
expectAssignable<FastifyViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })
97
98
expectDeprecated({} as PointOfViewOptions)
99
100
const nunjucksApp = fastify()
101
102
nunjucksApp.register(fastifyView, {
103
engine: {
104
nunjucks: require('nunjucks'),
105
},
106
templates: [
107
'templates/nunjucks-layout',
108
'templates/nunjucks-template'
109
],
110
})
111
112
nunjucksApp.get('/', (_request, reply) => {
113
reply.view('index.njk', { text: 'Sample data' })
114
})
115
116
expectType<Promise<string>>(nunjucksApp.view('/', { text: 'Hello world' }))
117
118
expectAssignable<FastifyViewOptions>({ engine: { nunjucks: require('nunjucks') }, templates: 'templates' })
119
120
expectAssignable<FastifyViewOptions>({ engine: { nunjucks: require('nunjucks') }, templates: ['templates/nunjucks-layout', 'templates/nunjucks-template'] })
121
122