allow-methods
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -0,1 +1,5 @@ | ||
/** | ||
* Allow-methods module | ||
* @module allow-methods | ||
*/ | ||
'use strict'; | ||
@@ -5,22 +9,46 @@ | ||
module.exports = allowMethods; | ||
function allowMethods(methods, message) { | ||
/** | ||
* Create an Express middleware function which errors if an HTTP method is not allowed. | ||
* | ||
* @access public | ||
* @param {Array<String>} [methods=[]] | ||
* The HTTP methods which will not throw 405 errors. | ||
* @param {String} [message] | ||
* The error message text to use if a disallowed method is used. | ||
* @returns {ExpressMiddleware} | ||
* Returns a middleware function. | ||
*/ | ||
module.exports = function allowMethods(methods, message) { | ||
methods = normalizeAllowedMethods(methods || []); | ||
return function(request, response, next) { | ||
if (methods.indexOf(request.method.toUpperCase()) === -1) { | ||
return (request, response, next) => { | ||
if (!methods.includes(request.method.toUpperCase())) { | ||
response.header('Allow', methods.join(', ')); | ||
return next(httpError(405, message)); | ||
return next(httpError(405, message || 'Method Not Allowed')); | ||
} | ||
next(); | ||
}; | ||
} | ||
}; | ||
/** | ||
* Normalise an array of HTTP methods. | ||
* | ||
* @access private | ||
* @param {Array<String>} methods | ||
* The HTTP methods to normalise. | ||
* @returns {Array<String>} | ||
* Returns an array of capitalised HTTP methods. | ||
*/ | ||
function normalizeAllowedMethods(methods) { | ||
methods = Array.prototype.slice.call(methods); | ||
return methods.map(toUpperCase); | ||
return methods.map(method => method.toUpperCase()); | ||
} | ||
function toUpperCase(str) { | ||
return str.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": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Express/connect middleware to handle 405 errors", | ||
@@ -13,3 +13,3 @@ "keywords": [ | ||
], | ||
"author": "Rowan Manning (http://rowanmanning.com/)", | ||
"author": "Rowan Manning (https://rowanmanning.com/)", | ||
"repository": { | ||
@@ -23,21 +23,39 @@ "type": "git", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=12", | ||
"npm": ">=7" | ||
}, | ||
"scripts": { | ||
"test": "npm run test:coverage && npm run test:integration", | ||
"verify": "eslint .", | ||
"test:unit": "mocha 'test/unit/**/*.test.js'", | ||
"test:coverage": "nyc npm run test:unit", | ||
"test:integration": "mocha 'test/integration/**/*.test.js'", | ||
"project:verify": "npx --yes @rowanmanning/validate-project --type git node-library", | ||
"project:fix": "npx --yes @rowanmanning/validate-project --type git node-library --fix" | ||
}, | ||
"dependencies": { | ||
"http-errors": "^1.6.2" | ||
"http-errors": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@rowanmanning/eslint-config": "^1.0.0", | ||
"@rowanmanning/make": "^1.0.0", | ||
"eslint": "^4.9.0", | ||
"mocha": "^4.0.1", | ||
"mockery": "^2.1.0", | ||
"nyc": "^11.2.1", | ||
"proclaim": "^3.5.0", | ||
"sinon": "^4.0.1" | ||
"@rowanmanning/eslint-config": "^3.3.0", | ||
"axios": "^0.26.0", | ||
"chai": "^4.3.6", | ||
"eslint": "^8.9.0", | ||
"express4": "npm:express@^4.17.3", | ||
"express5": "npm:express@^5.0.0-beta.1", | ||
"mocha": "^9.2.1", | ||
"nyc": "^15.1.0", | ||
"testdouble": "^3.16.4" | ||
}, | ||
"main": "./lib/allow-methods.js", | ||
"scripts": { | ||
"test": "make test" | ||
"main": "lib/allow-methods.js", | ||
"nyc": { | ||
"eager": true, | ||
"reporter": [ | ||
"html", | ||
"text" | ||
] | ||
}, | ||
"mocha": { | ||
"timeout": 10000 | ||
} | ||
} |
Allow-Methods | ||
============= | ||
# Allow-Methods | ||
Express/connect middleware to handle 405 errors, when a request method is not supported by your route or application. | ||
[![NPM version][shield-npm]][info-npm] | ||
[![Node.js version support][shield-node]][info-node] | ||
[![Build status][shield-build]][info-build] | ||
[![Code coverage][shield-coverage]][info-coverage] | ||
[![Dependencies][shield-dependencies]][info-dependencies] | ||
[![MIT licensed][shield-license]][info-license] | ||
## Table of Contents | ||
Install | ||
------- | ||
* [Requirements](#requirements) | ||
* [Usage](#usage) | ||
* [Contributing](#contributing) | ||
* [License](#license) | ||
Install Allow-Methods with [npm][npm]: | ||
## Requirements | ||
This library requires the following to run: | ||
* [Node.js](https://nodejs.org/) 12+ | ||
## Usage | ||
Install with [npm](https://www.npmjs.com/): | ||
```sh | ||
@@ -24,8 +30,6 @@ npm install allow-methods | ||
Load the library into your code with a `require` call: | ||
Usage | ||
----- | ||
```js | ||
var allowMethods = require('allow-methods'); | ||
const allowMethods = require('allow-methods'); | ||
``` | ||
@@ -50,4 +54,4 @@ | ||
```js | ||
var express = require('express'); | ||
var app = express(); | ||
const express = require('express'); | ||
const app = express(); | ||
@@ -65,5 +69,5 @@ app | ||
```js | ||
app.use(function (err, req, res, next) { | ||
res.status(err.status || 500).send({ | ||
message: err.message | ||
app.use(function (error, requst, response, next) { | ||
response.status(error.status || 500).send({ | ||
message: error.message | ||
}); | ||
@@ -76,4 +80,4 @@ }); | ||
```js | ||
var express = require('express'); | ||
var app = express(); | ||
const express = require('express'); | ||
const app = express(); | ||
@@ -85,51 +89,10 @@ // Only allow GET/HEAD methods across the entire application | ||
Contributing | ||
------------ | ||
## Contributing | ||
To contribute to Allow-Methods, clone this repo locally and commit your code on a separate branch. | ||
[The contributing guide is available here](docs/contributing.md). All contributors must follow [this library's code of conduct](docs/code_of_conduct.md). | ||
Please write unit tests for your code, and check that everything works by running the following before opening a pull-request: | ||
```sh | ||
make ci | ||
``` | ||
## License | ||
Support and Migration | ||
--------------------- | ||
Allow Methods major versions are normally supported for 6 months after their last minor release. This means that patch-level changes will be added and bugs will be fixed. The table below outlines the end-of-support dates for major versions, and the last minor release for that version. | ||
We also maintain a [migration guide](MIGRATION.md) to help you migrate. | ||
| :grey_question: | Major Version | Last Minor Release | Node.js Versions | Support End Date | | ||
| :-------------- | :-------------- | :----------------- | :--------------- | :--------------- | | ||
| :heart: | 2 | N/A | 4+ | N/A | | ||
| :hourglass: | [1][1.x-branch] | 1.0 | 0.10–7 | 2018-04-16 | | ||
If you're opening issues related to these, please mention the version that the issue relates to. | ||
License | ||
------- | ||
Allow-Methods is licensed under the [MIT][info-license] license. | ||
Licensed under the [MIT](LICENSE) license.<br/> | ||
Copyright © 2015, Rowan Manning | ||
[1.x-branch]: https://github.com/rowanmanning/allow-methods/tree/1.x | ||
[npm]: https://npmjs.org/ | ||
[info-coverage]: https://coveralls.io/github/rowanmanning/allow-methods | ||
[info-dependencies]: https://gemnasium.com/rowanmanning/allow-methods | ||
[info-license]: LICENSE | ||
[info-node]: package.json | ||
[info-npm]: https://www.npmjs.com/package/allow-methods | ||
[info-build]: https://travis-ci.org/rowanmanning/allow-methods | ||
[shield-coverage]: https://img.shields.io/coveralls/rowanmanning/allow-methods.svg | ||
[shield-dependencies]: https://img.shields.io/gemnasium/rowanmanning/allow-methods.svg | ||
[shield-license]: https://img.shields.io/badge/license-MIT-blue.svg | ||
[shield-node]: https://img.shields.io/badge/node.js%20support-4–8-brightgreen.svg | ||
[shield-npm]: https://img.shields.io/npm/v/allow-methods.svg | ||
[shield-build]: https://img.shields.io/travis/rowanmanning/allow-methods/master.svg |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
1
6165
9
4
49
94
+ Addeddepd@2.0.0(transitive)
+ Addedhttp-errors@2.0.0(transitive)
+ Addedstatuses@2.0.1(transitive)
- Removeddepd@1.1.2(transitive)
- Removedhttp-errors@1.8.1(transitive)
- Removedstatuses@1.5.0(transitive)
Updatedhttp-errors@^2.0.0