saci.cc 1.0.0 → 1.2.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
@@ -2,12 +2,18 @@
2
2
 
3
3
  Temporary email inboxes for end-to-end tests. Create an address on [saci.cc](https://saci.cc), use it to sign up in your app, and wait for the verification code or confirmation link.
4
4
 
5
- Free, no account, no API key. No dependencies; works in Node 18+, Bun, Deno and browsers.
5
+ Free. No dependencies; works in Node 18+, Bun, Deno and browsers.
6
6
 
7
7
  ```bash
8
8
  npm install --save-dev saci.cc
9
9
  ```
10
10
 
11
+ The API needs a key: sign in with GitHub at [saci.cc/en/account](https://saci.cc/en/account/), create one, and put it in the `SACI_API_KEY` environment variable (in CI, as a secret). The client reads it from there.
12
+
13
+ ```bash
14
+ export SACI_API_KEY=saci_...
15
+ ```
16
+
11
17
  ```ts
12
18
  import { createInbox } from "saci.cc";
13
19
 
@@ -17,6 +23,8 @@ console.log(inbox.address); // a1b2c3d4e5f60718@saci.cc
17
23
  // ...sign up with inbox.address...
18
24
 
19
25
  const code = await inbox.waitForCode(); // "482913"
26
+ // or, when the email has a button instead of a code:
27
+ const link = await inbox.waitForLink(); // "https://your-app.com/verify?token=..."
20
28
  ```
21
29
 
22
30
  ## Playwright
@@ -105,7 +113,7 @@ it("signs up with a verification code", () => {
105
113
 
106
114
  ### `createInbox(options?)`
107
115
 
108
- Creates an inbox and returns an `Inbox`. Without `name` the address is random (16 hex characters), so nobody else can guess it. With `name: "signup-test"` you get `signup-test@saci.cc`, which anyone can read.
116
+ Creates an inbox and returns an `Inbox`. Without `name` the address is random (16 hex characters). With `name: "signup-test"` you get `signup-test@saci.cc`. Either way the inbox belongs to your API key's account for 7 days: only that account can read it, and nobody else can create the same name (a `SaciError` with status 409).
109
117
 
110
118
  ### `inbox.waitForCode(options?)`, `inbox.waitForLink(options?)`, `inbox.waitForEmail(options?)`
111
119
 
@@ -117,7 +125,7 @@ Wait for an email and return its verification code, a link in it, or the whole e
117
125
  | `subject` | Only emails whose subject contains this text (case-insensitive) or matches a RegExp |
118
126
  | `after` | Only emails received after this `Date` or ISO string |
119
127
  | `timeout` | Milliseconds to wait. Default `60000` |
120
- | `match` | `waitForLink` only: the link must contain this text or match a RegExp. Without it, the first link |
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 |
121
129
 
122
130
  An email looks like this:
123
131
 
@@ -130,7 +138,9 @@ An email looks like this:
130
138
  html: "<p>Your code is <b>482913</b></p>",
131
139
  text: "Your code is 482913",
132
140
  verificationCode: "482913", // or null
133
- links: ["https://github.com/settings/emails"],
141
+ // The link to confirm the address, sign in, activate the account or reset the password, or null
142
+ verificationLink: "https://github.com/users/confirm_verification/7f3e...",
143
+ links: ["https://github.com/users/confirm_verification/7f3e...", "https://github.com/settings/emails"],
134
144
  attachments: [{ index: 0, filename: "invoice.pdf", contentType: "application/pdf", size: 48213, inline: false, url: "https://..." }],
135
145
  }
136
146
  ```
@@ -138,19 +148,21 @@ An email looks like this:
138
148
  ### Other methods
139
149
 
140
150
  - `inbox.address`: the email address (`String(inbox)` gives the same).
141
- - `inbox.list()`: summaries of the emails in the inbox, newest first.
151
+ - `inbox.list()`: summaries of the emails in the inbox, newest first, with `verificationCode` and `verificationLink`.
142
152
  - `inbox.get(messageId)`: one email in full.
143
153
  - `inbox.delete(messageId)` and `inbox.clear()`: delete one email or all of them.
144
154
  - `new Saci().inbox(address)`: an `Inbox` for an address created earlier.
155
+ - `new Saci({ apiKey })`: pass the key instead of using `SACI_API_KEY`.
145
156
  - `new Saci({ baseUrl, fetch })`: point at another deployment or pass your own `fetch`.
146
157
 
147
- Failed requests throw `SaciError`, which has the HTTP `status`.
158
+ Failed requests throw `SaciError`, which has the HTTP `status`. A missing or revoked key gives a 401.
148
159
 
149
160
  ## Limits
150
161
 
151
- - 120 requests per minute per IP. The client waits and retries when it gets a 429.
162
+ - 120 requests per minute per API key's account. The client waits and retries when it gets a 429.
152
163
  - Emails are deleted about a day after they arrive.
153
- - There are no passwords: anyone who knows an address can read its inbox. Don't use it for anything sensitive.
164
+ - Inboxes created with your key are private to your account for 7 days. Still, it's a service for tests and sign-ups: don't use it for anything sensitive.
165
+ - Want to be told instead of waiting? Set a webhook on [saci.cc/en/account](https://saci.cc/en/account/) and each email is POSTed to your URL ([format](https://saci.cc/en/docs/#webhooks)).
154
166
  - Addresses only receive email.
155
167
 
156
168
  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.
package/dist/index.d.ts CHANGED
@@ -4,6 +4,10 @@ export interface EmailSummary {
4
4
  from: string;
5
5
  subject: string;
6
6
  receivedAt?: string;
7
+ /** The verification code, found by the API when the email arrived */
8
+ verificationCode?: string | null;
9
+ /** The link to confirm the address or sign in, found by the API when the email arrived */
10
+ verificationLink?: string | null;
7
11
  }
8
12
  export interface Attachment {
9
13
  index: number;
@@ -20,10 +24,14 @@ export interface Email extends EmailSummary {
20
24
  text: string | null;
21
25
  /** The verification code found in the subject or body, if any */
22
26
  verificationCode: string | null;
27
+ /** The link to click to confirm the address, sign in (magic link), activate the account or reset the password, if any */
28
+ verificationLink: string | null;
23
29
  links: string[];
24
30
  attachments: Attachment[];
25
31
  }
26
32
  export interface SaciOptions {
33
+ /** Your API key (saci_...), from https://saci.cc/en/account/. Defaults to the SACI_API_KEY environment variable. */
34
+ apiKey?: string;
27
35
  /** Defaults to https://saci.cc/api */
28
36
  baseUrl?: string;
29
37
  fetch?: typeof fetch;
@@ -39,7 +47,10 @@ export interface WaitOptions {
39
47
  timeout?: number;
40
48
  }
41
49
  export interface WaitForLinkOptions extends WaitOptions {
42
- /** Only links that contain this text or match this pattern. Without it, the first link. */
50
+ /**
51
+ * Only links that contain this text or match this pattern. Without it, the link to confirm
52
+ * the address or sign in, or else the first link.
53
+ */
43
54
  match?: string | RegExp;
44
55
  }
45
56
  export declare class SaciError extends Error {
@@ -52,6 +63,7 @@ export declare class SaciTimeoutError extends Error {
52
63
  export declare class Saci {
53
64
  readonly baseUrl: string;
54
65
  private readonly fetch;
66
+ private readonly apiKey;
55
67
  constructor(options?: SaciOptions);
56
68
  /** Creates an inbox. Without a name the address is random, which keeps it private. */
57
69
  createInbox(options?: {
@@ -77,7 +89,10 @@ export declare class Inbox {
77
89
  waitForEmail(options?: WaitOptions): Promise<Email>;
78
90
  /** Waits for an email with a verification code and returns the code, e.g. "482913" */
79
91
  waitForCode(options?: WaitOptions): Promise<string>;
80
- /** Waits for an email with a link (matching `match`, if given) and returns the link */
92
+ /**
93
+ * Waits for an email with a link and returns it: the one matching `match`, if given,
94
+ * otherwise the link to confirm the address or sign in, or else the first link
95
+ */
81
96
  waitForLink(options?: WaitForLinkOptions): Promise<string>;
82
97
  private waitFor;
83
98
  toString(): string;
package/dist/index.js CHANGED
@@ -5,6 +5,8 @@ export const DEFAULT_BASE_URL = "https://saci.cc/api";
5
5
  const MAX_WAIT_SECONDS = 25;
6
6
  const DEFAULT_TIMEOUT_MS = 60_000;
7
7
  const MAX_RATE_LIMIT_RETRIES = 3;
8
+ // Read on every request, so it can be set after this module is imported
9
+ const envApiKey = () => globalThis.process?.env?.SACI_API_KEY;
8
10
  export class SaciError extends Error {
9
11
  status;
10
12
  constructor(message, status) {
@@ -26,7 +28,9 @@ const timeOf = (date) => (date ? Date.parse(date) : NaN);
26
28
  export class Saci {
27
29
  baseUrl;
28
30
  fetch;
31
+ apiKey;
29
32
  constructor(options = {}) {
33
+ this.apiKey = options.apiKey;
30
34
  this.baseUrl = (options.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
31
35
  this.fetch = options.fetch ?? ((...args) => globalThis.fetch(...args));
32
36
  }
@@ -43,8 +47,12 @@ export class Saci {
43
47
  /** @internal */
44
48
  async request(method, path) {
45
49
  for (let attempt = 0;; attempt++) {
46
- const res = await this.fetch(this.baseUrl + path, { method });
47
- // Over the per-IP rate limit: wait as long as the API asks, then try again
50
+ const apiKey = this.apiKey ?? envApiKey();
51
+ const res = await this.fetch(this.baseUrl + path, {
52
+ method,
53
+ headers: apiKey ? { Authorization: `Bearer ${apiKey}` } : undefined,
54
+ });
55
+ // Over the rate limit: wait as long as the API asks, then try again
48
56
  if (res.status === 429 && attempt < MAX_RATE_LIMIT_RETRIES) {
49
57
  const retryAfter = Number(res.headers.get("retry-after") ?? NaN);
50
58
  await sleep((Number.isFinite(retryAfter) ? retryAfter : 5) * 1000);
@@ -88,15 +96,21 @@ export class Inbox {
88
96
  }
89
97
  /** Waits for an email with a verification code and returns the code, e.g. "482913" */
90
98
  waitForCode(options = {}) {
91
- return this.waitFor(options, (email) => email.verificationCode, "verification code");
99
+ return this.waitFor(options, (email) => email.verificationCode, "verification code", (summary) => summary.verificationCode);
92
100
  }
93
- /** Waits for an email with a link (matching `match`, if given) and returns the link */
101
+ /**
102
+ * Waits for an email with a link and returns it: the one matching `match`, if given,
103
+ * otherwise the link to confirm the address or sign in, or else the first link
104
+ */
94
105
  waitForLink(options = {}) {
95
- return this.waitFor(options, (email) => email.links.find((link) => matches(link, options.match)) ?? null, options.match === undefined ? "link" : `link matching ${options.match}`);
106
+ const { match } = options;
107
+ const verification = (email) => email.verificationLink && matches(email.verificationLink, match) ? email.verificationLink : null;
108
+ return this.waitFor(options, (email) => verification(email) ?? email.links.find((link) => matches(link, match)) ?? null, match === undefined ? "link" : `link matching ${match}`, verification);
96
109
  }
97
110
  // Long-polls the inbox until `pick` finds something in an email that passes the filters.
98
- // Each email is opened at most once; newer emails are checked first.
99
- async waitFor(options, pick, what) {
111
+ // Each email is opened at most once; newer emails are checked first. The list already has
112
+ // the code and link the API found, so `fromSummary` can answer without opening the email.
113
+ async waitFor(options, pick, what, fromSummary) {
100
114
  const deadline = Date.now() + (options.timeout ?? DEFAULT_TIMEOUT_MS);
101
115
  const after = options.after === undefined ? NaN : new Date(options.after).getTime();
102
116
  const checked = new Set();
@@ -116,7 +130,7 @@ export class Inbox {
116
130
  continue;
117
131
  if (!matches(summary.from, options.from) || !matches(summary.subject, options.subject))
118
132
  continue;
119
- const found = pick(await this.get(summary.messageId));
133
+ const found = fromSummary?.(summary) ?? pick(await this.get(summary.messageId));
120
134
  if (found !== null && found !== undefined)
121
135
  return found;
122
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saci.cc",
3
- "version": "1.0.0",
3
+ "version": "1.2.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",