0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2024-12-30 20:33:54 -05:00

refactor(toolkit): update input params type of SendMessageFunction (#4287)

This commit is contained in:
Darcy Ye 2023-09-05 11:07:38 +08:00 committed by GitHub
parent 8e4e08f731
commit c5b1976117
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 25 deletions

View file

@ -69,7 +69,7 @@ export enum EmailServiceProvider {
SendGrid = 'SendGrid',
}
export const sendgridEmailServiceDataGuard = z.object({
export const sendgridEmailServiceConfigGuard = z.object({
provider: z.literal(EmailServiceProvider.SendGrid),
apiKey: z.string(),
templateId: z.string(),
@ -77,26 +77,26 @@ export const sendgridEmailServiceDataGuard = z.object({
fromEmail: z.string(),
});
export type SendgridEmailServiceData = z.infer<typeof sendgridEmailServiceDataGuard>;
export type SendgridEmailServiceConfig = z.infer<typeof sendgridEmailServiceConfigGuard>;
export const emailServiceDataGuard = z.discriminatedUnion('provider', [
sendgridEmailServiceDataGuard,
export const emailServiceConfigGuard = z.discriminatedUnion('provider', [
sendgridEmailServiceConfigGuard,
]);
export type EmailServiceData = z.infer<typeof emailServiceDataGuard>;
export type EmailServiceConfig = z.infer<typeof emailServiceConfigGuard>;
export enum EmailServiceProviderKey {
EmailServiceProvider = 'emailServiceProvider',
}
export type EmailServiceProviderType = {
[EmailServiceProviderKey.EmailServiceProvider]: EmailServiceData;
[EmailServiceProviderKey.EmailServiceProvider]: EmailServiceConfig;
};
export const emailServiceProviderGuard: Readonly<{
[key in EmailServiceProviderKey]: ZodType<EmailServiceProviderType[key]>;
}> = Object.freeze({
[EmailServiceProviderKey.EmailServiceProvider]: emailServiceDataGuard,
[EmailServiceProviderKey.EmailServiceProvider]: emailServiceConfigGuard,
});
// Demo social connectors

View file

@ -242,29 +242,31 @@ export const emailServiceBrandingGuard = z
export type EmailServiceBranding = z.infer<typeof emailServiceBrandingGuard>;
export type SendMessagePayload = {
to: string;
type: VerificationCodeType;
payload: {
/**
* The dynamic verification code to send.
*
* @example '123456'
*/
code: string;
} & EmailServiceBranding;
/**
* The dynamic verification code to send.
*
* @example '123456'
*/
code: string;
};
export const sendMessagePayloadGuard = z.object({
to: z.string(),
type: verificationCodeTypeGuard,
payload: z
.object({
code: z.string(),
})
.merge(emailServiceBrandingGuard),
code: z.string(),
}) satisfies z.ZodType<SendMessagePayload>;
export type SendMessageFunction = (data: SendMessagePayload, config?: unknown) => Promise<unknown>;
export type SendMessageData = {
to: string;
type: VerificationCodeType;
payload: SendMessagePayload;
};
export const sendMessageDataGuard = z.object({
to: z.string(),
type: verificationCodeTypeGuard,
payload: sendMessagePayloadGuard,
}) satisfies z.ZodType<SendMessageData>;
export type SendMessageFunction = (data: SendMessageData, config?: unknown) => Promise<unknown>;
export type GetUsageFunction = (startFrom?: Date) => Promise<number>;