Skip to content

Commit

Permalink
feat: add named export (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriedly authored Nov 9, 2023
1 parent f576933 commit 37c7bd0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,22 @@ Import it in a CommonJS project (`type: commonjs` or no `type` field in
`package.json`) as follows:

```ts
const RedisStore = require('rate-limit-redis')
const { RedisStore } = require('rate-limit-redis')
```

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

```ts
import RedisStore from 'rate-limit-redis'
import { RedisStore } from 'rate-limit-redis'
```

### Examples

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

```ts
import rateLimit from 'express-rate-limit'
import RedisStore from 'rate-limit-redis'
import { rateLimit } from 'express-rate-limit'
import { RedisStore } from 'rate-limit-redis'
import { createClient } from 'redis'

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

```ts
import rateLimit from 'express-rate-limit'
import RedisStore from 'rate-limit-redis'
import { rateLimit } from 'express-rate-limit'
import { RedisStore } from 'rate-limit-redis'
import RedisClient from 'ioredis'

// Create a `ioredis` client
Expand Down
2 changes: 1 addition & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
export * from './types.js'

// Export the RedisStore class as the default export
export { default } from './lib.js'
export { default, RedisStore } from './lib.js'
2 changes: 1 addition & 1 deletion source/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const parseScriptResponse = (results: RedisReply): ClientRateLimitInfo => {
* A `Store` for the `express-rate-limit` package that stores hit counts in
* Redis.
*/
class RedisStore implements Store {
export class RedisStore implements Store {
/**
* The function used to send raw commands to Redis.
*/
Expand Down
20 changes: 19 additions & 1 deletion test/store-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { createHash } from 'node:crypto'
import { expect, jest } from '@jest/globals'
import { type Options } from 'express-rate-limit'
import MockRedisClient from 'ioredis-mock'
import RedisStore, { type RedisReply } from '../source/index.js'
import DefaultExportRedisStore, {
RedisStore,
type RedisReply,
} from '../source/index.js'

// The mock redis client to use.
const client = new MockRedisClient()
Expand Down Expand Up @@ -196,4 +199,19 @@ describe('redis store test', () => {
expect(await client.get('rl:test-store-one')).toEqual(null)
expect(await client.get('rl:test-store-two')).toEqual(null)
})

it('default export works', async () => {
const store = new DefaultExportRedisStore({ sendCommand })
store.init({ windowMs: 10 } as Options)

const key = 'test-store'

const { totalHits } = await store.increment(key) // => 1

// Ensure the hit count is 1, and the expiry is 10 milliseconds (value of
// `windowMs`).
expect(totalHits).toEqual(1)
expect(Number(await client.get('rl:test-store'))).toEqual(1)
expect(Number(await client.pttl('rl:test-store'))).toEqual(10)
})
})

0 comments on commit 37c7bd0

Please sign in to comment.