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
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,48 @@ fastify.addHook('preHandler', function (request, reply, done) {
```
Properties from `reply.locals` will override those from `defaultContext`, but not from `data` parameter provided to `reply.view(template, data)` function.

Typing parameters from `reply.locals` in `typescript`:

```typescript
interface Locals {
appVersion: string,
isAuthorized: boolean,
user?: {
id: number
login: string
}
}

declare module 'fastify' {
interface FastifyReply {
locals: Partial<Locals> | undefined;
}
}

app.addHook('onRequest', (request, reply, done) => {
if (!reply.locals) {
reply.locals = {}
}

reply.locals.isAuthorized = true;
reply.locals.user = {
id: 1,
login: 'Admin'
}
})

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" });
});

```

To require `point-of-view` as a dependency to a [fastify-plugin](https://github.com/fastify/fastify-plugin), add the name `point-of-view` to the dependencies array in the [plugin's opts](https://github.com/fastify/fastify-plugin#dependencies).

```js
Expand Down Expand Up @@ -332,3 +374,4 @@ This project is kindly sponsored by:
## License

Licensed under [MIT](./LICENSE).

1 change: 0 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify';
declare module "fastify" {
interface FastifyReply {
view(page: string, data?: object): FastifyReply;
locals?: object;
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"example": "node example.js",
"example-with-options": "node example-ejs-with-some-options.js",
"example-typescript": "npx ts-node types.test.ts",
"example-typescript": "npx ts-node test/index.test-d.ts",
"test-with-snapshot": "cross-env TAP_SNAPSHOT=1 tap test/test-ejs-with-snapshot.js",
"test": "standard && tap -J test/*.js && tsd"
},
Expand Down
15 changes: 15 additions & 0 deletions test/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ 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, {
Expand All @@ -28,6 +37,12 @@ app.get("/", (request, reply) => {
});

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" });
});

Expand Down