From 71082eb918f055f07fa45cd44c90fcb0c8dd6a29 Mon Sep 17 00:00:00 2001 From: Madeline <46743919+MaddyUnderStars@users.noreply.github.com> Date: Fri, 20 Jan 2023 18:10:47 +1100 Subject: Add ESLint (#941) * Add eslint, switch to lint-staged for precommit * Fix all ESLint errors * Update GH workflow to check prettier and eslint --- src/api/middlewares/Authentication.ts | 10 ++++++---- src/api/middlewares/RateLimit.ts | 12 ++++-------- src/api/middlewares/TestClient.ts | 2 +- 3 files changed, 11 insertions(+), 13 deletions(-) (limited to 'src/api/middlewares') diff --git a/src/api/middlewares/Authentication.ts b/src/api/middlewares/Authentication.ts index 208c54d6..8e0dcc7c 100644 --- a/src/api/middlewares/Authentication.ts +++ b/src/api/middlewares/Authentication.ts @@ -54,11 +54,12 @@ export const API_PREFIX = /^\/api(\/v\d+)?/; export const API_PREFIX_TRAILING_SLASH = /^\/api(\/v\d+)?\//; declare global { + // eslint-disable-next-line @typescript-eslint/no-namespace namespace Express { interface Request { user_id: string; user_bot: boolean; - token: string; + token: { id: string; iat: number }; rights: Rights; } } @@ -87,7 +88,7 @@ export async function Authentication( try { const { jwtSecret } = Config.get().security; - const { decoded, user }: any = await checkToken( + const { decoded, user } = await checkToken( req.headers.authorization, jwtSecret, ); @@ -97,7 +98,8 @@ export async function Authentication( req.user_bot = user.bot; req.rights = new Rights(Number(user.rights)); return next(); - } catch (error: any) { - return next(new HTTPError(error?.toString(), 400)); + } catch (error) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return next(new HTTPError(error!.toString(), 400)); } } diff --git a/src/api/middlewares/RateLimit.ts b/src/api/middlewares/RateLimit.ts index ae102c94..1a28f356 100644 --- a/src/api/middlewares/RateLimit.ts +++ b/src/api/middlewares/RateLimit.ts @@ -42,7 +42,7 @@ type RateLimit = { expires_at: Date; }; -let Cache = new Map(); +const Cache = new Map(); const EventRateLimit = "RATELIMIT"; export default function rateLimit(opts: { @@ -57,12 +57,8 @@ export default function rateLimit(opts: { error?: boolean; success?: boolean; onlyIp?: boolean; -}): any { - return async ( - req: Request, - res: Response, - next: NextFunction, - ): Promise => { +}) { + return async (req: Request, res: Response, next: NextFunction) => { // exempt user? if so, immediately short circuit if (req.user_id) { const rights = await getRights(req.user_id); @@ -85,7 +81,7 @@ export default function rateLimit(opts: { ) max_hits = opts.MODIFY; - let offender = Cache.get(executor_id + bucket_id); + const offender = Cache.get(executor_id + bucket_id); if (offender) { let reset = offender.expires_at.getTime(); diff --git a/src/api/middlewares/TestClient.ts b/src/api/middlewares/TestClient.ts index 7113af3a..10d7e8f1 100644 --- a/src/api/middlewares/TestClient.ts +++ b/src/api/middlewares/TestClient.ts @@ -19,7 +19,7 @@ import express, { Application } from "express"; import fs from "fs"; import path from "path"; -import fetch, { Response as FetchResponse, Headers } from "node-fetch"; +import fetch, { Response as FetchResponse } from "node-fetch"; import ProxyAgent from "proxy-agent"; import { Config } from "@fosscord/util"; -- cgit 1.5.1