diff --git a/index.d.ts b/index.d.ts index 6891fa7a..19ed006d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,14 @@ import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify'; declare module "fastify" { + + interface RouteSpecificOptions { + layout?: string; + } + interface FastifyReply { - view(page: string, data: T): FastifyReply; - view(page: string, data?: object): FastifyReply; + view(page: string, data: T, opts?: RouteSpecificOptions): FastifyReply; + view(page: string, data?: object, opts?: RouteSpecificOptions): FastifyReply; } } diff --git a/templates/layout-ts-content-no-data.ejs b/templates/layout-ts-content-no-data.ejs new file mode 100644 index 00000000..59b2b201 --- /dev/null +++ b/templates/layout-ts-content-no-data.ejs @@ -0,0 +1 @@ +

No data

\ No newline at end of file diff --git a/templates/layout-ts-content-with-data.ejs b/templates/layout-ts-content-with-data.ejs new file mode 100644 index 00000000..3396918b --- /dev/null +++ b/templates/layout-ts-content-with-data.ejs @@ -0,0 +1 @@ +

<%= text %>

\ No newline at end of file diff --git a/templates/layout-ts.ejs b/templates/layout-ts.ejs new file mode 100644 index 00000000..3c467fac --- /dev/null +++ b/templates/layout-ts.ejs @@ -0,0 +1,6 @@ + + + + <%- body -%> + + diff --git a/test/index-global-layout.test-d.ts b/test/index-global-layout.test-d.ts new file mode 100644 index 00000000..2a561afa --- /dev/null +++ b/test/index-global-layout.test-d.ts @@ -0,0 +1,64 @@ +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 | undefined + } +} +const app = fastify(); + +app.register(pointOfView, { + 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" }); +}); + +app.listen({port: 3000}, (err, address) => { + if (err) throw err + console.log(`server listening on ${address} ...`) +}) + +expectAssignable({ engine: { twig: require('twig') }, propertyName: 'mobile' }) diff --git a/test/index.test-d.ts b/test/index.test-d.ts index df28ec79..1d228557 100644 --- a/test/index.test-d.ts +++ b/test/index.test-d.ts @@ -16,7 +16,7 @@ const app = fastify(); app.register(pointOfView, { engine: { - handlebars: require("handlebars"), + ejs: require("ejs"), }, templates: "templates", includeViewExtension: true, @@ -24,7 +24,6 @@ app.register(pointOfView, { dev: true, }, options: {}, - layout: "layout", charset: "utf-8", maxCache: 100, production: false, @@ -56,7 +55,11 @@ app.get("/dataTyped", (request, reply) => { reply.view<{ text: string; }>("/index", { text: "Sample data" }); }); -app.listen(3000, (err, address) => { +app.get("/use-layout", (request, reply) => { + reply.view("/layout-ts-content-with-data", {text: "Using a layout"}, {layout: "/layout-ts"}) +}); + +app.listen({port: 3000}, (err, address) => { if (err) throw err console.log(`server listening on ${address} ...`) })