From 2868ce08ee1c7a796289c31f3ad0b6429a89eb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Georget?= Date: Fri, 22 Jan 2016 18:36:15 +0100 Subject: [PATCH] Writting some TODOs, adding support for single ressource fetch --- .../hooks/jsonapi/helpers/response.js | 87 +++++++++++-------- lib/configuration/hooks/jsonapi/index.js | 11 ++- 2 files changed, 58 insertions(+), 40 deletions(-) diff --git a/lib/configuration/hooks/jsonapi/helpers/response.js b/lib/configuration/hooks/jsonapi/helpers/response.js index 2a546708b8..0a63b71eec 100644 --- a/lib/configuration/hooks/jsonapi/helpers/response.js +++ b/lib/configuration/hooks/jsonapi/helpers/response.js @@ -17,57 +17,63 @@ module.exports = { default: {}, /** - * Initialize the hook + * Set response */ - set: function(ctx, matchedRoute, actionRoute) { - const type = actionRoute.controller.toLowerCase(); - const kind = this.whichKind(matchedRoute); - const value = this.verifyAndSetValue(ctx, kind); + set: function (ctx, matchedRoute, actionRoute) { + const type = this.getType(actionRoute.controller); + const object = this.getObject(matchedRoute); + 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 = { - topLevelLinks: { self: ctx.request.origin + ctx.request.url } + topLevelLinks: {self: ctx.request.origin + ctx.request.url} }; - switch (kind) { + switch (object) { case 'collection': if (!_.isEmpty(value) && _.isArray(value)) { + + // TODO : + // - Detect PK and stringify the value + _.forEach(value, function (value) { + value.id = value.id.toString(); + }); + toSerialize.dataLinks = { 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) { - toSerialize.attributes = ['id']; - } + // TODO : + // - Parse the model based on the type value + // - Displayed attributes but also consider query parameters + + toSerialize.attributes = ['id']; return new JSONAPISerializer(type, value, toSerialize); - break; case 'ressource': - if (true) { - toSerialize.attributes = ['id']; - } + toSerialize.attributes = ['id']; return new JSONAPISerializer(type, value, toSerialize); - break; case 'relationships': - break; case 'related': - break; - default: - + default: + return new JSONAPISerializer(type, value, toSerialize); } }, @@ -75,7 +81,7 @@ module.exports = { * Verify type of the value */ - verifyAndSetValue: function(ctx, kind) { + verifyAndSetValue: function (ctx, object) { const data = ctx.body; switch (ctx.response.status) { @@ -83,9 +89,11 @@ module.exports = { ctx.status = 200; ctx.body = null; break; + default: + break; } - switch (kind) { + switch (object) { case 'collection': // Collection if (_.isArray(data) && _.size(data) > 1) { @@ -106,11 +114,10 @@ module.exports = { break; case 'relationships': // TODO: - // - Detect kind of relation + // - Detect object of relation // - MtM, OtM: array // - OtO, MtO: object - // Relationships if (_.isObject(data) || _.isArray(data)) { return data; @@ -120,11 +127,10 @@ module.exports = { break; case 'related': // TODO: - // - Detect kind of relation + // - Detect object of relation // - MtM, OtM: array // - OtO, MtO: object - // Related if (_.isObject(data) || _.isArray(data)) { return data; @@ -133,31 +139,40 @@ module.exports = { } break; default: - return 'collection' + return 'collection'; } }, /** - * Return kind of ressources + * Find data object */ - whichKind: function(matchedRoute) { + getObject: 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' + return 'collection'; + } + }, + + /** + * Find data type + */ + + getType: function (supposedType) { + if (strapi.models.hasOwnProperty(supposedType.toLowerCase())) { + return supposedType.toLowerCase(); + } else { + return 'Unknow'; } } }; diff --git a/lib/configuration/hooks/jsonapi/index.js b/lib/configuration/hooks/jsonapi/index.js index 93e6865a95..73b47c7689 100644 --- a/lib/configuration/hooks/jsonapi/index.js +++ b/lib/configuration/hooks/jsonapi/index.js @@ -6,7 +6,6 @@ // Public node modules. const _ = require('lodash'); -const regex = require('../../../../util/regex'); const response = require('./helpers/response'); /** @@ -29,7 +28,11 @@ module.exports = function (strapi) { */ 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; // Wait for downstream middleware/handlers to execute to build the response @@ -42,7 +45,7 @@ module.exports = function (strapi) { } // 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())) { return stack; } @@ -56,7 +59,7 @@ module.exports = function (strapi) { response.set(this, matchedRoute, actionRoute); } } - }; + } strapi.app.use(interceptor);