Skip to content

Instantly share code, notes, and snippets.

@mattlockyer
Created June 19, 2019 15:25
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattlockyer/3dac7c9618ac98d16b046e32c364899d to your computer and use it in GitHub Desktop.
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.
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 } : {})
},
})
@LukasDoesDev
Copy link

@abhisekgour
Copy link

Thank you, saved my day!

@aclisp
Copy link

aclisp commented May 18, 2023

Very helpful, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment