diff --git a/backend/src/constants/base-configuration.ts b/backend/src/constants/base-configuration.ts index ced6be919..799aa71cb 100644 --- a/backend/src/constants/base-configuration.ts +++ b/backend/src/constants/base-configuration.ts @@ -117,7 +117,8 @@ type BooleanSchema = { type StringSchema = { type: "string"; } & BaseSchema; -type ArraySchema = { + +type ArraySchema = { type: "array"; items: Schema[number]; } & BaseSchema; @@ -128,7 +129,7 @@ type ObjectSchema = { } & BaseSchema; type Schema = { - [P in keyof T]: T[P] extends any[] + [P in keyof T]: T[P] extends unknown[] ? ArraySchema : T[P] extends number ? NumberSchema diff --git a/backend/src/dal/config.ts b/backend/src/dal/config.ts index 046d162f6..ac7fb1ee7 100644 --- a/backend/src/dal/config.ts +++ b/backend/src/dal/config.ts @@ -24,16 +24,16 @@ const configLegacyProperties = [ export async function saveConfig( uid: string, - config: object + config: SharedTypes.Config ): Promise { const configChanges = _.mapKeys(config, (_value, key) => `config.${key}`); const unset = _.fromPairs( _.map(configLegacyProperties, (key) => [`config.${key}`, ""]) - ); + ) as Record; return await db - .collection("configs") + .collection("configs") .updateOne( { uid }, { $set: configChanges, $unset: unset }, @@ -41,11 +41,15 @@ export async function saveConfig( ); } -export async function getConfig(uid: string): Promise { - const config = await db.collection("configs").findOne({ uid }); +export async function getConfig( + uid: string +): Promise { + const config = await db + .collection("configs") + .findOne({ uid }); return config; } -export async function deleteConfig(uid: string): Promise { - return await db.collection("configs").deleteOne({ uid }); +export async function deleteConfig(uid: string): Promise { + await db.collection("configs").deleteOne({ uid }); } diff --git a/backend/src/dal/preset.ts b/backend/src/dal/preset.ts index 4b4fcb3f3..1cfd5e759 100644 --- a/backend/src/dal/preset.ts +++ b/backend/src/dal/preset.ts @@ -34,7 +34,7 @@ export async function getPresets(uid: string): Promise { export async function addPreset( uid: string, name: string, - config: any + config: SharedTypes.ConfigPreset ): Promise { const presets = await getPresets(uid); if (presets.length >= MAX_PRESETS) { @@ -56,7 +56,7 @@ export async function editPreset( uid: string, presetId: string, name: string, - config: any + config: SharedTypes.ConfigPreset ): Promise { const presetUpdates = config !== undefined && config !== null && Object.keys(config).length > 0 diff --git a/backend/src/dal/user.ts b/backend/src/dal/user.ts index 8ac560851..d6715c4bd 100644 --- a/backend/src/dal/user.ts +++ b/backend/src/dal/user.ts @@ -4,7 +4,7 @@ import { updateUserEmail } from "../utils/auth"; import { canFunboxGetPb, checkAndUpdatePb } from "../utils/pb"; import * as db from "../init/db"; import MonkeyError from "../utils/error"; -import { Collection, ObjectId, WithId, Long, UpdateFilter } from "mongodb"; +import { Collection, ObjectId, Long, UpdateFilter } from "mongodb"; import Logger from "../utils/logger"; import { flattenObjectDeep, isToday, isYesterday } from "../utils/misc"; diff --git a/backend/src/middlewares/error.ts b/backend/src/middlewares/error.ts index 6bf21b057..8a8e02c10 100644 --- a/backend/src/middlewares/error.ts +++ b/backend/src/middlewares/error.ts @@ -7,6 +7,19 @@ import { NextFunction, Response } from "express"; import { MonkeyResponse, handleMonkeyResponse } from "../utils/monkey-response"; import { recordClientErrorByVersion } from "../utils/prometheus"; import { isDevEnvironment } from "../utils/misc"; +import { ObjectId } from "mongodb"; + +type DBError = { + _id: ObjectId; + timestamp: number; + status: number; + uid: string; + message: string; + stack?: string; + endpoint: string; + method: string; + url: string; +}; async function errorHandlingMiddleware( error: Error, @@ -56,7 +69,7 @@ async function errorHandlingMiddleware( `${monkeyResponse.status} ${errorId} ${error.message} ${error.stack}`, uid ); - await db.collection("errors").insertOne({ + await db.collection("errors").insertOne({ _id: errorId, timestamp: Date.now(), status: monkeyResponse.status, diff --git a/backend/src/queues/george-queue.ts b/backend/src/queues/george-queue.ts index 0c5f731a9..7b884f8b2 100644 --- a/backend/src/queues/george-queue.ts +++ b/backend/src/queues/george-queue.ts @@ -1,13 +1,14 @@ +import { LbEntryWithRank } from "../utils/daily-leaderboards"; import { MonkeyQueue } from "./monkey-queue"; const QUEUE_NAME = "george-tasks"; type GeorgeTask = { name: string; - args: any[]; + args: unknown[]; }; -function buildGeorgeTask(taskName: string, taskArgs: any[]): GeorgeTask { +function buildGeorgeTask(taskName: string, taskArgs: unknown[]): GeorgeTask { return { name: taskName, args: taskArgs, @@ -60,7 +61,7 @@ class GeorgeQueue extends MonkeyQueue { } async announceLeaderboardUpdate( - newRecords: any[], + newRecords: SharedTypes.LeaderboardEntry[], leaderboardId: string ): Promise { const taskName = "announceLeaderboardUpdate"; @@ -88,7 +89,7 @@ class GeorgeQueue extends MonkeyQueue { async announceDailyLeaderboardTopResults( leaderboardId: string, leaderboardTimestamp: number, - topResults: any[] + topResults: LbEntryWithRank[] ): Promise { const taskName = "announceDailyLeaderboardTopResults"; diff --git a/backend/src/utils/daily-leaderboards.ts b/backend/src/utils/daily-leaderboards.ts index 232cb67ab..2decfd777 100644 --- a/backend/src/utils/daily-leaderboards.ts +++ b/backend/src/utils/daily-leaderboards.ts @@ -23,7 +23,7 @@ type GetRankResponse = { entry: DailyLeaderboardEntry | null; }; -type LbEntryWithRank = { +export type LbEntryWithRank = { rank: number; } & DailyLeaderboardEntry; diff --git a/backend/src/utils/logger.ts b/backend/src/utils/logger.ts index d6ffa6b27..d2ab557bb 100644 --- a/backend/src/utils/logger.ts +++ b/backend/src/utils/logger.ts @@ -17,7 +17,7 @@ type Log = { timestamp: number; uid: string; event: string; - message: string; + message: string | Record; }; const customLevels = { @@ -87,7 +87,7 @@ const logger = createLogger({ const logToDb = async ( event: string, - message: any, + message: string | Record, uid?: string ): Promise => { const logsCollection = db.collection("logs"); diff --git a/backend/src/utils/misc.ts b/backend/src/utils/misc.ts index 4700c39d6..e5177a75d 100644 --- a/backend/src/utils/misc.ts +++ b/backend/src/utils/misc.ts @@ -30,7 +30,7 @@ export function kogasa(cov: number): number { ); } -export function identity(value: any): string { +export function identity(value: unknown): string { return Object.prototype.toString .call(value) .replace(/^\[object\s+([a-z]+)\]$/i, "$1") @@ -122,10 +122,10 @@ export function kogascore(wpm: number, acc: number, timestamp: number): number { } export function flattenObjectDeep( - obj: Record, + obj: Record, prefix = "" -): Record { - const result: Record = {}; +): Record { + const result: Record = {}; const keys = Object.keys(obj); keys.forEach((key) => { @@ -134,7 +134,7 @@ export function flattenObjectDeep( const newPrefix = prefix.length > 0 ? `${prefix}.${key}` : key; if (_.isPlainObject(value)) { - const flattened = flattenObjectDeep(value); + const flattened = flattenObjectDeep(value as Record); const flattenedKeys = Object.keys(flattened); if (flattenedKeys.length === 0) { diff --git a/backend/src/utils/monkey-response.ts b/backend/src/utils/monkey-response.ts index 9e01685b6..ac7e781ee 100644 --- a/backend/src/utils/monkey-response.ts +++ b/backend/src/utils/monkey-response.ts @@ -1,11 +1,15 @@ import { Response } from "express"; import { isCustomCode } from "../constants/monkey-status-codes"; +//TODO FIX ANYS + export class MonkeyResponse { message: string; + // eslint-disable-next-line @typescript-eslint/no-explicit-any data: any; status: number; + // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(message?: string, data?: any, status = 200) { this.message = message ?? "ok"; this.data = data ?? null;