-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: types and tests coverage (#110)
* chore: upgrade package dependencies * test: add one more test case * feat (types) !: add namespaced redis instances + explicit naming * refactor: fix a typo * refactor: initialize redis decorator with `Object.create(null)` * chore: add a `lint:fix` command * chore: add a `unit:report` and a `unit:verbose` command * refactor (test/types): actually test our types * test: add one more test case * refactor (types): revert `FastifyRedisPluginOptions` to `FastifyRedisPlugin` name * fix: apply @climba03003 suggestion Co-authored-by: KaKa <climba03003@gmail.com> * fix: apply @climba03003 suggestion Co-authored-by: KaKa <climba03003@gmail.com> * test: add 2 more tests to achieve 100% code coverage * chore: check coverage with tap * chore: add test tap report to .gitignore * refactor (types): deprecate `FastifyRedisPlugin` and use `FastifyRedisPluginOptions` instead Co-authored-by: KaKa <climba03003@gmail.com>
- Loading branch information
1 parent
6ac0dac
commit 089deba
Showing
7 changed files
with
135 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,4 +127,7 @@ yarn.lock | |
|
||
# editor files | ||
.vscode | ||
.idea | ||
.idea | ||
|
||
# test tap report | ||
out.tap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ jsx: false | |
flow: false | ||
jobs: 1 | ||
coverage: true | ||
check-coverage: false | ||
check-coverage: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
import { FastifyPluginCallback } from 'fastify'; | ||
import { Redis, RedisOptions } from 'ioredis'; | ||
|
||
export interface FastifyRedisNamespacedInstance { | ||
[namespace: string]: Redis; | ||
} | ||
|
||
export type FastifyRedis = FastifyRedisNamespacedInstance & Redis; | ||
|
||
declare module 'fastify' { | ||
interface FastifyInstance { | ||
redis: Redis; | ||
redis: FastifyRedis; | ||
} | ||
} | ||
|
||
export type FastifyRedisPlugin = RedisOptions & | ||
export type FastifyRedisPluginOptions = (RedisOptions & | ||
{ | ||
url?: string; | ||
namespace?: string; | ||
} | | ||
{ | ||
}) | { | ||
client: Redis; | ||
namespace?: string; | ||
closeClient?: boolean; | ||
} | ||
|
||
declare const fastifyRedis: FastifyPluginCallback<FastifyRedisPlugin>; | ||
/** | ||
* @deprecated Use `FastifyRedisPluginOptions` instead | ||
*/ | ||
export type FastifyRedisPlugin = FastifyRedisPluginOptions; | ||
|
||
declare const fastifyRedis: FastifyPluginCallback<FastifyRedisPluginOptions>; | ||
export default fastifyRedis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,36 @@ | ||
import Fastify, { FastifyRequest } from 'fastify'; | ||
import fastifyRedis from '../..'; | ||
import IORedis from 'ioredis'; | ||
import Fastify, { FastifyInstance } from 'fastify' | ||
import IORedis, { Redis } from 'ioredis' | ||
import { expectAssignable, expectError, expectType } from 'tsd' | ||
import fastifyRedis, { FastifyRedis, FastifyRedisNamespacedInstance } from '../..' | ||
|
||
const app = Fastify(); | ||
const redis = new IORedis({ host: 'localhost', port: 6379 }); | ||
const app: FastifyInstance = Fastify() | ||
const redis: Redis = new IORedis({ host: 'localhost', port: 6379 }) | ||
|
||
app.register(fastifyRedis, { host: '127.0.0.1' }); | ||
app.register(fastifyRedis, { client: redis, namespace: 'hello', closeClient: true }); | ||
app.register(fastifyRedis, { url: 'redis://127.0.0.1:6379', keepAlive: 0 }); | ||
app.register(fastifyRedis, { host: '127.0.0.1' }) | ||
|
||
app.get('/foo', (req: FastifyRequest, reply) => { | ||
const { redis } = app; | ||
const query = req.query as { | ||
key: string | ||
} | ||
redis.get(query.key, (err, val) => { | ||
reply.send(err || val); | ||
}); | ||
}); | ||
app.register(fastifyRedis, { | ||
client: redis, | ||
closeClient: true, | ||
namespace: 'one' | ||
}) | ||
|
||
app.post('/foo', (req, reply) => { | ||
const { redis } = app; | ||
const body = req.body as { | ||
key: string, | ||
value: string | ||
} | ||
redis.set(body.key, body.value, err => { | ||
reply.send(err || { status: 'ok' }); | ||
}); | ||
}); | ||
app.register(fastifyRedis, { | ||
keepAlive: 0, | ||
namespace: 'two', | ||
url: 'redis://127.0.0.1:6379' | ||
}) | ||
|
||
expectError(app.register(fastifyRedis, { | ||
namespace: 'three', | ||
unknownOption: 'this should trigger a typescript error' | ||
})) | ||
|
||
// Plugin property available | ||
app.after(() => { | ||
expectAssignable<Redis>(app.redis) | ||
expectType<FastifyRedis>(app.redis) | ||
|
||
expectAssignable<FastifyRedisNamespacedInstance>(app.redis) | ||
expectType<Redis>(app.redis.one) | ||
expectType<Redis>(app.redis.two) | ||
}) |