Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ignoreResponseKeys #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ module.exports = ({ env }) => ({
responseTransforms: {
removeAttributesKey: true,
removeDataKey: true,
ignoreResponseKeys: []
},
requestTransforms : {
wrapBodyWithDataKey: true
Expand Down Expand Up @@ -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 |
Expand Down
2 changes: 1 addition & 1 deletion __tests__/response/all-response-transforms.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion __tests__/response/remove-attributes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion __tests__/response/remove-data.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions server/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = {
responseTransforms: {
removeAttributesKey: false,
removeDataKey: false,
ignoreResponseKeys: []
},
requestTransforms: {
wrapBodyWithDataKey: false,
Expand Down
1 change: 1 addition & 0 deletions server/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
8 changes: 8 additions & 0 deletions server/services/transform-service/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ const { removeObjectKey } = require('./util');
* @param {object} transforms
* @param {boolean} transforms.removeAttributesKey
* @param {boolean} transforms.removeDataKey
* @param {Array.<string>} 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);
}
}
Expand All @@ -21,6 +24,7 @@ function transformResponse(transforms = {}, ctx) {
* @param {object} transforms
* @param {boolean} transforms.removeAttributesKey
* @param {boolean} transforms.removeDataKey
* @param {Array.<string>} transforms.ignoreResponseKeys
* @param {object} data
* @returns {object} transformed body data
*/
Expand All @@ -44,6 +48,10 @@ function modifyResponseBodyData(transforms = {}, data) {
return;
}

if (transforms.ignoreResponseKeys.includes(key)) {
return;
}

// removeDataKey specific transformations
if (transforms.removeDataKey) {
// single
Expand Down