saci.cc 1.0.0 → 1.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/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
 
@@ -142,13 +148,14 @@ An email looks like this:
142
148
  - `inbox.get(messageId)`: one email in full.
143
149
  - `inbox.delete(messageId)` and `inbox.clear()`: delete one email or all of them.
144
150
  - `new Saci().inbox(address)`: an `Inbox` for an address created earlier.
151
+ - `new Saci({ apiKey })`: pass the key instead of using `SACI_API_KEY`.
145
152
  - `new Saci({ baseUrl, fetch })`: point at another deployment or pass your own `fetch`.
146
153
 
147
- Failed requests throw `SaciError`, which has the HTTP `status`.
154
+ Failed requests throw `SaciError`, which has the HTTP `status`. A missing or revoked key gives a 401.
148
155
 
149
156
  ## Limits
150
157
 
151
- - 120 requests per minute per IP. The client waits and retries when it gets a 429.
158
+ - 120 requests per minute per API key's account. The client waits and retries when it gets a 429.
152
159
  - Emails are deleted about a day after they arrive.
153
160
  - There are no passwords: anyone who knows an address can read its inbox. Don't use it for anything sensitive.
154
161
  - Addresses only receive email.
package/dist/index.d.ts CHANGED
@@ -24,6 +24,8 @@ export interface Email extends EmailSummary {
24
24
  attachments: Attachment[];
25
25
  }
26
26
  export interface SaciOptions {
27
+ /** Your API key (saci_...), from https://saci.cc/en/account/. Defaults to the SACI_API_KEY environment variable. */
28
+ apiKey?: string;
27
29
  /** Defaults to https://saci.cc/api */
28
30
  baseUrl?: string;
29
31
  fetch?: typeof fetch;
@@ -52,6 +54,7 @@ export declare class SaciTimeoutError extends Error {
52
54
  export declare class Saci {
53
55
  readonly baseUrl: string;
54
56
  private readonly fetch;
57
+ private readonly apiKey;
55
58
  constructor(options?: SaciOptions);
56
59
  /** Creates an inbox. Without a name the address is random, which keeps it private. */
57
60
  createInbox(options?: {
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "saci.cc",
3
- "version": "1.0.0",
3
+ "version": "1.1.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",