I am dealing with an interface that looks like this:
interface Response {
items: {
productId: string;
productName: string;
price: number;
}[]
}
interface APIResponse {
items: {
productId: string;
productName: string;
price: number;
factoryId: string,
status: number,
category: number,
priority: number
}[]
}
Currently, I have a function that returns Response
:
async function getList(): promise<Response> {
// data.htw.cart.list return APIResponse
const response = await data.htw.cart.list(postJson)
return response
}
However, the data.htw.cart.list
function returns an APIResponse
, which means I receive excessive data that I do not need.
Each time I have to manually filter out unnecessary information like this:
return {
items: response.items.map(item => {
return {
productId: item.productId,
productName: item.productName,
price: item.price
}
})
}
Is there a tool available that can automatically trim down the APIResponse
to match the structure of Response
?
Two important points to consider:
- The structure of
APIResponse
always includes that ofResponse
- The key names in
Response
are the same as those inAPIResponse