From 473b219c3d873a847e5e5a05c2f4a309e879d292 Mon Sep 17 00:00:00 2001 From: Darcy Ye Date: Wed, 17 Aug 2022 15:10:57 +0800 Subject: [PATCH] refactor(connector): create PasswordlessConnector abstract class to share common logics --- packages/connector-schemas/src/class.ts | 94 ++++++------------------- 1 file changed, 21 insertions(+), 73 deletions(-) diff --git a/packages/connector-schemas/src/class.ts b/packages/connector-schemas/src/class.ts index 296240ac9..7333433fa 100644 --- a/packages/connector-schemas/src/class.ts +++ b/packages/connector-schemas/src/class.ts @@ -1,58 +1,21 @@ +import { Optional } from '@silverhand/essentials'; + import { ConnectorMetadata } from './types'; -export type EmailMessageTypes = { - SignIn: { - code: string; - }; - Register: { - code: string; - }; - ForgotPassword: { - code: string; - }; - Test: Record; -}; +// We will have message type 'ForgotPassword' available in the near future. +type MessageTypes = 'SignIn' | 'Register' | 'Test'; -export type SmsMessageTypes = EmailMessageTypes; - -export type EmailSendMessageFunction = ( - address: string, - type: keyof EmailMessageTypes, - payload: EmailMessageTypes[typeof type] -) => Promise; - -export type EmailSendTestMessageFunction = ( - config: Record, - address: string, - type: keyof EmailMessageTypes, - payload: EmailMessageTypes[typeof type] -) => Promise; - -export type EmailSendMessageByFunction = ( - config: T, - address: string, - type: keyof EmailMessageTypes, - payload: EmailMessageTypes[typeof type] +type SendMessageFunction = ( + to: string, + type: MessageTypes, + payload: { code: string } ) => Promise; -export type SmsSendMessageFunction = ( - phone: string, - type: keyof SmsMessageTypes, - payload: SmsMessageTypes[typeof type] -) => Promise; - -export type SmsSendTestMessageFunction = ( - config: Record, - phone: string, - type: keyof SmsMessageTypes, - payload: SmsMessageTypes[typeof type] -) => Promise; - -export type SmsSendMessageByFunction = ( +type SendMessageByFunction> = ( config: T, - phone: string, - type: keyof SmsMessageTypes, - payload: SmsMessageTypes[typeof type] + to: string, + type: MessageTypes, + payload: { code: string } ) => Promise; export type ValidateConfig = (config: unknown) => asserts config is T; @@ -64,7 +27,7 @@ export type GetAuthorizationUri = (payload: { export type GetUserInfo = ( data: unknown -) => Promise<{ id: string } & Record>; +) => Promise<{ id: string } & Record>>; export type GetConnectorConfig = (id: string) => Promise; @@ -73,52 +36,37 @@ export type AuthResponseParser> = (response: unknown abstract class BaseConnector { public getConfig: GetConnectorConfig; public metadata!: ConnectorMetadata; + public abstract validateConfig: ValidateConfig; constructor(getConnectorConfig: GetConnectorConfig) { this.getConfig = getConnectorConfig; } - - public abstract validateConfig(config: unknown): asserts config is T; } -export abstract class SmsConnector extends BaseConnector { - protected abstract readonly sendMessageBy: EmailSendMessageByFunction; +abstract class PasswordlessConnector extends BaseConnector { + protected abstract readonly sendMessageBy: SendMessageByFunction; - public sendMessage: EmailSendMessageFunction = async (address, type, data) => { + public sendMessage: SendMessageFunction = async (address, type, data) => { const config = await this.getConfig(this.metadata.id); this.validateConfig(config); return this.sendMessageBy(config, address, type, data); }; - public sendTestMessage?: EmailSendTestMessageFunction = async (config, address, type, data) => { + public sendTestMessage?: SendMessageByFunction = async (config, address, type, data) => { this.validateConfig(config); return this.sendMessageBy(config, address, type, data); }; } -export abstract class EmailConnector extends BaseConnector { - protected abstract readonly sendMessageBy: SmsSendMessageByFunction; +export abstract class SmsConnector extends PasswordlessConnector {} - public sendMessage: SmsSendMessageFunction = async (address, type, data) => { - const config = await this.getConfig(this.metadata.id); - this.validateConfig(config); - - return this.sendMessageBy(config, address, type, data); - }; - - public sendTestMessage?: SmsSendTestMessageFunction = async (config, address, type, data) => { - this.validateConfig(config); - - return this.sendMessageBy(config, address, type, data); - }; -} +export abstract class EmailConnector extends PasswordlessConnector {} export abstract class SocialConnector extends BaseConnector { public abstract getAuthorizationUri: GetAuthorizationUri; - public abstract getUserInfo: GetUserInfo; - protected authResponseParser?: AuthResponseParser; + protected abstract authResponseParser?: AuthResponseParser; }