Skip to content

Commit

Permalink
refactor: add userId to state type
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik committed Jan 29, 2024
1 parent a0c4972 commit c406d27
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
"rules": {
"@typescript-eslint/no-floating-promises": "off",
"no-duplicate-imports": "off",
"no-extra-parens": "off",
"@typescript-eslint/dot-notation": "off"
"no-extra-parens": "off"
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/lib/http/middleware/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const authenticate: ExtendedMiddleware = async (ctx, next) => {
return;
}

ctx.state['userId'] = userId;
ctx.state.userId = userId;
}

return next();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/rate-limiter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ export const rateLimit = async (ctx: ExtendedContext, numberOfProbes: number) =>
let rateLimiter: RateLimiterRedis;
let id: string;

if (ctx.state['userId']) {
if (ctx.state.userId) {
rateLimiter = authenticatedRateLimiter;
id = ctx.state['userId'] as string;
id = ctx.state.userId;
} else {
rateLimiter = anonymousRateLimiter;
id = requestIp.getClientIp(ctx.req) ?? '';
Expand Down
2 changes: 1 addition & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type Koa from 'koa';
import type Router from '@koa/router';
import type { DocsLinkContext } from './lib/http/middleware/docs-link.js';

export type CustomState = Koa.DefaultState;
export type CustomState = Koa.DefaultState & { userId?: string };
export type CustomContext = Koa.DefaultContext & DocsLinkContext;

export type ExtendedContext = Router.RouterContext<CustomState, CustomContext>;
Expand Down

0 comments on commit c406d27

Please sign in to comment.