This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(nuxt): add 'ses' module * Add @aws-sdk/client-ses devdependency * docs: Add SES configuration for test environment * refactor SES and S3 module names * Remove empty object argument in use() method call
- Loading branch information
1 parent
b50b00f
commit 49613b9
Showing
15 changed files
with
494 additions
and
103 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
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,116 @@ | ||
--- | ||
title: Installation | ||
description: 'Pergel Nuxt Module for AWS SES' | ||
--- | ||
|
||
1. Add it to your `modules` section of `nuxt.config`: | ||
|
||
::code-group | ||
```ts [nuxt.config.ts] | ||
export default defineNuxtConfig({ | ||
modules: ['@pergel/nuxt'], | ||
pergel: { | ||
projects: { | ||
myproject: { | ||
ses: true, | ||
}, | ||
// bookList: { | ||
// ses: false | ||
// }, | ||
}, | ||
}, | ||
}) | ||
``` | ||
:: | ||
|
||
::callout{color="amber" icon="i-ph-warning-duotone"} | ||
Node >= 20.8.0 is required. | ||
Nuxt >= 3.9.0 is required. | ||
:: | ||
|
||
|
||
2. Auto install dependencies: | ||
|
||
::code-group | ||
```sh [pnpm] | ||
pergel install | ||
``` | ||
:: | ||
|
||
:read-more{title="Install Pergel CLI" to="/pergel/cli"} | ||
|
||
3. Add your server code | ||
|
||
::code-group | ||
```ts [server/api/sendemail.ts] | ||
export default defineEventHandler(async (event) => { | ||
try { | ||
const { sendEmail, templates, ... } = await pergelMyproject().ses().use(event) | ||
|
||
const _emailParams = { | ||
Destination: { | ||
ToAddresses: ['[email protected]'], | ||
}, | ||
Message: { | ||
Body: { | ||
Text: { | ||
Data: 'Test email', | ||
}, | ||
}, | ||
Subject: { | ||
Data: 'Test email', | ||
}, | ||
}, | ||
Source: '[email protected]', | ||
} | ||
|
||
const send = await sendEmail(sendEmail) | ||
|
||
// const result = await sendEmail(templates().changeEmail({ | ||
// to: '[email protected]', | ||
// code: '123456', | ||
// webUrl: 'https://productdevbook.com', | ||
// source: '[email protected]', | ||
// })) | ||
|
||
return { | ||
data: { | ||
mailResult: send, | ||
}, | ||
} | ||
} | ||
catch (error: any) { | ||
return error.message | ||
} | ||
}) | ||
|
||
``` | ||
|
||
```ts [composables] | ||
pergelMyproject().ses().use() | ||
``` | ||
|
||
:: | ||
|
||
4. Add .env file | ||
|
||
Root directory of your project `pergel` folder inside `README.yaml` file. Check for `projectName` and `ses` section. | ||
|
||
::code-group | ||
```sh [.env] | ||
# You see this in your README.yaml file | ||
env: | ||
NUXT_TEST_SES_REGION: | ||
NUXT_TEST_SES_ACCESS_KEY_ID: | ||
NUXT_TEST_SES_SECRET_ACCESS_KEY: | ||
|
||
# Copy and change `:` to `=` and add your credentials | ||
NUXT_TEST_SES_REGION=us-east-1 | ||
NUXT_TEST_SES_ACCESS_KEY_ID=123456 | ||
NUXT_TEST_SES_SECRET_ACCESS_KEY=123456 | ||
``` | ||
:: | ||
|
||
::callout{icon="i-ph-check-circle-duotone" color="green"} | ||
Well done! You have successfully installed Pergel SES module. | ||
:: |
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,2 @@ | ||
title: SES | ||
icon: i-simple-icons-amazonsimpleemailservice |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
export default defineEventHandler(async (event) => { | ||
try { | ||
const { sendEmail, templates } = await pergelTest().ses().use(event) | ||
|
||
const _emailParams = { | ||
Destination: { | ||
ToAddresses: ['[email protected]'], | ||
}, | ||
Message: { | ||
Body: { | ||
Text: { | ||
Data: 'Test email', | ||
}, | ||
}, | ||
Subject: { | ||
Data: 'Test email', | ||
}, | ||
}, | ||
Source: '[email protected]', | ||
} | ||
|
||
// const sendtest = await sendEmail(_emailParams) | ||
|
||
const result = await sendEmail(templates().changeEmail({ | ||
to: '[email protected]', | ||
code: '123456', | ||
webUrl: 'https://productdevbook.com', | ||
source: '[email protected]', | ||
})) | ||
|
||
return { | ||
data: { | ||
mailResult: result, | ||
}, | ||
} | ||
} | ||
catch (error: any) { | ||
return error.message | ||
} | ||
}) |
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
53 changes: 53 additions & 0 deletions
53
packages/nuxt/src/runtime/modules/ses/composables/usePergelSES.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,53 @@ | ||
import { CreateTemplateCommand, type CreateTemplateCommandInput, SESClient, SendEmailCommand, type SendEmailCommandInput } from '@aws-sdk/client-ses' | ||
import type { H3Event } from 'h3' | ||
import type { SesModuleRuntimeConfig } from '../types' | ||
import { templates } from '../templates' | ||
import { clientFunctionTemplate } from '../../../../runtime/core/useClient' | ||
import type { PergelGlobalContextOmitModule } from '#pergel' | ||
|
||
export interface Credentials { | ||
accessKeyId: string | ||
secretAccessKey: string | ||
} | ||
|
||
const { clientInit } = clientFunctionTemplate<SESClient, SesModuleRuntimeConfig>('ses') | ||
|
||
export async function usePergelSES( | ||
this: PergelGlobalContextOmitModule, | ||
event?: H3Event, | ||
data?: PergelGlobalContextOmitModule, | ||
) { | ||
const _pergel = data ?? this | ||
|
||
if (!_pergel || !_pergel.projectName) | ||
throw new Error('Pergel is not defined') | ||
|
||
const { client } = await clientInit(_pergel, (runtime) => { | ||
return new SESClient({ | ||
region: runtime.region, | ||
credentials: { | ||
accessKeyId: runtime.accessKeyId, | ||
secretAccessKey: runtime.secretAccessKey, | ||
}, | ||
}) | ||
}, event) | ||
|
||
async function sendEmail(params: SendEmailCommandInput) { | ||
const command = new SendEmailCommand(params) | ||
const data = await client.send(command) | ||
return data | ||
} | ||
|
||
async function createTemplate(params: CreateTemplateCommandInput) { | ||
const command = new CreateTemplateCommand(params) | ||
const data = await client.send(command) | ||
return data | ||
} | ||
|
||
return { | ||
sendEmail, | ||
createTemplate, | ||
templates, | ||
client, | ||
} | ||
} |
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,42 @@ | ||
import { addServerImportsDir, createResolver } from '@nuxt/kit' | ||
import { definePergelModule } from '../../core/definePergel' | ||
import { generateModuleRuntimeConfig } from '../../core/utils/moduleRuntimeConfig' | ||
import type { SesModuleRuntimeConfig } from './types' | ||
|
||
export default definePergelModule({ | ||
meta: { | ||
name: 'ses', | ||
version: '0.0.1', | ||
dependencies: { | ||
'@aws-sdk/client-ses': '^3.470.0', | ||
}, | ||
}, | ||
defaults: {}, | ||
async setup(options, nuxt) { | ||
const resolver = createResolver(import.meta.url) | ||
const projectName = options.resolvedModule.projectName | ||
|
||
generateModuleRuntimeConfig<SesModuleRuntimeConfig>(nuxt, options, { | ||
region: '', | ||
accessKeyId: '', | ||
secretAccessKey: '', | ||
}) | ||
|
||
addServerImportsDir(resolver.resolve('./composables')) | ||
|
||
options._contents.push({ | ||
moduleName: 'ses', | ||
projectName, | ||
content: /* ts */` | ||
function ses() { | ||
return { | ||
use: usePergelSES.bind(ctx), | ||
} | ||
} | ||
`, | ||
resolve: /* ts */` | ||
ses: ses, | ||
`, | ||
}) | ||
}, | ||
}) |
Oops, something went wrong.