From 91f8fe8444d1a50f8aa7c26349db6a80618b68f2 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Fri, 19 Aug 2022 10:36:08 +0800 Subject: [PATCH] refactor(connector): remove unnecessary ConnectorInstance type check --- packages/core/src/connectors/index.ts | 17 +++++------ packages/core/src/connectors/types.ts | 30 ------------------- packages/core/src/lib/passcode.ts | 3 +- packages/core/src/lib/sign-in-experience.ts | 3 +- packages/core/src/routes/connector.test.ts | 3 +- .../core/src/routes/connector.update.test.ts | 8 +++-- .../core/src/routes/session/social.test.ts | 2 +- 7 files changed, 19 insertions(+), 47 deletions(-) diff --git a/packages/core/src/connectors/index.ts b/packages/core/src/connectors/index.ts index dce0de090..8cca845b8 100644 --- a/packages/core/src/connectors/index.ts +++ b/packages/core/src/connectors/index.ts @@ -1,7 +1,7 @@ import { existsSync, readFileSync } from 'fs'; import path from 'path'; -import { GetConnectorConfig } from '@logto/connector-schemas'; +import { ConnectorType, GetConnectorConfig } from '@logto/connector-schemas'; import { Connector } from '@logto/schemas/lib/db-entries'; import resolvePackagePath from 'resolve-package-path'; @@ -10,7 +10,7 @@ import RequestError from '@/errors/RequestError'; import { findAllConnectors, insertConnector } from '@/queries/connector'; import { defaultConnectorPackages } from './consts'; -import { ConnectorInstance, SocialConnectorInstance, ConnectorType } from './types'; +import { ConnectorInstance, SocialConnectorInstance } from './types'; import { getConnectorConfig } from './utilities'; // eslint-disable-next-line @silverhand/fp/no-let @@ -61,10 +61,7 @@ export const getConnectorInstances = async (): Promise => { } // eslint-disable-next-line no-restricted-syntax - const instance: ConnectorInstance = new InstanceBuilder( - getConnectorConfig, - connector - ) as ConnectorInstance; + const instance = new InstanceBuilder(getConnectorConfig, connector) as ConnectorInstance; // eslint-disable-next-line unicorn/prefer-module const packagePath = resolvePackagePath(packageName, __dirname); @@ -136,10 +133,10 @@ export const getConnectorInstanceById = async (id: string): Promise { - return connector.metadata.type === ConnectorType.Social; +export const isSocialConnectorInstance = ( + instance: ConnectorInstance +): instance is SocialConnectorInstance => { + return instance.metadata.type === ConnectorType.Social; }; export const getSocialConnectorInstanceById = async ( diff --git a/packages/core/src/connectors/types.ts b/packages/core/src/connectors/types.ts index 842c85470..c14095476 100644 --- a/packages/core/src/connectors/types.ts +++ b/packages/core/src/connectors/types.ts @@ -1,8 +1,5 @@ import { LogtoConnector, - ConnectorType, - ConnectorError, - ConnectorErrorCodes, SendMessageFunction, AuthResponseParser, GetAuthorizationUri, @@ -11,9 +8,6 @@ import { import { Connector, PasscodeType } from '@logto/schemas'; import { z } from 'zod'; -export { ConnectorType } from '@logto/connector-schemas'; -export type { ConnectorMetadata } from '@logto/connector-schemas'; - export type TemplateType = PasscodeType | 'Test'; export const socialUserInfoGuard = z.object({ @@ -51,27 +45,3 @@ export class SocialConnectorInstance extends LogtoConnector { protected authResponseParser?: AuthResponseParser; } - -export const isSmsConnectorInstance = ( - instance: unknown -): asserts instance is SmsConnectorInstance => { - if (!(instance instanceof ConnectorInstance && instance.metadata.type === ConnectorType.SMS)) { - throw new ConnectorError(ConnectorErrorCodes.General); - } -}; - -export const isEmailConnectorInstance = ( - instance: unknown -): asserts instance is EmailConnectorInstance => { - if (!(instance instanceof ConnectorInstance && instance.metadata.type === ConnectorType.Email)) { - throw new ConnectorError(ConnectorErrorCodes.General); - } -}; - -export const isSocialConnectorInstance = ( - instance: unknown -): asserts instance is SocialConnectorInstance => { - if (!(instance instanceof ConnectorInstance && instance.metadata.type === ConnectorType.Social)) { - throw new ConnectorError(ConnectorErrorCodes.General); - } -}; diff --git a/packages/core/src/lib/passcode.ts b/packages/core/src/lib/passcode.ts index a7e44ac94..36641aacd 100644 --- a/packages/core/src/lib/passcode.ts +++ b/packages/core/src/lib/passcode.ts @@ -1,8 +1,9 @@ +import { ConnectorType } from '@logto/connector-schemas'; import { Passcode, PasscodeType } from '@logto/schemas'; import { customAlphabet, nanoid } from 'nanoid'; import { getConnectorInstances } from '@/connectors'; -import { ConnectorType, EmailConnectorInstance, SmsConnectorInstance } from '@/connectors/types'; +import { EmailConnectorInstance, SmsConnectorInstance } from '@/connectors/types'; import RequestError from '@/errors/RequestError'; import { consumePasscode, diff --git a/packages/core/src/lib/sign-in-experience.ts b/packages/core/src/lib/sign-in-experience.ts index 1631ab7ca..08f047fd2 100644 --- a/packages/core/src/lib/sign-in-experience.ts +++ b/packages/core/src/lib/sign-in-experience.ts @@ -1,3 +1,4 @@ +import { ConnectorType } from '@logto/connector-schemas'; import { Branding, BrandingStyle, @@ -7,7 +8,7 @@ import { } from '@logto/schemas'; import { Optional } from '@silverhand/essentials'; -import { ConnectorInstance, ConnectorType } from '@/connectors/types'; +import { ConnectorInstance } from '@/connectors/types'; import RequestError from '@/errors/RequestError'; import assertThat from '@/utils/assert-that'; diff --git a/packages/core/src/routes/connector.test.ts b/packages/core/src/routes/connector.test.ts index 17ccadff7..184a3743a 100644 --- a/packages/core/src/routes/connector.test.ts +++ b/packages/core/src/routes/connector.test.ts @@ -1,8 +1,7 @@ -import { ValidateConfig } from '@logto/connector-schemas'; +import { ConnectorMetadata, ValidateConfig } from '@logto/connector-schemas'; import { Connector, ConnectorType } from '@logto/schemas'; import { mockConnectorInstanceList, mockMetadata, mockConnector } from '@/__mocks__'; -import { ConnectorMetadata } from '@/connectors/types'; import RequestError from '@/errors/RequestError'; import assertThat from '@/utils/assert-that'; import { createRequester } from '@/utils/test-utils'; diff --git a/packages/core/src/routes/connector.update.test.ts b/packages/core/src/routes/connector.update.test.ts index 1d52ccc1d..e62b7fe52 100644 --- a/packages/core/src/routes/connector.update.test.ts +++ b/packages/core/src/routes/connector.update.test.ts @@ -1,8 +1,12 @@ -import { ConnectorError, ConnectorErrorCodes, ValidateConfig } from '@logto/connector-schemas'; +import { + ConnectorError, + ConnectorErrorCodes, + ConnectorMetadata, + ValidateConfig, +} from '@logto/connector-schemas'; import { Connector, ConnectorType } from '@logto/schemas'; import { mockConnectorInstanceList, mockMetadata, mockConnector } from '@/__mocks__'; -import { ConnectorMetadata } from '@/connectors/types'; import RequestError from '@/errors/RequestError'; import { updateConnector } from '@/queries/connector'; import assertThat from '@/utils/assert-that'; diff --git a/packages/core/src/routes/session/social.test.ts b/packages/core/src/routes/session/social.test.ts index d470a9b6d..49f4e6584 100644 --- a/packages/core/src/routes/session/social.test.ts +++ b/packages/core/src/routes/session/social.test.ts @@ -1,9 +1,9 @@ +import { ConnectorType } from '@logto/connector-schemas'; import { User } from '@logto/schemas'; import { Provider } from 'oidc-provider'; import { mockUser, mockConnectorInstances } from '@/__mocks__'; import { getConnectorInstanceById } from '@/connectors'; -import { ConnectorType } from '@/connectors/types'; import RequestError from '@/errors/RequestError'; import { createRequester } from '@/utils/test-utils';