Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify';

declare module "fastify" {

interface RouteSpecificOptions {
layout?: string;
}

interface FastifyReply {
view<T extends { [key: string]: any; }>(page: string, data: T): FastifyReply;
view(page: string, data?: object): FastifyReply;
view<T extends { [key: string]: any; }>(page: string, data: T, opts?: RouteSpecificOptions): FastifyReply;
view(page: string, data?: object, opts?: RouteSpecificOptions): FastifyReply;
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/layout-ts-content-no-data.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>No data</p>
1 change: 1 addition & 0 deletions templates/layout-ts-content-with-data.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><%= text %></p>
6 changes: 6 additions & 0 deletions templates/layout-ts.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<head></head>
<body>
<%- body -%>
</body>
</html>
64 changes: 64 additions & 0 deletions test/index-global-layout.test-d.ts
Original file line number Diff line number Diff line change
@@ -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<Locals> | 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<PointOfViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })
9 changes: 6 additions & 3 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ const app = fastify();

app.register(pointOfView, {
engine: {
handlebars: require("handlebars"),
ejs: require("ejs"),
},
templates: "templates",
includeViewExtension: true,
defaultContext: {
dev: true,
},
options: {},
layout: "layout",
charset: "utf-8",
maxCache: 100,
production: false,
Expand Down Expand Up @@ -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} ...`)
})
Expand Down