adds factory type for creds provider and rename default

Signed-off-by: Brian Fletcher <brian@roadie.io>
This commit is contained in:
Brian Fletcher
2021-12-20 13:54:19 +00:00
parent 6984c4988a
commit b2d67bde04
14 changed files with 71 additions and 51 deletions
@@ -31,11 +31,11 @@ jest.doMock('@octokit/rest', () => {
return { Octokit };
});
import { DefaultGithubCredentialsProvider } from './DefaultGithubCredentialsProvider';
import { SingleInstanceGithubCredentialsProvider } from './SingleInstanceGithubCredentialsProvider';
import { RestEndpointMethodTypes } from '@octokit/rest';
import { DateTime } from 'luxon';
const github = DefaultGithubCredentialsProvider.create({
const github = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [
{
@@ -204,7 +204,7 @@ describe('DefaultGithubCredentialsProvider tests', () => {
});
it('should return the default token if no app is configured', async () => {
const githubProvider = DefaultGithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [],
token: 'fallback_token',
@@ -218,7 +218,7 @@ describe('DefaultGithubCredentialsProvider tests', () => {
});
it('should return the configured token if there are no installations', async () => {
const githubProvider = DefaultGithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
apps: [
{
@@ -243,7 +243,7 @@ describe('DefaultGithubCredentialsProvider tests', () => {
});
it('should return undefined if no token or apps are configured', async () => {
const githubProvider = DefaultGithubCredentialsProvider.create({
const githubProvider = SingleInstanceGithubCredentialsProvider.create({
host: 'github.com',
});
@@ -22,6 +22,7 @@ import { DateTime } from 'luxon';
import {
GithubCredentials,
GithubCredentialsProvider,
GithubCredentialsProviderFactory,
GithubCredentialType,
} from './types';
@@ -228,17 +229,15 @@ export class GithubAppCredentialsMux {
*
* TODO: Possibly move this to a backend only package so that it's not used in the frontend by mistake
*/
export class DefaultGithubCredentialsProvider
export class SingleInstanceGithubCredentialsProvider
implements GithubCredentialsProvider
{
static create(
config: GitHubIntegrationConfig,
): DefaultGithubCredentialsProvider {
return new DefaultGithubCredentialsProvider(
static create: GithubCredentialsProviderFactory = config => {
return new SingleInstanceGithubCredentialsProvider(
new GithubAppCredentialsMux(config),
config.token,
);
}
};
private constructor(
private readonly githubAppCredentialsMux: GithubAppCredentialsMux,
+3 -2
View File
@@ -22,11 +22,12 @@ export type { GithubAppConfig, GitHubIntegrationConfig } from './config';
export { getGitHubFileFetchUrl, getGitHubRequestOptions } from './core';
export {
GithubAppCredentialsMux,
DefaultGithubCredentialsProvider,
} from './DefaultGithubCredentialsProvider';
SingleInstanceGithubCredentialsProvider,
} from './SingleInstanceGithubCredentialsProvider';
export type {
GithubCredentials,
GithubCredentialsProvider,
GithubCredentialsProviderFactory,
GithubCredentialType,
} from './types';
export { GitHubIntegration, replaceGitHubUrlType } from './GitHubIntegration';
+12
View File
@@ -14,6 +14,8 @@
* limitations under the License.
*/
import { GitHubIntegrationConfig } from './config';
/**
* The type of credentials produced by the credential provider.
*
@@ -41,3 +43,13 @@ export type GithubCredentials = {
export interface GithubCredentialsProvider {
getCredentials(opts: { url: string }): Promise<GithubCredentials>;
}
/**
* This allows implementations to provide factories to create credential providers
*
* @public
*
*/
export type GithubCredentialsProviderFactory = (
config: GitHubIntegrationConfig,
) => GithubCredentialsProvider;