Set headers after client creation, or per request?
See original GitHub issueHope this is not too much of an edge case:
Trying to use Villus in a Vuex store module in a Nuxt app.
This works, if I do not need to pass Authorization
headers:
// myStoreModule.js
import { createClient } from 'villus'
const client = createClient({
url: 'http://localhost:4000/graphql'
})
// ...
export const actions = {
async fetchData({ commit, dispatch }) {
const data = await client.executeQuery({
query: BOARD_QUERY
})
commit('SET_BOARD', data)
}
}
However, I do need to pass Authorization
headers. But in the Vuex store, I can only access the original headers from the http.request
object inside of actions. Therefore, I cannot do:
const client = createClient({
url: 'http://localhost:4000/graphql',
context: () => {
return {
fetchOptions: {
headers: {
Authorization: TOKEN
}
}
};
}
})
…because I do not have access to TOKEN outside of my actions.
Therefore, I would like to do something like this:
export const actions = {
async fetchData({ commit, dispatch }, { req }) {
// first get TOKEN from original request
const token = getToken(req.headers)
// the set the token to the previously created client
client.context = () => {
return {
fetchOptions: {
headers: {
Authorization: TOKEN
}
}
}
}
//...
}
But this does not work. So, how would you go around this issue?
With Axios, I could easily modify the headers on each request. Would something like that be possible with Villus client?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Error: Can't set headers after they are sent to the client
When a POST request is sent to /api/route1 it will run every line in the callback. A Can't set headers after they are...
Read more >Cannot set headers after they are sent to client [SOLVED]
The primary cause of the error, "Error: Cannot set headers after they are sent to client" is sending multiple HTTP responses per request....
Read more >Using Axios to set request headers
In this article, we examined how to set HTTP request headers with Axios by passing an object, creating a specific Axios instance, and...
Read more >Request Options - Guzzle Documentation
You can customize requests created and transferred by a client using request options. Request options control various aspects of a request including, headers, ......
Read more >cannot set headers after they are sent to the client nextjs
HTTP uses a cycle that requires one response per request. When the client sends a request (e.g. POST or GET) the server should...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Your method should work, I’m not sure about the problem. I’m overhauling the extension API, I haven’t settled yet on the final draft but It looks like this:
My goals are to allow retries, caching, error handling with simple API like that.
I would like to add to this request by asking that the context function support promises. This way we could first get an access token asynchronously, and then update headers and execute the query.