diff --git a/packages/strapi-generate-new/files/config/middleware.json b/packages/strapi-generate-new/files/config/middleware.json index 3f8fee49b5..b2f0036145 100755 --- a/packages/strapi-generate-new/files/config/middleware.json +++ b/packages/strapi-generate-new/files/config/middleware.json @@ -5,7 +5,8 @@ "responseTime", "logger", "cors", - "responses" + "responses", + "gzip" ], "order": [ "Define the middlewares' load order by putting their name in this array is the right order" diff --git a/packages/strapi/lib/middlewares/boom/index.js b/packages/strapi/lib/middlewares/boom/index.js index 339bb6d96d..75167a7b0e 100644 --- a/packages/strapi/lib/middlewares/boom/index.js +++ b/packages/strapi/lib/middlewares/boom/index.js @@ -27,7 +27,6 @@ module.exports = strapi => { // Log error. strapi.log.error(error); - // Wrap error into a Boom's response. ctx.status = error.status || 500; ctx.body = _.get(ctx.body, 'isBoom') diff --git a/packages/strapi/lib/middlewares/responses/policy.js b/packages/strapi/lib/middlewares/responses/policy.js deleted file mode 100755 index 656a31e872..0000000000 --- a/packages/strapi/lib/middlewares/responses/policy.js +++ /dev/null @@ -1,72 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ - -// Public node modules. -const _ = require('lodash'); -const Boom = require('boom'); -const delegate = require('delegates'); - -// Local utilities. -const responses = require('./responses/index'); - -// Custom function to avoid ctx.body repeat -const createResponses = ctx => { - return _.merge( - responses, - _.mapValues(_.omit(Boom, ['create']), fn => (...rest) => { - ctx.body = fn(...rest); - }) - ); -}; - -/** - * Policy used to add responses in the `this.response` object. - */ - -module.exports = async function(ctx, next) { - const delegator = delegate(ctx, 'response'); - - _.forEach(createResponses(ctx), (value, key) => { - // Assign new error methods to context.response - ctx.response[key] = value; - // Delegate error methods to context - delegator.method(key); - }); - - try { - // App logic. - await next(); - } catch (error) { - // Log error. - strapi.log.error(error); - - // Wrap error into a Boom's response. - ctx.status = error.status || 500; - ctx.body = _.get(ctx.body, 'isBoom') - ? ctx.body || error && error.message - : Boom.wrap(error, ctx.status, ctx.body || error.message); - } - - // Empty body is considered as `notFound` response. - if (_.isUndefined(ctx.body) && _.isUndefined(ctx.status)) { - ctx.notFound(); - } - - if (_.isObject(ctx.body)) { - if (ctx.body.isBoom && ctx.body.data) { - ctx.body.output.payload.data = ctx.body.data; - } - - // Format `ctx.body` and `ctx.status`. - ctx.status = ctx.body.isBoom ? ctx.body.output.statusCode : ctx.status; - ctx.body = ctx.body.isBoom ? ctx.body.output.payload : ctx.body; - } - - // Call custom responses. - if (_.isFunction(_.get(strapi.config, `functions.responses.${ctx.status}`))) { - await strapi.config.functions.responses[ctx.status].call(this, ctx); - } -};