@wrelik/email 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/package.json +21 -0
- package/src/index.ts +71 -0
- package/tsconfig.json +7 -0
package/CHANGELOG.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@wrelik/email",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "./dist/index.js",
|
|
5
|
+
"types": "./dist/index.d.ts",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"resend": "^3.2.0",
|
|
8
|
+
"@wrelik/errors": "0.1.0"
|
|
9
|
+
},
|
|
10
|
+
"devDependencies": {
|
|
11
|
+
"tsup": "^8.0.1",
|
|
12
|
+
"vitest": "^1.2.2",
|
|
13
|
+
"@wrelik/eslint-config": "0.1.0",
|
|
14
|
+
"@wrelik/tsconfig": "0.1.0"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
18
|
+
"lint": "eslint src/",
|
|
19
|
+
"test": "vitest run --passWithNoTests"
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Resend } from 'resend';
|
|
2
|
+
import { ValidationError } from '@wrelik/errors';
|
|
3
|
+
|
|
4
|
+
let client: Resend;
|
|
5
|
+
let defaultFrom: string;
|
|
6
|
+
|
|
7
|
+
export function initEmail(apiKey: string, defaultFromAddress: string) {
|
|
8
|
+
client = new Resend(apiKey);
|
|
9
|
+
defaultFrom = defaultFromAddress;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function getClient() {
|
|
13
|
+
if (!client) throw new Error('Email not initialized');
|
|
14
|
+
return client;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SendEmailOptions {
|
|
18
|
+
to: string | string[];
|
|
19
|
+
subject: string;
|
|
20
|
+
html?: string;
|
|
21
|
+
text?: string;
|
|
22
|
+
from?: string;
|
|
23
|
+
tags?: Array<{ name: string; value: string }>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export async function sendEmail({ to, subject, html, text, from, tags }: SendEmailOptions) {
|
|
27
|
+
const result = await getClient().emails.send({
|
|
28
|
+
from: from || defaultFrom,
|
|
29
|
+
to,
|
|
30
|
+
subject,
|
|
31
|
+
html,
|
|
32
|
+
text,
|
|
33
|
+
tags,
|
|
34
|
+
react: null,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (result.error) {
|
|
38
|
+
throw new Error(`Failed to send email: ${result.error.message}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return result.data;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Template Registry
|
|
45
|
+
type TemplateRenderer<T> = (data: T) => { subject: string; html: string; text?: string };
|
|
46
|
+
const templates = new Map<string, TemplateRenderer<any>>();
|
|
47
|
+
|
|
48
|
+
export function defineTemplate<T>(id: string, renderer: TemplateRenderer<T>) {
|
|
49
|
+
templates.set(id, renderer);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function sendTemplate<T>(
|
|
53
|
+
id: string,
|
|
54
|
+
to: string | string[],
|
|
55
|
+
data: T,
|
|
56
|
+
options?: Partial<SendEmailOptions>,
|
|
57
|
+
) {
|
|
58
|
+
const renderer = templates.get(id);
|
|
59
|
+
if (!renderer) {
|
|
60
|
+
throw new ValidationError(`Template ${id} not found`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { subject, html, text } = renderer(data);
|
|
64
|
+
return sendEmail({
|
|
65
|
+
to,
|
|
66
|
+
subject,
|
|
67
|
+
html,
|
|
68
|
+
text,
|
|
69
|
+
...options,
|
|
70
|
+
});
|
|
71
|
+
}
|