question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

7reactions
weswighamcommented, Feb 6, 2020

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;
    }
  }
}
6reactions
gr2mcommented, Apr 6, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Implement types for authentication strategies #323 - GitHub
[Typescript] Octokit "auth" option does not support example from docs for installation tokens octokit/rest.js#13.
Read more >
octokit/rest.js
Personal access token (create). This is the default authentication strategy. Set the options.auth option to the token in new Octokit(options) .
Read more >
octokit - npm
By default, the Octokit API client supports authentication using a static token. There are different means of authentication that are ...
Read more >
GitHub Automation with Octokit - Learn With Jason
May 18 @ 5:30 PM UTC — GitHub has powerful APIs, and with Octokit we can do just about anything on GitHub through...
Read more >
GitHub Automation with Octokit (with Gregor Martynus)
GitHub has powerful APIs, and with Octokit we can do just about anything on GitHub through code. Gregor Martynus will teach us how!...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found