elbywan / wretch

A tiny wrapper built around fetch with an intuitive syntax. :candy:
MIT License
4.83k stars 98 forks source link

Delete the auth header #95

Closed Azurox closed 4 years ago

Azurox commented 4 years ago

Hi, Is there a way to delete the Auth header without recreating the entire wrecht object ? I tried : api = api.auth(null); api = api.auth(undefined); api = api.auth();

And the authorization is still present in every request with either null or undefined as value

Thank you 😄

elbywan commented 4 years ago

Hi @Azurox,

Is there a way to delete the Auth header without recreating the entire wrecht object ?

I think that the following code should do the trick:

function removeAuth(w) {
  const headers = { ...w._options.headers }
  delete headers['Authorization']
  return w.options({ headers }, false)
}

const base = wretch('/').auth('token')

/* With Authorization header */
base.get().text(console.log)

/* Without Authorization header */
// The imperative way:
removeAuth(base).get().text(console.log)
// The declarative way:
base.defer(removeAuth).get().text(console.log)
elbywan commented 4 years ago

@Azurox closing the issue, feel free to reopen if you have other related questions

a-eid commented 3 years ago

@elbywan it would be nice if there was a way to completely strip a certain header from a wretch instance

delete w.defaults.header.Authentication

my use case:

when a user logs out the Authentication header should no longer exist on the instance.

@elbywan If you want I can try to submit a PR to add/fix this.

elbywan commented 3 years ago

Hey @a-eid,

it would be nice if there was a way to completely strip a certain header from a wretch instance

Unless I'm mistaken, I think that the code snippet above does exactly that?