H
Hono3mo ago
safwan

Cannot use `c.get` in `hono-rate-limiter`

I am trying to use the hono-rate-limiter package to create a middleware that enforces rate limits based on which consumer is accessing the API. Here's my implementation:
const consumerRateLimitMiddleware = rateLimiter({
windowMs: 15 * 60 * 1000,
limit: (c) => {
const consumer = c.get("consumer") as StoredConsumer | undefined;

console.log("Consumer Rate Limit Middleware", consumer);
return consumer?.rate_limit || 100; // Default to 100 if not set
},
standardHeaders: "draft-6",
keyGenerator: (c) => {
const consumer = c.get("consumer") as StoredConsumer | undefined;

if (consumer) {
return `consumer:${consumer.id}`;
}

const sessionId = getCookie(c, "sessionId") as string;
const connInfo = getConnInfo(c);
return `${sessionId}:${connInfo.remote.address}`;
},
});
const consumerRateLimitMiddleware = rateLimiter({
windowMs: 15 * 60 * 1000,
limit: (c) => {
const consumer = c.get("consumer") as StoredConsumer | undefined;

console.log("Consumer Rate Limit Middleware", consumer);
return consumer?.rate_limit || 100; // Default to 100 if not set
},
standardHeaders: "draft-6",
keyGenerator: (c) => {
const consumer = c.get("consumer") as StoredConsumer | undefined;

if (consumer) {
return `consumer:${consumer.id}`;
}

const sessionId = getCookie(c, "sessionId") as string;
const connInfo = getConnInfo(c);
return `${sessionId}:${connInfo.remote.address}`;
},
});
Both the limit and keyGenerator properties seem to be typed to have c: Context. But when I try and use c.get(), I get the following error:
Unhandled Rejection at: Promise {
<rejected> TypeError: Cannot read properties of undefined (reading 'get')
at keyGenerator (/Users/curamet/development/kommentar/kommentar/src/driver-adapters/http/hono-zod-openapi/middlewares.ts:159:24)
at r (/Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:1347)
at /Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:2313
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
} reason: TypeError: Cannot read properties of undefined (reading 'get')
at keyGenerator (/Users/curamet/development/kommentar/kommentar/src/driver-adapters/http/hono-zod-openapi/middlewares.ts:159:24)
at r (/Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:1347)
at /Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:2313
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Unhandled Rejection at: Promise {
<rejected> TypeError: Cannot read properties of undefined (reading 'get')
at keyGenerator (/Users/curamet/development/kommentar/kommentar/src/driver-adapters/http/hono-zod-openapi/middlewares.ts:159:24)
at r (/Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:1347)
at /Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:2313
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
} reason: TypeError: Cannot read properties of undefined (reading 'get')
at keyGenerator (/Users/curamet/development/kommentar/kommentar/src/driver-adapters/http/hono-zod-openapi/middlewares.ts:159:24)
at r (/Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:1347)
at /Users/curamet/development/kommentar/kommentar/node_modules/.pnpm/hono-rate-limiter@0.4.2_hono@4.7.11/node_modules/hono-rate-limiter/index.cjs.js:1:2313
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Any idea why this is happening? Am I not allowed to use c.get in the limit and keyGenerator props?
2 Replies
Josh
Josh3mo ago
How are you adding the middleware to your app? It seems like the context isn't flowing through the middleware.
Weespies
Weespies3mo ago
have you defined a store ? is get with an undefined fallback also going to work ?

Did you find this page helpful?