Remove logs, handle DELETE method and add some verifications

This commit is contained in:
Aurélien Georget 2016-01-29 13:17:38 +01:00
parent 42c06950e2
commit bd2ae13859
3 changed files with 46 additions and 16 deletions

View File

@ -21,9 +21,10 @@ module.exports = {
*/
parse: function * (ctx) {
// HTTP methods allowed
switch (ctx.method.toUpperCase()) {
case 'GET':
console.log('GET');
// Nothing to do
break;
case 'PATCH':
case 'POST':
@ -35,7 +36,7 @@ module.exports = {
}
break;
case 'DELETE':
console.log('DELETE');
// Nothing to do
break;
default:
throw {

View File

@ -31,6 +31,11 @@ module.exports = {
ctx.response.status = 404;
ctx.response.body = '';
return false;
} else if (ctx.method === 'DELETE') {
// Request successful and responds with only top-level meta data or nothing.
ctx.response.body = '';
return false;
}
@ -45,6 +50,7 @@ module.exports = {
*/
serialize: function (ctx, type, object, value) {
// TODO:
// - Handle configuration with a file to improve flexibility of JSON API support
const toSerialize = {
@ -66,20 +72,24 @@ module.exports = {
// Array
if (!_.isNull(PK)) {
_.forEach(value, function (record) {
record[PK] = record[PK].toString();
if (record.hasOwnProperty(PK)) {
record[PK] = record[PK].toString();
}
});
}
toSerialize.dataLinks = {
self: function (record) {
return ctx.request.origin + ctx.request.url + '/' + record.id;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + ctx.request.url + '/' + record[PK];
}
}
};
toSerialize.attributes = _.keys(value[0]);
toSerialize.attributes = _.keys(_.last(value));
} else if (_.isObject(value) && !_.isEmpty(value)) {
// Object
if (!_.isNull(PK)) {
if (!_.isNull(PK) && value.hasOwnProperty(PK)) {
value[PK] = value[PK].toString();
}
@ -120,24 +130,32 @@ module.exports = {
if (strapi.models.hasOwnProperty(type)) {
_.forEach(strapi.models[type].associations, function (relation) {
let PK = utils.getPK(relation.model) || utils.getPK(relation.collection);
switch (relation.nature) {
case 'oneToOne':
case 'manyToOne':
// Object
toSerialize[relation.alias] = {
ref: utils.getPK(relation.model),
ref: PK,
attributes: _.keys(strapi.models[type].attributes),
relationshipLinks: {
self: function (record) {
return ctx.request.origin + '/' + type + '/' + record.id + '/relationships/' + relation.alias;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + type + '/' + record[PK] + '/relationships/' + relation.alias;
}
},
related: function (record) {
return ctx.request.origin + '/' + type + '/' + record.id;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + type + '/' + record[PK];
}
}
},
includedLinks: {
self: function (data, record) {
return ctx.request.origin + '/' + relation.model + '/' + record.id;
if (!_.isUndefined(record) && record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + relation.model + '/' + record[PK];
}
}
}
};
@ -146,20 +164,26 @@ module.exports = {
case 'manyToMany':
// Array
toSerialize[relation.alias] = {
ref: utils.getPK(relation.collection),
ref: PK,
typeForAttribute: relation.collection,
attributes: _.keys(strapi.models[type].attributes),
relationshipLinks: {
self: function (record) {
return ctx.request.origin + '/' + type + '/' + record.id + '/relationships/' + relation.alias;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + type + '/' + record[PK] + '/relationships/' + relation.alias;
}
},
related: function (record) {
return ctx.request.origin + '/' + type + '/' + record.id;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + type + '/' + record[PK];
}
}
},
includedLinks: {
self: function (data, record) {
return ctx.request.origin + '/' + relation.collection + '/' + record.id;
if (record.hasOwnProperty(PK)) {
return ctx.request.origin + '/' + relation.collection + '/' + record[PK];
}
}
}
};

View File

@ -45,8 +45,8 @@ module.exports = function (strapi) {
if (this.request.type !== 'application/vnd.api+json') {
this.status = 406;
this.body = '';
} else {
// Intercept only GET request
} else if (_.startsWith(this.status, '2')) {
// Intercept success requests
// Detect route
const matchedRoute = _.find(strapi.router.stack, function (stack) {
@ -63,6 +63,11 @@ module.exports = function (strapi) {
response.set(this, matchedRoute, actionRoute);
}
}
} else {
// Intercept error requests
this.body = {
error: this.body
};
}
}
}