mirror of
https://github.com/strapi/strapi.git
synced 2025-12-29 08:04:51 +00:00
Serialize data for collections and ressources
This commit is contained in:
parent
03400e82f5
commit
684acb6f7d
@ -13,7 +13,151 @@ const JSONAPISerializer = require('jsonapi-serializer');
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
set: function(ctx, actionRoute) {
|
||||
console.log(actionRoute);
|
||||
|
||||
default: {},
|
||||
|
||||
/**
|
||||
* Initialize the hook
|
||||
*/
|
||||
|
||||
set: function(ctx, matchedRoute, actionRoute) {
|
||||
const type = actionRoute.controller.toLowerCase();
|
||||
const kind = this.whichKind(matchedRoute);
|
||||
const value = this.verifyAndSetValue(ctx, kind);
|
||||
|
||||
ctx.response.body = this.serialize(ctx, type, kind, value);
|
||||
},
|
||||
|
||||
/**
|
||||
* Verify type of the value
|
||||
*/
|
||||
|
||||
serialize: function(ctx, type, kind, value) {
|
||||
const toSerialize = {
|
||||
topLevelLinks: { self: ctx.request.origin + ctx.request.url }
|
||||
};
|
||||
|
||||
switch (kind) {
|
||||
case 'collection':
|
||||
if (!_.isEmpty(value) && _.isArray(value)) {
|
||||
toSerialize.dataLinks = {
|
||||
self: function (record) {
|
||||
return ctx.request.origin + ctx.request.url + '/' + record.id
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (true) {
|
||||
toSerialize.attributes = ['id'];
|
||||
}
|
||||
|
||||
return new JSONAPISerializer(type, value, toSerialize);
|
||||
break;
|
||||
case 'ressource':
|
||||
if (true) {
|
||||
toSerialize.attributes = ['id'];
|
||||
}
|
||||
|
||||
return new JSONAPISerializer(type, value, toSerialize);
|
||||
break;
|
||||
case 'relationships':
|
||||
|
||||
break;
|
||||
case 'related':
|
||||
|
||||
break;
|
||||
default:
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Verify type of the value
|
||||
*/
|
||||
|
||||
verifyAndSetValue: function(ctx, kind) {
|
||||
const data = ctx.body;
|
||||
|
||||
switch (ctx.response.status) {
|
||||
case 404:
|
||||
ctx.status = 200;
|
||||
ctx.body = null;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (kind) {
|
||||
case 'collection':
|
||||
// Collection
|
||||
if (_.isArray(data) && _.size(data) > 1) {
|
||||
return data;
|
||||
} else if (_.isArray(data) && (_.size(data) === 1 || _.size(data) === 0)) {
|
||||
return _.isObject(data[0]) ? data[0] : [];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'ressource':
|
||||
// Ressource
|
||||
if (_.isObject(data)) {
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'relationships':
|
||||
// TODO:
|
||||
// - Detect kind of relation
|
||||
// - MtM, OtM: array
|
||||
// - OtO, MtO: object
|
||||
|
||||
|
||||
// Relationships
|
||||
if (_.isObject(data) || _.isArray(data)) {
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
case 'related':
|
||||
// TODO:
|
||||
// - Detect kind of relation
|
||||
// - MtM, OtM: array
|
||||
// - OtO, MtO: object
|
||||
|
||||
|
||||
// Related
|
||||
if (_.isObject(data) || _.isArray(data)) {
|
||||
return data;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return 'collection'
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Return kind of ressources
|
||||
*/
|
||||
|
||||
whichKind: function(matchedRoute) {
|
||||
// Top level route
|
||||
switch (_.size(matchedRoute.regexp.keys)) {
|
||||
case 0:
|
||||
// Collection
|
||||
return 'collection';
|
||||
break;
|
||||
case 1:
|
||||
// Ressource
|
||||
return 'ressource';
|
||||
break;
|
||||
case 2:
|
||||
// Relationships or related ressource
|
||||
return (matchedRoute.path.indexOf('relationships')) ? 'relationships' : 'related';
|
||||
break;
|
||||
default:
|
||||
return 'collection'
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@ -35,6 +35,12 @@ module.exports = function (strapi) {
|
||||
// Wait for downstream middleware/handlers to execute to build the response
|
||||
yield next;
|
||||
|
||||
// Verify Content-Type header
|
||||
if (this.request.type !== 'application/vnd.api+json') {
|
||||
this.status = 406;
|
||||
this.body = '';
|
||||
}
|
||||
|
||||
// Detect route
|
||||
const matchedRoute = _.find(strapi.router.stack, function(stack) {
|
||||
if (new RegExp(stack.regexp).test(self.request.url) && _.includes(stack.methods, self.request.method.toUpperCase())) {
|
||||
@ -47,7 +53,7 @@ module.exports = function (strapi) {
|
||||
const actionRoute = strapi.config.routes[self.request.method.toUpperCase() + ' ' + matchedRoute.path];
|
||||
|
||||
if (!_.isUndefined(actionRoute)) {
|
||||
response.set(this, actionRoute);
|
||||
response.set(this, matchedRoute, actionRoute);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user