I attempted to implement a "multiple filter" feature in TS, so...
If I don't provide any input -> all products are returned as usual;
However, if I specify some parameters -> only products matching the parameters are returned.
(I used colors for demonstration purposes)
async load(params: params): LoadProducts.Result => {
const products = [];
if (params) {
if (params.red != undefined) // products.push( ...load.redProducts() );
if (params.blue != undefined) // products.push( ...load.blueProducts() );
if (params.green != undefined) // products.push( ...load.greenProducts() );
if (params.yellow != undefined) // products.push( ...load.yellowProducts() );
} else {
// products.push( ...load.products() );
}
return products;
}
Type of request:
type params = {
filter?: {
red?: boolean;
blue?: boolean;
green?: boolean;
yellow?: boolean;
};
};
Example of request:
{
"filter": {
"red": true,
"blue": true
}
}
How can I handle multiple IF statements to check the parameters, as each one corresponds to a different function for loading, and I intend to include more filters in the future.