/
index.js
43 lines (32 loc) · 957 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const debug = require('debug')('yeps:method:override');
const methods = require('methods');
const methodExists = method => methods.includes(method.toLowerCase());
module.exports = () => async (context) => {
debug('Method override');
let { method } = context.req;
debug('Original method: %s', method);
// wrapper
if (
context.req.body
&& context.req.body._method
&& methodExists(context.req.body._method)
) {
method = context.req.body._method.toUpperCase();
}
// body parser
if (
context.request
&& context.request.body
&& context.request.body._method
&& methodExists(context.request.body._method)
) {
method = context.request.body._method.toUpperCase();
}
// header support
const header = context.req.headers['x-http-method-override'];
if (header && methodExists(header)) {
method = header.toUpperCase();
}
context.req.method = method;
debug('New method: %s', method);
};