|
1 |
| -import Fastify, { FastifyRequest } from 'fastify'; |
2 |
| -import fastifyRedis from '../..'; |
3 |
| -import IORedis from 'ioredis'; |
| 1 | +import Fastify, { FastifyInstance } from 'fastify' |
| 2 | +import IORedis, { Redis } from 'ioredis' |
| 3 | +import { expectAssignable, expectError, expectType } from 'tsd' |
| 4 | +import fastifyRedis, { FastifyRedis, FastifyRedisNamespacedInstance } from '../..' |
4 | 5 |
|
5 |
| -const app = Fastify(); |
6 |
| -const redis = new IORedis({ host: 'localhost', port: 6379 }); |
| 6 | +const app: FastifyInstance = Fastify() |
| 7 | +const redis: Redis = new IORedis({ host: 'localhost', port: 6379 }) |
7 | 8 |
|
8 |
| -app.register(fastifyRedis, { host: '127.0.0.1' }); |
9 |
| -app.register(fastifyRedis, { client: redis, namespace: 'hello', closeClient: true }); |
10 |
| -app.register(fastifyRedis, { url: 'redis://127.0.0.1:6379', keepAlive: 0 }); |
| 9 | +app.register(fastifyRedis, { host: '127.0.0.1' }) |
11 | 10 |
|
12 |
| -app.get('/foo', (req: FastifyRequest, reply) => { |
13 |
| - const { redis } = app; |
14 |
| - const query = req.query as { |
15 |
| - key: string |
16 |
| - } |
17 |
| - redis.get(query.key, (err, val) => { |
18 |
| - reply.send(err || val); |
19 |
| - }); |
20 |
| -}); |
| 11 | +app.register(fastifyRedis, { |
| 12 | + client: redis, |
| 13 | + closeClient: true, |
| 14 | + namespace: 'one' |
| 15 | +}) |
21 | 16 |
|
22 |
| -app.post('/foo', (req, reply) => { |
23 |
| - const { redis } = app; |
24 |
| - const body = req.body as { |
25 |
| - key: string, |
26 |
| - value: string |
27 |
| - } |
28 |
| - redis.set(body.key, body.value, err => { |
29 |
| - reply.send(err || { status: 'ok' }); |
30 |
| - }); |
31 |
| -}); |
| 17 | +app.register(fastifyRedis, { |
| 18 | + keepAlive: 0, |
| 19 | + namespace: 'two', |
| 20 | + url: 'redis://127.0.0.1:6379' |
| 21 | +}) |
| 22 | + |
| 23 | +expectError(app.register(fastifyRedis, { |
| 24 | + namespace: 'three', |
| 25 | + unknownOption: 'this should trigger a typescript error' |
| 26 | +})) |
| 27 | + |
| 28 | +// Plugin property available |
| 29 | +app.after(() => { |
| 30 | + expectAssignable<Redis>(app.redis) |
| 31 | + expectType<FastifyRedis>(app.redis) |
| 32 | + |
| 33 | + expectAssignable<FastifyRedisNamespacedInstance>(app.redis) |
| 34 | + expectType<Redis>(app.redis.one) |
| 35 | + expectType<Redis>(app.redis.two) |
| 36 | +}) |
0 commit comments