Fix linting errors

This commit is contained in:
Dries Augustyns
2025-12-04 12:46:39 +01:00
parent 73a1cdfefa
commit 04634a447b
34 changed files with 517 additions and 534 deletions
@@ -1,4 +1,4 @@
import {describe, it, expect, beforeEach, vi, afterEach} from 'vitest';
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
import type {NextFunction, Request, Response} from 'express';
import {databaseRequestLogger} from '../requestLogger.js';
import {factories, getPrismaClient} from '../../../../../test/helpers';
@@ -21,7 +21,7 @@ describe('Request Logger Middleware', () => {
method: 'POST',
path: '/v1/send',
ip: '192.168.1.100',
socket: {remoteAddress: '192.168.1.100'} as any,
socket: {remoteAddress: '192.168.1.100'} as Request['socket'],
get: vi.fn((header: string) => {
if (header === 'user-agent') {
return 'Mozilla/5.0 Test Browser';
@@ -50,7 +50,7 @@ describe('Request Logger Middleware', () => {
set statusCode(value: number) {
statusCode = value;
},
json: vi.fn(function (this: any, body: any) {
json: vi.fn(function (this: Response, body: unknown) {
return body;
}),
};
@@ -69,7 +69,7 @@ describe('Request Logger Middleware', () => {
// ========================================
describe('Successful Request Logging', () => {
it('should log successful API request to database', async () => {
const middleware = databaseRequestLogger(req as Request, res as Response, next);
databaseRequestLogger(req as Request, res as Response, next);
// Middleware should call next immediately
expect(next).toHaveBeenCalled();
@@ -334,10 +334,11 @@ describe('Request Logger Middleware', () => {
await res.json!({success: true});
await new Promise(resolve => setTimeout(resolve, 100));
// Should not log when disabled
const loggedRequest = await prisma.apiRequest.findUnique({
where: {id: 'test-request-id-123'},
});
// TODO: Add assertion to verify request was NOT logged when disabled
// const loggedRequest = await prisma.apiRequest.findUnique({
// where: {id: 'test-request-id-123'},
// });
// expect(loggedRequest).toBeNull();
// Restore original value
if (originalEnv !== undefined) {
@@ -378,7 +379,7 @@ describe('Request Logger Middleware', () => {
const resInvalid = {
...res,
locals: {
requestId: null as any, // Invalid request ID - will cause database error
requestId: null as unknown, // Invalid request ID - will cause database error
},
};
@@ -460,7 +461,7 @@ describe('Request Logger Middleware', () => {
});
it('should calculate response size from JSON body', async () => {
const jsonMock = vi.fn(function (this: any, body: any) {
const jsonMock = vi.fn(function (this: Response, body: unknown) {
return body;
});
const resLarge = {
+18 -5
View File
@@ -107,7 +107,7 @@ export const databaseRequestLogger = (req: Request, res: Response, next: NextFun
// Capture the original res.json to log after response
const originalJson = res.json.bind(res);
res.json = function (body: any) {
res.json = function (body: unknown) {
// Call original json method first
const result = originalJson(body);
@@ -124,7 +124,12 @@ export const databaseRequestLogger = (req: Request, res: Response, next: NextFun
* Log the request to the database
* This runs asynchronously and failures are logged but don't affect the response
*/
async function logRequestToDatabase(req: Request, res: Response, startTime: number, responseBody: any): Promise<void> {
async function logRequestToDatabase(
req: Request,
res: Response,
startTime: number,
responseBody: unknown,
): Promise<void> {
try {
const requestId = res.locals.requestId as string | undefined;
const auth = res.locals.auth as {type?: string; userId?: string; projectId?: string} | undefined;
@@ -136,9 +141,17 @@ async function logRequestToDatabase(req: Request, res: Response, startTime: numb
let errorCode: string | undefined;
let errorMessage: string | undefined;
if (statusCode >= 400 && responseBody?.error) {
errorCode = responseBody.error.code;
errorMessage = responseBody.error.message;
if (
statusCode >= 400 &&
responseBody &&
typeof responseBody === 'object' &&
'error' in responseBody &&
responseBody.error &&
typeof responseBody.error === 'object'
) {
const error = responseBody.error as {code?: string; message?: string};
errorCode = error.code;
errorMessage = error.message;
}
// Calculate request/response sizes (approximate)