-
-
Save jukialen/5197840d37023523007b9eabca7f0af4 to your computer and use it in GitHub Desktop.
template email sendgrid node
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SENDING DYNAMIC TEMPLATE MAIL | |
import sgMail from '@sendgrid/mail'; | |
import { BadRequestException, HttpStatus } from '@nestjs/common'; | |
sgMail.setApiKey(process.env.SENDGRID_API_KEY); | |
export const sendMail = async (data: { | |
templateVersion: string; | |
verificationUrl: string; | |
email: string; | |
}): Promise<{ statusCode: number; message: string } | BadRequestException> => { | |
try { | |
const message = { | |
// personalizations: [ | |
// { | |
from: process.env.CONFIRM_EMAIL_ADDRESS, | |
to: data.email, | |
template_id: data.templateVersion, | |
dynamic_template_data: { | |
verificationUrl: data.verificationUrl, | |
}, | |
// }, | |
// ], | |
// from: { | |
// email: process.env.CONFIRM_EMAIL_ADDRESS, | |
// name: 'Administration Pfartists', | |
// }, | |
content: [ | |
{ | |
type: 'text/plain', | |
value: 'huhuhu', | |
}, | |
], | |
}; | |
await sgMail.send(message); | |
return { | |
statusCode: HttpStatus.OK, | |
message: 'E-mail sent successfully.', | |
}; | |
} catch (e) { | |
throw new BadRequestException(e.message); | |
} | |
}; | |
//TEMPLATES IDS | |
export const templates = { | |
confirmEmail: process.env.CONFIRM_TEMPLATE_ID, | |
}; | |
//DOCUMENTATION | |
https://supertokens.com/docs/thirdpartyemailpassword/custom-ui/enable-email-verification#changing-the-email-verification-link-domain--path | |
//SuperTokens SENDING | |
ThirdPartyEmailPassword.init({ | |
providers: [ | |
ThirdPartyEmailPassword.Google({ | |
clientId: process.env.GOOGLE_ID, | |
clientSecret: process.env.GOOGLE_SECRET_ID, | |
}), | |
], | |
emailDelivery: { | |
override: () => { | |
return { | |
sendEmail: async function (input): Promise< | |
| { statusCode: HttpStatus; message: string } | |
| BadRequestException | |
> { | |
return send({ | |
templateVersion: templates.forgottenPassword, | |
verificationUrl: input.passwordResetLink, | |
email: input.user.email, | |
}); | |
}, | |
}; | |
}, | |
}, | |
}), | |
EmailVerification.init({ | |
mode: 'OPTIONAL', | |
emailDelivery: { | |
override: () => { | |
return { | |
sendEmail: async function (input): Promise< | |
| { statusCode: HttpStatus; message: string } | |
| BadRequestException | |
> { | |
return send({ | |
templateVersion: templates.confirmEmail, | |
verificationUrl: input.emailVerifyLink, | |
email: input.user.email, | |
}); | |
}, | |
}; | |
}, | |
}, | |
}), | |
//ERRORS | |
//1 | |
<html>TS2345: | |
Argument of type ' | |
{ from: string; to: string; template_id: string; dynamic_template_data: { verificationUrl: string; }; | |
content: { type: string; value: string; }[]; } | |
' | |
is not assignable to parameter of type 'MailDataRequired | MailDataRequired[]'. | |
Type ' | |
{ from: string; to: string; template_id: string; dynamic_template_data: { verificationUrl: string; }; | |
content: { type: string; value: string; }[]; } | |
' | |
is not assignable to type 'MailData & { content: MailContent[] & { 0: MailContent; }; } | |
'. | |
Type ' | |
{ from: string; to: string; template_id: string; dynamic_template_data: { verificationUrl: string; }; | |
content: { type: string; value: string; }[]; } | |
' | |
is not assignable to type '{ content: MailContent[] & { 0: MailContent; }; }'. | |
Types of property 'content' are incompatible. | |
Type '{ type: string; value: string; }[]' is not assignable to type 'MailContent[] & { 0: MailContent; }'. | |
Property '0' is missing in type '{ type: string; value: string; }[]' but required in type '{ 0: MailContent; }'. | |
//2 | |
TS2322: Type ' | |
() => { sendEmail: (input: TypeEmailVerificationEmailDeliveryInput & { userContext: any; }) => | |
Promise<{ statusCode: HttpStatus; message: string;} | BadRequestException>; } | |
' | |
is not assignable to type ' | |
(originalImplementation: EmailDeliveryInterface<TypeEmailVerificationEmailDeliveryInput>, | |
builder: OverrideableBuilder<EmailDeliveryInterface<TypeEmailVerificationEmailDeliveryInput>>) => | |
EmailDeliveryInterface<...> | |
'. | |
Call signature return types ' | |
{ sendEmail: (input: TypeEmailVerificationEmailDeliveryInput & { userContext: any; }) => | |
Promise<BadRequestException | { ...; }>; } | |
' | |
and 'EmailDeliveryInterface<TypeEmailVerificationEmailDeliveryInput>' are incompatible. | |
The types returned by ' | |
sendEmail(...)' are incompatible between these types. | |
Type 'Promise<BadRequestException | { statusCode: HttpStatus; message: string; }>' | |
is not assignable to type 'Promise<void>'. | |
Type 'BadRequestException | { statusCode: HttpStatus; message: string; }' is not assignable to type 'void'. | |
Type 'BadRequestException' is not assignable to type 'void'. | |
//3 | |
TS2322: Type ' | |
() => { sendEmail: (input: TypeEmailPasswordPasswordResetEmailDeliveryInput & { userContext: any; }) => | |
Promise<{ statusCode: HttpStatus; message: string;} | BadRequestException>; } | |
' | |
is not assignable to type ' | |
(originalImplementation: EmailDeliveryInterface<TypeEmailPasswordPasswordResetEmailDeliveryInput>, builder: OverrideableBuilder<...>) => EmailDeliveryInterface<...>'. | |
Call signature return types ' | |
{ sendEmail: (input: TypeEmailPasswordPasswordResetEmailDeliveryInput & { userContext: any; }) => Promise<BadRequestException | { ...; }>; } | |
' | |
and ' | |
EmailDeliveryInterface<TypeEmailPasswordPasswordResetEmailDeliveryInput>' are incompatible. | |
The types returned by 'sendEmail(...)' are incompatible between these types. | |
Type 'Promise<BadRequestException | { statusCode: HttpStatus; message: string; }>' is not assignable to type 'Promise<void>'. | |
Type 'BadRequestException | { statusCode: HttpStatus; message: string; }' is not assignable to type 'void'. | |
Type 'BadRequestException' is not assignable to type 'void'. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment