diff --git a/lib/configuration/hooks/defaultHooks.js b/lib/configuration/hooks/defaultHooks.js index 93db13464b..c753dbf610 100755 --- a/lib/configuration/hooks/defaultHooks.js +++ b/lib/configuration/hooks/defaultHooks.js @@ -27,5 +27,6 @@ module.exports = { router: true, static: true, websockets: true, - studio: true + studio: true, + jsonapi: true }; diff --git a/lib/configuration/hooks/jsonapi/helpers/response.js b/lib/configuration/hooks/jsonapi/helpers/response.js new file mode 100644 index 0000000000..09435d32ad --- /dev/null +++ b/lib/configuration/hooks/jsonapi/helpers/response.js @@ -0,0 +1,19 @@ +'use strict'; + +/** + * Module dependencies + */ + +// Public node modules. +const _ = require('lodash'); +const JSONAPISerializer = require('jsonapi-serializer'); + +/** + * JSON API helper + */ + +module.exports = { + set: function(ctx, actionRoute) { + console.log(actionRoute); + } +}; diff --git a/lib/configuration/hooks/jsonapi/index.js b/lib/configuration/hooks/jsonapi/index.js new file mode 100644 index 0000000000..2d0870d646 --- /dev/null +++ b/lib/configuration/hooks/jsonapi/index.js @@ -0,0 +1,62 @@ +'use strict'; + +/** + * Module dependencies + */ + +// Public node modules. +const _ = require('lodash'); +const regex = require('../../../../util/regex'); +const response = require('./helpers/response'); + +/** + * JSON API hook + */ + +module.exports = function (strapi) { + const hook = { + + /** + * Default options + */ + + defaults: { + jsonapi: {} + }, + + /** + * Initialize the hook + */ + + initialize: function (cb) { + function *interceptor(next) { + const self = this; + + // Wait for downstream middleware/handlers to execute to build the response + yield next; + + // 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())) { + return stack; + } + }); + + // Handlers set the response body + if (!_.isUndefined(matchedRoute)) { + const actionRoute = strapi.config.routes[self.request.method.toUpperCase() + ' ' + matchedRoute.path]; + + if (!_.isUndefined(actionRoute)) { + response.set(this, actionRoute); + } + } + }; + + strapi.app.use(interceptor); + + cb(); + } + }; + + return hook; +}; diff --git a/package.json b/package.json index 4216bceb88..241a29c1a8 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "herd": "~1.0.0", "include-all": "~0.1.6", "json-stringify-safe": "~5.0.1", + "jsonapi-serializer": "^2.0.4", "koa": "~1.1.2", "koa-bodyparser": "~2.0.1", "koa-compose": "~2.3.0",