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

[Typescript] Octokit "auth" option does not support example from docs for installation tokens #13

Closed
douglascayers opened this issue Jan 29, 2020 · 13 comments · Fixed by octokit/octokit.js#1563
Assignees

Comments

@douglascayers
Copy link

I recently updated the following octokit modules and began receiving a deprecation warning about not providing an authStrategy.

Deprecation Warning

Setting the "new Octokit({ auth })" option to an object without also setting the "authStrategy" option is deprecated and will be removed in v17. See (https://octokit.github.io/rest.js/#authentication)

Octokit Modules

"@octokit/app": "4.1.1",
"@octokit/auth-app": "2.4.2",
"@octokit/plugin-retry": "3.0.1",
"@octokit/plugin-throttling": "3.2.0",
"@octokit/rest": "16.40.1",
"@octokit/webhooks": "7.0.0",

Code That Raises Deprecation Warning

import OctokitRest from '@octokit/rest';

const client = new OctokitRest({
    async auth(): Promise<string> {
        const token = await app.getInstallationAccessToken({
            installationId: MY_INSTALLATION_ID,
        });
        return `token ${token}`;
    },
    ...
});

Code I Would Like To Use

import { createAppAuth } from '@octokit/auth-app';
import OctokitRest from '@octokit/rest';

const client = new OctokitRest({
    authStrategy: createAppAuth,
    auth: {
        id: MY_APP_ID,
        privateKey: MY_PRIVATE_KEY,
        installationId: MY_INSTALLATION_ID
    },
    ...
});

Discrepancy to Documentation

Per the example in the documentation, I should be able to specify the app id and privateKey as the auth settings but those are not valid per the current typescript definition.

image

Proposal

Not sure if this is on your roadmap to v17, but if not then here's my two cents.

Since the octokit rest module's authentication plugin just passes options.auth to the auth strategy via const auth = options.authStrategy(options.auth);, should the typescript definition for auth in this octokit rest module match StrategyOptions type as shown in the auth-app usages?

Thanks!

@gr2m
Copy link
Contributor

gr2m commented Jan 29, 2020

Thank you for reporting the bug Doug.

v17 of @octokit/rest will be built upon https://github.com/octokit/core.js. I tried to come up with a helpful TypeScript definition where auth types depend on the value of authStrategy, but to no avail. It does not seem that TypeScript currently supports that. Here is my question on StackOverflow on that: https://stackoverflow.com/q/59828577/206879

So for now, I will set auth to any, unless you have a better idea?

@gr2m
Copy link
Contributor

gr2m commented Jan 29, 2020

Could you have a glance at my PR at https://github.com/octokit/rest.js/pull/1563? It should remove the TypeScript error you are seeing?

@douglascayers
Copy link
Author

douglascayers commented Jan 30, 2020

Thanks for the quick follow up @gr2m. I’ll try to review the PR this week. Just wanted to reply to say I saw your message and on my todo to follow up. Thanks

Yes, the addition of any type will remove the compilation error. It gives us time in the short term to explore how to provide type checking in a future update.

@gr2m
Copy link
Contributor

gr2m commented Jan 30, 2020

Thanks Doug, I'll go ahead and merge it then. I'd love to improve the Types for the auth option. Please let me know if you have any ideas

@gr2m
Copy link
Contributor

gr2m commented Jan 30, 2020

fixed in v16.40.2

@gr2m gr2m closed this as completed Jan 30, 2020
@douglascayers
Copy link
Author

Thanks!

@weswigham
Copy link
Contributor

@gr2m all you want is a union type for Options, rather than for the auth field alone, so you can tie the types of the authStrategy and auth fields together.

Something like

  
  export interface AuthStrategyOne {
    authStrategy: AnAuthStrategy;
    auth: OptionsAnAuthStrategy;
  }
  export interface AuthStrategyTwo {
    authStrategy: TheThing;
    auth: OptionsForTheThing;
  }
  export interface NoAuthStrategy {
    authStrategy?: undefined;
  }
  export type Options = BaseOptions & (AuthStrategyOne | AuthStrategyTwo | NoAuthStrategy);
  export interface BaseOptions {
    userAgent?: string;
    previews?: string[];
    baseUrl?: string;
    log?: {
      debug?: (message: string, info?: object) => void;
      info?: (message: string, info?: object) => void;
      warn?: (message: string, info?: object) => void;
      error?: (message: string, info?: object) => void;
    };
    request?: {
      agent?: http.Agent;
      timeout?: number;
    };
    /**
     * @deprecated Use {request: {timeout}} instead. See https://github.com/octokit/request.js#request
     */
    timeout?: number;
    /**
     * @deprecated Use {userAgent, previews} instead. See https://github.com/octokit/request.js#request
     */
    headers?: { [header: string]: any };
    /**
     * @deprecated Use {request: {agent}} instead. See https://github.com/octokit/request.js#request
     */
    agent?: http.Agent;
    [option: string]: any;
  }

@gr2m
Copy link
Contributor

gr2m commented Feb 5, 2020

Unfortunately we don't know the full set of authentication strategies. Users can build their own. But I guess we could have some kind of fallback if it doesn't fit any of the known strategies?

@weswigham
Copy link
Contributor

Or you could make a sort of strategy registry;

export interface OctokitAuthStrategies {
  AuthStrategyOne: {
    authStrategy: AnAuthStrategy;
    auth: OptionsAnAuthStrategy;
  }
  AuthStrategyTwo: {
    authStrategy: TheThing;
    auth: OptionsForTheThing;
  }
  NoAuthStrategy: {
    authStrategy?: undefined;
  }
}
export type Options = BaseOptions & (OctokitAuthStrategies[keyof OctokitAuthStrategies]);

which can then be augmented by consumers/strategy authors with

declare module "@octokit/rest" {
  export interface OctokitAuthStrategies {
    MyCustomStrategy: {
      authStrategy: CustomThing;
      auth: CustomOptionsThing;
    }
  }
}

@gr2m
Copy link
Contributor

gr2m commented Feb 6, 2020

I see, thank you! Didn't think about that possibility!

@gr2m gr2m reopened this Feb 6, 2020
@gr2m
Copy link
Contributor

gr2m commented Feb 9, 2020

This would need to be implemented in @octokit/core, the upcoming version of @octokit/rest will be built on top of it. If anyone wants to beat me to it, please go ahead. I'll comment here once I start working on it myself

@gr2m gr2m self-assigned this Oct 26, 2020
@gr2m gr2m transferred this issue from octokit/octokit.js Apr 18, 2021
@gr2m
Copy link
Contributor

gr2m commented Apr 18, 2021

closing in favor of octokit/core.js#323

@gr2m gr2m closed this as completed Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@gr2m @weswigham @douglascayers and others