@flowmails/flowmails-sdk 0.2.0 → 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 +47 -0
- package/README.md +77 -8
- package/dist/index.cjs +139 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +357 -12
- package/dist/index.d.ts +357 -12
- package/dist/index.js +139 -6
- package/dist/index.js.map +1 -1
- package/openapi.json +3023 -0
- package/package.json +4 -2
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 (
|
|
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,12 +28,79 @@ const result = await fm.send({
|
|
|
28
28
|
// html: "<p>Thanks for your order…</p>",
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
console.log(result); // { id: "
|
|
31
|
+
console.log(result); // { id: "8421", status: "sent", messageId: "<abc123@yourdomain.com>", … }
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
+
## A send the relay refused
|
|
35
|
+
|
|
36
|
+
`send()` answers `201` for a message the relay **refused** — the row
|
|
37
|
+
exists, the mail does not. The result carries the reason, so a failure
|
|
38
|
+
is never silent:
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
const result = await fm.send({ … });
|
|
42
|
+
if (result.status === "failed") {
|
|
43
|
+
console.error(result.error); // "send_failed" — always present
|
|
44
|
+
console.error(result.sendFeedback); // the relay's own reason, when it gave one
|
|
45
|
+
}
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
`result.error` is never `null` on a failed send: *"failed with no
|
|
49
|
+
reason"* is not a state the wire can be in.
|
|
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
|
+
|
|
86
|
+
## Is my domain able to send?
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const me = await fm.me();
|
|
90
|
+
if (me.sending && !me.sending.canSend) {
|
|
91
|
+
console.warn(me.sending.state); // "not_onboarded" | "unconfigured" | "unknown" | …
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`me.sending` is the platform's own verdict, read from the same document
|
|
96
|
+
the dashboard reads — `null` when it cannot be known (no bound domain,
|
|
97
|
+
or the platform could not be read). Read it together with `state`:
|
|
98
|
+
`canSend: false` with `state: "unknown"` means *"we have not looked"*,
|
|
99
|
+
not *"this domain cannot send"*.
|
|
100
|
+
|
|
34
101
|
## About Flowmails
|
|
35
102
|
|
|
36
|
-
[Flowmails](https://flowmails.net) is a
|
|
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.
|
|
37
104
|
|
|
38
105
|
## API keys
|
|
39
106
|
|
|
@@ -104,13 +171,15 @@ inside it cannot succeed.
|
|
|
104
171
|
|
|
105
172
|
## The read half
|
|
106
173
|
|
|
107
|
-
Sending is one half of the SDK; the other is **reading** —
|
|
108
|
-
|
|
109
|
-
|
|
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.
|
|
110
179
|
|
|
111
180
|
```ts
|
|
112
181
|
// Check the credential without sending anything.
|
|
113
|
-
const me = await fm.me(); // {
|
|
182
|
+
const me = await fm.me(); // { userId, domain, scopes[], sending }
|
|
114
183
|
|
|
115
184
|
// Tail the inbound log with a cursor (an event id, never a timestamp).
|
|
116
185
|
const page = await fm.events.list({ after: lastEventId });
|
|
@@ -133,7 +202,7 @@ The body is **fetched**, never pushed: an event carries the routing
|
|
|
133
202
|
facts and the subject line, and `fm.messages.get(messageId)` is the
|
|
134
203
|
ask. The SDK never hands you a message you did not ask for.
|
|
135
204
|
|
|
136
|
-
## Limitations (
|
|
205
|
+
## Limitations (v2.0)
|
|
137
206
|
|
|
138
207
|
- Custom domain (HTTP/HTTPS) attachment hosting is not configurable by SDK callers; the SDK backend always sends via R2.
|
|
139
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 ??
|
|
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;
|
|
@@ -90,6 +88,26 @@ var Flowmails = class {
|
|
|
90
88
|
* Returns `{ id, status, messageId }` on success; throws one of
|
|
91
89
|
* `AuthenticationError` / `ValidationError` / `RateLimitError` /
|
|
92
90
|
* `UpstreamError` on failure (all subclasses of `FlowmailsError`).
|
|
91
|
+
*
|
|
92
|
+
* **A send the relay refused is not a throw.** The backend answers
|
|
93
|
+
* `201` with `status: "failed"`, and the result carries the
|
|
94
|
+
* reason: `error` is the typed slug and `sendFeedback` is the
|
|
95
|
+
* relay's own words. Check `result.error` before treating a `201`
|
|
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
|
+
* ```
|
|
93
111
|
*/
|
|
94
112
|
async send(opts) {
|
|
95
113
|
const body = {
|
|
@@ -113,7 +131,13 @@ var Flowmails = class {
|
|
|
113
131
|
// It is deliberately **not** renamed: the backend's body is
|
|
114
132
|
// camelCase already, and a rename here would be one more place
|
|
115
133
|
// for the two sides to disagree.
|
|
116
|
-
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
|
|
117
141
|
};
|
|
118
142
|
return this.request(this.baseURL, "POST", "/api/v1/messages", {
|
|
119
143
|
body
|
|
@@ -128,12 +152,48 @@ var Flowmails = class {
|
|
|
128
152
|
* scopes — and it sends nothing, so running it can never mail
|
|
129
153
|
* somebody by accident.
|
|
130
154
|
*
|
|
155
|
+
* It also answers the question the 2026-10-31 audit (P0-1) found
|
|
156
|
+
* missing: **may this domain actually send?** — `info.sending`
|
|
157
|
+
* carries the platform's own verdict, read from the same document
|
|
158
|
+
* the dashboard reads.
|
|
159
|
+
*
|
|
160
|
+
* ```ts
|
|
161
|
+
* const me = await fm.me();
|
|
162
|
+
* if (me.sending && !me.sending.canSend) {
|
|
163
|
+
* // me.sending.configured: false means sending was never set up;
|
|
164
|
+
* // otherwise me.sending.state says why: "setup_required", …
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*
|
|
131
168
|
* @throws {AuthenticationError} when the key is missing, malformed
|
|
132
169
|
* or revoked.
|
|
133
170
|
*/
|
|
134
171
|
async me() {
|
|
135
172
|
return this.request(this.baseURL, "GET", "/api/v1/me");
|
|
136
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* The shared mail worker, and the version of it.
|
|
176
|
+
*
|
|
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
|
|
180
|
+
* Message-ID lookup, `1.6.0` the event reporting that fills the
|
|
181
|
+
* log this SDK reads. An agent whose worker is older than the
|
|
182
|
+
* platform expects sees an empty event log and has no way to tell
|
|
183
|
+
* "no mail yet" from "my worker cannot report".
|
|
184
|
+
*
|
|
185
|
+
* ```ts
|
|
186
|
+
* const fm = new Flowmails({ apiKey: process.env.FLOWMAILS_API_KEY! });
|
|
187
|
+
* const worker = await fm.workerVersion();
|
|
188
|
+
* if (!worker.version || worker.version < "1.6.0") { … }
|
|
189
|
+
* ```
|
|
190
|
+
*
|
|
191
|
+
* @throws {AuthenticationError} when the key is missing, malformed
|
|
192
|
+
* or revoked.
|
|
193
|
+
*/
|
|
194
|
+
async workerVersion() {
|
|
195
|
+
return this.request(this.baseURL, "GET", "/api/v1/worker-version");
|
|
196
|
+
}
|
|
137
197
|
/**
|
|
138
198
|
* The event log — the listen half of the surface, read with a
|
|
139
199
|
* cursor.
|
|
@@ -203,6 +263,79 @@ var Flowmails = class {
|
|
|
203
263
|
`/api/v1/messages/${encodeURIComponent(id)}`
|
|
204
264
|
);
|
|
205
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
|
+
},
|
|
206
339
|
/**
|
|
207
340
|
* Look back through the mailbox.
|
|
208
341
|
*
|
|
@@ -449,5 +582,6 @@ exports.FlowmailsError = FlowmailsError;
|
|
|
449
582
|
exports.RateLimitError = RateLimitError;
|
|
450
583
|
exports.UpstreamError = UpstreamError;
|
|
451
584
|
exports.ValidationError = ValidationError;
|
|
585
|
+
exports.WAIT_RESOLVED_EVENT = WAIT_RESOLVED_EVENT;
|
|
452
586
|
//# sourceMappingURL=index.cjs.map
|
|
453
587
|
//# sourceMappingURL=index.cjs.map
|