@plexuslabs/simmail 0.0.0-stage → 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 +14 -0
- package/LICENSE +202 -0
- package/README.md +79 -2
- package/index.d.ts +112 -0
- package/index.js +111 -0
- package/package.json +28 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
First release.
|
|
6
|
+
|
|
7
|
+
- `waitForEmail`, `waitForReply`: wait for the next email to an address, or a person's reply; codes and magic links already pulled out.
|
|
8
|
+
- `extract`: codes and links from any text or HTML.
|
|
9
|
+
- `mailboxes`, `messages`, `message`: what is in the universe's mailboxes.
|
|
10
|
+
- `send` (with `inReplyTo`), `health` (the inspection report).
|
|
11
|
+
- `accounts`, `createAccount`: your app's SMTP/IMAP logins.
|
|
12
|
+
- `personas`, `createPersona`, `pushCase`.
|
|
13
|
+
- `clock`, `advance`, `freeze`, `speed`, `live`.
|
|
14
|
+
- Settings from `SIMMAIL_URL`, `SIMMAIL_API_KEY`, `SIMMAIL_UNIVERSE`; errors as `SimmailError` with the HTTP status.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @plexuslabs/simmail
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The official JavaScript client for [simmail](https://www.simmail.dev): a simulated email
|
|
4
|
+
world for testing apps and email agents. Wait for the email your app sends, pull out the
|
|
5
|
+
code or link, and get replies from simulated people, in a few lines of test code.
|
|
6
|
+
|
|
7
|
+
No dependencies; Node 18 or newer (it uses `fetch`). Full docs: https://www.simmail.dev/docs/
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install --save-dev @plexuslabs/simmail
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Setup
|
|
14
|
+
|
|
15
|
+
The client reads its settings from the environment (or pass them to `new Simmail({...})`):
|
|
16
|
+
|
|
17
|
+
| Variable | Meaning |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| `SIMMAIL_URL` | Your universe's server, such as `https://c1.simmail.dev` (shown on your account page). |
|
|
20
|
+
| `SIMMAIL_API_KEY` | An API key, `smk_…` (Organization → API keys). |
|
|
21
|
+
| `SIMMAIL_UNIVERSE` | Optional: only needed when the key could reach more than one universe. |
|
|
22
|
+
|
|
23
|
+
Your app sends through simmail's SMTP as usual (the dashboard's *Get started* shows the
|
|
24
|
+
settings); this client is for your tests.
|
|
25
|
+
|
|
26
|
+
## Wait for an email and use its code (Playwright)
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { test, expect } from "@playwright/test";
|
|
30
|
+
import { Simmail } from "@plexuslabs/simmail";
|
|
31
|
+
|
|
32
|
+
const mail = new Simmail();
|
|
33
|
+
|
|
34
|
+
test("new users can verify their email", async ({ page }) => {
|
|
35
|
+
const email = `user-${Date.now()}@yourcompany.test`;
|
|
36
|
+
await page.goto("/signup");
|
|
37
|
+
await page.fill("#email", email);
|
|
38
|
+
|
|
39
|
+
const waiting = mail.waitForEmail({ to: email, subject: "verify", timeout: "20s" });
|
|
40
|
+
await page.click("text=Create account");
|
|
41
|
+
const msg = await waiting;
|
|
42
|
+
|
|
43
|
+
expect(msg.codes).toHaveLength(1);
|
|
44
|
+
await page.fill("#code", msg.codes[0]); // or: await page.goto(msg.magic_links[0])
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Start the wait before the click: a wait only sees mail that arrives after it starts, unless
|
|
49
|
+
you pass `existing: true`.
|
|
50
|
+
|
|
51
|
+
## Read what is there
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
const inbox = await mail.messages("user@yourcompany.test"); // newest first
|
|
55
|
+
const latest = await mail.message("user@yourcompany.test", "INBOX", inbox[0].uid);
|
|
56
|
+
const report = await mail.health(latest.message_id); // spam score, links, compatibility
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## A conversation with a simulated person
|
|
60
|
+
|
|
61
|
+
```js
|
|
62
|
+
const sent = await mail.send({
|
|
63
|
+
from: "sales@yourcompany.test",
|
|
64
|
+
to: ["dana@harbor.test"],
|
|
65
|
+
subject: "Offer",
|
|
66
|
+
text: "Would you take 165k?",
|
|
67
|
+
});
|
|
68
|
+
const reply = await mail.waitForReply({ from: "dana@harbor.test", inReplyTo: sent.message_id });
|
|
69
|
+
console.log(reply.text);
|
|
70
|
+
|
|
71
|
+
await mail.advance("2d"); // move the simulated clock: follow-ups and nudges that were due happen
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Errors
|
|
75
|
+
|
|
76
|
+
A failed call throws `SimmailError` with the HTTP `status` and the server's message.
|
|
77
|
+
|
|
78
|
+
## License
|
|
79
|
+
|
|
80
|
+
Apache-2.0
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/** A message as the wait endpoints return it: with the codes and links pulled out. */
|
|
2
|
+
export interface Email {
|
|
3
|
+
address: string;
|
|
4
|
+
folder: string;
|
|
5
|
+
uid: number;
|
|
6
|
+
message_id: string;
|
|
7
|
+
in_reply_to?: string;
|
|
8
|
+
from: string;
|
|
9
|
+
from_name?: string;
|
|
10
|
+
to: string[];
|
|
11
|
+
subject: string;
|
|
12
|
+
date: string;
|
|
13
|
+
text: string;
|
|
14
|
+
html?: string;
|
|
15
|
+
links: string[];
|
|
16
|
+
/** One-time codes found in the text. */
|
|
17
|
+
codes: string[];
|
|
18
|
+
/** Links that verify, confirm, reset or sign in. */
|
|
19
|
+
magic_links: string[];
|
|
20
|
+
/** Written by a simulated person. */
|
|
21
|
+
by_persona?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A message in a mailbox listing (text shortened) or read whole. */
|
|
25
|
+
export interface Message {
|
|
26
|
+
uid: number;
|
|
27
|
+
folder: string;
|
|
28
|
+
flags: string[];
|
|
29
|
+
time: string;
|
|
30
|
+
size: number;
|
|
31
|
+
message_id: string;
|
|
32
|
+
in_reply_to?: string;
|
|
33
|
+
references?: string[];
|
|
34
|
+
from: string;
|
|
35
|
+
from_name?: string;
|
|
36
|
+
to?: string[];
|
|
37
|
+
cc?: string[];
|
|
38
|
+
subject: string;
|
|
39
|
+
date: string;
|
|
40
|
+
text: string;
|
|
41
|
+
html?: string;
|
|
42
|
+
headers?: Record<string, string>;
|
|
43
|
+
attachments?: { filename: string; content_type: string; size?: number }[];
|
|
44
|
+
format: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface WaitForEmail {
|
|
48
|
+
to: string;
|
|
49
|
+
from?: string;
|
|
50
|
+
subject?: string;
|
|
51
|
+
contains?: string;
|
|
52
|
+
folder?: string;
|
|
53
|
+
/** Also match messages already there. */
|
|
54
|
+
existing?: boolean;
|
|
55
|
+
/** Milliseconds, or a duration like "45s". Default 30s; at most 5m. */
|
|
56
|
+
timeout?: number | string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface WaitForReply {
|
|
60
|
+
/** The person's address. */
|
|
61
|
+
from: string;
|
|
62
|
+
/** Only a reply to this Message-ID. */
|
|
63
|
+
inReplyTo?: string;
|
|
64
|
+
/** Have them write their due reply now instead of on the simulated clock. Default true. */
|
|
65
|
+
fast?: boolean;
|
|
66
|
+
timeout?: number | string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface SendEmail {
|
|
70
|
+
from: string;
|
|
71
|
+
to: string[];
|
|
72
|
+
cc?: string[];
|
|
73
|
+
subject: string;
|
|
74
|
+
text?: string;
|
|
75
|
+
html?: string;
|
|
76
|
+
/** Thread this onto a conversation. */
|
|
77
|
+
inReplyTo?: string;
|
|
78
|
+
references?: string[];
|
|
79
|
+
attachments?: { filename: string; content_type: string; /** base64 */ data: string }[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class SimmailError extends Error {
|
|
83
|
+
status: number;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class Simmail {
|
|
87
|
+
/** Defaults: SIMMAIL_URL (such as https://c1.simmail.dev), SIMMAIL_API_KEY, SIMMAIL_UNIVERSE. */
|
|
88
|
+
constructor(opts?: { baseUrl?: string; apiKey?: string; universe?: string });
|
|
89
|
+
baseUrl: string;
|
|
90
|
+
request<T = unknown>(method: string, path: string, body?: unknown, query?: Record<string, unknown>): Promise<T>;
|
|
91
|
+
me(): Promise<Record<string, unknown>>;
|
|
92
|
+
waitForEmail(q: WaitForEmail): Promise<Email>;
|
|
93
|
+
waitForReply(q: WaitForReply): Promise<Email>;
|
|
94
|
+
extract(x: { text?: string; html?: string }): Promise<{ codes: string[]; links: string[]; magic_links: string[] }>;
|
|
95
|
+
mailboxes(): Promise<unknown[]>;
|
|
96
|
+
messages(address: string, opts?: { folder?: string }): Promise<Message[]>;
|
|
97
|
+
message(address: string, folder: string, uid: number): Promise<Message>;
|
|
98
|
+
health(messageId: string): Promise<Record<string, unknown>>;
|
|
99
|
+
send(msg: SendEmail): Promise<{ message_id: string; status: string }>;
|
|
100
|
+
accounts(): Promise<unknown[]>;
|
|
101
|
+
createAccount(address: string, password: string): Promise<unknown>;
|
|
102
|
+
personas(): Promise<unknown[]>;
|
|
103
|
+
createPersona(p: Record<string, unknown>): Promise<unknown>;
|
|
104
|
+
pushCase(id: string, kase: Record<string, unknown>): Promise<unknown>;
|
|
105
|
+
clock(): Promise<unknown>;
|
|
106
|
+
advance(duration: string): Promise<unknown>;
|
|
107
|
+
freeze(): Promise<unknown>;
|
|
108
|
+
speed(factor: number): Promise<unknown>;
|
|
109
|
+
live(): Promise<unknown>;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export default Simmail;
|
package/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// simmail client: wait for the email your app sends, pull out codes and
|
|
2
|
+
// links, and get replies from simulated people. No dependencies; Node 18+
|
|
3
|
+
// (uses fetch).
|
|
4
|
+
|
|
5
|
+
export class SimmailError extends Error {
|
|
6
|
+
constructor(status, message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "SimmailError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// A wait's timeout: milliseconds as a number, or a duration such as "45s".
|
|
14
|
+
const ms = v => (typeof v === "number" ? `${v}ms` : v || undefined);
|
|
15
|
+
const seg = s => encodeURIComponent(String(s));
|
|
16
|
+
|
|
17
|
+
export class Simmail {
|
|
18
|
+
/**
|
|
19
|
+
* @param {{baseUrl?: string, apiKey?: string, universe?: string}} opts
|
|
20
|
+
* Defaults: SIMMAIL_URL, SIMMAIL_API_KEY and SIMMAIL_UNIVERSE from the environment.
|
|
21
|
+
* baseUrl is the server's address, such as https://c1.simmail.dev (a trailing /api is fine too).
|
|
22
|
+
*/
|
|
23
|
+
constructor(opts = {}) {
|
|
24
|
+
const env = (typeof process !== "undefined" && process.env) || {};
|
|
25
|
+
this.baseUrl = (opts.baseUrl || env.SIMMAIL_URL || "http://127.0.0.1:8025").replace(/\/+$/, "").replace(/\/api$/, "");
|
|
26
|
+
this.apiKey = opts.apiKey || env.SIMMAIL_API_KEY || "";
|
|
27
|
+
this.universe = opts.universe || env.SIMMAIL_UNIVERSE || "";
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async request(method, path, body, query) {
|
|
31
|
+
const url = new URL(this.baseUrl + path);
|
|
32
|
+
for (const [k, v] of Object.entries(query || {})) if (v !== undefined && v !== null && v !== "") url.searchParams.set(k, String(v));
|
|
33
|
+
const headers = { Accept: "application/json" };
|
|
34
|
+
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
35
|
+
if (this.apiKey) headers.Authorization = `Bearer ${this.apiKey}`;
|
|
36
|
+
if (this.universe) headers["X-Simmail-Universe"] = this.universe;
|
|
37
|
+
const res = await fetch(url, { method, headers, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
38
|
+
const text = await res.text();
|
|
39
|
+
let data;
|
|
40
|
+
try { data = text ? JSON.parse(text) : null; } catch { data = text; }
|
|
41
|
+
if (!res.ok) throw new SimmailError(res.status, (data && data.error) || res.statusText);
|
|
42
|
+
return data;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Who the key is and which universe it belongs to. */
|
|
46
|
+
me() { return this.request("GET", "/api/me"); }
|
|
47
|
+
|
|
48
|
+
// ---- waiting for mail ----
|
|
49
|
+
|
|
50
|
+
/** The next email to an address (optionally from someone, with words in the subject or body). */
|
|
51
|
+
waitForEmail({ to, from, subject, contains, folder, existing, timeout = 30000 }) {
|
|
52
|
+
return this.request("GET", "/api/wait", undefined, { to, from, subject, contains, folder, existing, timeout: ms(timeout) });
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The next email a person sends, optionally in reply to a message. fast has them write it now. */
|
|
56
|
+
waitForReply({ from, inReplyTo, fast = true, timeout = 30000 }) {
|
|
57
|
+
return this.request("GET", "/api/wait/reply", undefined, { from, in_reply_to: inReplyTo, fast, timeout: ms(timeout) });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Codes and links from any text or HTML. */
|
|
61
|
+
extract({ text, html }) { return this.request("POST", "/api/extract", { text, html }); }
|
|
62
|
+
|
|
63
|
+
// ---- mailboxes and messages ----
|
|
64
|
+
|
|
65
|
+
/** Every mailbox in the universe: your app's logins and the people's addresses. */
|
|
66
|
+
mailboxes() { return this.request("GET", "/api/mailboxes"); }
|
|
67
|
+
|
|
68
|
+
/** A mailbox's messages, newest first (text shortened; message() has all of it). */
|
|
69
|
+
messages(address, { folder } = {}) {
|
|
70
|
+
return this.request("GET", `/api/mailboxes/${seg(address)}/messages`, undefined, { folder });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** One whole message. */
|
|
74
|
+
message(address, folder, uid) {
|
|
75
|
+
return this.request("GET", `/api/mailboxes/${seg(address)}/messages/${seg(folder)}/${seg(uid)}`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The health report of a message your app sent: spam score, links, compatibility. */
|
|
79
|
+
health(messageId) { return this.request("GET", "/api/inspect", undefined, { message_id: messageId }); }
|
|
80
|
+
|
|
81
|
+
/** Send an email as an account or mailbox (as the app would). */
|
|
82
|
+
send(msg) {
|
|
83
|
+
const { inReplyTo, ...rest } = msg;
|
|
84
|
+
return this.request("POST", "/api/send", inReplyTo ? { ...rest, in_reply_to: inReplyTo } : rest);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// ---- your app's mail logins ----
|
|
88
|
+
|
|
89
|
+
accounts() { return this.request("GET", "/api/accounts"); }
|
|
90
|
+
/** A login your app sends and reads with over SMTP and IMAP. */
|
|
91
|
+
createAccount(address, password) { return this.request("POST", "/api/accounts", { address, password }); }
|
|
92
|
+
|
|
93
|
+
// ---- people and stories ----
|
|
94
|
+
|
|
95
|
+
personas() { return this.request("GET", "/api/personas"); }
|
|
96
|
+
createPersona(p) { return this.request("POST", "/api/personas", p); }
|
|
97
|
+
/** Push what your system knows about a matter; the people in it learn the story. */
|
|
98
|
+
pushCase(id, kase) { return this.request("PUT", `/api/cases/${seg(id)}`, kase); }
|
|
99
|
+
|
|
100
|
+
// ---- the simulated clock ----
|
|
101
|
+
|
|
102
|
+
clock() { return this.request("GET", "/api/clock"); }
|
|
103
|
+
/** Move time on, such as "2d"; what was due happens. */
|
|
104
|
+
advance(duration) { return this.request("POST", "/api/clock/advance", { duration }); }
|
|
105
|
+
freeze() { return this.request("POST", "/api/clock/freeze", {}); }
|
|
106
|
+
speed(factor) { return this.request("POST", "/api/clock/speed", { factor }); }
|
|
107
|
+
/** Back to real time. */
|
|
108
|
+
live() { return this.request("POST", "/api/clock/live", {}); }
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export default Simmail;
|
package/package.json
CHANGED
|
@@ -1,6 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plexuslabs/simmail",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Client for simmail: wait for the email your app sends, pull out codes and links, and get replies from simulated people.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./index.d.ts",
|
|
11
|
+
"default": "./index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"files": ["index.js", "index.d.ts", "README.md", "CHANGELOG.md", "LICENSE"],
|
|
15
|
+
"engines": { "node": ">=18" },
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test test/*.test.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["email", "testing", "smtp", "e2e", "playwright", "simmail", "email-testing", "magic-link", "otp"],
|
|
20
|
+
"license": "Apache-2.0",
|
|
21
|
+
"author": "Plexus Labs",
|
|
22
|
+
"homepage": "https://www.simmail.dev/docs/",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/PlexusLabsDev/simmail-sdks.git",
|
|
26
|
+
"directory": "js"
|
|
27
|
+
},
|
|
28
|
+
"bugs": { "url": "https://github.com/PlexusLabsDev/simmail-sdks/issues" },
|
|
29
|
+
"publishConfig": { "access": "public", "provenance": true }
|
|
30
|
+
}
|