0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-04-14 23:11:31 -05:00

feat(aliyun-dm): validate config (#184)

* feat(aliyun-dm): validate config

* fix: use zod
This commit is contained in:
Wang Sijie 2022-01-20 10:54:04 +08:00 committed by GitHub
parent 99f85ca44c
commit 764de712d7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 14 deletions

View file

@ -1,10 +1,12 @@
import { ConnectorType } from '@logto/schemas';
import { z } from 'zod';
import {
ConnectorConfigError,
ConnectorError,
ConnectorMetadata,
EmailMessageTypes,
EmailSendMessageFunction,
ValidateConfig,
} from '../types';
import { getConnectorConfig } from '../utilities';
import { singleSendMail } from './single-send-mail';
@ -24,19 +26,32 @@ export const metadata: ConnectorMetadata = {
},
};
interface Template {
type: keyof EmailMessageTypes;
subject: string;
content: string; // With variable {{code}}, support HTML
}
export const validateConfig: ValidateConfig = async (config: unknown) => {
if (!config) {
throw new ConnectorConfigError('Missing config');
}
export interface AliyunDmConfig {
accessKeyId: string;
accessKeySecret: string;
accountName: string;
fromAlias?: string;
templates: Template[];
}
const result = configGuard.safeParse(config);
if (!result.success) {
throw new ConnectorConfigError(result.error.message);
}
};
const configGuard = z.object({
accessKeyId: z.string(),
accessKeySecret: z.string(),
accountName: z.string(),
fromAlias: z.string().optional(),
templates: z.array(
z.object({
type: z.string(),
subject: z.string(),
content: z.string(), // With variable {{code}}, support HTML
})
),
});
export type AliyunDmConfig = z.infer<typeof configGuard>;
export const sendMessage: EmailSendMessageFunction = async (address, type, data) => {
const config: AliyunDmConfig = await getConnectorConfig<AliyunDmConfig>(

View file

@ -1,5 +1,5 @@
import { Languages } from '@logto/phrases';
import { ConnectorType } from '@logto/schemas';
import { ConnectorConfig, ConnectorType } from '@logto/schemas';
export interface ConnectorMetadata {
id: string;
@ -34,3 +34,9 @@ export type EmailSendMessageFunction<T = unknown> = (
) => Promise<T>;
export class ConnectorError extends Error {}
export class ConnectorConfigError extends ConnectorError {}
export type ValidateConfig<T extends ConnectorConfig = ConnectorConfig> = (
config: T
) => Promise<void>;