Skip to content

Commit 506f8ff

Browse files
authoredJan 22, 2022
feat: add named export for memory store (#292)
1 parent 79a593f commit 506f8ff

File tree

11 files changed

+96
-18
lines changed

11 files changed

+96
-18
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
},
5151
"scripts": {
5252
"clean": "del-cli dist/ coverage/ *.log *.tmp *.bak *.tgz",
53-
"build:cjs": "esbuild --bundle --format=cjs --outfile=dist/index.cjs --footer:js=\"module.exports = rateLimit; module.exports.default = rateLimit; module.exports.rateLimit = rateLimit;\" source/index.ts",
53+
"build:cjs": "esbuild --bundle --format=cjs --outfile=dist/index.cjs --footer:js=\"module.exports = rateLimit; module.exports.default = rateLimit; module.exports.rateLimit = rateLimit; module.exports.MemoryStore = MemoryStore;\" source/index.ts",
5454
"build:esm": "esbuild --bundle --format=esm --outfile=dist/index.mjs source/index.ts",
5555
"build:types": "dts-bundle-generator --out-file=dist/index.d.ts source/index.ts",
5656
"compile": "run-s clean build:*",

‎readme.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,7 @@ app.post('/create-account', createAccountLimiter, (request, response) => {
139139
To use a custom store:
140140

141141
```ts
142-
import rateLimit from 'express-rate-limit'
143-
import MemoryStore from 'express-rate-limit/memory-store.js'
142+
import rateLimit, { MemoryStore } from 'express-rate-limit'
144143

145144
const apiLimiter = rateLimit({
146145
windowMs: 15 * 60 * 1000, // 15 minutes

‎source/index.ts

+4
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ export * from './types.js'
77
// Export the rateLimit function as a default export and as a named export, if
88
// the default export does not work (see https://github.com/nfriedly/express-rate-limit/issues/280)
99
export { default, default as rateLimit } from './lib.js'
10+
11+
// Export the memory store in case someone wants to use or extend it
12+
// (see https://github.com/nfriedly/express-rate-limit/issues/289)
13+
export { default as MemoryStore } from './memory-store.js'

‎test/external/imports/default-import/js-cjs/source/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33

44
const createServer = require('express')
55
const rateLimit = require('express-rate-limit')
6+
const { MemoryStore } = require('express-rate-limit')
67

78
const app = createServer()
89

9-
app.use(rateLimit({ max: 2 }))
10+
app.use(
11+
rateLimit({
12+
max: 2,
13+
legacyHeaders: false,
14+
standardHeaders: true,
15+
store: new MemoryStore(),
16+
}),
17+
)
1018

1119
app.get('/', (request, response) => response.send('Hello!'))
1220

‎test/external/imports/default-import/js-esm/source/app.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import rateLimit from 'express-rate-limit'
5+
import rateLimit, { MemoryStore } from 'express-rate-limit'
66

77
const app = createServer()
88

9-
app.use(rateLimit({ max: 2 }))
9+
app.use(
10+
rateLimit({
11+
max: 2,
12+
legacyHeaders: false,
13+
standardHeaders: true,
14+
store: new MemoryStore(),
15+
}),
16+
)
1017

1118
app.get('/', (request, response) => response.send('Hello!'))
1219

‎test/external/imports/default-import/ts-cjs/source/app.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import rateLimit, { Store, IncrementResponse } from 'express-rate-limit'
5+
import rateLimit, {
6+
MemoryStore,
7+
Store,
8+
IncrementResponse,
9+
} from 'express-rate-limit'
610

711
class TestStore implements Store {
812
hits: Record<string, number> = {}
@@ -28,7 +32,14 @@ class TestStore implements Store {
2832

2933
const app = createServer()
3034

31-
app.use(rateLimit({ max: 2, store: new TestStore() }))
35+
app.use(
36+
rateLimit({
37+
max: 2,
38+
legacyHeaders: false,
39+
standardHeaders: true,
40+
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
41+
}),
42+
)
3243

3344
app.get('/', (request, response) => response.send('Hello!'))
3445

‎test/external/imports/default-import/ts-esm/source/app.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import rateLimit, { Store, IncrementResponse } from 'express-rate-limit'
5+
import rateLimit, {
6+
MemoryStore,
7+
Store,
8+
IncrementResponse,
9+
} from 'express-rate-limit'
610

711
class TestStore implements Store {
812
hits: Record<string, number> = {}
@@ -28,7 +32,14 @@ class TestStore implements Store {
2832

2933
const app = createServer()
3034

31-
app.use(rateLimit({ max: 2, store: new TestStore() }))
35+
app.use(
36+
rateLimit({
37+
max: 2,
38+
legacyHeaders: false,
39+
standardHeaders: true,
40+
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
41+
}),
42+
)
3243

3344
app.get('/', (request, response) => response.send('Hello!'))
3445

‎test/external/imports/named-import/js-cjs/source/app.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
const createServer = require('express')
5-
const { rateLimit } = require('express-rate-limit')
5+
const { rateLimit, MemoryStore } = require('express-rate-limit')
66

77
const app = createServer()
88

9-
app.use(rateLimit({ max: 2 }))
9+
app.use(
10+
rateLimit({
11+
max: 2,
12+
legacyHeaders: false,
13+
standardHeaders: true,
14+
store: new MemoryStore(),
15+
}),
16+
)
1017

1118
app.get('/', (request, response) => response.send('Hello!'))
1219

‎test/external/imports/named-import/js-esm/source/app.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,18 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import { rateLimit } from 'express-rate-limit'
5+
import { rateLimit, MemoryStore } from 'express-rate-limit'
66

77
const app = createServer()
88

9-
app.use(rateLimit({ max: 2 }))
9+
app.use(
10+
rateLimit({
11+
max: 2,
12+
legacyHeaders: false,
13+
standardHeaders: true,
14+
store: new MemoryStore(),
15+
}),
16+
)
1017

1118
app.get('/', (request, response) => response.send('Hello!'))
1219

‎test/external/imports/named-import/ts-cjs/source/app.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import { rateLimit, Store, IncrementResponse } from 'express-rate-limit'
5+
import {
6+
rateLimit,
7+
MemoryStore,
8+
Store,
9+
IncrementResponse,
10+
} from 'express-rate-limit'
611

712
class TestStore implements Store {
813
hits: Record<string, number> = {}
@@ -28,7 +33,14 @@ class TestStore implements Store {
2833

2934
const app = createServer()
3035

31-
app.use(rateLimit({ max: 2, store: new TestStore() }))
36+
app.use(
37+
rateLimit({
38+
max: 2,
39+
legacyHeaders: false,
40+
standardHeaders: true,
41+
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
42+
}),
43+
)
3244

3345
app.get('/', (request, response) => response.send('Hello!'))
3446

‎test/external/imports/named-import/ts-esm/source/app.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
// Create a basic server that uses express-rate-limit to rate limit requests
33

44
import createServer from 'express'
5-
import { rateLimit, Store, IncrementResponse } from 'express-rate-limit'
5+
import {
6+
rateLimit,
7+
MemoryStore,
8+
Store,
9+
IncrementResponse,
10+
} from 'express-rate-limit'
611

712
class TestStore implements Store {
813
hits: Record<string, number> = {}
@@ -28,7 +33,14 @@ class TestStore implements Store {
2833

2934
const app = createServer()
3035

31-
app.use(rateLimit({ max: 2, store: new TestStore() }))
36+
app.use(
37+
rateLimit({
38+
max: 2,
39+
legacyHeaders: false,
40+
standardHeaders: true,
41+
store: Math.floor(Math.random() * 2) ? new TestStore() : new MemoryStore(),
42+
}),
43+
)
3244

3345
app.get('/', (request, response) => response.send('Hello!'))
3446

0 commit comments

Comments
 (0)
Please sign in to comment.