-
Notifications
You must be signed in to change notification settings - Fork 0
feat: SendGrid email helper #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| interface EmailResult { | ||
| accepted: number; | ||
| } | ||
|
|
||
| export async function sendEmail( | ||
| to: string, | ||
| subject: string, | ||
| body: string, | ||
| ): Promise<EmailResult> { | ||
| const sendgridKey = process.env.SENDGRID_API_KEY; | ||
| const fromAddress = process.env.EMAIL_FROM_ADDRESS; | ||
| if (!sendgridKey || !fromAddress) { | ||
| throw new Error("SENDGRID_API_KEY and EMAIL_FROM_ADDRESS must be set"); | ||
| } | ||
|
|
||
| const response = await fetch("https://api.sendgrid.com/v3/mail/send", { | ||
| method: "POST", | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] Raw string URL and email type are hardcoded as magic strings: "https://api.sendgrid.com/v3/mail/send", "text/plain". Suggestion: Move URLs and fixed values to named constants, or, if configurable, into environment variables. |
||
| headers: { | ||
| Authorization: `Bearer ${sendgridKey}`, | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ | ||
| personalizations: [{ to: [{ email: to }] }], | ||
| from: { email: fromAddress }, | ||
| subject, | ||
| content: [{ type: "text/plain", value: body }], | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] No error handling is present for the fetch API call. Failures will throw uncaught exceptions. Suggestion: Wrap the fetch call in try/catch and handle or propagate errors appropriately. |
||
| }), | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error(`SendGrid send failed: ${response.status} ${response.statusText}`); | ||
| } | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] The function promises an explicit Suggestion: Explicitly shape and check the response JSON to match |
||
| return response.json(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[major] No runtime validation of the input parameters is performed before passing them to the SendGrid API (e.g.,
to,subject, orbody).Suggestion: Add validation for parameters (
to,subject,body) at the start of the function, rejecting improperly formed or empty values before proceeding.