Within my code, I have showcased two distinct types of response
. Upon closer examination of the following code snippets, it becomes evident that the structure of the response
from a service differs slightly between the two types. In the first type, there is a single data
, whereas in the second type, this data
contains another nested data
.
{
"data": {
"hasError": false,
"developerMessage": null,
"totalCount": 43
},
"hasError": false,
"developerMessage": null
}
Alternatively:
{
"data": {
"hasError": false,
"developerMessage": null,
"data": {
"hasError": false,
"developerMessage": null,
"totalCount": 43
},
"totalCount": 43
},
"hasError": false,
"developerMessage": null
}
To address this issue, I found myself needing to assess the response
result and categorize it into two distinct sections. The challenge lies in the fact that due to unforeseeable circumstances, the hierarchy within the response
may expand, resulting in multiple levels of data. However, one constant remains - the data
within the response
always exists at the deepest level. (As shown by the third item in this example)
return this.httpWrapperService.get(url + tableNameWithSlash, {
params: params, additionalOptions: {spinnerEnabled: false}
}).then((result: any) => {
if (result.data.data) {
// code
return [...result.data.data];
}
else if (result.data) {
// code
return [...result.data];
}
});
I am exploring alternative approaches to efficiently manage this dynamic response
.