Skip to content

Commit 88d7567

Browse files
committed
fix: remove http-errors dependency
We're now using our own simple error class to avoid including an unnecessary dependency.
1 parent d5b4f6c commit 88d7567

File tree

4 files changed

+36
-79
lines changed

4 files changed

+36
-79
lines changed

lib/allow-methods.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
'use strict';
22

3-
const httpError = require('http-errors');
3+
class MethodNotAllowedError extends Error {
4+
5+
/** @type {string} */
6+
name = 'MethodNotAllowedError';
7+
8+
/** @type {number} */
9+
status = 405;
10+
11+
/** @type {number} */
12+
statusCode = 405;
13+
14+
}
415

516
/**
617
* Create an Express middleware function which errors if an HTTP method is not allowed.
@@ -18,7 +29,7 @@ function allowMethods(methods = [], message = 'Method Not Allowed') {
1829
return (request, response, next) => {
1930
if (!normalizedMethods.includes(request.method.toUpperCase())) {
2031
response.header('Allow', normalizedMethods.join(', '));
21-
return next(httpError(405, message));
32+
return next(new MethodNotAllowedError(message));
2233
}
2334
next();
2435
};

package-lock.json

Lines changed: 11 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
"project:fix": "npx --yes @rowanmanning/validate-project@2 --type git node-library --fix",
3737
"prepare": "husky install"
3838
},
39-
"dependencies": {
40-
"http-errors": "^2.0.0"
41-
},
4239
"devDependencies": {
4340
"@commitlint/cli": "^17.0.0",
4441
"@commitlint/config-conventional": "^17.0.0",

test/unit/lib/allow-methods.test.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ const td = require('testdouble');
55

66
describe('lib/allow-methods', () => {
77
let allowMethods;
8-
let error405;
9-
let httpError;
108

119
beforeEach(() => {
12-
httpError = td.replace('http-errors', td.func());
13-
error405 = {status: 405};
14-
td.when(httpError(), {ignoreExtraArgs: true}).thenReturn(error405);
1510
allowMethods = require('../../../lib/allow-methods');
1611
});
1712

@@ -71,17 +66,21 @@ describe('lib/allow-methods', () => {
7166
it('calls back with a 405 error', done => {
7267
request.method = 'BAZ';
7368
allowMethods(['FOO', 'bar'])(request, response, error => {
74-
assert.strictEqual(error, error405);
75-
td.verify(httpError(405, 'Method Not Allowed'), {times: 1});
69+
assert.ok(error instanceof Error);
70+
assert.strictEqual(error.status, 405);
71+
assert.strictEqual(error.statusCode, 405);
72+
assert.strictEqual(error.message, 'Method Not Allowed');
7673
done();
7774
});
7875
});
7976

8077
it('calls back with a 405 error with a custom message if specified', done => {
8178
request.method = 'BAZ';
8279
allowMethods(['FOO', 'bar'], 'mock message')(request, response, error => {
83-
assert.strictEqual(error, error405);
84-
td.verify(httpError(405, 'mock message'), {times: 1});
80+
assert.ok(error instanceof Error);
81+
assert.strictEqual(error.status, 405);
82+
assert.strictEqual(error.statusCode, 405);
83+
assert.strictEqual(error.message, 'mock message');
8584
done();
8685
});
8786
});
@@ -101,8 +100,10 @@ describe('lib/allow-methods', () => {
101100
it('calls back with a 405 error', done => {
102101
request.method = 'FOO';
103102
allowMethods({})(request, response, error => {
104-
assert.strictEqual(error, error405);
105-
td.verify(httpError(405, 'Method Not Allowed'), {times: 1});
103+
assert.ok(error instanceof Error);
104+
assert.strictEqual(error.status, 405);
105+
assert.strictEqual(error.statusCode, 405);
106+
assert.strictEqual(error.message, 'Method Not Allowed');
106107
done();
107108
});
108109
});

0 commit comments

Comments
 (0)