Skip to content

Commit 37c7bd0

Browse files
authored
feat: add named export (#201)
1 parent f576933 commit 37c7bd0

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

readme.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ Import it in a CommonJS project (`type: commonjs` or no `type` field in
5353
`package.json`) as follows:
5454

5555
```ts
56-
const RedisStore = require('rate-limit-redis')
56+
const { RedisStore } = require('rate-limit-redis')
5757
```
5858

5959
Import it in a ESM project (`type: module` in `package.json`) as follows:
6060

6161
```ts
62-
import RedisStore from 'rate-limit-redis'
62+
import { RedisStore } from 'rate-limit-redis'
6363
```
6464

6565
### Examples
6666

6767
To use it with a [`node-redis`](https://github.com/redis/node-redis) client:
6868

6969
```ts
70-
import rateLimit from 'express-rate-limit'
71-
import RedisStore from 'rate-limit-redis'
70+
import { rateLimit } from 'express-rate-limit'
71+
import { RedisStore } from 'rate-limit-redis'
7272
import { createClient } from 'redis'
7373

7474
// Create a `node-redis` client
@@ -97,8 +97,8 @@ app.use(limiter)
9797
To use it with a [`ioredis`](https://github.com/luin/ioredis) client:
9898

9999
```ts
100-
import rateLimit from 'express-rate-limit'
101-
import RedisStore from 'rate-limit-redis'
100+
import { rateLimit } from 'express-rate-limit'
101+
import { RedisStore } from 'rate-limit-redis'
102102
import RedisClient from 'ioredis'
103103

104104
// Create a `ioredis` client

source/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
export * from './types.js'
66

77
// Export the RedisStore class as the default export
8-
export { default } from './lib.js'
8+
export { default, RedisStore } from './lib.js'

source/lib.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const parseScriptResponse = (results: RedisReply): ClientRateLimitInfo => {
4646
* A `Store` for the `express-rate-limit` package that stores hit counts in
4747
* Redis.
4848
*/
49-
class RedisStore implements Store {
49+
export class RedisStore implements Store {
5050
/**
5151
* The function used to send raw commands to Redis.
5252
*/

test/store-test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { createHash } from 'node:crypto'
55
import { expect, jest } from '@jest/globals'
66
import { type Options } from 'express-rate-limit'
77
import MockRedisClient from 'ioredis-mock'
8-
import RedisStore, { type RedisReply } from '../source/index.js'
8+
import DefaultExportRedisStore, {
9+
RedisStore,
10+
type RedisReply,
11+
} from '../source/index.js'
912

1013
// The mock redis client to use.
1114
const client = new MockRedisClient()
@@ -196,4 +199,19 @@ describe('redis store test', () => {
196199
expect(await client.get('rl:test-store-one')).toEqual(null)
197200
expect(await client.get('rl:test-store-two')).toEqual(null)
198201
})
202+
203+
it('default export works', async () => {
204+
const store = new DefaultExportRedisStore({ sendCommand })
205+
store.init({ windowMs: 10 } as Options)
206+
207+
const key = 'test-store'
208+
209+
const { totalHits } = await store.increment(key) // => 1
210+
211+
// Ensure the hit count is 1, and the expiry is 10 milliseconds (value of
212+
// `windowMs`).
213+
expect(totalHits).toEqual(1)
214+
expect(Number(await client.get('rl:test-store'))).toEqual(1)
215+
expect(Number(await client.pttl('rl:test-store'))).toEqual(10)
216+
})
199217
})

0 commit comments

Comments
 (0)