Support X-HTTP-Method-Override for old clients

This commit is contained in:
Aurélien Georget 2016-02-01 14:22:29 +01:00
parent 06549e6bb7
commit 348428132c

View File

@ -16,13 +16,24 @@ const utils = require('../utils/utils');
module.exports = { module.exports = {
default: {},
/** /**
* Parse request * Parse request
*/ */
parse: function * (ctx) { parse: function * (ctx) {
// Assign used method
this.default.method = ctx.method.toUpperCase();
// Detect X-HTTP-Method-Override for some clients
// which don't support PATCH method
if (ctx.header.hasOwnProperty('x-http-method-override') && ctx.header['x-http-method-override'] === 'PATCH') {
this.default.method = 'POST';
}
// HTTP methods allowed // HTTP methods allowed
switch (ctx.method.toUpperCase()) { switch (this.default.method) {
case 'GET': case 'GET':
// Nothing to do // Nothing to do
break; break;
@ -75,14 +86,14 @@ module.exports = {
message: 'Missing `data` member' message: 'Missing `data` member'
} }
}; };
} else if (!utils.isRessourceObject(body.data) && ctx.method !== 'POST') { } else if (!utils.isRessourceObject(body.data) && this.default.method !== 'POST') {
throw { throw {
status: 403, status: 403,
body: { body: {
message: 'Invalid ressource object' message: 'Invalid ressource object'
} }
}; };
} else if (!body.data.hasOwnProperty('type') && ctx.method === 'POST') { } else if (!body.data.hasOwnProperty('type') && this.default.method === 'POST') {
throw { throw {
status: 403, status: 403,
body: { body: {