Merge pull request #3418 from bluewave-labs/fix/middleware-typing
fix: middleware typing
This commit is contained in:
7 files changed
+44
-28
No files matched your search
@@ -37,6 +37,7 @@ export default [
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
rules: {
|
rules: {
|
||||||
|
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }], // Ignore unused variables that start with '_'
|
||||||
"@typescript-eslint/no-explicit-any": "warn", // Warn on 'any' types
|
"@typescript-eslint/no-explicit-any": "warn", // Warn on 'any' types
|
||||||
"mocha/max-top-level-suites": "warn", // Warn if there are too many top-level suites instead of failing
|
"mocha/max-top-level-suites": "warn", // Warn if there are too many top-level suites instead of failing
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { Schema, model, type Types } from "mongoose";
|
import { Schema, model, type Types } from "mongoose";
|
||||||
import type { MaintenanceWindow, DurationUnit } from "@/types/maintenanceWindow.js";
|
import type { MaintenanceWindow } from "@/types/maintenanceWindow.js";
|
||||||
|
|
||||||
type MaintenanceWindowDocumentBase = Omit<MaintenanceWindow, "id" | "monitorId" | "teamId" | "start" | "end" | "createdAt" | "updatedAt"> & {
|
type MaintenanceWindowDocumentBase = Omit<MaintenanceWindow, "id" | "monitorId" | "teamId" | "start" | "end" | "createdAt" | "updatedAt"> & {
|
||||||
monitorId: Types.ObjectId;
|
monitorId: Types.ObjectId;
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
import type { NextFunction, Request, Response } from "express";
|
import type { NextFunction, Request, Response } from "express";
|
||||||
import { logger } from "@/utils/logger.js";
|
import { logger } from "@/utils/logger.js";
|
||||||
|
import { AppError } from "@/utils/AppError.js";
|
||||||
|
|
||||||
const handleErrors = (error: any, req: Request, res: Response, next: NextFunction) => {
|
const handleErrors = (error: unknown, req: Request, res: Response, _next: NextFunction) => {
|
||||||
const status = error.status || 500;
|
const status = error instanceof AppError ? error.status || 500 : 500;
|
||||||
const message = error.message || "Server error";
|
const message = error instanceof AppError ? error.message : "Server error";
|
||||||
const service = error.service || "unknownService";
|
const service = error instanceof AppError ? error.service : "unknownService";
|
||||||
|
const method = error instanceof AppError ? error.method : "unknownMethod";
|
||||||
logger.error({
|
logger.error({
|
||||||
message: message,
|
message: message,
|
||||||
service: service,
|
service: service,
|
||||||
method: error.method,
|
method: method,
|
||||||
stack: error.stack,
|
stack: error instanceof AppError ? error.stack : undefined,
|
||||||
|
details: error instanceof AppError ? error.details : undefined,
|
||||||
});
|
});
|
||||||
res.status(status).json({
|
res.status(status).json({
|
||||||
status,
|
status,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import type { Request, Response, NextFunction } from "express";
|
import type { Request, Response, NextFunction } from "express";
|
||||||
import DOMPurify from "isomorphic-dompurify";
|
import DOMPurify from "isomorphic-dompurify";
|
||||||
|
|
||||||
export const sanitizeInput = (input: any, options = {}) => {
|
export function sanitizeInput(input: string, options?: object): string;
|
||||||
|
export function sanitizeInput(input: unknown, options?: object): unknown;
|
||||||
|
export function sanitizeInput(input: unknown, options = {}): unknown {
|
||||||
if (typeof input !== "string") {
|
if (typeof input !== "string") {
|
||||||
return input;
|
return input;
|
||||||
}
|
}
|
||||||
@@ -15,9 +17,9 @@ export const sanitizeInput = (input: any, options = {}) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return DOMPurify.sanitize(input, defaultConfig);
|
return DOMPurify.sanitize(input, defaultConfig);
|
||||||
};
|
}
|
||||||
|
|
||||||
export const sanitizeObject = (obj: Record<string, any>, options = {}): Record<string, any> => {
|
export const sanitizeObject = (obj: unknown, options = {}): unknown => {
|
||||||
if (typeof obj !== "object" || obj === null) {
|
if (typeof obj !== "object" || obj === null) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
@@ -26,7 +28,7 @@ export const sanitizeObject = (obj: Record<string, any>, options = {}): Record<s
|
|||||||
return obj.map((item) => sanitizeObject(item, options));
|
return obj.map((item) => sanitizeObject(item, options));
|
||||||
}
|
}
|
||||||
|
|
||||||
const sanitized: Record<string, any> = {};
|
const sanitized: Record<string, unknown> = {};
|
||||||
for (const [key, value] of Object.entries(obj)) {
|
for (const [key, value] of Object.entries(obj)) {
|
||||||
if (typeof value === "string") {
|
if (typeof value === "string") {
|
||||||
sanitized[key] = sanitizeInput(value, options);
|
sanitized[key] = sanitizeInput(value, options);
|
||||||
@@ -52,7 +54,12 @@ export const sanitizeBody = (options = {}): ((req: Request, res: Response, next:
|
|||||||
export const sanitizeQuery = (options = {}): ((req: Request, res: Response, next: NextFunction) => void) => {
|
export const sanitizeQuery = (options = {}): ((req: Request, res: Response, next: NextFunction) => void) => {
|
||||||
return (req: Request, res: Response, next: NextFunction) => {
|
return (req: Request, res: Response, next: NextFunction) => {
|
||||||
if (req.query && typeof req.query === "object") {
|
if (req.query && typeof req.query === "object") {
|
||||||
req.query = sanitizeObject(req.query, options);
|
for (const key of Object.keys(req.query)) {
|
||||||
|
const value = req.query[key];
|
||||||
|
if (typeof value === "string") {
|
||||||
|
req.query[key] = sanitizeInput(value, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
next();
|
next();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,10 +3,15 @@ import { NextFunction, Request, Response } from "express";
|
|||||||
import jwt from "jsonwebtoken";
|
import jwt from "jsonwebtoken";
|
||||||
import { AppError } from "@/utils/AppError.js";
|
import { AppError } from "@/utils/AppError.js";
|
||||||
import { Settings } from "@/types/settings.js";
|
import { Settings } from "@/types/settings.js";
|
||||||
|
import type { User } from "@/types/user.js";
|
||||||
|
|
||||||
const SERVICE_NAME = "verifyJWT";
|
const SERVICE_NAME = "verifyJWT";
|
||||||
const TOKEN_PREFIX = "Bearer ";
|
const TOKEN_PREFIX = "Bearer ";
|
||||||
|
|
||||||
|
const isUser = (payload: unknown): payload is User => {
|
||||||
|
return typeof payload === "object" && payload !== null && "id" in payload && "teamId" in payload && "role" in payload;
|
||||||
|
};
|
||||||
|
|
||||||
export const createVerifyJWT = (settingsService: { getSettings: () => Settings }) => {
|
export const createVerifyJWT = (settingsService: { getSettings: () => Settings }) => {
|
||||||
return (req: Request, res: Response, next: NextFunction) => {
|
return (req: Request, res: Response, next: NextFunction) => {
|
||||||
const token = req.headers["authorization"];
|
const token = req.headers["authorization"];
|
||||||
@@ -31,21 +36,22 @@ export const createVerifyJWT = (settingsService: { getSettings: () => Settings }
|
|||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
jwt.verify(parsedToken, jwtSecret, (err: any, decoded: any) => {
|
jwt.verify(parsedToken, jwtSecret, (err: jwt.VerifyErrors | null, decoded: jwt.JwtPayload | string | undefined) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
const error = new AppError({
|
const error = new AppError({
|
||||||
message: "Failed to authenticate token",
|
message: err instanceof Error ? err.message : "Failed to authenticate token",
|
||||||
details: err,
|
|
||||||
status: 401,
|
status: 401,
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
|
details: err instanceof Error ? { error: err } : undefined,
|
||||||
method: "verifyJWT",
|
method: "verifyJWT",
|
||||||
});
|
});
|
||||||
next(error);
|
next(error);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else if (isUser(decoded)) {
|
||||||
// Token is valid, carry on
|
|
||||||
req.user = decoded;
|
req.user = decoded;
|
||||||
next();
|
next();
|
||||||
|
} else {
|
||||||
|
next(new AppError({ message: "Invalid token payload", status: 401, service: SERVICE_NAME, method: "verifyJWT" }));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -360,8 +360,7 @@ export class StatusService implements IStatusService {
|
|||||||
};
|
};
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
throw new AppError({
|
throw new AppError({
|
||||||
message: `Failed to update monitor status: ${error instanceof Error ? error.message : "Unknown error"}`,
|
message: `Failed to update monitor with id ${check.metadata.monitorId} with status: ${error instanceof Error ? error.message : "Unknown error"}`,
|
||||||
details: `Error updating status for monitor with id ${check.metadata.monitorId}`,
|
|
||||||
service: SERVICE_NAME,
|
service: SERVICE_NAME,
|
||||||
method: "updateMonitorStatus",
|
method: "updateMonitorStatus",
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
export interface AppErrorConfig {
|
export interface AppErrorConfig {
|
||||||
message: string;
|
message?: string;
|
||||||
status?: number;
|
status?: number;
|
||||||
service?: string | null;
|
service?: string;
|
||||||
method?: string | null;
|
method?: string;
|
||||||
details?: unknown;
|
details?: Record<string, unknown> | undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class AppError extends Error {
|
export class AppError extends Error {
|
||||||
private status: number;
|
readonly status: number;
|
||||||
private service: string | null;
|
readonly service: string;
|
||||||
private method: string | null;
|
readonly method: string;
|
||||||
private details: unknown;
|
readonly details: Record<string, unknown> | undefined;
|
||||||
|
|
||||||
constructor({ message, status = 500, service = null, method = null, details = null }: AppErrorConfig) {
|
constructor({ message, status = 500, service = "unknownService", method = "unknownMethod", details = undefined }: AppErrorConfig) {
|
||||||
super(message);
|
super(message);
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.service = service;
|
this.service = service;
|
||||||
|
|||||||
Reference in new issue
Block a user