0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-24 22:05:56 -05:00
logto/packages/connectors/connector-aliyun-dm/src/index.ts
Gao Sun 570a4ea9e2
feat: create invitation (#5245)
* feat: create invitation

* refactor: update test imports

* refactor: update unit tests

* refactor: update docs

* refactor: update api tests

* chore: add changesets

* refactor: add comments

* refactor: fix swagger check

* refactor: keep compatibility
2024-01-25 19:44:20 +08:00

108 lines
3 KiB
TypeScript

import { assert } from '@silverhand/essentials';
import { HTTPError } from 'got';
import type {
CreateConnector,
EmailConnector,
GetConnectorConfig,
SendMessageFunction,
} from '@logto/connector-kit';
import {
ConnectorError,
ConnectorErrorCodes,
ConnectorType,
validateConfig,
parseJson,
replaceSendMessageHandlebars,
} from '@logto/connector-kit';
import { defaultMetadata } from './constant.js';
import { singleSendMail } from './single-send-mail.js';
import {
aliyunDmConfigGuard,
sendEmailResponseGuard,
sendMailErrorResponseGuard,
} from './types.js';
const sendMessage =
(getConfig: GetConnectorConfig): SendMessageFunction =>
async (data, inputConfig) => {
const { to, type, payload } = data;
const config = inputConfig ?? (await getConfig(defaultMetadata.id));
validateConfig(config, aliyunDmConfigGuard);
const { accessKeyId, accessKeySecret, accountName, fromAlias, templates } = config;
const template = templates.find((template) => template.usageType === type);
assert(
template,
new ConnectorError(
ConnectorErrorCodes.TemplateNotFound,
`Cannot find template for type: ${type}`
)
);
try {
const httpResponse = await singleSendMail(
{
AccessKeyId: accessKeyId,
AccountName: accountName,
ReplyToAddress: 'false',
AddressType: '1',
ToAddress: to,
FromAlias: fromAlias,
Subject: replaceSendMessageHandlebars(template.subject, payload),
HtmlBody: replaceSendMessageHandlebars(template.content, payload),
},
accessKeySecret
);
const result = sendEmailResponseGuard.safeParse(parseJson(httpResponse.body));
if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, result.error);
}
return result.data;
} catch (error: unknown) {
if (error instanceof HTTPError) {
const {
response: { body: rawBody },
} = error;
assert(
typeof rawBody === 'string',
new ConnectorError(
ConnectorErrorCodes.InvalidResponse,
`Invalid response raw body type: ${typeof rawBody}`
)
);
errorHandler(rawBody);
}
throw error;
}
};
const errorHandler = (errorResponseBody: string) => {
const result = sendMailErrorResponseGuard.safeParse(parseJson(errorResponseBody));
if (!result.success) {
throw new ConnectorError(ConnectorErrorCodes.InvalidResponse, result.error);
}
const { Message: errorDescription, ...rest } = result.data;
throw new ConnectorError(ConnectorErrorCodes.General, { errorDescription, ...rest });
};
const createAliyunDmConnector: CreateConnector<EmailConnector> = async ({ getConfig }) => {
return {
metadata: defaultMetadata,
type: ConnectorType.Email,
configGuard: aliyunDmConfigGuard,
sendMessage: sendMessage(getConfig),
};
};
export default createAliyunDmConnector;