Skip to content

Commit 2e907df

Browse files
authored
Add TypeScript (#46)
1 parent fa20a2a commit 2e907df

File tree

5 files changed

+73
-2
lines changed

5 files changed

+73
-2
lines changed

index.d.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as Fastify from 'fastify';
2+
import { Redis, RedisOptions } from 'ioredis';
3+
4+
import { Server, IncomingMessage, ServerResponse } from 'http';
5+
import { Http2Server, Http2SecureServer, Http2ServerRequest, Http2ServerResponse } from 'http2';
6+
7+
declare module 'fastify' {
8+
interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> {
9+
redis: Redis;
10+
}
11+
}
12+
13+
declare interface FastifyRedisPlugin<HttpServer, HttpRequest, HttpResponse>
14+
extends Fastify.Plugin<
15+
HttpServer,
16+
HttpRequest,
17+
HttpResponse,
18+
RedisOptions | { client: Redis }
19+
> {}
20+
21+
22+
declare const fastifyRedis: FastifyRedisPlugin<
23+
Server | Http2Server | Http2SecureServer,
24+
IncomingMessage | Http2ServerRequest,
25+
ServerResponse | Http2ServerResponse
26+
>;
27+
28+
export = fastifyRedis;

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
"version": "3.2.0",
44
"description": "Plugin to share a common Redis connection across Fastify.",
55
"main": "index.js",
6+
"types": "index.d.ts",
67
"scripts": {
7-
"test": "npm run lint && npm run unit",
8+
"test": "npm run lint && npm run unit && npm run typescript",
89
"lint": "standard",
910
"unit": "tap test.js",
11+
"typescript": "tsc --project ./test/types/tsconfig.json",
1012
"redis": "docker run -p 6379:6379 --rm redis:5"
1113
},
1214
"repository": {
@@ -28,6 +30,8 @@
2830
},
2931
"homepage": "https://github.com/fastify/fastify-redis#readme",
3032
"devDependencies": {
33+
"@types/ioredis": "^4.0.18",
34+
"@types/node": "^12.11.1",
3135
"fastify": "^2.5.0",
3236
"redis": "^2.8.0",
3337
"standard": "^14.0.2",

test.js renamed to test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const t = require('tap')
44
const test = t.test
55
const Fastify = require('fastify')
6-
const fastifyRedis = require('./index')
6+
const fastifyRedis = require('../index')
77

88
t.beforeEach((done) => {
99
const fastify = Fastify()

test/types/index.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as Fastify from 'fastify';
2+
import * as fastifyRedis from '../..';
3+
import * as IORedis from 'ioredis';
4+
5+
import * as http from 'http';
6+
import * as http2 from 'http2';
7+
8+
const app: Fastify.FastifyInstance<
9+
http.Server | http2.Http2Server | http2.Http2SecureServer,
10+
http.IncomingMessage | http2.Http2ServerRequest,
11+
http.ServerResponse | http2.Http2ServerResponse
12+
> = Fastify();
13+
const redis = new IORedis({ host: 'localhost', port: 6379 });
14+
15+
app.register(fastifyRedis, { host: '127.0.0.1' });
16+
app.register(fastifyRedis, { client: redis });
17+
18+
app.get('/foo', (req, reply) => {
19+
const { redis } = app;
20+
redis.get(req.query.key, (err, val) => {
21+
reply.send(err || val);
22+
});
23+
});
24+
25+
app.post('/foo', (req, reply) => {
26+
const { redis } = app;
27+
redis.set(req.body.key, req.body.value, err => {
28+
reply.send(err || { status: 'ok' });
29+
});
30+
});

test/types/tsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"module": "commonjs",
5+
"noEmit": true,
6+
"strict": true
7+
},
8+
"files": ["./index.ts"]
9+
}

0 commit comments

Comments
 (0)