diff --git a/README.md b/README.md index 2452904..e4a0401 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ module.exports = ({ env }) => ({ responseTransforms: { removeAttributesKey: true, removeDataKey: true, + ignoreResponseKeys: [] }, requestTransforms : { wrapBodyWithDataKey: true @@ -96,6 +97,7 @@ module.exports = ({ env }) => ({ | responseTransforms | The transformations to enable for the API response | Object | N/A | No | | responseTransforms.removeAttributesKey | Removes the attributes key from the response | Boolean | false | No | | responseTransforms.removeDataKey | Removes the data key from the response | Boolean | false | No | +| responseTransforms.ignoreResponseKeys | Ignores to remove at specific response data has any key from the keys | Array | [] | No | | requestTransforms | The transformations to enable for an API request | Object | N/A | No | | requestTransforms.wrapBodyWithDataKey | Auto wraps the body of PUT and POST requests with a data key | Boolean | false | No | | hooks | The hooks to enable for the plugin | Object | N/A | No | diff --git a/__tests__/response/all-response-transforms.spec.js b/__tests__/response/all-response-transforms.spec.js index a3b95f3..7492e4d 100644 --- a/__tests__/response/all-response-transforms.spec.js +++ b/__tests__/response/all-response-transforms.spec.js @@ -3,7 +3,7 @@ const { modifyResponseBodyData } = require('../../server/services/transform-service/response'); const { initial, allResponseTransforms } = require('../mock/response'); -const transformOptions = { removeAttributesKey: true, removeDataKey: true }; +const transformOptions = { removeAttributesKey: true, removeDataKey: true, ignoreResponseKeys: [] }; describe('All response transforms', () => { // single relation diff --git a/__tests__/response/remove-attributes.spec.js b/__tests__/response/remove-attributes.spec.js index 5012633..7f17ffa 100644 --- a/__tests__/response/remove-attributes.spec.js +++ b/__tests__/response/remove-attributes.spec.js @@ -3,7 +3,7 @@ const { modifyResponseBodyData } = require('../../server/services/transform-service/response'); const { initial, removeAttributesKey } = require('../mock/response'); -const transformOptions = { removeAttributesKey: true }; +const transformOptions = { removeAttributesKey: true, ignoreResponseKeys: [] }; describe('removeAttributesKey', () => { // single relation diff --git a/__tests__/response/remove-data.spec.js b/__tests__/response/remove-data.spec.js index a2ec7a2..ad8f3f3 100644 --- a/__tests__/response/remove-data.spec.js +++ b/__tests__/response/remove-data.spec.js @@ -3,7 +3,7 @@ const { modifyResponseBodyData } = require('../../server/services/transform-service/response'); const { initial, removeDataKey } = require('../mock/response'); -const transformOptions = { removeDataKey: true }; +const transformOptions = { removeDataKey: true, ignoreResponseKeys: [] }; describe('removeDataKey', () => { // single relation diff --git a/server/config/index.js b/server/config/index.js index 355f44c..ef221c2 100644 --- a/server/config/index.js +++ b/server/config/index.js @@ -7,6 +7,7 @@ module.exports = { responseTransforms: { removeAttributesKey: false, removeDataKey: false, + ignoreResponseKeys: [] }, requestTransforms: { wrapBodyWithDataKey: false, diff --git a/server/config/schema.js b/server/config/schema.js index f40f4eb..037fd4f 100644 --- a/server/config/schema.js +++ b/server/config/schema.js @@ -6,6 +6,7 @@ const pluginConfigSchema = yup.object().shape({ responseTransforms: yup.object().shape({ removeAttributesKey: yup.bool(), removeDataKey: yup.bool(), + ignoreResponseKeys: yup.array(yup.string()) }), requestTransforms: yup.object().shape({ wrapBodyWithDataKey: yup.bool(), diff --git a/server/services/transform-service/response.js b/server/services/transform-service/response.js index e7b7c84..0713214 100644 --- a/server/services/transform-service/response.js +++ b/server/services/transform-service/response.js @@ -6,11 +6,14 @@ const { removeObjectKey } = require('./util'); * @param {object} transforms * @param {boolean} transforms.removeAttributesKey * @param {boolean} transforms.removeDataKey + * @param {Array.} transforms.ignoreResponseKeys * @param {object} ctx */ function transformResponse(transforms = {}, ctx) { // transform data if (transforms.removeAttributesKey || transforms.removeDataKey) { + transforms.ignoreResponseKeys = transforms.ignoreResponseKeys || []; + ctx.body.data = modifyResponseBodyData(transforms, ctx.body.data); } } @@ -21,6 +24,7 @@ function transformResponse(transforms = {}, ctx) { * @param {object} transforms * @param {boolean} transforms.removeAttributesKey * @param {boolean} transforms.removeDataKey + * @param {Array.} transforms.ignoreResponseKeys * @param {object} data * @returns {object} transformed body data */ @@ -44,6 +48,10 @@ function modifyResponseBodyData(transforms = {}, data) { return; } + if (transforms.ignoreResponseKeys.includes(key)) { + return; + } + // removeDataKey specific transformations if (transforms.removeDataKey) { // single