diff --git a/packages/connector-aliyun-sms/README.md b/packages/connector-aliyun-sms/README.md index 46e2e96f..b3dc4872 100644 --- a/packages/connector-aliyun-sms/README.md +++ b/packages/connector-aliyun-sms/README.md @@ -62,11 +62,13 @@ Go to the [Aliyun website](https://cn.aliyun.com/) and register your Aliyun acco - You can add multiple SMS connector templates for different cases. Here is an example of adding a single template: - Fill the `templateCode` field, which is how you can control SMS context, with "Template Code" (模板 CODE) from step 2. - Fill out `usageType` field with either `Register`, `SignIn`, `ForgotPassword`, `Generic` or `Test` for different use cases. (`usageType` is a Logto property to identify the proper use case.) In order to enable full user flows, templates with usageType `Register`, `SignIn` and `ForgotPassword` are required. + - If you need to send international SMS please set `endpoint` to the access point of the corresponding region Here is an example of Aliyun SMS connector config JSON. ```json { + "endpoint": "", "accessKeyId": "", "accessKeySecret": "", "signName": "", @@ -90,7 +92,7 @@ Here is an example of Aliyun SMS connector config JSON. { "templateCode": "", "usageType": "Test" - }, + } ] } ``` @@ -103,12 +105,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs ### Config types -| Name | Type | -|-----------------|------------| -| accessKeyId | string | -| accessKeySecret | string | -| signName | string | -| templates | Template[] | +| Name | Type | Description | +|-----------------|-------------|----------------------------------------| +| endpoint | string null | Use mainland China endpoint when empty | +| accessKeyId | string | | +| accessKeySecret | string | | +| signName | string | | +| templates | Template[] | | | Template Properties | Type | Enum values | |---------------------|-------------|------------------------------------------------------| @@ -160,11 +163,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs - 你可以添加多个短信服务模板以应对不同的用户场景。这里展示填写单个模板的例子: - `templateCode` 栏是你可以用来控制所发送短信内容的属性。它们的值从步骤 2 中的「模板 CODE」获取。 - `usageType` 栏填写 `Register`,`SignIn`,`ForgotPassword`,`Generic` 或者 `Test` 其中之一以分别对应 _注册_,_登录_,_忘记密码_,_用户档案补全_ 和 _测试_ 的不同场景。(`usageType` 是 Logto 的属性,用来确定使用场景。)为了能够使用完成的流程,需要配置 `usageType` 为 `Register`,`SignIn` 以及 `ForgotPassword` 的模板。 + - 如果你需要发送国际短息请设置 `endpoint` 为对应地区的接入点 这是一个阿里云短信服务连接器 JSON 配置的样例。 ```json { + "endpoint": "", "accessKeyId": "", "accessKeySecret": "", "signName": "", @@ -188,7 +193,7 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs { "templateCode": "", "usageType": "Test" - }, + } ] } ``` @@ -201,12 +206,13 @@ That's it. Don't forget to [Enable connector in sign-in experience](https://docs ### 配置类型 -| 名称 | 类型 | -|-----------------|------------| -| accessKeyId | string | -| accessKeySecret | string | -| signName | string | -| templates | Template[] | +| 名称 | 类型 | 描述 | +|------------------|-------------|--------------| +| endpoint | string null | 为空时使用中国大陆接入点 | +| accessKeyId | string | | +| accessKeySecret | string | | +| signName | string | | +| templates | Template[] | | | 模板属性 | 类型 | 枚举值 | |--------------|-------------|------------------------------------------------------| diff --git a/packages/connector-aliyun-sms/docs/config-template.json b/packages/connector-aliyun-sms/docs/config-template.json index b4afb6db..620c9475 100644 --- a/packages/connector-aliyun-sms/docs/config-template.json +++ b/packages/connector-aliyun-sms/docs/config-template.json @@ -1,4 +1,5 @@ { + "endpoint": "", "accessKeyId": "", "accessKeySecret": "", "signName": "", diff --git a/packages/connector-aliyun-sms/src/constant.ts b/packages/connector-aliyun-sms/src/constant.ts index 9b0ed00b..67d811ce 100644 --- a/packages/connector-aliyun-sms/src/constant.ts +++ b/packages/connector-aliyun-sms/src/constant.ts @@ -1,7 +1,5 @@ import type { ConnectorMetadata } from '@logto/connector-kit'; -export const endpoint = 'https://dysmsapi.aliyuncs.com/'; - export const staticConfigs = { Format: 'json', RegionId: 'cn-hangzhou', diff --git a/packages/connector-aliyun-sms/src/index.ts b/packages/connector-aliyun-sms/src/index.ts index 38f10710..e2bc667f 100644 --- a/packages/connector-aliyun-sms/src/index.ts +++ b/packages/connector-aliyun-sms/src/index.ts @@ -25,7 +25,7 @@ const sendMessage = const { to, type, payload } = data; const config = inputConfig ?? (await getConfig(defaultMetadata.id)); validateConfig(config, aliyunSmsConfigGuard); - const { accessKeyId, accessKeySecret, signName, templates } = config; + const { endpoint, accessKeyId, accessKeySecret, signName, templates } = config; const template = templates.find(({ usageType }) => usageType === type); assert( @@ -33,8 +33,11 @@ const sendMessage = new ConnectorError(ConnectorErrorCodes.TemplateNotFound, `Cannot find template!`) ); + const _endpoint = endpoint ?? 'https://dysmsapi.aliyuncs.com/'; + try { const httpResponse = await sendSms( + _endpoint, { AccessKeyId: accessKeyId, PhoneNumbers: to, diff --git a/packages/connector-aliyun-sms/src/mock.ts b/packages/connector-aliyun-sms/src/mock.ts index cbd6a046..9ee2b4a2 100644 --- a/packages/connector-aliyun-sms/src/mock.ts +++ b/packages/connector-aliyun-sms/src/mock.ts @@ -1,4 +1,5 @@ export const mockedConnectorConfig = { + endpoint: 'https://dysmsapi.aliyuncs.com/', accessKeyId: 'accessKeyId', accessKeySecret: 'accessKeySecret', signName: 'signName', diff --git a/packages/connector-aliyun-sms/src/single-send-text.test.ts b/packages/connector-aliyun-sms/src/single-send-text.test.ts index 64d269e9..8d39ac1d 100644 --- a/packages/connector-aliyun-sms/src/single-send-text.test.ts +++ b/packages/connector-aliyun-sms/src/single-send-text.test.ts @@ -13,6 +13,7 @@ describe('sendSms', () => { const code = mockedRandomCode; await sendSms( + 'https://dysmsapi.aliyuncs.com', { AccessKeyId: '', PhoneNumbers: '13912345678', diff --git a/packages/connector-aliyun-sms/src/single-send-text.ts b/packages/connector-aliyun-sms/src/single-send-text.ts index 1c963ed0..93fa4ffc 100644 --- a/packages/connector-aliyun-sms/src/single-send-text.ts +++ b/packages/connector-aliyun-sms/src/single-send-text.ts @@ -1,10 +1,14 @@ -import { endpoint, staticConfigs } from './constant.js'; +import { staticConfigs } from './constant.js'; import type { PublicParameters, SendSms } from './types.js'; import { request } from './utils.js'; /** * @doc https://help.aliyun.com/document_detail/101414.html */ -export const sendSms = async (parameters: PublicParameters & SendSms, accessKeySecret: string) => { +export const sendSms = async ( + endpoint: string, + parameters: PublicParameters & SendSms, + accessKeySecret: string +) => { return request(endpoint, { Action: 'SendSms', ...staticConfigs, ...parameters }, accessKeySecret); }; diff --git a/packages/connector-aliyun-sms/src/types.ts b/packages/connector-aliyun-sms/src/types.ts index 39fa2f66..0fe73e77 100644 --- a/packages/connector-aliyun-sms/src/types.ts +++ b/packages/connector-aliyun-sms/src/types.ts @@ -52,6 +52,8 @@ const templateGuard = z.object({ }); export const aliyunSmsConfigGuard = z.object({ + // https://help.aliyun.com/document_detail/419270.html + endpoint: z.string().nullable().default('https://dysmsapi.aliyuncs.com/'), accessKeyId: z.string(), accessKeySecret: z.string(), signName: z.string(),