allow-methods
Advanced tools
Comparing version 4.0.1 to 4.1.0
/** | ||
* Allow-methods module | ||
* @module allow-methods | ||
@@ -13,15 +12,15 @@ */ | ||
* @access public | ||
* @param {Array<String>} [methods=[]] | ||
* @param {Array<string>} [methods=[]] | ||
* The HTTP methods which will not throw 405 errors. | ||
* @param {String} [message] | ||
* @param {string} [message] | ||
* The error message text to use if a disallowed method is used. | ||
* @returns {ExpressMiddleware} | ||
* @returns {import('express').Handler} | ||
* Returns a middleware function. | ||
*/ | ||
module.exports = function allowMethods(methods, message) { | ||
methods = normalizeAllowedMethods(methods || []); | ||
module.exports = function allowMethods(methods = [], message = 'Method Not Allowed') { | ||
const normalizedMethods = normalizeAllowedMethods(methods); | ||
return (request, response, next) => { | ||
if (!methods.includes(request.method.toUpperCase())) { | ||
response.header('Allow', methods.join(', ')); | ||
return next(httpError(405, message || 'Method Not Allowed')); | ||
if (!normalizedMethods.includes(request.method.toUpperCase())) { | ||
response.header('Allow', normalizedMethods.join(', ')); | ||
return next(httpError(405, message)); | ||
} | ||
@@ -36,20 +35,14 @@ next(); | ||
* @access private | ||
* @param {Array<String>} methods | ||
* @param {Array<string>} methods | ||
* The HTTP methods to normalise. | ||
* @returns {Array<String>} | ||
* @returns {Array<string>} | ||
* Returns an array of capitalised HTTP methods. | ||
*/ | ||
function normalizeAllowedMethods(methods) { | ||
return methods.map(method => method.toUpperCase()); | ||
if (!Array.isArray(methods)) { | ||
return []; | ||
} | ||
return methods | ||
.filter(method => typeof method === 'string') | ||
.map(method => method.toUpperCase()); | ||
} | ||
/** | ||
* A middleware function. | ||
* @callback ExpressMiddleware | ||
* @param {Object} request | ||
* An Express Request object. | ||
* @param {Object} response | ||
* An Express Response object. | ||
* @returns {undefined} | ||
* Returns nothing. | ||
*/ |
{ | ||
"name": "allow-methods", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Express/connect middleware to handle 405 errors", | ||
@@ -27,3 +27,3 @@ "keywords": [ | ||
"test": "npm run test:coverage && npm run test:integration", | ||
"verify": "eslint .", | ||
"verify": "npm run verify:eslint && npm run verify:types", | ||
"test:unit": "mocha 'test/unit/**/*.test.js'", | ||
@@ -34,3 +34,5 @@ "test:coverage": "nyc npm run test:unit", | ||
"project:fix": "npx --yes @rowanmanning/validate-project@2 --type git node-library --fix", | ||
"prepare": "husky install" | ||
"prepare": "husky install", | ||
"verify:eslint": "eslint .", | ||
"verify:types": "tsc --noEmit --project ./jsconfig.json" | ||
}, | ||
@@ -43,3 +45,4 @@ "dependencies": { | ||
"@commitlint/config-conventional": "^17.0.0", | ||
"@rowanmanning/eslint-config": "^3.3.0", | ||
"@rowanmanning/eslint-config": "^4.0.2", | ||
"@types/express": "^4.17.13", | ||
"axios": "^0.27.2", | ||
@@ -53,3 +56,4 @@ "chai": "^4.3.6", | ||
"nyc": "^15.1.0", | ||
"testdouble": "^3.16.4" | ||
"testdouble": "^3.16.4", | ||
"typescript": "^4.7.3" | ||
}, | ||
@@ -67,2 +71,2 @@ "main": "lib/allow-methods.js", | ||
} | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6807
5
65
14