diff --git a/packages/connector-aliyun-dm/src/index.ts b/packages/connector-aliyun-dm/src/index.ts index 470de315e..aa6e419b6 100644 --- a/packages/connector-aliyun-dm/src/index.ts +++ b/packages/connector-aliyun-dm/src/index.ts @@ -4,6 +4,7 @@ import { ConnectorMetadata, EmailMessageTypes, EmailSendMessageFunction, + EmailSendTestMessageFunction, ValidateConfig, EmailConnector, GetConnectorConfig, @@ -40,11 +41,7 @@ export default class AliyunDmConnector implements EmailConnector { return this.sendMessageBy(emailConfig, address, type, data); }; - public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters); - } - + public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => { await this.validateConfig(config); return this.sendMessageBy(config as AliyunDmConfig, address, type, data); diff --git a/packages/connector-aliyun-sms/src/index.ts b/packages/connector-aliyun-sms/src/index.ts index de19714a5..19f6d8110 100644 --- a/packages/connector-aliyun-sms/src/index.ts +++ b/packages/connector-aliyun-sms/src/index.ts @@ -4,6 +4,7 @@ import { ConnectorMetadata, SmsMessageTypes, SmsSendMessageFunction, + SmsSendTestMessageFunction, ValidateConfig, SmsConnector, GetConnectorConfig, @@ -35,14 +36,10 @@ export default class AliyunSmsConnector implements SmsConnector { return this.sendMessageBy(smsConfig, phone, type, { code }); }; - public sendTestMessage: SmsSendMessageFunction = async (phone, type, { code }, config) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters); - } - + public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, config) => { await this.validateConfig(config); - return this.sendMessageBy(config as AliyunSmsConfig, phone, type, { code }); + return this.sendMessageBy(config as AliyunSmsConfig, phone, type, data); }; private readonly sendMessageBy = async ( diff --git a/packages/connector-sendgrid-mail/src/index.ts b/packages/connector-sendgrid-mail/src/index.ts index 6a40de97e..202a0956f 100644 --- a/packages/connector-sendgrid-mail/src/index.ts +++ b/packages/connector-sendgrid-mail/src/index.ts @@ -4,6 +4,7 @@ import { ConnectorMetadata, EmailMessageTypes, EmailSendMessageFunction, + EmailSendTestMessageFunction, ValidateConfig, EmailConnector, GetConnectorConfig, @@ -41,11 +42,7 @@ export default class SendGridMailConnector implements EmailConnector { return this.sendMessageBy(emailConfig, address, type, data); }; - public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters); - } - + public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => { await this.validateConfig(config); return this.sendMessageBy(config as SendGridMailConfig, address, type, data); diff --git a/packages/connector-smtp/src/index.ts b/packages/connector-smtp/src/index.ts index 7ed40532c..1422154c0 100644 --- a/packages/connector-smtp/src/index.ts +++ b/packages/connector-smtp/src/index.ts @@ -4,6 +4,7 @@ import { ConnectorMetadata, EmailMessageTypes, EmailSendMessageFunction, + EmailSendTestMessageFunction, ValidateConfig, EmailConnector, GetConnectorConfig, @@ -35,11 +36,7 @@ export default class SmtpConnector implements EmailConnector { return this.sendMessageBy(emailConfig, address, type, data); }; - public sendTestMessage: EmailSendMessageFunction = async (address, type, data, config) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters); - } - + public sendTestMessage: EmailSendTestMessageFunction = async (address, type, data, config) => { await this.validateConfig(config); return this.sendMessageBy(config as SmtpConfig, address, type, data); diff --git a/packages/connector-twilio-sms/src/index.ts b/packages/connector-twilio-sms/src/index.ts index c17b4fb22..743ed9a77 100644 --- a/packages/connector-twilio-sms/src/index.ts +++ b/packages/connector-twilio-sms/src/index.ts @@ -4,6 +4,7 @@ import { ConnectorMetadata, SmsMessageTypes, SmsSendMessageFunction, + SmsSendTestMessageFunction, ValidateConfig, SmsConnector, GetConnectorConfig, @@ -34,14 +35,10 @@ export default class TwilioSmsConnector implements SmsConnector { return this.sendMessageBy(smsConfig, address, type, data); }; - public sendTestMessage: SmsSendMessageFunction = async (address, type, data, config) => { - if (!config) { - throw new ConnectorError(ConnectorErrorCodes.InsufficientRequestParameters); - } - + public sendTestMessage: SmsSendTestMessageFunction = async (phone, type, data, config) => { await this.validateConfig(config); - return this.sendMessageBy(config as TwilioSmsConfig, address, type, data); + return this.sendMessageBy(config as TwilioSmsConfig, phone, type, data); }; private readonly sendMessageBy = async ( diff --git a/packages/connector-types/src/index.ts b/packages/connector-types/src/index.ts index 7ccc070f1..6f93f9e21 100644 --- a/packages/connector-types/src/index.ts +++ b/packages/connector-types/src/index.ts @@ -33,6 +33,7 @@ export enum ConnectorErrorCodes { InvalidConfig, InvalidResponse, TemplateNotFound, + NotImplemented, SocialAuthCodeInvalid, SocialAccessTokenInvalid, SocialIdTokenInvalid, @@ -67,17 +68,29 @@ export type EmailMessageTypes = { export type SmsMessageTypes = EmailMessageTypes; export type EmailSendMessageFunction = ( + address: string, + type: keyof EmailMessageTypes, + payload: EmailMessageTypes[typeof type] +) => Promise; + +export type EmailSendTestMessageFunction = ( address: string, type: keyof EmailMessageTypes, payload: EmailMessageTypes[typeof type], - config?: Record + config: Record ) => Promise; export type SmsSendMessageFunction = ( + phone: string, + type: keyof SmsMessageTypes, + payload: SmsMessageTypes[typeof type] +) => Promise; + +export type SmsSendTestMessageFunction = ( phone: string, type: keyof SmsMessageTypes, payload: SmsMessageTypes[typeof type], - config?: Record + config: Record ) => Promise; export interface BaseConnector { @@ -88,12 +101,12 @@ export interface BaseConnector { export interface SmsConnector extends BaseConnector { sendMessage: SmsSendMessageFunction; - sendTestMessage: SmsSendMessageFunction; + sendTestMessage?: SmsSendTestMessageFunction; } export interface EmailConnector extends BaseConnector { sendMessage: EmailSendMessageFunction; - sendTestMessage: EmailSendMessageFunction; + sendTestMessage?: EmailSendTestMessageFunction; } export interface SocialConnector extends BaseConnector { diff --git a/packages/core/src/middleware/koa-connector-error-handler.ts b/packages/core/src/middleware/koa-connector-error-handler.ts index 894971baf..1f689429f 100644 --- a/packages/core/src/middleware/koa-connector-error-handler.ts +++ b/packages/core/src/middleware/koa-connector-error-handler.ts @@ -53,6 +53,14 @@ export default function koaConnectorErrorHandler(): Middleware }, data ); + case ConnectorErrorCodes.NotImplemented: + throw new RequestError( + { + code: 'connector.not_implemented', + status: 500, + }, + data + ); case ConnectorErrorCodes.SocialAuthCodeInvalid: throw new RequestError( { diff --git a/packages/core/src/routes/connector.ts b/packages/core/src/routes/connector.ts index c0bc18c08..c785e237f 100644 --- a/packages/core/src/routes/connector.ts +++ b/packages/core/src/routes/connector.ts @@ -187,17 +187,16 @@ export default function connectorRoutes(router: T) { }) ); - if (config) { - await connector.validateConfig(config); - } + const { sendTestMessage } = connector; + assertThat(sendTestMessage, new RequestError('connector.not_implemented')); - await connector.sendTestMessage( + await sendTestMessage( subject, 'Test', { code: phone ? '123456' : 'email-test', }, - config + config as Record ); ctx.status = 204; diff --git a/packages/phrases/src/locales/en.ts b/packages/phrases/src/locales/en.ts index a60999c39..4bf425e53 100644 --- a/packages/phrases/src/locales/en.ts +++ b/packages/phrases/src/locales/en.ts @@ -606,6 +606,7 @@ const errors = { invalid_config: "The connector's config is invalid.", invalid_response: "The connector's response is invalid.", template_not_found: 'Unable to find correct template in connector config.', + not_implemented: 'The method has not been implemented.', invalid_access_token: "The connector's access token is invalid.", invalid_auth_code: "The connector's auth code is invalid.", invalid_id_token: "The connector's id token is invalid.", diff --git a/packages/phrases/src/locales/zh-cn.ts b/packages/phrases/src/locales/zh-cn.ts index e4ae9ce4d..c6bd8a392 100644 --- a/packages/phrases/src/locales/zh-cn.ts +++ b/packages/phrases/src/locales/zh-cn.ts @@ -584,6 +584,7 @@ const errors = { invalid_config: '连接器配置错误', invalid_response: '连接器错误响应', template_not_found: '无法从连接器配置中找到对应的模板', + not_implemented: '该方法尚未实现', invalid_access_token: '当前连接器的 access_token 无效', invalid_auth_code: '当前连接器的授权码无效', invalid_id_token: '当前连接器的 id_token 无效',