You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
3 years ago
|
import fastify from "fastify";
|
||
|
import pointOfView, { PointOfViewOptions } from "..";
|
||
|
import { expectAssignable } from "tsd";
|
||
|
import * as path from "path";
|
||
|
|
||
|
interface Locals {
|
||
|
appVersion: string,
|
||
|
}
|
||
|
|
||
|
declare module "fastify" {
|
||
|
interface FastifyReply {
|
||
|
locals: Partial<Locals> | undefined
|
||
|
}
|
||
|
}
|
||
|
const app = fastify();
|
||
|
|
||
|
app.register(pointOfView, {
|
||
|
engine: {
|
||
|
handlebars: require("handlebars"),
|
||
|
},
|
||
|
templates: "templates",
|
||
|
includeViewExtension: true,
|
||
|
defaultContext: {
|
||
|
dev: true,
|
||
|
},
|
||
|
options: {},
|
||
|
layout: "layout",
|
||
|
charset: "utf-8",
|
||
|
maxCache: 100,
|
||
|
production: false,
|
||
|
root: path.resolve(__dirname, "../templates"),
|
||
|
viewExt: "ejs",
|
||
|
});
|
||
|
|
||
|
app.get("/", (request, reply) => {
|
||
|
reply.view("/index-with-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("/index", { 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; }>("/index", { text: "Sample data" });
|
||
|
});
|
||
|
|
||
|
app.listen(3000, (err, address) => {
|
||
|
if (err) throw err
|
||
|
console.log(`server listening on ${address} ...`)
|
||
|
})
|
||
|
|
||
|
expectAssignable<PointOfViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })
|