Skip to content
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

Export default request handler from rest package #3403

Open
2 tasks done
dejan-crocoder opened this issue Sep 6, 2023 · 6 comments
Open
2 tasks done

Export default request handler from rest package #3403

dejan-crocoder opened this issue Sep 6, 2023 · 6 comments
Labels
type:technical debt Changes only affect the internal code, improving performance/quality

Comments

@dejan-crocoder
Copy link

Description

I would like to be able to reuse the default request handler defined in packages/rest/src/Requester.ts to allow extending the default behaviour of the client.
Use case: need to add retry logic when access token expires, so I can refresh the token.

This would sort-of solve issue #279

Proposal

Export the defaultRequestHandler and defaultRequestOptions from @gitbeaker/rest package.

Checklist

  • I have checked that this is not a duplicate issue.
  • I have read the documentation.
@jdalrymple
Copy link
Owner

Hmm, the use case sounds more like some good middleware. Perhaps its time to actually implement that haha

@jdalrymple
Copy link
Owner

Though, it wouldnt be a bad feature to just add either...

@jdalrymple
Copy link
Owner

Ill reorganize things to move more of the logic to the utils package which should make it more reusable. If you could whip up a small sample of how that retry logic would work, i could incorporate it into that as well

@nhollander-alert
Copy link
Contributor

I encountered the same issue, I was able to work around it by writing an asynchronous function that renews and returns the token and passing it as the token when constructing the GitLab object. The following is roughly what I am using for an internal project at my employer using oauth4webapi to handle issuing/renewing the token.

let token: {
    access_token: string,
    refresh_token: string,
    expires_in: number,
    expires_at: number  // Non-standard field calculated at time token is issued
} = {...};

async function refresh_token() {
    token = ...;  // Refresh using `refreshTokenGrantRequest` from `oauth4webapi`
    token.expires_at = token.expires_in + (Date.now() / 1000);
}

async function get_token() {
    if(token.expires_at - (Date.now() / 1000) < 0) {
        token = await refresh_token();
    }
    return token.access_token.
}

const gl = new Gitlab({
    oauthToken: get_token
});

@jdalrymple jdalrymple added the type:technical debt Changes only affect the internal code, improving performance/quality label Sep 15, 2023
@dejan-crocoder
Copy link
Author

Hey, sorry for leaving this issue unattended, had a busy month.

My usecase is a bit different than the default refresh token flow defined in RFC 6749. I'm using a third party integration which abstracts using refresh tokens, and it doesn't provide me with an expiration date either, so I need a custom flow to reauthorize.

Not sure what the best approach is at this

@jdalrymple
Copy link
Owner

hmm, how do you know when to refresh? I could expose a handler of some sort, that would be executed when an expiry happens, but hmm you said there is not expiry date. The latest stuff ive been working on exposes everything adequately, and allows you to replace the handler either in pieces or its entirety. I havent added the refresh flow as of yet since im not sure the best way to do this that isnt highly restrictive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:technical debt Changes only affect the internal code, improving performance/quality
Projects
None yet
Development

No branches or pull requests

3 participants