resend-cli 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 stretch
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # resend-cli
2
+ Simple, pretty command-line based tool for sending emails, managing audiences, etc via [Resend](https://resend.com).
3
+
4
+ ## install
5
+ Install resend-cli by running `npm i -g resend-cli`.
6
+ Do not install resend-cli locally, without the `-g` flag, since it does not make sense for it to be a project dependency.
7
+
8
+ ## usage
9
+ resend-cli does not require any arguments. If you are running resend-cli for the first time, the command-line will prompt you for an API key.
10
+ API keys must have full access for all features of the CLI to work properly. You may create an API key from the [dashboard](https://resend.com/api-keys).
11
+
12
+ After this, the API key will be saved in plain text (!) at ~/.resend_config.json. For this reason, avoid installing resend-cli on public or shared computers.
13
+
14
+ To exit resend-cli, use the Ctrl+C keyboard shortcut.
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "resend-cli",
3
+ "version": "1.0.0",
4
+ "description": "GUI for sending emails via resend",
5
+ "main": "src/index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "author": "stretch07",
10
+ "license": "MIT",
11
+ "dependencies": {
12
+ "enquirer": "^2.4.1",
13
+ "figlet": "^1.7.0",
14
+ "ora": "^8.0.1",
15
+ "resend": "^3.2.0"
16
+ },
17
+ "type": "module"
18
+ }
package/src/index.js ADDED
@@ -0,0 +1,53 @@
1
+ import enquirer from "enquirer"
2
+ import * as fs from "fs/promises"
3
+ import {homedir} from "os"
4
+ import figlet from "figlet"
5
+ import {promisify} from "util"
6
+ const fig = promisify(figlet)
7
+ import ora from "ora"
8
+ import { Resend } from "resend"
9
+ import routes from "./routes.js"
10
+ import readline from "readline"
11
+
12
+ const text = await fig("resend-cli")
13
+ console.log(text)
14
+ const configPath = `${homedir()}/.resend_config.json`
15
+ const fileExists = await fs.access(configPath).then(() => true).catch(() => false);
16
+ if (fileExists) {
17
+ let config, instance
18
+ try {
19
+ let fileContents = await fs.readFile(configPath)
20
+ fileContents = fileContents + ''
21
+ config = JSON.parse(fileContents)
22
+ //console.log(config.apiKey)
23
+ instance = new Resend(config.apiKey)
24
+ } catch (e) {
25
+ console.error("Error reading config file")
26
+ console.error(e)
27
+ process.exit(1)
28
+ }
29
+
30
+ try {
31
+ await routes({resend: instance, apiKey: config.apiKey, config})
32
+ } catch {} // this is needed so that Ctrl+C doesn't throw an exception
33
+ } else {
34
+ const apiKeyResponse = await enquirer.password({
35
+ message: "Enter an API key with full access: "
36
+ })
37
+ const spinner = ora({text: "Authenticating...", spinner: "toggle9"}).start()
38
+ const resend = new Resend(apiKeyResponse)
39
+ const apiKeys = await resend.apiKeys.list()
40
+ spinner.stop()
41
+ if (apiKeys?.error?.statusCode === 400) {
42
+ console.error("Invalid API key")
43
+ process.exit(1)
44
+ } else {
45
+ const config = {
46
+ apiKey: apiKeyResponse
47
+ }
48
+ await fs.writeFile(configPath, JSON.stringify(config))
49
+ console.log("API key saved in plain text to ~/.resend_config.json.")
50
+ console.log("Run `resend-cli` again to use the CLI.")
51
+ process.exit(0)
52
+ }
53
+ }
package/src/routes.js ADDED
@@ -0,0 +1,37 @@
1
+ import enquirer from "enquirer";
2
+ import email from "./sections/email.js";
3
+ import domain from "./sections/domain.js";
4
+ import apikeys from "./sections/apikeys.js";
5
+ import audiences from "./sections/audiences.js";
6
+ import contacts from "./sections/contacts.js";
7
+
8
+ export default async ({resend, apiKey, config}) => {
9
+ const prompt = new enquirer.Select({
10
+ message: "Choose a section",
11
+ choices: [
12
+ {name: "email", message: "Emails"},
13
+ {name: "domain", message: "Domains"},
14
+ {name: "apiKeys", message: "API Keys"},
15
+ {name: "audiences", message: "Audiences"},
16
+ {name: "contacts", message: "Contacts"},
17
+ ]
18
+ })
19
+ const answer = await prompt.run()
20
+ switch (answer) {
21
+ case "email":
22
+ await email({resend, apiKey, config})
23
+ break
24
+ case "domain":
25
+ await domain({resend, apiKey, config})
26
+ break
27
+ case "apiKeys":
28
+ await apikeys({resend, apiKey, config})
29
+ break
30
+ case "audiences":
31
+ await audiences({resend, apiKey, config})
32
+ break
33
+ case "contacts":
34
+ await contacts({resend, apiKey, config})
35
+ break
36
+ }
37
+ }
@@ -0,0 +1,99 @@
1
+ import ora from "ora"
2
+ import enquirer from "enquirer";
3
+
4
+ const logAPIKey = (apiKey) => {
5
+ console.log("- ID: " + apiKey.id)
6
+ if (apiKey.token) {
7
+ console.log("- API Key: " + apiKey.token)
8
+ }
9
+ if (apiKey.name) {
10
+ console.log("- Name: " + apiKey.name)
11
+ }
12
+ if (apiKey.created_at) {
13
+ console.log("- Created At: " + apiKey.created_at)
14
+ }
15
+ }
16
+
17
+ export default async function({resend}, apiKey) {
18
+ const prompt = new enquirer.Select({
19
+ message: "Choose an action",
20
+ choices: [
21
+ {name: "create", message: "Create API Key"},
22
+ {name: "list", message: "List API Keys"},
23
+ {name: "delete", message: "Delete API Key"},
24
+ ]
25
+ })
26
+ const answer = await prompt.run()
27
+ switch (answer) {
28
+ case "create":
29
+ {
30
+ let form = new enquirer.Form({
31
+ name: "createApiKey",
32
+ message: "Create an API Key.\nPossible values for permission: full, sending.",
33
+ choices: [
34
+ {name: "name", message: "Name", initial: "My API Key"},
35
+ {name: "permission", message: "Permission", initial: "full"}
36
+ ]
37
+ })
38
+ form = await form.run()
39
+ const name = form.name
40
+ const permission = form.permission === "sending" ? "sending_access" : "full_access"
41
+ const spinner = ora({text: "Creating API Key...", spinner: "toggle9"}).start()
42
+ const apiKey = await resend.apiKeys.create({name, permission})
43
+ spinner.stop()
44
+ console.log("IMPORTANT! Save this API key in a safe place. It will not be shown again.")
45
+ logAPIKey(apiKey.data)
46
+ }
47
+ break;
48
+ case "list":
49
+ {
50
+ const spinner2 = ora({text: "Fetching API Keys...", spinner: "toggle9"}).start()
51
+ const listApiKeysResponse = await resend.apiKeys.list()
52
+ spinner2.stop()
53
+ const choices = listApiKeysResponse.data.data.map((apiKey) => {
54
+ return {message: apiKey.name, name: apiKey.id}
55
+ })
56
+ //console.log(choices)
57
+ let answer = new enquirer.Select({
58
+ name: "listApiKeys",
59
+ message: "Choose an API Key to view info on",
60
+ choices
61
+ })
62
+ answer = await answer.run()
63
+ for (let apiKey of listApiKeysResponse.data.data) {
64
+ if (apiKey.id === answer) {
65
+ logAPIKey(apiKey)
66
+ }
67
+ }
68
+ }
69
+ break
70
+ case "delete":
71
+ {
72
+ const spinner3 = ora({text: "Fetching API Keys...", spinner: "toggle9"}).start()
73
+ const listApiKeysResponse2 = await resend.apiKeys.list()
74
+ spinner3.stop()
75
+ const choices2 = listApiKeysResponse2.data.data.map((apiKey) => {return {message: apiKey.name, name: apiKey.id}})
76
+ let answer2 = new enquirer.Select({
77
+ name: "deleteApiKey",
78
+ message: "Choose an API Key to delete",
79
+ choices: choices2
80
+ })
81
+ answer2 = await answer2.run()
82
+ console.log(answer2)
83
+ let confirmDelete = new enquirer.Confirm({
84
+ message: "If this API Key is the one you make resend-cli with, you will not be able to use resend-cli anymore with the same key. This action is IRREVERSIBLE!!"
85
+ })
86
+ confirmDelete = await confirmDelete.run()
87
+ if (!confirmDelete) {
88
+ console.log("Aborting...")
89
+ process.exit(0)
90
+ }
91
+ const spinner4 = ora({text: "Deleting API Key...", spinner: "toggle9"}).start()
92
+ await resend.apiKeys.remove(answer2)
93
+ spinner4.stop()
94
+ console.log("API Key deleted.")
95
+ }
96
+ break
97
+ }
98
+ process.exit(0)
99
+ }
@@ -0,0 +1,84 @@
1
+ import ora from "ora"
2
+ import enquirer from "enquirer";
3
+
4
+ const logAudience = (audience) => {
5
+ console.log(`- ID: ${audience.id}`)
6
+ console.log(`- Name: ${audience.name}`)
7
+ if (audience.created_at) {
8
+ console.log(`- Created At: ${audience.created_at}`)
9
+ }
10
+ }
11
+
12
+ export default async function({resend, apiKey}) {
13
+ let mainForm = new enquirer.Select({
14
+ message: "Choose an action",
15
+ choices: [
16
+ {name: "add", message: "Add Audience"},
17
+ {name: "retrieve", message: "List/Retrieve Audience"},
18
+ {name: "delete", message: "Delete Audience"},
19
+ ]
20
+ })
21
+ mainForm = await mainForm.run()
22
+
23
+ switch (mainForm) {
24
+ case "add":
25
+ {
26
+ let form = new enquirer.Form({
27
+ name: "createAudience",
28
+ message: "Create an audience",
29
+ choices: [
30
+ {name: "name", message: "Name", initial: "My Audience"},
31
+ ]
32
+ })
33
+ form = await form.run()
34
+ const spinner = ora({text: "Creating audience...", spinner: "toggle9"}).start()
35
+ const audience = await resend.audiences.create({name: form.name})
36
+ spinner.stop()
37
+ console.log(`Audience created.`)
38
+ logAudience(audience.data)
39
+ }
40
+ break
41
+ case "retrieve":
42
+ {
43
+ const audienceId = await selectAudience({resend}, "Choose an audience")
44
+ const spinner = ora({text: "Retrieving audience...", spinner: "toggle9"}).start()
45
+ const audience = await resend.audiences.get(audienceId)
46
+ spinner.stop()
47
+ console.log(`Audience '${audience.data.name}' retrieved.`)
48
+ logAudience(audience.data)
49
+ }
50
+ break
51
+ case "delete":
52
+ {
53
+ const audienceId = await selectAudience({resend}, "Choose an audience")
54
+ let confirmation = new enquirer.Confirm({
55
+ message: "Are you sure you want to delete this audience?",
56
+ initial: false
57
+ })
58
+ confirmation = await confirmation.run()
59
+ if (!confirmation) {
60
+ console.log("Cancelled.")
61
+ process.exit(0)
62
+ }
63
+ const spinner = ora({text: "Deleting audience...", spinner: "toggle9"}).start()
64
+ await resend.audiences.remove(audienceId)
65
+ spinner.stop()
66
+ console.log("Audience deleted.")
67
+ }
68
+ break
69
+ }
70
+ }
71
+
72
+ export async function selectAudience({resend}, message) {
73
+ const spinner = ora({text: "Fetching audiences...", spinner: "toggle9"}).start()
74
+ const audiences = await resend.audiences.list()
75
+ spinner.stop()
76
+ const choices = audiences.data.data.map((audience) => {return {message: audience.name, name: audience.id}})
77
+ let answer = new enquirer.Select({
78
+ name: "listAudiences",
79
+ message: message ?? "Choose an audience",
80
+ choices
81
+ })
82
+ answer = await answer.run()
83
+ return answer
84
+ }
@@ -0,0 +1,177 @@
1
+ import ora from "ora"
2
+ import enquirer from "enquirer";
3
+ import {selectAudience} from "./audiences.js";
4
+
5
+ const logContact = (contact) => {
6
+ console.log("- ID: " + contact.id)
7
+ console.log("- Email: " + contact.email)
8
+ console.log("- First Name: " + (contact.first_name ?? "N/A"))
9
+ console.log("- Last Name: " + (contact.last_name ?? "N/A"))
10
+ console.log("- Created At: " + contact.created_at)
11
+ console.log("- Unsubscribed: " + contact.unsubscribed)
12
+
13
+ }
14
+
15
+ export default async function({resend, apiKey}) {
16
+ const mainForm = new enquirer.Select({
17
+ message: "Choose an action",
18
+ choices: [
19
+ {name: "add", message: "Add Contact"},
20
+ {name: "retrieve", message: "Retrieve Contact"},
21
+ {name: "update", message: "Update Contact"},
22
+ {name: "delete", message: "Delete Contact"},
23
+ {name: "list", message: "List Contacts"},
24
+ ]
25
+ })
26
+ const mainFormResponse = await mainForm.run()
27
+ switch (mainFormResponse) {
28
+ case "add":
29
+ {
30
+ const audienceID = await selectAudience({resend}, "Choose an audience to add a contact to")
31
+ let form = new enquirer.Form({
32
+ name: "createContact",
33
+ message: "Add a contact",
34
+ choices: [
35
+ {name: "email", message: "Email"},
36
+ {name: "firstName", message: "First Name", initial: ""},
37
+ {name: "lastName", message: "Last Name", initial: ""},
38
+ ]
39
+ })
40
+ form = await form.run()
41
+ const spin = ora({text: "Adding contact...", spinner: "toggle9"}).start()
42
+ const contact = await resend.contacts.create({
43
+ audienceId: audienceID,
44
+ email: form.email,
45
+ firstName: form.firstName,
46
+ lastName: form.lastName
47
+ })
48
+ spin.stop()
49
+ //console.log(contact)
50
+ console.log(`Contact with ID ${contact.data.id} added.`)
51
+ }
52
+ break
53
+ case "retrieve":
54
+ {
55
+ const audienceId = await selectAudience({resend}, "Choose an audience")
56
+ let form = new enquirer.Form({
57
+ name: "retrieveContact",
58
+ message: "Retrieve a contact",
59
+ choices: [
60
+ {name: "emailOrID", message: "Email or Contact ID"},
61
+ ]
62
+ })
63
+ form = await form.run()
64
+ if (form.emailOrID.includes("@")) {
65
+ // we need to list contacts and find the contact with the email
66
+ const spin = ora({text: "Retrieving contact...", spinner: "toggle9"}).start()
67
+ const contacts = await resend.contacts.list({audienceId})
68
+ for (let contact of contacts.data.data) {
69
+ if (contact.email === form.emailOrID) {
70
+ spin.stop()
71
+ logContact(contact)
72
+ }
73
+ }
74
+ } else {
75
+ const spin = ora({text: "Retrieving contact...", spinner: "toggle9"}).start()
76
+ const contact = await resend.contacts.get({id: form.emailOrID, audienceId})
77
+ spin.stop()
78
+ console.log("- Email: " + contact.data.email)
79
+ console.log("- First Name: " + (contact.data.first_name ?? "N/A"))
80
+ console.log("- Last Name: " + (contact.data.last_name ?? "N/A"))
81
+ console.log("- Created At: " + contact.data.created_at)
82
+ console.log("- Unsubscribed: " + contact.data.unsubscribed)
83
+ }
84
+ }
85
+ break
86
+ case "update":
87
+ {
88
+ const audienceId = await selectAudience({resend}, "Choose an audience")
89
+ let form = new enquirer.Form({
90
+ name: "updateContact",
91
+ message: "Update a contact",
92
+ choices: [
93
+ {name: "emailOrID", message: "Email or Contact ID"},
94
+ {name: "firstName", message: "First Name", initial: ""},
95
+ {name: "lastName", message: "Last Name", initial: ""},
96
+ {name: "unsubscribed", message: "Unsubscribed (either true or false)"}
97
+ ]
98
+ })
99
+ form = await form.run()
100
+ if (form.emailOrID.includes("@")) {
101
+ // we need to list contacts and find the contact with the email
102
+ const spin = ora({text: "Updating contact...", spinner: "toggle9"}).start()
103
+ const contacts = await resend.contacts.list({audienceId})
104
+ for (let contact of contacts.data.data) {
105
+ if (contact.email === form.emailOrID) {
106
+ const contactUpdate = await resend.contacts.update({
107
+ id: contact.id,
108
+ audienceId,
109
+ firstName: form.firstName,
110
+ lastName: form.lastName,
111
+ unsubscribed: form.unsubscribed
112
+ })
113
+ spin.stop()
114
+ console.log(`Contact with ID ${contactUpdate.data.id} updated.`)
115
+ }
116
+ }
117
+ } else {
118
+ const spin = ora({text: "Updating contact...", spinner: "toggle9"}).start()
119
+ const contactUpdate = await resend.contacts.update({
120
+ id: form.emailOrID,
121
+ audienceId,
122
+ firstName: form.firstName,
123
+ lastName: form.lastName,
124
+ unsubscribed: form.unsubscribed
125
+ })
126
+ spin.stop()
127
+ console.log(`Contact with ID ${contactUpdate.data.id} updated.`)
128
+ }
129
+ }
130
+ break
131
+ case "delete":
132
+ {
133
+ const audienceId = await selectAudience({resend}, "Choose an audience")
134
+ let form = new enquirer.Form({
135
+ name: "deleteContact",
136
+ message: "Delete a contact",
137
+ choices: [
138
+ {name: "emailOrID", message: "Email or Contact ID"},
139
+ ]
140
+ })
141
+ form = await form.run()
142
+ if (form.emailOrID.includes("@")) {
143
+ const spin = ora({text: "Deleting contact...", spinner: "toggle9"}).start()
144
+ const contactDelete = await resend.contacts.remove({email: form.emailOrID, audienceId})
145
+ spin.stop()
146
+ console.log(`Contact with ID ${form.emailOrID} deleted.`)
147
+ } else {
148
+ const spin = ora({text: "Deleting contact...", spinner: "toggle9"}).start()
149
+ const contactDelete = await resend.contacts.remove({id: form.emailOrID, audienceId})
150
+ spin.stop()
151
+ console.log(`Contact with ID ${form.emailOrID} deleted.`)
152
+ }
153
+ }
154
+ break
155
+ case "list":
156
+ {
157
+ const audienceId = await selectAudience({resend}, "Choose an audience")
158
+ const spin = ora({text: "Fetching contacts...", spinner: "toggle9"}).start()
159
+ const contacts = await resend.contacts.list({audienceId})
160
+ spin.stop()
161
+ //show a select with contact names
162
+ const choices = contacts.data.data.map((contact) => {return {message: contact.email, name: contact.id}})
163
+ let answer = new enquirer.Select({
164
+ name: "listContacts",
165
+ message: "Choose a contact to view info on",
166
+ choices
167
+ })
168
+ answer = await answer.run()
169
+ for (let contact of contacts.data.data) {
170
+ if (contact.id === answer) {
171
+ logContact(contact)
172
+ }
173
+ }
174
+ }
175
+ }
176
+ process.exit(0)
177
+ }
@@ -0,0 +1,195 @@
1
+ import ora from "ora"
2
+ import enquirer from "enquirer";
3
+
4
+ const logDomain = (domain) => {
5
+ console.log(`ID: ${domain.data.id}`)
6
+ console.log(`Name: ${domain.data.name}`)
7
+ console.log(`Region: ${domain.data.region}`)
8
+ console.log(`Status: ${domain.data.status}`)
9
+ console.log(`Created At: ${domain.data.created_at}`)
10
+ }
11
+
12
+ export default async function({resend}) {
13
+ let mainForm = new enquirer.Select({
14
+ message: "Choose an action",
15
+ choices: [
16
+ {name: "add", message: "Add Domain"},
17
+ {name: "retrieve", message: "Retrieve Domain"},
18
+ {name: "verify", message: "Verify Domain"},
19
+ {name: "update", message: "Update Domain"},
20
+ {name: "list", message: "List Domains"},
21
+ {name: "delete", message: "Delete Domain"},
22
+ ]
23
+ })
24
+ mainForm = await mainForm.run()
25
+ switch (mainForm) {
26
+ case "add":
27
+ {
28
+ let form = new enquirer.Form({
29
+ name: "createDomain",
30
+ message: "Add a domain",
31
+ choices: [
32
+ {name: "name", message: "Domain/subdomain without http"},
33
+ {name: "region", message: "Region (possible values: us-east-1, eu-west-1, sa-east-1, ap-northeast-1)", initial: "us-east-1"},
34
+ ]
35
+ })
36
+ form = await form.run()
37
+ const spin = ora({text: "Adding domain...", spinner: "toggle9"}).start()
38
+ const domain = await resend.domains.create({
39
+ name: form.name,
40
+ region: form.region
41
+ })
42
+ if (domain.error) {
43
+ spin.stop()
44
+ console.error(domain.error.message)
45
+ process.exit(1)
46
+ }
47
+ spin.stop()
48
+ console.log(`Domain with ID ${domain.data.id} added.`)
49
+
50
+ const choices = domain.data.records.map((record, i) => { return {name: i, message: `Record #${i+1}`} })
51
+
52
+ const records = new enquirer.Select({
53
+ name: "createRecords",
54
+ message: "Add all of the following records to your DNS provider",
55
+ choices
56
+ })
57
+ let recordsResult
58
+ async function iterateRecords() {
59
+ recordsResult = await records.run()
60
+ let record = domain.data.records[recordsResult]
61
+ console.log("Add the following record to your DNS provider:")
62
+ console.log(`- Type: ${record.type}`)
63
+ console.log(`- Value: ${record.value}`)
64
+ if (record.priority) {
65
+ console.log(`- Priority: ${record.priority}`)
66
+ }
67
+ if (record.ttl) {
68
+ console.log(`- TTL: ${record.ttl}`)
69
+ }
70
+ console.log("Once you have finished adding the records, run resend-cli and navigate to Domains -> Verify Domain.")
71
+ await iterateRecords()
72
+ }
73
+ await iterateRecords()
74
+ }
75
+ break
76
+ case "retrieve":
77
+ {
78
+ let form = new enquirer.Form({
79
+ name: "retrieveDomain",
80
+ message: "Retrieve a domain",
81
+ choices: [{name: "id", message: "Domain ID"},]
82
+ })
83
+ form = await form.run()
84
+ const spin = ora({text: "Retrieving domain...", spinner: "toggle9"}).start()
85
+ const domain = await resend.domains.get(form.id)
86
+ if (domain.error) {
87
+ spin.stop()
88
+ console.error(domain.error.message)
89
+ process.exit(1)
90
+ }
91
+ spin.stop()
92
+ logDomain(domain)
93
+ }
94
+ break
95
+ case "verify":
96
+ {
97
+ let form = new enquirer.Form({
98
+ name: "verifyDomain",
99
+ message: "Verify a domain",
100
+ choices: [{name: "id", message: "Domain ID"},]
101
+ })
102
+ form = await form.run()
103
+ const spin = ora({text: "Submitting request...", spinner: "toggle9"}).start()
104
+ const domain = await resend.domains.verify(form.id)
105
+ if (domain.error) {
106
+ spin.stop()
107
+ console.error(domain.error.message)
108
+ process.exit(1)
109
+ }
110
+ spin.stop()
111
+ console.log(`Domain with ID ${domain.data.id} submitted for verification.`)
112
+ }
113
+ break
114
+ case "update":
115
+ {
116
+ let id = new enquirer.Input({
117
+ message: "Enter the ID of the domain you want to update"
118
+ })
119
+ id = await id.run()
120
+ let confirm1 = new enquirer.Confirm({
121
+ message: "Enable click tracking?",
122
+ initial: false
123
+ })
124
+ confirm1 = await confirm1.run()
125
+ let confirm2 = new enquirer.Confirm({
126
+ message: "Enable open tracking?",
127
+ initial: false
128
+ })
129
+ confirm2 = await confirm2.run()
130
+
131
+ const spin = ora({text: "Updating domain...", spinner: "toggle9"}).start()
132
+ const domain = await resend.domains.update({
133
+ id,
134
+ clickTracking: confirm1,
135
+ openTracking: confirm2
136
+ })
137
+ if (domain.error) {
138
+ spin.stop()
139
+ console.error(domain.error.message)
140
+ process.exit(1)
141
+ }
142
+ spin.stop()
143
+ console.log(`Domain with ID ${domain.data.id} updated.`)
144
+ }
145
+ break
146
+ case "list":
147
+ {
148
+ const spin = ora({text: "Fetching domains...", spinner: "toggle9"}).start()
149
+ const domains = await resend.domains.list()
150
+ if (domains.error) {
151
+ spin.stop()
152
+ console.error(domains.error.message)
153
+ process.exit(1)
154
+ }
155
+ spin.stop()
156
+ const choices = domains.data.data.map((domain) => {return {message: domain.name, name: domain.id}})
157
+ let answer = new enquirer.Select({
158
+ name: "listDomains",
159
+ message: "Choose a domain",
160
+ choices
161
+ })
162
+ answer = await answer.run()
163
+ const domain = await resend.domains.get(answer)
164
+ logDomain(domain)
165
+ }
166
+ break
167
+ case "delete":
168
+ {
169
+ let form = new enquirer.Form({
170
+ name: "deleteDomain",
171
+ message: "Delete a domain",
172
+ choices: [{name: "id", message: "Domain ID"},]
173
+ })
174
+ form = await form.run()
175
+ let confirm = new enquirer.Confirm({
176
+ message: "Are you sure you want to delete this domain?",
177
+ initial: false
178
+ })
179
+ confirm = await confirm.run()
180
+ if (!confirm) {
181
+ console.log("Cancelled.")
182
+ process.exit(0)
183
+ }
184
+ const spin = ora({text: "Deleting domain...", spinner: "toggle9"}).start()
185
+ const domain = await resend.domains.remove(form.id)
186
+ if (domain.error) {
187
+ spin.stop()
188
+ console.error(domain.error.message)
189
+ process.exit(1)
190
+ }
191
+ spin.stop()
192
+ console.log(`Domain with ID ${form.id} deleted.`)
193
+ }
194
+ }
195
+ }
@@ -0,0 +1,132 @@
1
+ import ora from "ora"
2
+ import enquirer from "enquirer";
3
+ import {selectAudience} from "./audiences.js";
4
+ import fs from "fs/promises"
5
+
6
+ const logEmail = (email) => {
7
+ console.log(`ID: ${email.id}`)
8
+ console.log(`From: ${email.from}`)
9
+ console.log(`To: ${email.to}`)
10
+ console.log(`Subject: ${email.subject}`)
11
+ console.log(`BCC: ${email.bcc}`)
12
+ console.log(`CC: ${email.cc}`)
13
+ console.log(`Reply To: ${email.reply_to}`)
14
+ console.log(`HTML: ${email.html}`)
15
+ console.log(`Text: ${email.text}`)
16
+ }
17
+
18
+ export const addRecipient = async ({resend}, msg) => {
19
+ let method = new enquirer.Select({
20
+ message: (msg) + "Choose a method",
21
+ choices: [
22
+ {name: "manual", message: "Manual Entry"},
23
+ {name: "audience", message: "Send to entire Audience"},
24
+ {name: "none", message: "None"}
25
+ ]
26
+ })
27
+ method = await method.run()
28
+ if (method === "manual") {
29
+ let email = new enquirer.Input({
30
+ message: "Email Addresses, separated by commas",
31
+ initial: "something@someone.co, delivery@resend.com"
32
+ })
33
+ email = await email.run()
34
+ return email.split(",").map((email) => email.trim())
35
+ } else if (method === "audience") {
36
+ const audienceId = await selectAudience({resend}, "Choose an audience")
37
+ const spinner = ora({text: "Retrieving audience...", spinner: "toggle9"}).start()
38
+ const audience = await resend.contacts.list({audienceId})
39
+ spinner.stop()
40
+ // map audience.data.data array into a comma-separated list of email addresses
41
+ return audience.data.data.map((contact) => contact.email)
42
+ } else {
43
+ return []
44
+ }
45
+ }
46
+
47
+ export default async function({resend, apiKey}) {
48
+ let mainForm = new enquirer.Select({
49
+ message: "Choose an action",
50
+ choices: [
51
+ {name: "send", message: "Send Email"},
52
+ {name: "retrieve", message: "Retrieve Email"},
53
+ ]
54
+ })
55
+ mainForm = await mainForm.run()
56
+ switch (mainForm) {
57
+ case "send":
58
+ {
59
+ let from = new enquirer.Input({
60
+ message: "From",
61
+ initial: "Acme <onboarding@resend.dev>"
62
+ })
63
+ from = await from.run()
64
+ const to = await addRecipient({resend}, "To: ")
65
+ let subject = new enquirer.Input({
66
+ message: "Subject",
67
+ initial: "Changelog | Acne Alpha"
68
+ })
69
+ subject = await subject.run()
70
+ const bcc = await addRecipient({resend}, "BCC: ")
71
+ const cc = await addRecipient({resend}, "CC: ")
72
+ const reply_to = await addRecipient({resend}, "Reply To: ")
73
+ let html = new enquirer.Select({
74
+ message: "HTML insert method",
75
+ choices: [
76
+ {name: "file", message: "From file"},
77
+ {name: "input", message: "From input"},
78
+ {name: "none", message: "None (text instead)"}
79
+ ]
80
+ })
81
+ html = await html.run()
82
+ if (html === "file") {
83
+ let file = new enquirer.Input({
84
+ message: "File path"
85
+ })
86
+ file = await file.run()
87
+ html = await fs.readFile(file, "utf8")
88
+ } else if (html === "input") {
89
+ let htmlInput = new enquirer.Input({
90
+ message: "HTML",
91
+ initial: "<h1>This is an email</h1>"
92
+ })
93
+ html = await htmlInput.run()
94
+ }
95
+ let text = new enquirer.Input({
96
+ message: "Text (not required if HTML is provided)",
97
+ initial: "This is a text email"
98
+ })
99
+ text = await text.run()
100
+ const spinner = ora({text: "Sending email...", spinner: "toggle9"}).start()
101
+ const email = await resend.emails.create({
102
+ from,
103
+ to,
104
+ subject,
105
+ bcc,
106
+ cc,
107
+ reply_to,
108
+ html,
109
+ text
110
+ })
111
+ if (email.error) {
112
+ spinner.stop()
113
+ console.error(email.error.message)
114
+ process.exit(1)
115
+ }
116
+ spinner.stop()
117
+ console.log(`Email with ID ${email.data.id} sent.`)
118
+ }
119
+ break
120
+ case "retrieve":
121
+ {
122
+ let emailId = new enquirer.Input({
123
+ message: "Email ID"
124
+ })
125
+ emailId = await emailId.run()
126
+ const spinner = ora({text: "Retrieving email...", spinner: "toggle9"}).start()
127
+ const email = await resend.emails.get(emailId)
128
+ spinner.stop()
129
+ logEmail(email.data)
130
+ }
131
+ }
132
+ }