saci.cc 1.3.0 → 1.4.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/README.md CHANGED
@@ -126,6 +126,7 @@ Wait for an email and return its verification code, a link in it, or the whole e
126
126
  | `after` | Only emails received after this `Date` or ISO string |
127
127
  | `timeout` | Milliseconds to wait. Default `60000` |
128
128
  | `match` | `waitForLink` only: the link must contain this text or match a RegExp. Without it, the link to confirm the address or sign in (see `verificationLink`), or else the first link |
129
+ | `origin` | `waitForCode` only: the code must come from an [OTP-Token header](https://www.ietf.org/archive/id/draft-goto-otp-token-01.html) bound to this site, e.g. `"https://your-app.com"` or `"your-app.com"`. Emails without the header don't count, so use it only when your app sends it |
129
130
 
130
131
  An email looks like this:
131
132
 
@@ -138,6 +139,8 @@ An email looks like this:
138
139
  html: "<p>Your code is <b>482913</b></p>",
139
140
  text: "Your code is 482913",
140
141
  verificationCode: "482913", // or null
142
+ // The site the code is for, when the sender put it in an OTP-Token header; otherwise null
143
+ verificationCodeOrigin: null,
141
144
  // The link to confirm the address, sign in, activate the account or reset the password, or null
142
145
  verificationLink: "https://github.com/users/confirm_verification/7f3e...",
143
146
  links: ["https://github.com/users/confirm_verification/7f3e...", "https://github.com/settings/emails"],
@@ -182,6 +185,13 @@ try {
182
185
 
183
186
  The full HTTP API is documented at [saci.cc/en/docs](https://saci.cc/en/docs/). There's also an [MCP server](https://saci.cc/en/mcp/) for AI agents.
184
187
 
188
+ ## Guides
189
+
190
+ - [Test email verification in Playwright](https://saci.cc/en/blog/playwright-email-verification/)
191
+ - [Test password reset end to end](https://saci.cc/en/blog/password-reset-tests/)
192
+ - [Email tests in GitHub Actions: secrets, shards and limits](https://saci.cc/en/blog/github-actions-email-tests/)
193
+ - [Email verification in Selenium and Puppeteer](https://saci.cc/en/blog/selenium-puppeteer-email/)
194
+
185
195
  ## Development
186
196
 
187
197
  ```bash
package/dist/index.d.ts CHANGED
@@ -6,6 +6,8 @@ export interface EmailSummary {
6
6
  receivedAt?: string;
7
7
  /** The verification code, found by the API when the email arrived */
8
8
  verificationCode?: string | null;
9
+ /** The site the sender bound the code to in an OTP-Token header, e.g. "https://your-app.com" */
10
+ verificationCodeOrigin?: string | null;
9
11
  /** The link to confirm the address or sign in, found by the API when the email arrived */
10
12
  verificationLink?: string | null;
11
13
  }
@@ -22,8 +24,10 @@ export interface Attachment {
22
24
  export interface Email extends EmailSummary {
23
25
  html: string | null;
24
26
  text: string | null;
25
- /** The verification code found in the subject or body, if any */
27
+ /** The verification code from the OTP-Token header, subject or body, if any */
26
28
  verificationCode: string | null;
29
+ /** The site the sender bound the code to, when it came from an OTP-Token header */
30
+ verificationCodeOrigin: string | null;
27
31
  /** The link to click to confirm the address, sign in (magic link), activate the account or reset the password, if any */
28
32
  verificationLink: string | null;
29
33
  links: string[];
@@ -46,6 +50,13 @@ export interface WaitOptions {
46
50
  /** How long to wait, in milliseconds. Defaults to 60 seconds. */
47
51
  timeout?: number;
48
52
  }
53
+ export interface WaitForCodeOptions extends WaitOptions {
54
+ /**
55
+ * Only codes the sender bound to this site with an OTP-Token header, e.g.
56
+ * "https://your-app.com" or "your-app.com". Emails without the header don't count.
57
+ */
58
+ origin?: string;
59
+ }
49
60
  export interface WaitForLinkOptions extends WaitOptions {
50
61
  /**
51
62
  * Only links that contain this text or match this pattern. Without it, the link to confirm
@@ -103,7 +114,7 @@ export declare class Inbox {
103
114
  /** Waits for an email that matches the filters and returns it in full */
104
115
  waitForEmail(options?: WaitOptions): Promise<Email>;
105
116
  /** Waits for an email with a verification code and returns the code, e.g. "482913" */
106
- waitForCode(options?: WaitOptions): Promise<string>;
117
+ waitForCode(options?: WaitForCodeOptions): Promise<string>;
107
118
  /**
108
119
  * Waits for an email with a link and returns it: the one matching `match`, if given,
109
120
  * otherwise the link to confirm the address or sign in, or else the first link
package/dist/index.js CHANGED
@@ -29,6 +29,18 @@ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
29
29
  const matches = (value, filter) => filter === undefined ||
30
30
  (typeof filter === "string" ? value.toLowerCase().includes(filter.toLowerCase()) : filter.test(value));
31
31
  const timeOf = (date) => (date ? Date.parse(date) : NaN);
32
+ // "your-app.com" or "https://Your-App.com/login" as the API returns origins: "https://your-app.com"
33
+ function normalizeOrigin(input) {
34
+ const value = input.trim();
35
+ let origin = "null";
36
+ try {
37
+ origin = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(value) ? value : `https://${value}`).origin;
38
+ }
39
+ catch { }
40
+ if (origin === "null")
41
+ throw new TypeError(`origin must be a site like https://your-app.com, got ${JSON.stringify(input)}`);
42
+ return origin;
43
+ }
32
44
  export class Saci {
33
45
  baseUrl;
34
46
  fetch;
@@ -101,7 +113,9 @@ export class Inbox {
101
113
  }
102
114
  /** Waits for an email with a verification code and returns the code, e.g. "482913" */
103
115
  waitForCode(options = {}) {
104
- return this.waitFor(options, (email) => email.verificationCode, "verification code", (summary) => summary.verificationCode);
116
+ const origin = options.origin === undefined ? undefined : normalizeOrigin(options.origin);
117
+ const code = (email) => email.verificationCode && (!origin || email.verificationCodeOrigin === origin) ? email.verificationCode : null;
118
+ return this.waitFor(options, code, origin ? `verification code for ${origin}` : "verification code", code);
105
119
  }
106
120
  /**
107
121
  * Waits for an email with a link and returns it: the one matching `match`, if given,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saci.cc",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Temporary email inboxes for end-to-end tests: wait for verification codes and links from saci.cc",
5
5
  "keywords": [
6
6
  "temporary email",