How do you use context? #1733
-
|
Hi, Sorry, but how do you use context for storing tokens, and then how do you retrieve them? I was trying something along the lines of below, but I'm wrong :) |
Beta Was this translation helpful? Give feedback.
Answered by
sindresorhus
Oct 11, 2025
Replies: 2 comments 1 reply
-
|
You're setting |
Beta Was this translation helpful? Give feedback.
1 reply
-
const api = got.extend({
hooks: {
beforeRequest: [
options => {
if (!options.context.token) {
options.context.token = getToken();
}
options.headers.authorization = `Bearer ${options.context.token}`;
}
]
}
});
// use it
await api('https://example.com/endpoint', {
context: {token: 'my-token'}
});
const api = got.extend({
mutableDefaults: true,
context: {token: 'initial-token'}
});
// later, update the token
api.defaults.options.context.token = 'new-token'; |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
sindresorhus
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
contextis never frozen, so you can always modify it. you can also update the token in mutable defaults: