outers
Version:
outers - a all in one package for your day to day use
92 lines • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const Middleware_Constant_1 = require("../../Config/Constant/Middleware.Constant"); // Import X-Powered-By Header
// Import Console from Utilities
const Console_log_1 = require("../../Logs/Console.log"); // import Red Console
// Import Serve Function
const JSON_Response_1 = require("../../Response/JSON-Response"); // Import JSON Response
// Import Status Codes
const Code_1 = require("../../StatusCode/Code"); // Import Status Codes
// Main Function
/**
* Middleware function that validates the request method against a list of allowed methods.
* If the request method is not allowed, it sends an error response.
* @param Methods - Optional array of allowed methods. If not provided, all HTTP methods are allowed.
* @param reverse - Optional flag to reverse the validation. If true, the request method must not be in the allowed methods list.
* @returns The middleware function.
*/
exports.default = (Methods, reverse) => {
// Check if the method exists in the allowed methods
const AllowedMethods = Methods !== null && Methods !== void 0 ? Methods : Middleware_Constant_1.AllowedMethods; // Allowed Methods
const Reverse = reverse !== null && reverse !== void 0 ? reverse : false; // Reverse the validation
// Convert to Upper Case in Array if any of the method is in lower case
AllowedMethods.forEach((Method, Index) => {
AllowedMethods[Index] = Method.toUpperCase(); // Convert to Upper Case
});
return (Request, Response, Next) => {
try {
// Change Response X-Powered-By Header & Server Header
Response.setHeader("X-Powered-By", (0, Middleware_Constant_1.XPoweredBy)()); // Set X-Powered-By Header
Response.setHeader("Server", (0, Middleware_Constant_1.ServerName)()); // Set Server Header
// Check if Request has Headers
if (!Request.headers) {
return (0, JSON_Response_1.JSONSendResponse)({
response: Response,
status: false,
statusCode: Code_1.StatusCode.BAD_REQUEST,
Title: "Bad Request",
message: "No headers provided",
data: null,
cookieData: undefined,
contentType: "application/json",
});
}
// Check if Request Method is Allowed if Reverse is false
if (Reverse == false) {
if (AllowedMethods.includes(Request.method.toUpperCase()) === true) {
Next(); // Request Method is Allowed if Reverse is false
}
else {
return (0, JSON_Response_1.JSONSendResponse)({
status: false,
statusCode: Code_1.StatusCode.METHOD_NOT_ALLOWED,
Title: "Method Not Allowed",
message: `The Request Method ${Request.method} is not allowed`,
response: Response,
data: Request.method,
contentType: "application/json",
});
}
}
else if (Reverse == true) {
if (AllowedMethods.includes(Request.method.toUpperCase()) === false) {
Next(); // Request Method is Allowed if Reverse is true
}
else {
return (0, JSON_Response_1.JSONSendResponse)({
status: false,
statusCode: Code_1.StatusCode.METHOD_NOT_ALLOWED,
Title: "Method Not Allowed",
message: `The Request Method ${Request.method} is not allowed`,
response: Response,
data: Request.method,
contentType: "application/json",
});
}
}
}
catch (error) {
(0, Console_log_1.red)(error); // Send Error Response
return (0, JSON_Response_1.JSONSendResponse)({
status: false,
statusCode: Code_1.StatusCode.INTERNAL_SERVER_ERROR,
Title: "Cannot Process Request",
message: "Something went wrong while processing your request. Please try again later.",
response: Response,
data: error,
contentType: "application/json",
}); // Send Error Response
}
}; // Export The main function
};
//# sourceMappingURL=Base.middleware.js.map