Skip to content

Commit 961c011

Browse files
authored
Removed the definition of locals from index.d.ts (#239)
* Removed the definition of locals from index. d. ts * Update README.md * Removed undeclared types that can be confusing
1 parent f533de2 commit 961c011

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,48 @@ fastify.addHook('preHandler', function (request, reply, done) {
302302
```
303303
Properties from `reply.locals` will override those from `defaultContext`, but not from `data` parameter provided to `reply.view(template, data)` function.
304304

305+
Typing parameters from `reply.locals` in `typescript`:
306+
307+
```typescript
308+
interface Locals {
309+
appVersion: string,
310+
isAuthorized: boolean,
311+
user?: {
312+
id: number
313+
login: string
314+
}
315+
}
316+
317+
declare module 'fastify' {
318+
interface FastifyReply {
319+
locals: Partial<Locals> | undefined;
320+
}
321+
}
322+
323+
app.addHook('onRequest', (request, reply, done) => {
324+
if (!reply.locals) {
325+
reply.locals = {}
326+
}
327+
328+
reply.locals.isAuthorized = true;
329+
reply.locals.user = {
330+
id: 1,
331+
login: 'Admin'
332+
}
333+
})
334+
335+
app.get("/data", (request, reply) => {
336+
if (!reply.locals) {
337+
reply.locals = {}
338+
}
339+
340+
// reply.locals.appVersion = 1 // not a valid type
341+
reply.locals.appVersion = '4.14.0'
342+
reply.view("/index", { text: "Sample data" });
343+
});
344+
345+
```
346+
305347
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).
306348

307349
```js
@@ -332,3 +374,4 @@ This project is kindly sponsored by:
332374
## License
333375

334376
Licensed under [MIT](./LICENSE).
377+

index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { FastifyPlugin, FastifyReply, RawServerBase } from 'fastify';
33
declare module "fastify" {
44
interface FastifyReply {
55
view(page: string, data?: object): FastifyReply;
6-
locals?: object;
76
}
87
}
98

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"scripts": {
77
"example": "node example.js",
88
"example-with-options": "node example-ejs-with-some-options.js",
9-
"example-typescript": "npx ts-node types.test.ts",
9+
"example-typescript": "npx ts-node test/index.test-d.ts",
1010
"test-with-snapshot": "cross-env TAP_SNAPSHOT=1 tap test/test-ejs-with-snapshot.js",
1111
"test": "standard && tap -J test/*.js && tsd"
1212
},

test/index.test-d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import pointOfView, {PointOfViewOptions} from "..";
33
import {expectAssignable} from "tsd";
44
import * as path from "path";
55

6+
interface Locals {
7+
appVersion: string,
8+
}
9+
10+
declare module "fastify" {
11+
interface FastifyReply {
12+
locals: Partial<Locals> | undefined
13+
}
14+
}
615
const app = fastify();
716

817
app.register(pointOfView, {
@@ -28,6 +37,12 @@ app.get("/", (request, reply) => {
2837
});
2938

3039
app.get("/data", (request, reply) => {
40+
if (!reply.locals) {
41+
reply.locals = {}
42+
}
43+
44+
// reply.locals.appVersion = 1 // not a valid type
45+
reply.locals.appVersion = '4.14.0'
3146
reply.view("/index", { text: "Sample data" });
3247
});
3348

0 commit comments

Comments
 (0)