-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support callback for defaults #326
Comments
I don't think it's necessary. If you really need a defaults callback then you can use https://github.com/octokit/core.js which does support it. For your particular case you can set That's how |
Hi @gr2m, thanks for the response! Do you think you could provide some guidance on how to accomplish that? That's where I'm at right now and needless to say it's not working + those TS types are definitely wrong... import { withCustomRequest } from '@octokit/graphql';
import { request } from '@octokit/request';
const myRequest = request.defaults({
request: {
async hook(req: any, options: any) {
const oAuthToken = await getOAuthToken();
req.headers = { authorization: `token ${oAuthToken}` };
return req(options);
},
},
});
const graphql = withCustomRequest(myRequest); Thanks! |
try this - req.headers = { authorization: `token ${oAuthToken}` };
+ options.headers.authorization = `token ${oAuthToken}` |
I'll try that, thank you! Do you happen to know how I can type this properly? It seemed like some types were not exported for that.
|
Alright, so the following works beautifully: export const graphql = withCustomRequest(
request.defaults({
request: {
async hook(req: any, options: any) {
const oAuthToken = await getOAuthToken();
if (!options.headers) {
options.headers = {};
}
options.headers.authorization = `token ${oAuthToken}`;
return req(options);
},
},
}),
); Regarding the types:
Any chance |
So in case anyone needs this, in the end I went with the following, until I can figure out the types a little better 😕 : export const graphql = withCustomRequest(
request.defaults({
request: {
async hook(
req: (options: { headers?: Record<string, string> }) => typeof request,
options: { headers?: Record<string, string> },
) {
const oAuthToken = await getOAuthToken();
if (!options.headers) {
options.headers = {};
}
options.headers.authorization = `token ${oAuthToken}`;
return req(options);
},
},
}),
); I also tried the following with import { Octokit } from '@octokit/core';
const OctokitWithAuth = Octokit.defaults(
async (options: { headers?: Record<string, string> }) => {
const oAuthToken = await getOAuthToken();
return {
...options,
headers: {
...options.headers,
authorization: `token ${oAuthToken}`,
},
};
},
);
const { graphql } = new OctokitWithAuth();
export { graphql }; It seems like the header doesn't get picked up at all, the requests would not get authenticated. |
@gr2m, if you have any suggestions, I'll take 'em 😅 |
Hi there!
All the examples of the
.defaults()
function that I could find are pretty straightforward, for example: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:
The underlying type of
defaults
would be: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
:The text was updated successfully, but these errors were encountered: