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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ app.get("/data", (request, reply) => {

// reply.locals.appVersion = 1 // not a valid type
reply.locals.appVersion = '4.14.0'
reply.view("/index", { text: "Sample data" });
reply.view<{ text: string; }>("/index", { text: "Sample data" });
});

```
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify';

declare module "fastify" {
interface FastifyReply {
view<T extends { [key: string]: any; }>(page: string, data: T): FastifyReply;
view(page: string, data?: object): FastifyReply;
}
}
Expand Down
16 changes: 13 additions & 3 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fastify from "fastify";
import pointOfView, {PointOfViewOptions} from "..";
import {expectAssignable} from "tsd";
import pointOfView, { PointOfViewOptions } from "..";
import { expectAssignable } from "tsd";
import * as path from "path";

interface Locals {
Expand Down Expand Up @@ -46,9 +46,19 @@ app.get("/data", (request, reply) => {
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' })
expectAssignable<PointOfViewOptions>({ engine: { twig: require('twig') }, propertyName: 'mobile' })