Skip to content

Instantly share code, notes, and snippets.

@ni8mr
Forked from mattlockyer/conditional-json.js
Created November 3, 2021 08:51
Show Gist options
  • Select an option

  • Save ni8mr/2d8654e1e102f27abf83ab38c4125b10 to your computer and use it in GitHub Desktop.

Select an option

Save ni8mr/2d8654e1e102f27abf83ab38c4125b10 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 } : {})
},
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment