@flowmails/flowmails-sdk 0.2.1 → 2.0.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 ADDED
@@ -0,0 +1,47 @@
1
+ # Changelog
2
+
3
+ ## 2.0.0 — the worker's address is gone
4
+
5
+ **Breaking change.** One field was removed from two responses, and one
6
+ optional field was removed from the credential record the platform
7
+ writes.
8
+
9
+ ### Removed
10
+
11
+ - `GET /api/v1/me` no longer answers `workerHost`.
12
+ - `GET /api/v1/worker-version` no longer answers `workerHost`, and it
13
+ **no longer 404s** when the deployment publishes no address — it
14
+ answers `{ version, bootstrapped, lastDeployAt, lastDeployError }`
15
+ from the platform's own probe of the shared worker.
16
+ - `AccountInfo.workerHost` and `WorkerVersionInfo.workerHost` are
17
+ gone from the TypeScript types.
18
+
19
+ ### Why
20
+
21
+ The shared mail worker is ONE platform-owned deployment, reached
22
+ through the `MAIL_WORKER` service binding — it needs no address and no
23
+ bearer. The address was therefore the same string for every tenant and
24
+ carried no per-credential information: a per-row copy of a deployment
25
+ fact, kept alive only because the old per-account worker model still
26
+ had a place for it.
27
+
28
+ It also cost something real. A deployment whose worker publishes no
29
+ public hostname (the shared worker runs with `workers_dev = false`, so
30
+ it has none) could not issue an API key at all: creating one failed
31
+ with *"the shared mail worker is not configured on this deployment"*,
32
+ while the key's row was already written — leaving a key in the
33
+ dashboard that authenticated nothing and still occupied one slot of
34
+ the plan's `maxApiKeys` quota. And `/api/v1/worker-version` answered
35
+ `404 worker_not_found` on exactly those deployments, making a working
36
+ platform look broken.
37
+
38
+ ### Migrating
39
+
40
+ - If you read `me().workerHost` or `workerVersion().workerHost`, drop
41
+ it. There is no replacement value: the worker is the same for every
42
+ tenant, and its **version** — `workerVersion().version` — is the fact
43
+ worth reporting.
44
+ - If you compared `workerHost` against a configured address to decide
45
+ whether the platform was configured, that check no longer exists (and
46
+ no longer needs to): a key that authenticates is the proof that the
47
+ platform is configured.
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  The official JavaScript / TypeScript SDK for the **Flowmails SDK backend** (`https://sdk.flowmails.net`).
4
4
 
5
- > **Status: public preview (v0.2.x)** — published from the Flowmails monorepo. Wire format and surface can still change in v0.x; pin the minor version (`~0.2`) in production.
5
+ > **Status: public preview (v2.0.x)** — published from the Flowmails monorepo. v2.0 is a breaking release: the shared worker's address is gone from `fm.me()` and `fm.workerVersion()` (see `CHANGELOG.md`). The wire format and surface are stable within the 2.x line; pin the minor version (`~2.0`) in production.
6
6
 
7
7
  ## Install
8
8
 
@@ -28,7 +28,7 @@ const result = await fm.send({
28
28
  // html: "<p>Thanks for your order…</p>",
29
29
  });
30
30
 
31
- console.log(result); // { id: "msg_8421", status: "queued" }
31
+ console.log(result); // { id: "8421", status: "sent", messageId: "<abc123@yourdomain.com>", … }
32
32
  ```
33
33
 
34
34
  ## A send the relay refused
@@ -48,6 +48,41 @@ if (result.status === "failed") {
48
48
  `result.error` is never `null` on a failed send: *"failed with no
49
49
  reason"* is not a state the wire can be in.
50
50
 
51
+ ## Several recipients, and the partial case
52
+
53
+ Each recipient gets their **own** message by default — they cannot see
54
+ each other, and one refused address does not hold up the others. So the
55
+ relay can accept some and refuse others, and the answer says so:
56
+
57
+ ```ts
58
+ const result = await fm.send({
59
+ from: "support@yourdomain.com",
60
+ to: ["a@example.com", "b@example.com", "c@example.com"],
61
+ subject: "Order #1234 confirmed",
62
+ text: "…",
63
+ });
64
+
65
+ if (result.status === "partial") {
66
+ // Some recipients got it and some were refused. `error` is still
67
+ // set — check it before treating a 201 as "sent".
68
+ for (const r of result.recipients ?? []) {
69
+ if (r.status === "failed") {
70
+ console.error(`${r.to}: ${r.sendFeedback}`); // retry only these
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
76
+ `status` is the aggregate over every recipient — `"sent"`, `"partial"`
77
+ or `"failed"` — and `recipients` carries what happened to **each**
78
+ address: its row id, its own verdict and the relay's reason when it
79
+ was refused.
80
+
81
+ Pass `perRecipient: false` to put everybody on **one** shared envelope
82
+ instead: one message, one relay call, one bill from a provider that
83
+ charges per message — and then the send is all-or-nothing by
84
+ construction.
85
+
51
86
  ## Is my domain able to send?
52
87
 
53
88
  ```ts
@@ -65,7 +100,7 @@ not *"this domain cannot send"*.
65
100
 
66
101
  ## About Flowmails
67
102
 
68
- [Flowmails](https://flowmails.net) is a Cloudflare-native email platform: custom-domain mail (inbox, outbox, and routing rules) that lives in your own D1, with a REST API and webhooks on top. The dashboard, docs, and blog live at **<https://flowmails.net>** — sign up there to mint an API key and bind a domain before using this SDK.
103
+ [Flowmails](https://flowmails.net) is a custom-domain email platform: mail (inbox, outbox, and routing rules) held in the platform's own storage and scoped to your domain, with a REST API and webhooks on top. Inbound routing and outbound sending are provider-pluggable — you connect your domain provider over OAuth and pick the outbound channel per domain (Cloudflare Email Sending or Resend). The dashboard, docs, and blog live at **<https://flowmails.net>** — sign up there to mint an API key and bind a domain before using this SDK.
69
104
 
70
105
  ## API keys
71
106
 
@@ -136,9 +171,11 @@ inside it cannot succeed.
136
171
 
137
172
  ## The read half
138
173
 
139
- Sending is one half of the SDK; the other is **reading** — and it
140
- talks to a different origin (the platform worker, not the SDK
141
- backend), so a self-hosted install sets `platformURL` too.
174
+ Sending is one half of the SDK; the other is **reading** — the event
175
+ log, the durable wait and the message read. It used to talk to a
176
+ different origin (the platform worker); the agent platform moved to
177
+ the SDK backend, so the read half is on the **same** host as the send
178
+ half now.
142
179
 
143
180
  ```ts
144
181
  // Check the credential without sending anything.
@@ -165,7 +202,7 @@ The body is **fetched**, never pushed: an event carries the routing
165
202
  facts and the subject line, and `fm.messages.get(messageId)` is the
166
203
  ask. The SDK never hands you a message you did not ask for.
167
204
 
168
- ## Limitations (v0.2)
205
+ ## Limitations (v2.0)
169
206
 
170
207
  - Custom domain (HTTP/HTTPS) attachment hosting is not configurable by SDK callers; the SDK backend always sends via R2.
171
208
  - The webhook delivery-status events (`message.delivered` / `message.bounced`) do not exist yet — and neither does bounce detection. The receive worker has no DSN ingestion, so a send's status is `sent` (the relay accepted it) or `failed` (the relay rejected it) and nothing more. The vocabulary holds `delivered` / `bounced` / `complained` / `delayed`, but nothing writes them.
package/dist/index.cjs CHANGED
@@ -55,6 +55,7 @@ var UpstreamError = class extends FlowmailsError {
55
55
  };
56
56
 
57
57
  // src/client.ts
58
+ var WAIT_RESOLVED_EVENT = "wait.resolved";
58
59
  var Flowmails = class {
59
60
  apiKey;
60
61
  baseURL;
@@ -71,10 +72,7 @@ var Flowmails = class {
71
72
  /\/+$/,
72
73
  ""
73
74
  );
74
- this.platformURL = (opts.platformURL ?? "https://flowmails.net").replace(
75
- /\/+$/,
76
- ""
77
- );
75
+ this.platformURL = (opts.platformURL ?? this.baseURL).replace(/\/+$/, "");
78
76
  this.fetchFn = opts.fetch ?? globalThis.fetch.bind(globalThis);
79
77
  this.maxRetries = opts.maxRetries ?? 2;
80
78
  this.requestId = opts.requestId;
@@ -96,6 +94,20 @@ var Flowmails = class {
96
94
  * reason: `error` is the typed slug and `sendFeedback` is the
97
95
  * relay's own words. Check `result.error` before treating a `201`
98
96
  * as "sent".
97
+ *
98
+ * **A partial send is not a success either.** Several recipients
99
+ * each get their own message, and the relay can refuse some of them.
100
+ * `status` is then `"partial"` — `error` is still set, and
101
+ * `result.recipients` says exactly which addresses were refused and
102
+ * why:
103
+ *
104
+ * ```ts
105
+ * const result = await fm.send({ from, to: ["a@x.test", "b@x.test"], subject, text });
106
+ * if (result.status === "partial") {
107
+ * const refused = result.recipients?.filter((r) => r.status === "failed") ?? [];
108
+ * // retry only these
109
+ * }
110
+ * ```
99
111
  */
100
112
  async send(opts) {
101
113
  const body = {
@@ -119,7 +131,13 @@ var Flowmails = class {
119
131
  // It is deliberately **not** renamed: the backend's body is
120
132
  // camelCase already, and a rename here would be one more place
121
133
  // for the two sides to disagree.
122
- idempotencyKey: opts.idempotencyKey
134
+ idempotencyKey: opts.idempotencyKey,
135
+ // The delivery mode travels under the same name as on the wire.
136
+ // It is deliberately **not** defaulted here: the backend owns the
137
+ // default, so a new SDK talking to an older backend and an old
138
+ // SDK talking to a new backend both behave the way the backend
139
+ // says they should.
140
+ perRecipient: opts.perRecipient
123
141
  };
124
142
  return this.request(this.baseURL, "POST", "/api/v1/messages", {
125
143
  body
@@ -142,7 +160,8 @@ var Flowmails = class {
142
160
  * ```ts
143
161
  * const me = await fm.me();
144
162
  * if (me.sending && !me.sending.canSend) {
145
- * // me.sending.state says why: "not_onboarded", "unknown", …
163
+ * // me.sending.configured: false means sending was never set up;
164
+ * // otherwise me.sending.state says why: "setup_required", …
146
165
  * }
147
166
  * ```
148
167
  *
@@ -153,10 +172,11 @@ var Flowmails = class {
153
172
  return this.request(this.baseURL, "GET", "/api/v1/me");
154
173
  }
155
174
  /**
156
- * The account's receive worker, and the version of it.
175
+ * The shared mail worker, and the version of it.
157
176
  *
158
- * The receive worker does the actual receiving and sending, and it
159
- * is versioned: `1.4.0` brought the route tables, `1.5.1` the
177
+ * The mail worker does the actual receiving and sending — it is
178
+ * Flowmails' own deployment, one shared worker for every tenant —
179
+ * and it is versioned: `1.4.0` brought the route tables, `1.5.1` the
160
180
  * Message-ID lookup, `1.6.0` the event reporting that fills the
161
181
  * log this SDK reads. An agent whose worker is older than the
162
182
  * platform expects sees an empty event log and has no way to tell
@@ -243,6 +263,79 @@ var Flowmails = class {
243
263
  `/api/v1/messages/${encodeURIComponent(id)}`
244
264
  );
245
265
  },
266
+ /**
267
+ * Claim a message for processing.
268
+ *
269
+ * The event log says a mail arrived; it does not say whether the
270
+ * agent already handled it. This writes that fact down, so a
271
+ * restarted agent can tell "I never saw this" from "I saw it and
272
+ * never finished".
273
+ *
274
+ * The claim is a **mutex, not a decision**: the platform records
275
+ * that this credential is working on the message and refuses a
276
+ * second live claim by another credential of the same account. It
277
+ * does not judge whether the mail matters and does not act on it.
278
+ *
279
+ * ```ts
280
+ * await fm.messages.claim(event.messageId, { domainId });
281
+ * // …process…
282
+ * await fm.messages.complete(event.messageId, { domainId, state: "done" });
283
+ * ```
284
+ *
285
+ * @throws {ValidationError} when `messageId` or `domainId` is
286
+ * missing.
287
+ */
288
+ claim: async (messageId, opts) => {
289
+ if (!messageId) {
290
+ throw new ValidationError("message_id_required", "messageId is required");
291
+ }
292
+ if (!opts?.domainId) {
293
+ throw new ValidationError("domain_id_required", "domainId is required");
294
+ }
295
+ return this.request(
296
+ this.platformURL,
297
+ "POST",
298
+ `/api/agent/messages/${encodeURIComponent(messageId)}/claim`,
299
+ { body: { domainId: opts.domainId, claimedBy: opts.claimedBy } }
300
+ );
301
+ },
302
+ /**
303
+ * Record how a message turned out.
304
+ *
305
+ * `done` or `failed`, optionally with labels and a note. The
306
+ * platform stores what the agent said and takes no further action
307
+ * on it.
308
+ *
309
+ * @throws {ValidationError} when `messageId`, `domainId` or
310
+ * `state` is missing, or `state` is neither `done` nor `failed`.
311
+ */
312
+ complete: async (messageId, opts) => {
313
+ if (!messageId) {
314
+ throw new ValidationError("message_id_required", "messageId is required");
315
+ }
316
+ if (!opts?.domainId) {
317
+ throw new ValidationError("domain_id_required", "domainId is required");
318
+ }
319
+ if (opts.state !== "done" && opts.state !== "failed") {
320
+ throw new ValidationError(
321
+ "invalid_state",
322
+ "state must be 'done' or 'failed'"
323
+ );
324
+ }
325
+ return this.request(
326
+ this.platformURL,
327
+ "POST",
328
+ `/api/agent/messages/${encodeURIComponent(messageId)}/complete`,
329
+ {
330
+ body: {
331
+ domainId: opts.domainId,
332
+ state: opts.state,
333
+ labels: opts.labels,
334
+ note: opts.note
335
+ }
336
+ }
337
+ );
338
+ },
246
339
  /**
247
340
  * Look back through the mailbox.
248
341
  *
@@ -489,5 +582,6 @@ exports.FlowmailsError = FlowmailsError;
489
582
  exports.RateLimitError = RateLimitError;
490
583
  exports.UpstreamError = UpstreamError;
491
584
  exports.ValidationError = ValidationError;
585
+ exports.WAIT_RESOLVED_EVENT = WAIT_RESOLVED_EVENT;
492
586
  //# sourceMappingURL=index.cjs.map
493
587
  //# sourceMappingURL=index.cjs.map