@constructive-io/postmaster 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 +23 -0
- package/README.md +130 -0
- package/esm/index.d.ts +26 -0
- package/esm/index.js +101 -0
- package/index.d.ts +26 -0
- package/index.js +109 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# @constructive-io/postmaster
|
|
2
|
+
|
|
3
|
+
<p align="center" width="100%">
|
|
4
|
+
<img height="250" src="https://raw.githubusercontent.com/constructive-io/constructive/refs/heads/main/assets/outline-logo.svg" />
|
|
5
|
+
</p>
|
|
6
|
+
|
|
7
|
+
<p align="center" width="100%">
|
|
8
|
+
<a href="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml">
|
|
9
|
+
<img height="20" src="https://github.com/constructive-io/constructive/actions/workflows/run-tests.yaml/badge.svg" />
|
|
10
|
+
</a>
|
|
11
|
+
<a href="https://github.com/constructive-io/constructive/blob/main/LICENSE">
|
|
12
|
+
<img height="20" src="https://img.shields.io/badge/license-MIT-blue.svg"/>
|
|
13
|
+
</a>
|
|
14
|
+
<a href="https://www.npmjs.com/package/@constructive-io/postmaster">
|
|
15
|
+
<img height="20" src="https://img.shields.io/github/package-json/v/constructive-io/constructive?filename=packages%2Fpostmaster%2Fpackage.json"/>
|
|
16
|
+
</a>
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
> Mailgun email sender for Constructive services
|
|
20
|
+
|
|
21
|
+
This package exposes a `send` helper with the same call shape used by `simple-smtp-server` (e.g. `{ to, subject, html, text }`).
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install @constructive-io/postmaster
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Usage
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { send } from '@constructive-io/postmaster';
|
|
33
|
+
|
|
34
|
+
await send({
|
|
35
|
+
to: 'user@example.com',
|
|
36
|
+
subject: 'Hello',
|
|
37
|
+
html: '<p>Hello World</p>'
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Environment Variables
|
|
42
|
+
|
|
43
|
+
| Variable | Description | Required |
|
|
44
|
+
|----------|-------------|----------|
|
|
45
|
+
| `MAILGUN_KEY` | Mailgun API key | Yes |
|
|
46
|
+
| `MAILGUN_DOMAIN` | Mailgun domain (e.g., `mg.example.com`) | Yes |
|
|
47
|
+
| `MAILGUN_FROM` | Default sender email address | No |
|
|
48
|
+
| `MAILGUN_REPLY` | Default reply-to email address | No |
|
|
49
|
+
| `MAILGUN_DEV_EMAIL` | Development email redirect (all emails sent to this address with original recipient encoded) | No |
|
|
50
|
+
|
|
51
|
+
## Development Email Redirect
|
|
52
|
+
|
|
53
|
+
When `MAILGUN_DEV_EMAIL` is set, all emails are redirected to that address with the original recipient encoded in the local part. For example, if `MAILGUN_DEV_EMAIL=dev@example.com` and you send to `user@domain.com`, the email will be sent to `dev+user_at_domain.com@example.com`.
|
|
54
|
+
|
|
55
|
+
## API
|
|
56
|
+
|
|
57
|
+
### `send(options, mailgunOverrides?)`
|
|
58
|
+
|
|
59
|
+
Send an email via Mailgun.
|
|
60
|
+
|
|
61
|
+
**Parameters:**
|
|
62
|
+
- `options.to` - Recipient email address(es)
|
|
63
|
+
- `options.subject` - Email subject
|
|
64
|
+
- `options.html` - HTML body (required if no text)
|
|
65
|
+
- `options.text` - Plain text body (required if no html)
|
|
66
|
+
- `options.from` - Sender email (defaults to `MAILGUN_FROM`)
|
|
67
|
+
- `options.replyTo` - Reply-to address (defaults to `MAILGUN_REPLY`)
|
|
68
|
+
- `mailgunOverrides` - Optional overrides for Mailgun configuration
|
|
69
|
+
|
|
70
|
+
### `resetClient()`
|
|
71
|
+
|
|
72
|
+
Reset the cached Mailgun client. Useful for testing or when configuration changes.
|
|
73
|
+
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Education and Tutorials
|
|
77
|
+
|
|
78
|
+
1. ๐ [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
79
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
80
|
+
|
|
81
|
+
2. ๐ฆ [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
82
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
83
|
+
|
|
84
|
+
3. โ๏ธ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
85
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
86
|
+
|
|
87
|
+
4. ๐งช [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
88
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
89
|
+
|
|
90
|
+
5. โก [Supabase Testing](https://constructive.io/learn/supabase)
|
|
91
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
92
|
+
|
|
93
|
+
6. ๐ง [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
94
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
95
|
+
|
|
96
|
+
7. ๐ง [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
97
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
98
|
+
|
|
99
|
+
## Related Constructive Tooling
|
|
100
|
+
|
|
101
|
+
### ๐ฆ Package Management
|
|
102
|
+
|
|
103
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **๐ฅ๏ธ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
104
|
+
|
|
105
|
+
### ๐งช Testing
|
|
106
|
+
|
|
107
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **๐ Isolated testing environments** with per-test transaction rollbacksโideal for integration tests, complex migrations, and RLS simulation.
|
|
108
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **๐ฑ PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
109
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **๐งช Supabase-native test harness** preconfigured for the local Supabase stackโper-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
110
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **๐ Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
111
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **๐ Session context injection** to add session-local context (e.g., `SET LOCAL`) into queriesโideal for setting `role`, `jwt.claims`, and other session settings.
|
|
112
|
+
|
|
113
|
+
### ๐ง Parsing & AST
|
|
114
|
+
|
|
115
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **๐ SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
116
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **๐ Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
117
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **๐ฆ Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
118
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **๐ท๏ธ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
119
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **๐ Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
120
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **๐ ๏ธ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
121
|
+
|
|
122
|
+
## Credits
|
|
123
|
+
|
|
124
|
+
**๐ Built by the [Constructive](https://constructive.io) team โ creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
125
|
+
|
|
126
|
+
## Disclaimer
|
|
127
|
+
|
|
128
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
129
|
+
|
|
130
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/esm/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mailgun configuration options
|
|
3
|
+
*/
|
|
4
|
+
export interface MailgunOptions {
|
|
5
|
+
/** Mailgun API key */
|
|
6
|
+
key?: string;
|
|
7
|
+
/** Mailgun domain (e.g., 'mg.example.com') */
|
|
8
|
+
domain?: string;
|
|
9
|
+
/** Default sender email address */
|
|
10
|
+
from?: string;
|
|
11
|
+
/** Default reply-to email address */
|
|
12
|
+
replyTo?: string;
|
|
13
|
+
/** Development email address - when set, all emails are redirected to this address */
|
|
14
|
+
devEmail?: string;
|
|
15
|
+
}
|
|
16
|
+
type SendInput = {
|
|
17
|
+
to: string | string[];
|
|
18
|
+
subject: string;
|
|
19
|
+
html?: string;
|
|
20
|
+
text?: string;
|
|
21
|
+
from?: string;
|
|
22
|
+
replyTo?: string;
|
|
23
|
+
};
|
|
24
|
+
export declare const send: (options: SendInput, mailgunOverrides?: MailgunOptions) => Promise<void>;
|
|
25
|
+
export declare const resetClient: () => void;
|
|
26
|
+
export type { SendInput as SendOptions };
|
package/esm/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import Mailgun from 'mailgun.js';
|
|
2
|
+
import FormData from 'form-data';
|
|
3
|
+
import { str, email, host, env } from '12factor-env';
|
|
4
|
+
let cachedEnvConfig;
|
|
5
|
+
const getEnvConfig = () => {
|
|
6
|
+
if (cachedEnvConfig) {
|
|
7
|
+
return cachedEnvConfig;
|
|
8
|
+
}
|
|
9
|
+
cachedEnvConfig = env(process.env, {
|
|
10
|
+
MAILGUN_KEY: str()
|
|
11
|
+
}, {
|
|
12
|
+
MAILGUN_DOMAIN: host(),
|
|
13
|
+
MAILGUN_FROM: email(),
|
|
14
|
+
MAILGUN_REPLY: email()
|
|
15
|
+
});
|
|
16
|
+
return cachedEnvConfig;
|
|
17
|
+
};
|
|
18
|
+
// MAILGUN_DEV_EMAIL is read directly from process.env (not validated by 12factor-env)
|
|
19
|
+
// to match the original behavior where it was accessed but not in the env validation
|
|
20
|
+
const getEnvOptions = () => {
|
|
21
|
+
const envConfig = getEnvConfig();
|
|
22
|
+
return {
|
|
23
|
+
key: envConfig.MAILGUN_KEY,
|
|
24
|
+
domain: envConfig.MAILGUN_DOMAIN,
|
|
25
|
+
from: envConfig.MAILGUN_FROM,
|
|
26
|
+
replyTo: envConfig.MAILGUN_REPLY,
|
|
27
|
+
devEmail: process.env.MAILGUN_DEV_EMAIL
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
let client;
|
|
31
|
+
let cachedMailgunOpts;
|
|
32
|
+
const getClient = (overrides) => {
|
|
33
|
+
const envOpts = getEnvOptions();
|
|
34
|
+
const mailgunOpts = {
|
|
35
|
+
...envOpts,
|
|
36
|
+
...overrides
|
|
37
|
+
};
|
|
38
|
+
if (!client || overrides) {
|
|
39
|
+
if (!mailgunOpts.key) {
|
|
40
|
+
throw new Error('Missing MAILGUN_KEY');
|
|
41
|
+
}
|
|
42
|
+
if (!mailgunOpts.domain) {
|
|
43
|
+
throw new Error('Missing MAILGUN_DOMAIN');
|
|
44
|
+
}
|
|
45
|
+
const mailgun = new Mailgun(FormData);
|
|
46
|
+
client = mailgun.client({
|
|
47
|
+
username: 'api',
|
|
48
|
+
key: mailgunOpts.key
|
|
49
|
+
});
|
|
50
|
+
cachedMailgunOpts = mailgunOpts;
|
|
51
|
+
}
|
|
52
|
+
return { client, mailgunOpts: cachedMailgunOpts ?? mailgunOpts };
|
|
53
|
+
};
|
|
54
|
+
const resolveFrom = (from, mailgunOpts) => {
|
|
55
|
+
const resolved = from ?? mailgunOpts.from;
|
|
56
|
+
if (!resolved) {
|
|
57
|
+
throw new Error('Missing from address. Set MAILGUN_FROM or pass from in send().');
|
|
58
|
+
}
|
|
59
|
+
return resolved;
|
|
60
|
+
};
|
|
61
|
+
const resolveRecipient = (to, mailgunOpts) => {
|
|
62
|
+
const devEmail = mailgunOpts.devEmail;
|
|
63
|
+
if (!devEmail) {
|
|
64
|
+
return to;
|
|
65
|
+
}
|
|
66
|
+
const [localPart, domainPart] = devEmail.split('@');
|
|
67
|
+
const recipients = Array.isArray(to) ? to : [to];
|
|
68
|
+
return recipients.map(recipient => {
|
|
69
|
+
const encodedRecipient = recipient.replace('@', '_at_');
|
|
70
|
+
return `${localPart}+${encodedRecipient}@${domainPart}`;
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
export const send = async (options, mailgunOverrides) => {
|
|
74
|
+
if (!options.to) {
|
|
75
|
+
throw new Error('Missing "to"');
|
|
76
|
+
}
|
|
77
|
+
if (!options.subject) {
|
|
78
|
+
throw new Error('Missing "subject"');
|
|
79
|
+
}
|
|
80
|
+
if (!options.html && !options.text) {
|
|
81
|
+
throw new Error('Missing "html" or "text"');
|
|
82
|
+
}
|
|
83
|
+
const { client: mailgunClient, mailgunOpts } = getClient(mailgunOverrides);
|
|
84
|
+
const to = resolveRecipient(options.to, mailgunOpts);
|
|
85
|
+
const from = resolveFrom(options.from, mailgunOpts);
|
|
86
|
+
const replyTo = options.replyTo ?? mailgunOpts.replyTo;
|
|
87
|
+
const messageData = {
|
|
88
|
+
to: Array.isArray(to) ? to.join(',') : to,
|
|
89
|
+
from,
|
|
90
|
+
subject: options.subject,
|
|
91
|
+
...(options.html && { html: options.html }),
|
|
92
|
+
...(options.text && { text: options.text }),
|
|
93
|
+
...(replyTo && { 'h:Reply-To': replyTo })
|
|
94
|
+
};
|
|
95
|
+
await mailgunClient.messages.create(mailgunOpts.domain, messageData);
|
|
96
|
+
};
|
|
97
|
+
export const resetClient = () => {
|
|
98
|
+
client = undefined;
|
|
99
|
+
cachedMailgunOpts = undefined;
|
|
100
|
+
cachedEnvConfig = undefined;
|
|
101
|
+
};
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mailgun configuration options
|
|
3
|
+
*/
|
|
4
|
+
export interface MailgunOptions {
|
|
5
|
+
/** Mailgun API key */
|
|
6
|
+
key?: string;
|
|
7
|
+
/** Mailgun domain (e.g., 'mg.example.com') */
|
|
8
|
+
domain?: string;
|
|
9
|
+
/** Default sender email address */
|
|
10
|
+
from?: string;
|
|
11
|
+
/** Default reply-to email address */
|
|
12
|
+
replyTo?: string;
|
|
13
|
+
/** Development email address - when set, all emails are redirected to this address */
|
|
14
|
+
devEmail?: string;
|
|
15
|
+
}
|
|
16
|
+
type SendInput = {
|
|
17
|
+
to: string | string[];
|
|
18
|
+
subject: string;
|
|
19
|
+
html?: string;
|
|
20
|
+
text?: string;
|
|
21
|
+
from?: string;
|
|
22
|
+
replyTo?: string;
|
|
23
|
+
};
|
|
24
|
+
export declare const send: (options: SendInput, mailgunOverrides?: MailgunOptions) => Promise<void>;
|
|
25
|
+
export declare const resetClient: () => void;
|
|
26
|
+
export type { SendInput as SendOptions };
|
package/index.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.resetClient = exports.send = void 0;
|
|
7
|
+
const mailgun_js_1 = __importDefault(require("mailgun.js"));
|
|
8
|
+
const form_data_1 = __importDefault(require("form-data"));
|
|
9
|
+
const _12factor_env_1 = require("12factor-env");
|
|
10
|
+
let cachedEnvConfig;
|
|
11
|
+
const getEnvConfig = () => {
|
|
12
|
+
if (cachedEnvConfig) {
|
|
13
|
+
return cachedEnvConfig;
|
|
14
|
+
}
|
|
15
|
+
cachedEnvConfig = (0, _12factor_env_1.env)(process.env, {
|
|
16
|
+
MAILGUN_KEY: (0, _12factor_env_1.str)()
|
|
17
|
+
}, {
|
|
18
|
+
MAILGUN_DOMAIN: (0, _12factor_env_1.host)(),
|
|
19
|
+
MAILGUN_FROM: (0, _12factor_env_1.email)(),
|
|
20
|
+
MAILGUN_REPLY: (0, _12factor_env_1.email)()
|
|
21
|
+
});
|
|
22
|
+
return cachedEnvConfig;
|
|
23
|
+
};
|
|
24
|
+
// MAILGUN_DEV_EMAIL is read directly from process.env (not validated by 12factor-env)
|
|
25
|
+
// to match the original behavior where it was accessed but not in the env validation
|
|
26
|
+
const getEnvOptions = () => {
|
|
27
|
+
const envConfig = getEnvConfig();
|
|
28
|
+
return {
|
|
29
|
+
key: envConfig.MAILGUN_KEY,
|
|
30
|
+
domain: envConfig.MAILGUN_DOMAIN,
|
|
31
|
+
from: envConfig.MAILGUN_FROM,
|
|
32
|
+
replyTo: envConfig.MAILGUN_REPLY,
|
|
33
|
+
devEmail: process.env.MAILGUN_DEV_EMAIL
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
let client;
|
|
37
|
+
let cachedMailgunOpts;
|
|
38
|
+
const getClient = (overrides) => {
|
|
39
|
+
const envOpts = getEnvOptions();
|
|
40
|
+
const mailgunOpts = {
|
|
41
|
+
...envOpts,
|
|
42
|
+
...overrides
|
|
43
|
+
};
|
|
44
|
+
if (!client || overrides) {
|
|
45
|
+
if (!mailgunOpts.key) {
|
|
46
|
+
throw new Error('Missing MAILGUN_KEY');
|
|
47
|
+
}
|
|
48
|
+
if (!mailgunOpts.domain) {
|
|
49
|
+
throw new Error('Missing MAILGUN_DOMAIN');
|
|
50
|
+
}
|
|
51
|
+
const mailgun = new mailgun_js_1.default(form_data_1.default);
|
|
52
|
+
client = mailgun.client({
|
|
53
|
+
username: 'api',
|
|
54
|
+
key: mailgunOpts.key
|
|
55
|
+
});
|
|
56
|
+
cachedMailgunOpts = mailgunOpts;
|
|
57
|
+
}
|
|
58
|
+
return { client, mailgunOpts: cachedMailgunOpts ?? mailgunOpts };
|
|
59
|
+
};
|
|
60
|
+
const resolveFrom = (from, mailgunOpts) => {
|
|
61
|
+
const resolved = from ?? mailgunOpts.from;
|
|
62
|
+
if (!resolved) {
|
|
63
|
+
throw new Error('Missing from address. Set MAILGUN_FROM or pass from in send().');
|
|
64
|
+
}
|
|
65
|
+
return resolved;
|
|
66
|
+
};
|
|
67
|
+
const resolveRecipient = (to, mailgunOpts) => {
|
|
68
|
+
const devEmail = mailgunOpts.devEmail;
|
|
69
|
+
if (!devEmail) {
|
|
70
|
+
return to;
|
|
71
|
+
}
|
|
72
|
+
const [localPart, domainPart] = devEmail.split('@');
|
|
73
|
+
const recipients = Array.isArray(to) ? to : [to];
|
|
74
|
+
return recipients.map(recipient => {
|
|
75
|
+
const encodedRecipient = recipient.replace('@', '_at_');
|
|
76
|
+
return `${localPart}+${encodedRecipient}@${domainPart}`;
|
|
77
|
+
});
|
|
78
|
+
};
|
|
79
|
+
const send = async (options, mailgunOverrides) => {
|
|
80
|
+
if (!options.to) {
|
|
81
|
+
throw new Error('Missing "to"');
|
|
82
|
+
}
|
|
83
|
+
if (!options.subject) {
|
|
84
|
+
throw new Error('Missing "subject"');
|
|
85
|
+
}
|
|
86
|
+
if (!options.html && !options.text) {
|
|
87
|
+
throw new Error('Missing "html" or "text"');
|
|
88
|
+
}
|
|
89
|
+
const { client: mailgunClient, mailgunOpts } = getClient(mailgunOverrides);
|
|
90
|
+
const to = resolveRecipient(options.to, mailgunOpts);
|
|
91
|
+
const from = resolveFrom(options.from, mailgunOpts);
|
|
92
|
+
const replyTo = options.replyTo ?? mailgunOpts.replyTo;
|
|
93
|
+
const messageData = {
|
|
94
|
+
to: Array.isArray(to) ? to.join(',') : to,
|
|
95
|
+
from,
|
|
96
|
+
subject: options.subject,
|
|
97
|
+
...(options.html && { html: options.html }),
|
|
98
|
+
...(options.text && { text: options.text }),
|
|
99
|
+
...(replyTo && { 'h:Reply-To': replyTo })
|
|
100
|
+
};
|
|
101
|
+
await mailgunClient.messages.create(mailgunOpts.domain, messageData);
|
|
102
|
+
};
|
|
103
|
+
exports.send = send;
|
|
104
|
+
const resetClient = () => {
|
|
105
|
+
client = undefined;
|
|
106
|
+
cachedMailgunOpts = undefined;
|
|
107
|
+
cachedEnvConfig = undefined;
|
|
108
|
+
};
|
|
109
|
+
exports.resetClient = resetClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constructive-io/postmaster",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"author": "Constructive <developers@constructive.io>",
|
|
5
|
+
"description": "Mailgun email sender for Constructive",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"clean": "makage clean",
|
|
24
|
+
"prepack": "npm run build",
|
|
25
|
+
"build": "makage build",
|
|
26
|
+
"build:dev": "makage build --dev",
|
|
27
|
+
"lint": "eslint . --fix",
|
|
28
|
+
"test": "jest --passWithNoTests",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"12factor-env": "^1.0.0",
|
|
33
|
+
"form-data": "^4.0.0",
|
|
34
|
+
"mailgun.js": "^10.2.3"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"makage": "^0.1.10"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"mailgun",
|
|
41
|
+
"email",
|
|
42
|
+
"postmaster"
|
|
43
|
+
],
|
|
44
|
+
"gitHead": "390f4dd57fc158554518ec454bf2a4856d550552"
|
|
45
|
+
}
|