Skip to content
This repository has been archived by the owner on Dec 20, 2020. It is now read-only.

Pass functions in as header values #120

Open
anthonator opened this issue May 26, 2016 · 6 comments
Open

Pass functions in as header values #120

anthonator opened this issue May 26, 2016 · 6 comments
Labels

Comments

@anthonator
Copy link

It would be nice if you could specify a function as a header value so you could have a set of default headers that may be calculated per request.

qwest.setDefaultOptions({
  headers: {
    'Accept': process.env.API_VERSION,
    'Authorization': () => `Bearer ${jwtFromLocalStorage()}`
  },
  responseType: 'json'
});
@pyrsmk
Copy link
Owner

pyrsmk commented May 26, 2016

If the Authorization header is updated at each request, then it shouldn't be a default option, isn't it? Maybe I don't understand well the problem ^^

@anthonator
Copy link
Author

I'd argue it is a default option if every request requires it.

For my specific use case I need a header to be sent with a variable value always derived from the same function. However, that header needs to be sent on every request. From my perspective it's default behavior to always send that header.

@anthonator
Copy link
Author

This is what I'm trying to avoid.

qwest.post('/things', {}, { headers: { 'MyHeader': myFunc() } })
qwest.get(`/things/${id}`, {}, { headers: { 'MyHeader': myFunc() } })
qwest.get('/things', {}, { headers: { 'MyHeader': myFunc() } })
... + 50 more

@pyrsmk
Copy link
Owner

pyrsmk commented May 28, 2016

Ok. I'll take a look at this as soon as possible ;)

@pyrsmk
Copy link
Owner

pyrsmk commented May 28, 2016

Or you can make a PR if you need this quickly

@pyrsmk
Copy link
Owner

pyrsmk commented Jun 30, 2016

It still seems weird to me to support that kind of feature in qwest since it just matches a really specific case.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants