From 872b1d6450d74b88ed45452a3de88ccac578c0bc Mon Sep 17 00:00:00 2001 From: Wang Sijie Date: Fri, 21 Jan 2022 20:37:12 +0800 Subject: [PATCH] feat(github): validateConfig (#189) --- .../core/src/connectors/github/index.test.ts | 17 ++++++++++++++++- packages/core/src/connectors/github/index.ts | 19 ++++++++++++++++++- packages/core/src/connectors/types.ts | 1 + 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/core/src/connectors/github/index.test.ts b/packages/core/src/connectors/github/index.test.ts index cfc20998c..f714f04af 100644 --- a/packages/core/src/connectors/github/index.test.ts +++ b/packages/core/src/connectors/github/index.test.ts @@ -1,6 +1,6 @@ import nock from 'nock'; -import { getAccessToken, getAuthorizationUri } from '.'; +import { getAccessToken, getAuthorizationUri, validateConfig } from '.'; import { getConnectorConfig } from '../utilities'; import { accessTokenEndpoint, authorizationEndpoint } from './constant'; @@ -36,3 +36,18 @@ describe('getAccessToken', () => { expect(accessToken).toEqual('access_token'); }); }); + +describe('validateConfig', () => { + it('should pass on valid config', async () => { + await expect( + validateConfig({ clientId: 'clientId', clientSecret: 'clientSecret' }) + ).resolves.not.toThrow(); + }); + it('should throw on empty config', async () => { + // @ts-expect-error + await expect(validateConfig()).rejects.toThrowError(); + }); + it('should throw when missing clientSecret', async () => { + await expect(validateConfig({ clientId: 'clientId' })).rejects.toThrowError(); + }); +}); diff --git a/packages/core/src/connectors/github/index.ts b/packages/core/src/connectors/github/index.ts index 6938cf669..d9fdf151c 100644 --- a/packages/core/src/connectors/github/index.ts +++ b/packages/core/src/connectors/github/index.ts @@ -3,7 +3,13 @@ import got from 'got'; import { stringify } from 'query-string'; import { z } from 'zod'; -import { ConnectorMetadata, GetAccessToken, GetAuthorizationUri } from '../types'; +import { + ConnectorConfigError, + ConnectorMetadata, + GetAccessToken, + GetAuthorizationUri, + ValidateConfig, +} from '../types'; import { getConnectorConfig } from '../utilities'; import { authorizationEndpoint, accessTokenEndpoint, scope } from './constant'; @@ -28,6 +34,17 @@ const githubConfigGuard = z.object({ type GithubConfig = z.infer; +export const validateConfig: ValidateConfig = async (config: unknown) => { + if (!config) { + throw new ConnectorConfigError('Missing config'); + } + + const result = githubConfigGuard.safeParse(config); + if (!result.success) { + throw new ConnectorConfigError(result.error.message); + } +}; + export const getAuthorizationUri: GetAuthorizationUri = async (redirectUri, state) => { const config = await getConnectorConfig(metadata.id, metadata.type); return `${authorizationEndpoint}?${stringify({ diff --git a/packages/core/src/connectors/types.ts b/packages/core/src/connectors/types.ts index 07b108d7d..16d565042 100644 --- a/packages/core/src/connectors/types.ts +++ b/packages/core/src/connectors/types.ts @@ -14,6 +14,7 @@ export type ConnectorInstance = EmailConector | SocialConector; export interface BaseConnector { metadata: ConnectorMetadata; + validateConfig: ValidateConfig; } export interface EmailConector extends BaseConnector {