I want to conditionally append different headers to my request object. I don't want to use if/else
.
The following ...
gives syntax error expression expected
.
I've looked at some other examples on SO but they don't seem to work. I just can't get my syntax right.
headers
is some object that comes from function args which may or may not exist.
What is the correct way to write this?
const req = {
meta: {
id: "asd123"
}
}
{...req.meta, ...( headers ? headers : { "Content-Type": "application-json" })}
I want my output to look something like this
const req = {
meta: {
id: "asd123"
}
headers: {
ContentType: "application-json",
}
}