@certik/skynet 0.25.3 → 0.25.4
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 +4 -0
- package/dist/email.d.ts +9 -0
- package/dist/email.js +31 -0
- package/package.json +7 -1
- package/src/email.ts +40 -0
package/CHANGELOG.md
CHANGED
package/dist/email.d.ts
ADDED
package/dist/email.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// src/email.ts
|
|
2
|
+
import Mailgun from "mailgun.js";
|
|
3
|
+
import formData from "form-data";
|
|
4
|
+
async function sendEmail({ to, subject, html, from, text }) {
|
|
5
|
+
const apiKey = process.env.MAILGUN_API_KEY;
|
|
6
|
+
if (!apiKey) {
|
|
7
|
+
throw new Error("missing MAILGUN_API_KEY");
|
|
8
|
+
}
|
|
9
|
+
const domain = process.env.MAILGUN_DOMAIN;
|
|
10
|
+
if (!domain) {
|
|
11
|
+
throw new Error("missing MAILGUN_DOMAIN");
|
|
12
|
+
}
|
|
13
|
+
const mailgun = new Mailgun(formData);
|
|
14
|
+
const mg = mailgun.client({ username: "api", key: apiKey });
|
|
15
|
+
const recipients = Array.isArray(to) ? to : [to];
|
|
16
|
+
try {
|
|
17
|
+
await mg.messages.create(domain, {
|
|
18
|
+
from,
|
|
19
|
+
to: recipients,
|
|
20
|
+
subject,
|
|
21
|
+
...text ? { text } : {},
|
|
22
|
+
html
|
|
23
|
+
});
|
|
24
|
+
} catch (error) {
|
|
25
|
+
console.error("failed to send email", error);
|
|
26
|
+
throw error;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
export {
|
|
30
|
+
sendEmail
|
|
31
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@certik/skynet",
|
|
3
|
-
"version": "0.25.
|
|
3
|
+
"version": "0.25.4",
|
|
4
4
|
"description": "Skynet Shared JS library",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -44,6 +44,10 @@
|
|
|
44
44
|
"import": "./dist/dynamodb.js",
|
|
45
45
|
"types": "./dist/dynamodb.d.ts"
|
|
46
46
|
},
|
|
47
|
+
"./email": {
|
|
48
|
+
"import": "./dist/email.js",
|
|
49
|
+
"types": "./dist/email.d.ts"
|
|
50
|
+
},
|
|
47
51
|
"./env": {
|
|
48
52
|
"import": "./dist/env.js",
|
|
49
53
|
"types": "./dist/env.d.ts"
|
|
@@ -116,6 +120,8 @@
|
|
|
116
120
|
"chalk": "^5.6.2",
|
|
117
121
|
"execa": "^9.6.1",
|
|
118
122
|
"express": "^5.2.1",
|
|
123
|
+
"form-data": "^4.0.5",
|
|
124
|
+
"mailgun.js": "^12.7.1",
|
|
119
125
|
"meow": "^14.0.0",
|
|
120
126
|
"p-memoize": "^8.0.0",
|
|
121
127
|
"p-throttle": "^8.1.0",
|
package/src/email.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import Mailgun from "mailgun.js";
|
|
2
|
+
import formData from "form-data";
|
|
3
|
+
|
|
4
|
+
interface SendEmailOptions {
|
|
5
|
+
to: string | string[];
|
|
6
|
+
subject: string;
|
|
7
|
+
html: string;
|
|
8
|
+
from: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function sendEmail({ to, subject, html, from, text }: SendEmailOptions) {
|
|
13
|
+
const apiKey = process.env.MAILGUN_API_KEY;
|
|
14
|
+
if (!apiKey) {
|
|
15
|
+
throw new Error("missing MAILGUN_API_KEY");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const domain = process.env.MAILGUN_DOMAIN;
|
|
19
|
+
if (!domain) {
|
|
20
|
+
throw new Error("missing MAILGUN_DOMAIN");
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const mailgun = new Mailgun(formData);
|
|
24
|
+
const mg = mailgun.client({ username: "api", key: apiKey });
|
|
25
|
+
|
|
26
|
+
const recipients = Array.isArray(to) ? to : [to];
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
await mg.messages.create(domain, {
|
|
30
|
+
from,
|
|
31
|
+
to: recipients,
|
|
32
|
+
subject,
|
|
33
|
+
...(text ? { text } : {}),
|
|
34
|
+
html,
|
|
35
|
+
});
|
|
36
|
+
} catch (error) {
|
|
37
|
+
console.error("failed to send email", error);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|