Created
June 19, 2019 15:25
-
-
Save mattlockyer/3dac7c9618ac98d16b046e32c364899d to your computer and use it in GitHub Desktop.
Using spread syntax to conditionally add JSON key, value to object. Fetch example using POST and Authorization.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const POST = (auth) => ({ | |
method: "POST", // *GET, POST, PUT, DELETE, etc. | |
mode: "cors", // no-cors, cors, *same-origin | |
headers: { | |
"Content-Type": "application/json", | |
...(auth ? { "Authorization": auth } : {}) | |
}, | |
}) |
Thank you, saved my day!
Very helpful, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what I was looking for! Came from https://stackoverflow.com/questions/11704267/in-javascript-how-to-conditionally-add-a-member-to-an-object#40560953