Writting some TODOs, adding support for single ressource fetch

This commit is contained in:
Aurélien Georget 2016-01-22 18:36:15 +01:00
parent 684acb6f7d
commit 2868ce08ee
2 changed files with 58 additions and 40 deletions

View File

@ -17,57 +17,63 @@ module.exports = {
default: {}, default: {},
/** /**
* Initialize the hook * Set response
*/ */
set: function(ctx, matchedRoute, actionRoute) { set: function (ctx, matchedRoute, actionRoute) {
const type = actionRoute.controller.toLowerCase(); const type = this.getType(actionRoute.controller);
const kind = this.whichKind(matchedRoute); const object = this.getObject(matchedRoute);
const value = this.verifyAndSetValue(ctx, kind); const value = this.verifyAndSetValue(ctx, object);
ctx.response.body = this.serialize(ctx, type, kind, value); ctx.response.body = this.serialize(ctx, type, object, value);
}, },
/** /**
* Verify type of the value * Serialize response with JSON API specification
*/ */
serialize: function(ctx, type, kind, value) { serialize: function (ctx, type, object, value) {
const toSerialize = { const toSerialize = {
topLevelLinks: { self: ctx.request.origin + ctx.request.url } topLevelLinks: {self: ctx.request.origin + ctx.request.url}
}; };
switch (kind) { switch (object) {
case 'collection': case 'collection':
if (!_.isEmpty(value) && _.isArray(value)) { if (!_.isEmpty(value) && _.isArray(value)) {
// TODO :
// - Detect PK and stringify the value
_.forEach(value, function (value) {
value.id = value.id.toString();
});
toSerialize.dataLinks = { toSerialize.dataLinks = {
self: function (record) { self: function (record) {
return ctx.request.origin + ctx.request.url + '/' + record.id return ctx.request.origin + ctx.request.url + '/' + record.id;
} }
}; };
} else if (!_.isEmpty(value) && _.isObject(value)) {
// TODO :
// - Detect PK and stringify the value
} }
if (true) { // TODO :
// - Parse the model based on the type value
// - Displayed attributes but also consider query parameters
toSerialize.attributes = ['id']; toSerialize.attributes = ['id'];
}
return new JSONAPISerializer(type, value, toSerialize); return new JSONAPISerializer(type, value, toSerialize);
break;
case 'ressource': case 'ressource':
if (true) {
toSerialize.attributes = ['id']; toSerialize.attributes = ['id'];
}
return new JSONAPISerializer(type, value, toSerialize); return new JSONAPISerializer(type, value, toSerialize);
break;
case 'relationships': case 'relationships':
break; break;
case 'related': case 'related':
break; break;
default: default:
return new JSONAPISerializer(type, value, toSerialize);
} }
}, },
@ -75,7 +81,7 @@ module.exports = {
* Verify type of the value * Verify type of the value
*/ */
verifyAndSetValue: function(ctx, kind) { verifyAndSetValue: function (ctx, object) {
const data = ctx.body; const data = ctx.body;
switch (ctx.response.status) { switch (ctx.response.status) {
@ -83,9 +89,11 @@ module.exports = {
ctx.status = 200; ctx.status = 200;
ctx.body = null; ctx.body = null;
break; break;
default:
break;
} }
switch (kind) { switch (object) {
case 'collection': case 'collection':
// Collection // Collection
if (_.isArray(data) && _.size(data) > 1) { if (_.isArray(data) && _.size(data) > 1) {
@ -106,11 +114,10 @@ module.exports = {
break; break;
case 'relationships': case 'relationships':
// TODO: // TODO:
// - Detect kind of relation // - Detect object of relation
// - MtM, OtM: array // - MtM, OtM: array
// - OtO, MtO: object // - OtO, MtO: object
// Relationships // Relationships
if (_.isObject(data) || _.isArray(data)) { if (_.isObject(data) || _.isArray(data)) {
return data; return data;
@ -120,11 +127,10 @@ module.exports = {
break; break;
case 'related': case 'related':
// TODO: // TODO:
// - Detect kind of relation // - Detect object of relation
// - MtM, OtM: array // - MtM, OtM: array
// - OtO, MtO: object // - OtO, MtO: object
// Related // Related
if (_.isObject(data) || _.isArray(data)) { if (_.isObject(data) || _.isArray(data)) {
return data; return data;
@ -133,31 +139,40 @@ module.exports = {
} }
break; break;
default: default:
return 'collection' return 'collection';
} }
}, },
/** /**
* Return kind of ressources * Find data object
*/ */
whichKind: function(matchedRoute) { getObject: function (matchedRoute) {
// Top level route // Top level route
switch (_.size(matchedRoute.regexp.keys)) { switch (_.size(matchedRoute.regexp.keys)) {
case 0: case 0:
// Collection // Collection
return 'collection'; return 'collection';
break;
case 1: case 1:
// Ressource // Ressource
return 'ressource'; return 'ressource';
break;
case 2: case 2:
// Relationships or related ressource // Relationships or related ressource
return (matchedRoute.path.indexOf('relationships')) ? 'relationships' : 'related'; return (matchedRoute.path.indexOf('relationships')) ? 'relationships' : 'related';
break;
default: default:
return 'collection' return 'collection';
}
},
/**
* Find data type
*/
getType: function (supposedType) {
if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) {
return supposedType.toLowerCase();
} else {
return 'Unknow';
} }
} }
}; };

View File

@ -6,7 +6,6 @@
// Public node modules. // Public node modules.
const _ = require('lodash'); const _ = require('lodash');
const regex = require('../../../../util/regex');
const response = require('./helpers/response'); const response = require('./helpers/response');
/** /**
@ -29,7 +28,11 @@ module.exports = function (strapi) {
*/ */
initialize: function (cb) { initialize: function (cb) {
function *interceptor(next) { // TODO:
// - Force or not the routes?
// - Add middleware before called the controller action to check parameters structure
function * interceptor(next) {
const self = this; const self = this;
// Wait for downstream middleware/handlers to execute to build the response // Wait for downstream middleware/handlers to execute to build the response
@ -42,7 +45,7 @@ module.exports = function (strapi) {
} }
// Detect route // Detect route
const matchedRoute = _.find(strapi.router.stack, function(stack) { const matchedRoute = _.find(strapi.router.stack, function (stack) {
if (new RegExp(stack.regexp).test(self.request.url) && _.includes(stack.methods, self.request.method.toUpperCase())) { if (new RegExp(stack.regexp).test(self.request.url) && _.includes(stack.methods, self.request.method.toUpperCase())) {
return stack; return stack;
} }
@ -56,7 +59,7 @@ module.exports = function (strapi) {
response.set(this, matchedRoute, actionRoute); response.set(this, matchedRoute, actionRoute);
} }
} }
}; }
strapi.app.use(interceptor); strapi.app.use(interceptor);