-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path+handler.ts
41 lines (37 loc) · 926 Bytes
/
+handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import type { FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import cache from '~/utilities/cache';
export default (async (app) => {
/*
```sh
$ curl --request GET --url http://127.0.0.1:3000/api/hello-world/caching?text=foo
$ curl --request GET --url http://127.0.0.1:3000/api/hello-world/caching?text=bar
```
*/
app.get(
'',
{
schema: {
querystring: Type.Object({
text: Type.String(),
}),
response: {
200: Type.Object({
message: Type.String(),
}),
},
},
},
async (req, reply) => {
const { text } = req.query;
const cached = await cache.wrap(
'myText',
async () => {
return { message: text };
},
5_000,
);
return reply.send(cached);
},
);
}) as FastifyPluginAsyncTypebox;