@haatlabs-core/mailer 0.0.0-stage → 0.10.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,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Haatlabs
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,35 @@
1
- # Temporary Holding Version
1
+ # @haatlabs-core/mailer
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Sends the apps' email (sign-in codes). The apps call a `Mailer`; how mail leaves is an
4
+ adapter, a `MailTransport`, one per provider.
5
+
6
+ const mailer = mailerFromConfig(config.mail); // SMTP from config-core's MailConfig, or not configured
7
+ if (mailer.configured) await mailer.send({ to, subject, text });
8
+
9
+ createMailer({ from, transport: smtpTransport({ host, port, secure, user, password }) });
10
+ createMailer({ from, transport: memoryTransport() }); // tests: keeps what it is sent
11
+
12
+ **Adding a provider** (an email API with a key): write an adapter, and choose it in
13
+ `mailerFromConfig` from the configuration. Nothing in the apps changes.
14
+
15
+ export function exampleApiTransport(options: { apiKey: string }): MailTransport {
16
+ return {
17
+ name: 'example-api',
18
+ send: async ({ from, to, subject, text }) => {
19
+ const response = await fetch('https://api.example.com/send', {
20
+ method: 'POST',
21
+ headers: { authorization: `Bearer ${options.apiKey}`, 'content-type': 'application/json' },
22
+ body: JSON.stringify({ from, to, subject, text }),
23
+ });
24
+ if (!response.ok) throw new Error(`example-api refused the email (HTTP ${response.status})`);
25
+ },
26
+ };
27
+ }
28
+
29
+ An adapter throws when the provider refuses or cannot be reached, and never logs what it
30
+ sends: a sign-in code is as good as a password. Without a provider, `configured` is false
31
+ and `send` refuses with `EMAIL_ERROR`.
32
+
33
+ ## Install
34
+
35
+ pnpm add @haatlabs-core/mailer
@@ -0,0 +1,7 @@
1
+ export { createMailer, mailerFromConfig } from './mailer.js';
2
+ export type { Mail, Mailer } from './mailer.js';
3
+ export { memoryTransport } from './memory.js';
4
+ export type { MemoryTransport } from './memory.js';
5
+ export { smtpTransport } from './smtp.js';
6
+ export type { SmtpOptions } from './smtp.js';
7
+ export type { MailTransport, OutgoingMail } from './transport.js';
@@ -0,0 +1,35 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var index_exports = {};
19
+ __export(index_exports, {
20
+ createMailer: () => import_mailer.createMailer,
21
+ mailerFromConfig: () => import_mailer.mailerFromConfig,
22
+ memoryTransport: () => import_memory.memoryTransport,
23
+ smtpTransport: () => import_smtp.smtpTransport
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ var import_mailer = require("./mailer.js");
27
+ var import_memory = require("./memory.js");
28
+ var import_smtp = require("./smtp.js");
29
+ // Annotate the CommonJS export names for ESM import in node:
30
+ 0 && (module.exports = {
31
+ createMailer,
32
+ mailerFromConfig,
33
+ memoryTransport,
34
+ smtpTransport
35
+ });
@@ -0,0 +1,26 @@
1
+ import type { MailConfig } from '@haatlabs-core/config-core';
2
+ import type { MailTransport } from './transport.js';
3
+ export interface Mail {
4
+ to: string;
5
+ subject: string;
6
+ text: string;
7
+ }
8
+ /** Sends an email. What is sent is never logged: a sign-in code is as good as a password. */
9
+ export interface Mailer {
10
+ /** False when no mail provider is configured: nothing can be sent. */
11
+ readonly configured: boolean;
12
+ send(mail: Mail): Promise<void>;
13
+ }
14
+ /**
15
+ * A mailer sending from `from` through `transport`, whichever provider it is. Null: no
16
+ * provider is configured; `send` refuses with EMAIL_ERROR, not worth retrying.
17
+ */
18
+ export declare function createMailer(options: {
19
+ from: string;
20
+ transport: MailTransport;
21
+ } | null): Mailer;
22
+ /**
23
+ * The mailer an app's configuration describes. Today that is an SMTP server; another
24
+ * provider is one more adapter (a `MailTransport`) chosen here, and nothing else changes.
25
+ */
26
+ export declare function mailerFromConfig(config: MailConfig | null): Mailer;
@@ -0,0 +1,46 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var mailer_exports = {};
19
+ __export(mailer_exports, {
20
+ createMailer: () => createMailer,
21
+ mailerFromConfig: () => mailerFromConfig
22
+ });
23
+ module.exports = __toCommonJS(mailer_exports);
24
+ var import_errors = require("@haatlabs-core/errors");
25
+ var import_smtp = require("./smtp.js");
26
+ function createMailer(options) {
27
+ if (!options) {
28
+ return {
29
+ configured: false,
30
+ send: () => Promise.reject(new import_errors.AppError("EMAIL_ERROR", "Email is not set up on this server", { transient: false }))
31
+ };
32
+ }
33
+ const { from, transport } = options;
34
+ return {
35
+ configured: true,
36
+ send: (mail) => transport.send({ from, ...mail })
37
+ };
38
+ }
39
+ function mailerFromConfig(config) {
40
+ return createMailer(config ? { from: config.from, transport: (0, import_smtp.smtpTransport)(config) } : null);
41
+ }
42
+ // Annotate the CommonJS export names for ESM import in node:
43
+ 0 && (module.exports = {
44
+ createMailer,
45
+ mailerFromConfig
46
+ });
@@ -0,0 +1,7 @@
1
+ import type { MailTransport, OutgoingMail } from './transport.js';
2
+ /** Keeps what it is given instead of sending it: for tests and local machines. */
3
+ export interface MemoryTransport extends MailTransport {
4
+ /** Every email sent, oldest first. */
5
+ readonly sent: OutgoingMail[];
6
+ }
7
+ export declare function memoryTransport(): MemoryTransport;
@@ -0,0 +1,36 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var memory_exports = {};
19
+ __export(memory_exports, {
20
+ memoryTransport: () => memoryTransport
21
+ });
22
+ module.exports = __toCommonJS(memory_exports);
23
+ function memoryTransport() {
24
+ const sent = [];
25
+ return {
26
+ name: "memory",
27
+ sent,
28
+ send: async (mail) => {
29
+ sent.push({ ...mail });
30
+ }
31
+ };
32
+ }
33
+ // Annotate the CommonJS export names for ESM import in node:
34
+ 0 && (module.exports = {
35
+ memoryTransport
36
+ });
@@ -0,0 +1,8 @@
1
+ {
2
+ "type": "commonjs",
3
+ "publishConfig": {
4
+ "access": "public",
5
+ "registry": "https://registry.npmjs.org"
6
+ },
7
+ "platformRegistry": "npm"
8
+ }
@@ -0,0 +1,11 @@
1
+ import type { MailTransport } from './transport.js';
2
+ export interface SmtpOptions {
3
+ host: string;
4
+ port: number;
5
+ /** True for a server that speaks TLS from the first byte (usually port 465). */
6
+ secure: boolean;
7
+ user: string | undefined;
8
+ password: string | undefined;
9
+ }
10
+ /** Mail through any SMTP server. */
11
+ export declare function smtpTransport(options: SmtpOptions): MailTransport;
@@ -0,0 +1,54 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+ var smtp_exports = {};
29
+ __export(smtp_exports, {
30
+ smtpTransport: () => smtpTransport
31
+ });
32
+ module.exports = __toCommonJS(smtp_exports);
33
+ var import_nodemailer = __toESM(require("nodemailer"));
34
+ const SEND_TIMEOUT_MS = 15e3;
35
+ function smtpTransport(options) {
36
+ const transport = import_nodemailer.default.createTransport({
37
+ host: options.host,
38
+ port: options.port,
39
+ secure: options.secure,
40
+ auth: options.user ? { user: options.user, pass: options.password ?? "" } : void 0,
41
+ connectionTimeout: SEND_TIMEOUT_MS,
42
+ socketTimeout: SEND_TIMEOUT_MS
43
+ });
44
+ return {
45
+ name: "smtp",
46
+ send: async ({ from, to, subject, text }) => {
47
+ await transport.sendMail({ from, to, subject, text });
48
+ }
49
+ };
50
+ }
51
+ // Annotate the CommonJS export names for ESM import in node:
52
+ 0 && (module.exports = {
53
+ smtpTransport
54
+ });
@@ -0,0 +1,17 @@
1
+ /** One email, ready to go: who it is from is the mailer's to add. */
2
+ export interface OutgoingMail {
3
+ from: string;
4
+ to: string;
5
+ subject: string;
6
+ text: string;
7
+ }
8
+ /**
9
+ * How mail leaves: one adapter per provider (an SMTP server, an email API with its key).
10
+ * The mailer and the apps never know which. An adapter throws when the provider refuses or
11
+ * cannot be reached, and never logs what it sends: a sign-in code is as good as a password.
12
+ */
13
+ export interface MailTransport {
14
+ /** The provider's name, for logs and health: `smtp`, `memory`, … */
15
+ readonly name: string;
16
+ send(mail: OutgoingMail): Promise<void>;
17
+ }
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var transport_exports = {};
15
+ module.exports = __toCommonJS(transport_exports);
@@ -0,0 +1,7 @@
1
+ export { createMailer, mailerFromConfig } from './mailer.js';
2
+ export type { Mail, Mailer } from './mailer.js';
3
+ export { memoryTransport } from './memory.js';
4
+ export type { MemoryTransport } from './memory.js';
5
+ export { smtpTransport } from './smtp.js';
6
+ export type { SmtpOptions } from './smtp.js';
7
+ export type { MailTransport, OutgoingMail } from './transport.js';
package/dist/index.js ADDED
@@ -0,0 +1,4 @@
1
+ export { createMailer, mailerFromConfig } from './mailer.js';
2
+ export { memoryTransport } from './memory.js';
3
+ export { smtpTransport } from './smtp.js';
4
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE7D,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAE9C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC"}
@@ -0,0 +1,26 @@
1
+ import type { MailConfig } from '@haatlabs-core/config-core';
2
+ import type { MailTransport } from './transport.js';
3
+ export interface Mail {
4
+ to: string;
5
+ subject: string;
6
+ text: string;
7
+ }
8
+ /** Sends an email. What is sent is never logged: a sign-in code is as good as a password. */
9
+ export interface Mailer {
10
+ /** False when no mail provider is configured: nothing can be sent. */
11
+ readonly configured: boolean;
12
+ send(mail: Mail): Promise<void>;
13
+ }
14
+ /**
15
+ * A mailer sending from `from` through `transport`, whichever provider it is. Null: no
16
+ * provider is configured; `send` refuses with EMAIL_ERROR, not worth retrying.
17
+ */
18
+ export declare function createMailer(options: {
19
+ from: string;
20
+ transport: MailTransport;
21
+ } | null): Mailer;
22
+ /**
23
+ * The mailer an app's configuration describes. Today that is an SMTP server; another
24
+ * provider is one more adapter (a `MailTransport`) chosen here, and nothing else changes.
25
+ */
26
+ export declare function mailerFromConfig(config: MailConfig | null): Mailer;
package/dist/mailer.js ADDED
@@ -0,0 +1,27 @@
1
+ import { AppError } from '@haatlabs-core/errors';
2
+ import { smtpTransport } from './smtp.js';
3
+ /**
4
+ * A mailer sending from `from` through `transport`, whichever provider it is. Null: no
5
+ * provider is configured; `send` refuses with EMAIL_ERROR, not worth retrying.
6
+ */
7
+ export function createMailer(options) {
8
+ if (!options) {
9
+ return {
10
+ configured: false,
11
+ send: () => Promise.reject(new AppError('EMAIL_ERROR', 'Email is not set up on this server', { transient: false })),
12
+ };
13
+ }
14
+ const { from, transport } = options;
15
+ return {
16
+ configured: true,
17
+ send: (mail) => transport.send({ from, ...mail }),
18
+ };
19
+ }
20
+ /**
21
+ * The mailer an app's configuration describes. Today that is an SMTP server; another
22
+ * provider is one more adapter (a `MailTransport`) chosen here, and nothing else changes.
23
+ */
24
+ export function mailerFromConfig(config) {
25
+ return createMailer(config ? { from: config.from, transport: smtpTransport(config) } : null);
26
+ }
27
+ //# sourceMappingURL=mailer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mailer.js","sourceRoot":"","sources":["../src/mailer.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAgB1C;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAA0D;IACrF,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,UAAU,EAAE,KAAK;YACjB,IAAI,EAAE,GAAG,EAAE,CACT,OAAO,CAAC,MAAM,CACZ,IAAI,QAAQ,CAAC,aAAa,EAAE,oCAAoC,EAAE,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,CACxF;SACJ,CAAC;IACJ,CAAC;IACD,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO,CAAC;IACpC,OAAO;QACL,UAAU,EAAE,IAAI;QAChB,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,GAAG,IAAI,EAAE,CAAC;KAClD,CAAC;AACJ,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAAC,MAAyB;IACxD,OAAO,YAAY,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,SAAS,EAAE,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;AAC/F,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { MailTransport, OutgoingMail } from './transport.js';
2
+ /** Keeps what it is given instead of sending it: for tests and local machines. */
3
+ export interface MemoryTransport extends MailTransport {
4
+ /** Every email sent, oldest first. */
5
+ readonly sent: OutgoingMail[];
6
+ }
7
+ export declare function memoryTransport(): MemoryTransport;
package/dist/memory.js ADDED
@@ -0,0 +1,11 @@
1
+ export function memoryTransport() {
2
+ const sent = [];
3
+ return {
4
+ name: 'memory',
5
+ sent,
6
+ send: async (mail) => {
7
+ sent.push({ ...mail });
8
+ },
9
+ };
10
+ }
11
+ //# sourceMappingURL=memory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memory.js","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,eAAe;IAC7B,MAAM,IAAI,GAAmB,EAAE,CAAC;IAChC,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,IAAI;QACJ,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;YACnB,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC;QACzB,CAAC;KACF,CAAC;AACJ,CAAC"}
package/dist/smtp.d.ts ADDED
@@ -0,0 +1,11 @@
1
+ import type { MailTransport } from './transport.js';
2
+ export interface SmtpOptions {
3
+ host: string;
4
+ port: number;
5
+ /** True for a server that speaks TLS from the first byte (usually port 465). */
6
+ secure: boolean;
7
+ user: string | undefined;
8
+ password: string | undefined;
9
+ }
10
+ /** Mail through any SMTP server. */
11
+ export declare function smtpTransport(options: SmtpOptions): MailTransport;
package/dist/smtp.js ADDED
@@ -0,0 +1,20 @@
1
+ import nodemailer from 'nodemailer';
2
+ const SEND_TIMEOUT_MS = 15_000;
3
+ /** Mail through any SMTP server. */
4
+ export function smtpTransport(options) {
5
+ const transport = nodemailer.createTransport({
6
+ host: options.host,
7
+ port: options.port,
8
+ secure: options.secure,
9
+ auth: options.user ? { user: options.user, pass: options.password ?? '' } : undefined,
10
+ connectionTimeout: SEND_TIMEOUT_MS,
11
+ socketTimeout: SEND_TIMEOUT_MS,
12
+ });
13
+ return {
14
+ name: 'smtp',
15
+ send: async ({ from, to, subject, text }) => {
16
+ await transport.sendMail({ from, to, subject, text });
17
+ },
18
+ };
19
+ }
20
+ //# sourceMappingURL=smtp.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"smtp.js","sourceRoot":"","sources":["../src/smtp.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,YAAY,CAAC;AAGpC,MAAM,eAAe,GAAG,MAAM,CAAC;AAW/B,oCAAoC;AACpC,MAAM,UAAU,aAAa,CAAC,OAAoB;IAChD,MAAM,SAAS,GAAG,UAAU,CAAC,eAAe,CAAC;QAC3C,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,IAAI,EAAE,OAAO,CAAC,IAAI;QAClB,MAAM,EAAE,OAAO,CAAC,MAAM;QACtB,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,QAAQ,IAAI,EAAE,EAAE,CAAC,CAAC,CAAC,SAAS;QACrF,iBAAiB,EAAE,eAAe;QAClC,aAAa,EAAE,eAAe;KAC/B,CAAC,CAAC;IACH,OAAO;QACL,IAAI,EAAE,MAAM;QACZ,IAAI,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE;YAC1C,MAAM,SAAS,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC,CAAC;QACxD,CAAC;KACF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,17 @@
1
+ /** One email, ready to go: who it is from is the mailer's to add. */
2
+ export interface OutgoingMail {
3
+ from: string;
4
+ to: string;
5
+ subject: string;
6
+ text: string;
7
+ }
8
+ /**
9
+ * How mail leaves: one adapter per provider (an SMTP server, an email API with its key).
10
+ * The mailer and the apps never know which. An adapter throws when the provider refuses or
11
+ * cannot be reached, and never logs what it sends: a sign-in code is as good as a password.
12
+ */
13
+ export interface MailTransport {
14
+ /** The provider's name, for logs and health: `smtp`, `memory`, … */
15
+ readonly name: string;
16
+ send(mail: OutgoingMail): Promise<void>;
17
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=transport.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transport.js","sourceRoot":"","sources":["../src/transport.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,55 @@
1
1
  {
2
2
  "name": "@haatlabs-core/mailer",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.10.0",
4
+ "description": "Send an app's email through a pluggable transport (SMTP and others).",
5
+ "keywords": [
6
+ "haatlabs"
7
+ ],
8
+ "homepage": "https://haatlabs.shop/docs/packages/mailer/",
9
+ "license": "Apache-2.0",
10
+ "type": "module",
11
+ "engines": {
12
+ "node": ">=24"
13
+ },
14
+ "repository": {
15
+ "type": "git",
16
+ "url": "git+https://github.com/Haatlabs/haatlabs-platform.git",
17
+ "directory": "packages/mailer"
18
+ },
19
+ "publishConfig": {
20
+ "access": "public",
21
+ "registry": "https://registry.npmjs.org"
22
+ },
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "exports": {
27
+ ".": {
28
+ "import": {
29
+ "types": "./dist/index.d.ts",
30
+ "default": "./dist/index.js"
31
+ },
32
+ "require": {
33
+ "types": "./dist/cjs/index.d.ts",
34
+ "default": "./dist/cjs/index.js"
35
+ }
36
+ }
37
+ },
38
+ "devDependencies": {
39
+ "@types/nodemailer": "^6.4.24",
40
+ "@haatlabs-core/vitest-config": "0.10.0",
41
+ "@haatlabs-core/tsconfig": "0.10.0"
42
+ },
43
+ "dependencies": {
44
+ "nodemailer": "^6.10.1",
45
+ "@haatlabs-core/errors": "0.10.0",
46
+ "@haatlabs-core/config-core": "0.10.0"
47
+ },
48
+ "scripts": {
49
+ "build": "rm -rf dist && tsc -p tsconfig.build.json && node ../../scripts/build-cjs.mjs",
50
+ "typecheck": "tsc --noEmit",
51
+ "test": "vitest run --typecheck",
52
+ "check:package": "publint --strict && attw --pack . --profile node16"
53
+ },
54
+ "platformRegistry": "npm"
55
+ }