Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register multiple Redis client instances with Typescript #103

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Redis, RedisOptions } from 'ioredis';

declare module 'fastify' {
interface FastifyInstance {
redis: Redis;
redis: Redis & Record<string, Redis>;
}
}

export type FastifyRedisPlugin = RedisOptions &
{
url?: string;
namespace?: string;
} |
{
client: Redis;
namespace?: string;
closeClient?: boolean;
}
export type FastifyRedisPlugin =
| (RedisOptions & {
url?: string;
namespace?: string;
})
| {
client: Redis;
namespace?: string;
closeClient?: boolean;
};

declare const fastifyRedis: FastifyPluginCallback<FastifyRedisPlugin>;

Expand Down
103 changes: 70 additions & 33 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ test('fastify.redis should support url', (t) => {
otherOption: 'foo'
})
this.quit = () => {}
this.info = cb => cb(null, 'info')
this.info = (cb) => cb(null, 'info')
this.on = function (name, handler) {
if (name === 'ready') {
handler(null, 'ready')
}

return this
}
this.off = function () { return this }
this.off = function () {
return this
}

return this
}
Expand Down Expand Up @@ -190,7 +192,10 @@ test('promises support', (t) => {
test('custom client', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const redis = require('redis').createClient({
host: 'localhost',
port: 6379
})

fastify.register(fastifyRedis, { client: redis })

Expand Down Expand Up @@ -218,7 +223,10 @@ test('custom client', (t) => {
test('custom client gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const redis = require('redis').createClient({
host: 'localhost',
port: 6379
})

fastify.register(fastifyRedis, { client: redis, closeClient: true })

Expand Down Expand Up @@ -249,7 +257,10 @@ test('custom client gets closed', (t) => {
test('custom client inside a namespace', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const redis = require('redis').createClient({
host: 'localhost',
port: 6379
})

fastify.register(fastifyRedis, {
namespace: 'test',
Expand Down Expand Up @@ -280,7 +291,10 @@ test('custom client inside a namespace', (t) => {
test('custom client inside a namespace gets closed', (t) => {
t.plan(7)
const fastify = Fastify()
const redis = require('redis').createClient({ host: 'localhost', port: 6379 })
const redis = require('redis').createClient({
host: 'localhost',
port: 6379
})

fastify.register(fastifyRedis, {
namespace: 'test',
Expand Down Expand Up @@ -331,7 +345,10 @@ test('fastify.redis.test should throw with duplicate connection namespaces', (t)
})

fastify.ready((err) => {
t.equal(err.message, `Redis '${namespace}' instance namespace has already been registered`)
t.equal(
err.message,
`Redis '${namespace}' instance namespace has already been registered`
)
})
})

Expand Down Expand Up @@ -391,10 +408,9 @@ test('Should throw when trying to connect on an invalid host', (t) => {
const fastify = Fastify({ pluginTimeout: 20000 })
t.teardown(() => fastify.close())

fastify
.register(fastifyRedis, {
host: 'invalid_host'
})
fastify.register(fastifyRedis, {
host: 'invalid_host'
})

fastify.ready((err) => {
t.ok(err)
Expand All @@ -407,11 +423,10 @@ test('Should not throw when trying to connect on an invalid host but the lazyCon
const fastify = Fastify()
t.teardown(() => fastify.close())

fastify
.register(fastifyRedis, {
host: 'invalid_host',
lazyConnect: true
})
fastify.register(fastifyRedis, {
host: 'invalid_host',
lazyConnect: true
})

fastify.ready((err) => {
t.error(err)
Expand All @@ -427,17 +442,16 @@ test('Should throw authentication error when trying to connect on a valid host w
await unsetRedisPassword(TEST_PASSWORD)
})

setRedisPassword(TEST_PASSWORD)
.then(_ => {
fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: 'my_wrong_secret_password'
})
setRedisPassword(TEST_PASSWORD).then((_) => {
fastify.register(fastifyRedis, {
host: '127.0.0.1',
password: 'my_wrong_secret_password'
})

fastify.ready(err => {
t.ok(err)
})
fastify.ready((err) => {
t.ok(err)
})
})
})

test('Should throw authentication error when trying to connect on a valid host without a password', (t) => {
Expand All @@ -449,14 +463,37 @@ test('Should throw authentication error when trying to connect on a valid host w
await unsetRedisPassword(TEST_PASSWORD)
})

setRedisPassword(TEST_PASSWORD)
.then(_ => {
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})
setRedisPassword(TEST_PASSWORD).then((_) => {
fastify.register(fastifyRedis, {
host: '127.0.0.1'
})

fastify.ready(err => {
t.ok(err)
})
fastify.ready((err) => {
t.ok(err)
})
})
})

test('Registering multiple Redis client instances', (t) => {
t.plan(2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.plan(2)
t.plan(3)


const fastify = Fastify()

fastify
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'hello'
})
.register(fastifyRedis, {
host: '127.0.0.1',
namespace: 'world'
})

fastify.ready((err) => {
t.error(err)
t.ok(fastify.redis.hello)
t.ok(fastify.redis.world)

fastify.close()
})
})