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

Improve types for ThrottlingOctokitOptions #457

Merged
merged 15 commits into from Mar 17, 2022

Conversation

oscard0m
Copy link
Member

@oscard0m oscard0m commented Feb 18, 2022

Description

To improve typing for octokitOptions for plugin-throttling

Context

It can be interesting for:

  • deprecating properties/methods
  • auto-complete

@ghost ghost added this to Inbox in JS Feb 18, 2022
src/index.d.ts Outdated
};
}

export type OctokitThrottlingOptions = OctokitOptions | ThrottlingOptions;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A loooong time without touching TS. If I don't do this Union the compiler does not allow me to use ThrottlingOptions in a place where OctokitOptions is expected.

Doing this union we are not making options.throttle mandatory which is not good.

I still have to play around, maybe with generics OctokitOptions<T>?

This comment was marked as resolved.

@oscard0m oscard0m changed the title WIP: Improve types for ThrottlingOctokitOptions Improve types for ThrottlingOctokitOptions Feb 18, 2022
Copy link
Member

@wolfy1339 wolfy1339 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

Left some comments

src/index.d.ts Outdated Show resolved Hide resolved
src/index.d.ts Outdated
};
}

export type OctokitThrottlingOptions = OctokitOptions | ThrottlingOptions;

This comment was marked as resolved.

@wolfy1339 wolfy1339 added Type: Feature New feature or request typescript labels Feb 18, 2022
@ghost ghost moved this from Inbox to Features in JS Feb 18, 2022
src/index.d.ts Outdated Show resolved Hide resolved
src/index.d.ts Outdated Show resolved Hide resolved
src/index.d.ts Outdated Show resolved Hide resolved
@oscard0m oscard0m force-pushed the improve-types-to-octokit-options-plugin branch 2 times, most recently from 04e3648 to c567474 Compare February 22, 2022 19:01
@wolfy1339
Copy link
Member

We can get better types for Bottleneck. They just aren't published to npm for some reason.

We'll have to vendor them in the repo.

https://github.com/SGrondin/bottleneck/blob/master/light.d.ts

As for the options type, I'm not entirely sure.
The only idea, I have is to check if the throttle property is present and that might satisfy TS

@oscard0m
Copy link
Member Author

oscard0m commented Mar 2, 2022

We can get better types for Bottleneck. They just aren't published to npm for some reason.

We'll have to vendor them in the repo.

https://github.com/SGrondin/bottleneck/blob/master/light.d.ts

As for the options type, I'm not entirely sure. The only idea, I have is to check if the throttle property is present and that might satisfy TS

I had to create a TS Playground to explain the problem:

Basically the [option: string]: any inside type OctokitOptions is preventing TypeScript to get the branch for type ThrottlingOptions when the shape of throttling is wrong.

Any ideas on how to bypass this? Would this imply to rethink type OctokitOptions? @wolfy1339 (@G-Rath, master of TS, I invoke you 🌋 )

@G-Rath
Copy link
Member

G-Rath commented Mar 2, 2022

If I understand what you're wanting correctly (which is that when plugin-throttling is being used, GithubOptions should have an additional property throttle for options for this plugin), I think you should be using Declaration Merging.

Roughly:

interface OctokitOptions {
    authStrategy?: any;
    auth?: any;
    userAgent?: string;
    previews?: string[];
    baseUrl?: string;
    log?: {
        debug: (message: string) => unknown;
        info: (message: string) => unknown;
        warn: (message: string) => unknown;
        error: (message: string) => unknown;
    };
    request?: any //OctokitTypes.RequestRequestOptions;
    timeZone?: string;
    [option: string]: unknown
};

interface OctokitOptions {
  throttle?: ThrottlingOptions;
}

interface ThrottlingOptions {
  enabled?: boolean;
  Bottleneck?: any;
  id?: string;
  timeout?: number;
  connection?: any;
  onAbuseLimit: LimitHandler;
  onRateLimit: LimitHandler;
}

See playground

This requires that the type be an interface (and in general, why it's generally better for libraries to use interfaces rather than type as it allows downstream consumers to enhance the types), so it'll need a change in @octokit/core.

Once that change has been made, the final code will be something like this:

declare module '@octokit/core' {
  interface OctokitOptions {
    throttle?: ThrottlingOptions;
  }
}

interface ThrottlingOptions {
  enabled?: boolean;
  Bottleneck?: any;
  id?: string;
  timeout?: number;
  connection?: any;
  onAbuseLimit: LimitHandler;
  onRateLimit: LimitHandler;
}

(module pathing can be a bit funny, so that might need adjusting but it's easier to do when the interface actually exists in the package - in the meantime this is a good example of this in action)

src/types.ts Show resolved Hide resolved
@oscard0m oscard0m force-pushed the improve-types-to-octokit-options-plugin branch 4 times, most recently from d7659c8 to acf03b4 Compare March 10, 2022 15:06
@oscard0m
Copy link
Member Author

Types are getting more complex than expected, it will get me some time to sort them out. I'll keep you posted next week

Pushed a second iteration of the types. Still having problems with

export const TestOctokit = Octokit.plugin(testPlugin, throttling);

I get the following TS error:

Argument of type 'typeof throttling' is not assignable to parameter of type 'OctokitPlugin'.
  Types of parameters 'options' and 'options' are incompatible.
    Type 'OctokitOptions' is not assignable to type '{ throttle: ThrottlingOptionsBase & SecondaryLimitHandler; }'.
      Types of property 'throttle' are incompatible.
        Type 'ThrottlingOptions' is not assignable to type 'ThrottlingOptionsBase & SecondaryLimitHandler'.
          Type 'ThrottlingOptionsBase & AbuseLimitHandler' is not assignable to type 'ThrottlingOptionsBase & SecondaryLimitHandler'.
            Property 'onSecondaryRateLimit' is missing in type 'ThrottlingOptionsBase & AbuseLimitHandler' but required in type 'SecondaryLimitHandler'.ts(2345)
[types.ts(24, 3): ]()'onSecondaryRateLimit' is declared here.

Any clue on what could be the issue? @wolfy1339 @G-Rath

💡 The change in @octokit/core.js to migrate type OctokitOptions to an interface failed 💣 , until the issue is fixed, you will need to replace it manually in your local node_modules to run the tests.

src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/types.ts Outdated Show resolved Hide resolved
src/route-matcher.ts Outdated Show resolved Hide resolved
@oscard0m oscard0m force-pushed the improve-types-to-octokit-options-plugin branch from d9a4210 to ab7bbab Compare March 12, 2022 21:57
@oscard0m oscard0m force-pushed the improve-types-to-octokit-options-plugin branch from ab7bbab to 0011470 Compare March 12, 2022 22:44
@ghost ghost moved this from Features to Blocked in JS Mar 12, 2022
@oscard0m
Copy link
Member Author

oscard0m commented Mar 12, 2022

Blocked by octokit/core.js#451 resolution to update @octokit/core dependency with latest interface OctokitOptions change (octokit/core.js#450)

@oscard0m oscard0m force-pushed the improve-types-to-octokit-options-plugin branch from e92e997 to 750f3a3 Compare March 14, 2022 09:16
@oscard0m oscard0m marked this pull request as ready for review March 14, 2022 09:19
@oscard0m oscard0m removed the blocked label Mar 14, 2022
@oscard0m oscard0m requested a review from wolfy1339 March 14, 2022 09:20
@ghost ghost moved this from Blocked to Features in JS Mar 14, 2022
@oscard0m oscard0m dismissed wolfy1339’s stale review March 14, 2022 09:20

I want to re-run checks

@oscard0m oscard0m requested a review from gr2m March 16, 2022 23:29
Copy link
Contributor

@gr2m gr2m left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR, thanks for adding type checks!

@gr2m gr2m merged commit fd92be9 into master Mar 17, 2022
@gr2m gr2m deleted the improve-types-to-octokit-options-plugin branch March 17, 2022 04:23
JS automation moved this from Features to Done Mar 17, 2022
@github-actions
Copy link
Contributor

🎉 This PR is included in version 3.6.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
No open projects
JS
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants