-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from Selleo/jw/emails
feat: emails
- Loading branch information
Showing
34 changed files
with
3,917 additions
and
415 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
# GENERAL | ||
CORS_ORIGIN="https://app.guidebook.localhost" | ||
EMAIL_ADAPTER="mailhog" | ||
|
||
# DATABASE | ||
DATABASE_URL="postgres://postgres:guidebook@localhost:5432/guidebook" | ||
|
||
# JWT | ||
JWT_SECRET= | ||
JWT_REFRESH_SECRET= | ||
CORS_ORIGIN= | ||
JWT_EXPIRATION_TIME= | ||
|
||
# MAILS | ||
SMTP_HOST= | ||
SMTP_PORT= | ||
SMTP_USER= | ||
SMTP_PASSWORD= | ||
|
||
# AWS | ||
AWS_REGION= | ||
AWS_ACCESS_KEY_ID= | ||
AWS_SECRET_ACCESS_KEY= |
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
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
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
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
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
23 changes: 23 additions & 0 deletions
23
examples/common_nestjs_remix/apps/api/src/common/configuration/aws.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { registerAs } from "@nestjs/config"; | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { configValidator } from "src/utils/configValidator"; | ||
|
||
const schema = Type.Object({ | ||
AWS_REGION: Type.String(), | ||
AWS_ACCESS_KEY_ID: Type.String(), | ||
AWS_SECRET_ACCESS_KEY: Type.String(), | ||
}); | ||
|
||
type AWSConfigSchema = Static<typeof schema>; | ||
|
||
const validateAwsConfig = configValidator(schema); | ||
|
||
export default registerAs("aws", (): AWSConfigSchema => { | ||
const values = { | ||
AWS_REGION: process.env.AWS_REGION, | ||
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID, | ||
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY, | ||
}; | ||
|
||
return validateAwsConfig(values); | ||
}); |
6 changes: 4 additions & 2 deletions
6
examples/common_nestjs_remix/apps/api/src/common/configuration/database.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import { registerAs } from "@nestjs/config"; | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { Value } from "@sinclair/typebox/value"; | ||
import { configValidator } from "src/utils/configValidator"; | ||
|
||
const schema = Type.Object({ | ||
url: Type.String(), | ||
}); | ||
|
||
type DatabaseConfig = Static<typeof schema>; | ||
|
||
const validateDatabaseConfig = configValidator(schema); | ||
|
||
export default registerAs("database", (): DatabaseConfig => { | ||
const values = { | ||
url: process.env.DATABASE_URL, | ||
}; | ||
|
||
return Value.Decode(schema, values); | ||
return validateDatabaseConfig(values); | ||
}); |
31 changes: 31 additions & 0 deletions
31
examples/common_nestjs_remix/apps/api/src/common/configuration/email.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { registerAs } from "@nestjs/config"; | ||
import { Static, Type } from "@sinclair/typebox"; | ||
import { configValidator } from "src/utils/configValidator"; | ||
|
||
const schema = Type.Object({ | ||
SMTP_HOST: Type.String(), | ||
SMTP_PORT: Type.Number(), | ||
SMTP_USER: Type.String(), | ||
SMTP_PASSWORD: Type.String(), | ||
EMAIL_ADAPTER: Type.Union([ | ||
Type.Literal("mailhog"), | ||
Type.Literal("smtp"), | ||
Type.Literal("ses"), | ||
]), | ||
}); | ||
|
||
export type EmailConfigSchema = Static<typeof schema>; | ||
|
||
const valdateEmailConfig = configValidator(schema); | ||
|
||
export default registerAs("email", (): EmailConfigSchema => { | ||
const values = { | ||
SMTP_HOST: process.env.SMTP_HOST, | ||
SMTP_PORT: parseInt(process.env.SMTP_PORT || "465", 10), | ||
SMTP_USER: process.env.SMTP_USER, | ||
SMTP_PASSWORD: process.env.SMTP_PASSWORD, | ||
EMAIL_ADAPTER: process.env.EMAIL_ADAPTER, | ||
}; | ||
|
||
return valdateEmailConfig(values); | ||
}); |
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
5 changes: 5 additions & 0 deletions
5
examples/common_nestjs_remix/apps/api/src/common/emails/adapters/email.adapter.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Email } from "../email.interface"; | ||
|
||
export abstract class EmailAdapter { | ||
abstract sendMail(email: Email): Promise<void>; | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/common_nestjs_remix/apps/api/src/common/emails/adapters/local.adapter.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import * as nodemailer from "nodemailer"; | ||
import { Email } from "../email.interface"; | ||
import { EmailAdapter } from "./email.adapter"; | ||
|
||
@Injectable() | ||
export class LocalAdapter extends EmailAdapter { | ||
private transporter: nodemailer.Transporter; | ||
|
||
constructor() { | ||
super(); | ||
this.transporter = nodemailer.createTransport({ | ||
host: "localhost", | ||
port: 1025, | ||
ignoreTLS: true, | ||
}); | ||
} | ||
|
||
async sendMail(email: Email): Promise<void> { | ||
await this.transporter.sendMail(email); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
examples/common_nestjs_remix/apps/api/src/common/emails/adapters/ses.adapter.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { Injectable } from "@nestjs/common"; | ||
import { ConfigService } from "@nestjs/config"; | ||
import { SES, SESClientConfig } from "@aws-sdk/client-ses"; | ||
import { EmailAdapter } from "./email.adapter"; | ||
import { Email } from "../email.interface"; | ||
|
||
@Injectable() | ||
export class AWSSESAdapter extends EmailAdapter { | ||
private ses: SES; | ||
|
||
constructor(private configService: ConfigService) { | ||
super(); | ||
const config: SESClientConfig = this.getAWSConfig(); | ||
this.ses = new SES(config); | ||
} | ||
|
||
private getAWSConfig(): SESClientConfig { | ||
const region = this.configService.get<string>("aws.AWS_REGION"); | ||
const accessKeyId = this.configService.get<string>("aws.AWS_ACCESS_KEY_ID"); | ||
const secretAccessKey = this.configService.get<string>( | ||
"aws.AWS_SECRET_ACCESS_KEY", | ||
); | ||
|
||
if (!region || !accessKeyId || !secretAccessKey) { | ||
throw new Error("Missing AWS configuration"); | ||
} | ||
|
||
return { | ||
region, | ||
credentials: { | ||
accessKeyId, | ||
secretAccessKey, | ||
}, | ||
}; | ||
} | ||
|
||
async sendMail(email: Email): Promise<void> { | ||
const params = { | ||
Source: email.from, | ||
Destination: { | ||
ToAddresses: [email.to], | ||
}, | ||
Message: { | ||
Subject: { | ||
Data: email.subject, | ||
}, | ||
Body: { | ||
Text: { | ||
Data: email.text, | ||
}, | ||
Html: { | ||
Data: email.html, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
try { | ||
await this.ses.sendEmail(params); | ||
} catch (error) { | ||
console.error("Error sending email via AWS SES:", error); | ||
throw error; | ||
} | ||
} | ||
} |
Oops, something went wrong.