Open
Description
Hi there!
All the examples of the .defaults()
function that I could find are pretty straightforward, for example:
export const graphql = baseGraphql.defaults({
headers: { authorization: 'token 1234' },
});
However, there are cases where some defaults are not available during the initial importing of modules, for examples if they are loaded from somewhere (i.e. async, e.g. from disk or another API) or if they require additional checking/validation.
Essentially, it would be great if we could do something like this:
export const graphql = baseGraphql.defaults(async () => {
const oAuthToken = await getOAuthToken();
return { headers: { authorization: `token ${oAuthToken}` } };
});
The underlying type of defaults
would be:
defaults: (newDefaults: RequestParameters | (() => Promise<RequestParameters>)) => graphql;
In the meantime, we could probably get away with something like the following, but TypeScript types are getting in the way and I don't want to end up with something too hacky just to be able to use .defaults
:
// This does not work
export const graphql: typeof baseGraphql = async (...args) => {
const oAuthToken = await getOAuthToken();
const graphqlWithAuth = baseGraphql.defaults({
headers: { authorization: `token ${oAuthToken}` },
});
return graphqlWithAuth(...args);
};