Skip to content

Commit

Permalink
Fix bug in Requester for nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
trickreich committed Feb 28, 2019
1 parent fb6e805 commit cd8571a
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 3 deletions.
Expand Up @@ -12,22 +12,79 @@ const defaultOptions = {
function transformResponseObject(data: Object) {
return Object.keys(data).reduce((transformedData: Object, key) => {
const value = data[key];
transformedData[key] = value === null || value === '' ? undefined : data[key];

if (value === null || value === '') {
transformedData[key] = undefined;

return transformedData;
}

if (Array.isArray(value)) {
transformedData[key] = transformResponseArray(value);

return transformedData;
}

if (value instanceof Object) {
transformedData[key] = transformResponseObject(value);

return transformedData;
}

transformedData[key] = value;

return transformedData;
}, {});
}

function transformResponseArray(data: Array<Object>) {
return data.map(transformResponseObject);
return data.map((value => {
if (value instanceof Object) {
return transformResponseObject(value);
}

return value;
}));
}

function transformRequestObject(data: Object): Object {
return Object.keys(data).reduce((transformedData: Object, key) => {
transformedData[key] = data[key] === undefined ? null : data[key];
const value = data[key];

if (value === undefined || value === null) {
transformedData[key] = null;

return transformedData;
}

if (Array.isArray(value)) {
transformedData[key] = transformRequestArray(value);

return transformedData;
}

if (value instanceof Object) {
transformedData[key] = transformRequestObject(value);

return transformedData;
}

transformedData[key] = value;

return transformedData;
}, {});
}

function transformRequestArray(data: Array<Object>) {
return data.map((value => {
if (value instanceof Object) {
return transformRequestObject(value);
}

return value;
}));
}

function transformRequestData(data: Object | Array<Object>): Object | Array<Object> {
if (Array.isArray(data)) {
return data.map(transformRequestObject);
Expand Down
Expand Up @@ -51,6 +51,15 @@ test('Should execute GET request and replace null and empty string with undefine
test2: null,
test3: '',
test4: 'something',
test5: {
test5_id: 5,
test5_test: null,
},
test6: [
{id: 1, test: 'abc', test2: null},
{id: 2, test: 'abc', test2: 'Test2'},
],
test7: ['test1', 'test2'],
}));
const promise = new Promise((resolve) => resolve(response));

Expand All @@ -63,6 +72,15 @@ test('Should execute GET request and replace null and empty string with undefine
test2: undefined,
test3: undefined,
test4: 'something',
test5: {
test5_id: 5,
test5_test: undefined,
},
test6: [
{id: 1, test: 'abc', test2: undefined},
{id: 2, test: 'abc', test2: 'Test2'},
],
test7: ['test1', 'test2'],
});
});

Expand All @@ -89,6 +107,17 @@ test('Should execute POST request and return JSON', () => {
title: 'Titel',
description: 'Description',
test: undefined,
contacts: [
{id: 1, test: 'Titel', other: undefined},
{id: 2, test: 'Titel', other: 'Other'},
],
address: {
id: 1,
title: 'Title',
other: 'Other',
other2: undefined,
},
types: ['type1', 'type2'],
};
const requestPromise = Requester.post('/some-url', data).then((response) => {
expect(response).toEqual({test: undefined, value: 'test'});
Expand All @@ -100,6 +129,17 @@ test('Should execute POST request and return JSON', () => {
title: 'Titel',
description: 'Description',
test: null,
contacts: [
{id: 1, test: 'Titel', other: null},
{id: 2, test: 'Titel', other: 'Other'},
],
address: {
id: 1,
title: 'Title',
other: 'Other',
other2: null,
},
types: ['type1', 'type2'],
}),
credentials: 'same-origin',
headers: {'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest'},
Expand Down

0 comments on commit cd8571a

Please sign in to comment.