rateLimiter -> ts, remove resonse handler
This commit is contained in:
1 parent
ffd67150eb
commit
c6ac48b4ff
3 files changed
+2
-72
No files matched your search
@@ -1,6 +1,5 @@
|
|||||||
import express from "express";
|
import express from "express";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import { responseHandler } from "./middleware/v1/responseHandler.js";
|
|
||||||
import cors from "cors";
|
import cors from "cors";
|
||||||
import helmet from "helmet";
|
import helmet from "helmet";
|
||||||
import compression from "compression";
|
import compression from "compression";
|
||||||
@@ -19,9 +18,6 @@ export const createApp = ({ services, controllers, envSettings, frontendPath, op
|
|||||||
// Static files
|
// Static files
|
||||||
app.use(express.static(frontendPath));
|
app.use(express.static(frontendPath));
|
||||||
|
|
||||||
// Response handler
|
|
||||||
app.use(responseHandler);
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
origin: allowedOrigin,
|
origin: allowedOrigin,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import rateLimit from "express-rate-limit";
|
import rateLimit from "express-rate-limit";
|
||||||
|
|
||||||
export const generalApiLimiter = rateLimit({
|
export const generalApiLimiter = rateLimit({
|
||||||
window: 60 * 1000,
|
windowMs: 60 * 1000,
|
||||||
limit: 600,
|
limit: 600,
|
||||||
standardHeaders: true,
|
standardHeaders: true,
|
||||||
legacyHeaders: false,
|
legacyHeaders: false,
|
||||||
@@ -9,7 +9,7 @@ export const generalApiLimiter = rateLimit({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const authApiLimiter = rateLimit({
|
export const authApiLimiter = rateLimit({
|
||||||
window: 60 * 1000,
|
windowMs: 60 * 1000,
|
||||||
limit: 15,
|
limit: 15,
|
||||||
standardHeaders: true,
|
standardHeaders: true,
|
||||||
legacyHeaders: false,
|
legacyHeaders: false,
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
/**
|
|
||||||
* Middleware that adds standardized response methods to the Express response object.
|
|
||||||
* This allows for consistent API responses throughout the application.
|
|
||||||
*
|
|
||||||
* @param {import('express').Request} req - Express request object
|
|
||||||
* @param {import('express').Response} res - Express response object
|
|
||||||
* @param {import('express').NextFunction} next - Express next middleware function
|
|
||||||
*/
|
|
||||||
const responseHandler = (req, res, next) => {
|
|
||||||
/**
|
|
||||||
* Sends a standardized success response
|
|
||||||
*
|
|
||||||
* @param {Object} options - Success response options
|
|
||||||
* @param {number} [options.status=200] - HTTP status code
|
|
||||||
* @param {string} [options.msg="OK"] - Success message
|
|
||||||
* @param {*} [options.data=null] - Response data payload
|
|
||||||
* @returns {Object} Express response object
|
|
||||||
*/
|
|
||||||
res.success = ({ status = 200, msg = "OK", data = null, headers = {} }) => {
|
|
||||||
// Set custom headers if provided
|
|
||||||
Object.entries(headers).forEach(([key, value]) => {
|
|
||||||
res.set(key, value);
|
|
||||||
});
|
|
||||||
|
|
||||||
return res.status(status).json({
|
|
||||||
success: true,
|
|
||||||
msg: msg,
|
|
||||||
data: data,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a standardized error response
|
|
||||||
*
|
|
||||||
* @param {Object} options - Error response options
|
|
||||||
* @param {number} [options.status=500] - HTTP status code
|
|
||||||
* @param {string} [options.msg="Internal server error"] - Error message
|
|
||||||
* @param {*} [options.data=null] - Additional error data (if any)
|
|
||||||
* @returns {Object} Express response object
|
|
||||||
*/
|
|
||||||
res.error = ({ status = 500, msg = "Internal server error", data = null }) => {
|
|
||||||
return res.status(status).json({
|
|
||||||
success: false,
|
|
||||||
msg,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a raw file response (for CSV, PDF, etc.)
|
|
||||||
* @param {Object} options
|
|
||||||
* @param {Buffer|string} options.data - The file content
|
|
||||||
* @param {Object} options.headers - Headers to set (e.g. Content-Type, Content-Disposition)
|
|
||||||
* @param {number} [options.status=200] - HTTP status code
|
|
||||||
*/
|
|
||||||
res.file = ({ data, headers = {}, status = 200 }) => {
|
|
||||||
Object.entries(headers).forEach(([key, value]) => {
|
|
||||||
res.setHeader(key, value);
|
|
||||||
});
|
|
||||||
return res.status(status).send(data);
|
|
||||||
};
|
|
||||||
|
|
||||||
next();
|
|
||||||
};
|
|
||||||
|
|
||||||
export { responseHandler };
|
|
||||||
Reference in new issue
Block a user