Below is a function that is currently working fine:
export const optionsFunc: Function = (token: string) => {
const options = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
}
};
return options;
};
I now want to enhance it by adding params
to the options
variable. The params
should be an optional key/value pair.
How can I adjust the options
variable and include the params
parameter in the function?
The final result I'm aiming for is something like this:
export const optionsFunc: Function = (token: string, params: any) => {
const options = {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
};
if (params) {
const filteredParams = Object.entries(params).reduce(
(a, [k, v]) => (v == null || v === 'null' ? a : (a[k] = v, a)), {}
);
options.params = filteredParams;
}
return options;
};