-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (40 loc) · 1.34 KB
/
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
44
45
46
47
48
49
/**
* Created by Obscurity on 2016/5/7.
*/
module.exports = function () {
return function* (next) {
var methods = require('methods').map(function (method) {
return method.toUpperCase();
});
var overriden_methods = [];
// header support
var header = this.get('x-http-method-override');
if (header) {
overriden_methods.push(header.toUpperCase());
}
// query support
var query = this.request.query;
if (query && query._method) {
overriden_methods.push(query._method.toUpperCase());
}
// body support
var body = this.request.body;
if (body && body._method) {
overriden_methods.push(body._method.toUpperCase());
}
// only allow supported methods
// if the method don't match the following: GET POST PUT DELETE
// this middleware will simply fail the override method and use the original request.method instead
var selected_method = null;
overriden_methods.map(function (method) {
if (methods.indexOf(method) !== -1) {
selected_method = method;
}
return method;
});
if (selected_method != null) {
this.request.method = selected_method;
}
yield* next;
};
};