@venturekit/notify 0.0.0-dev.20260507015349
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/LICENSE +191 -0
- package/README.md +304 -0
- package/dist/admin/index.d.ts +88 -0
- package/dist/admin/index.d.ts.map +1 -0
- package/dist/admin/index.js +105 -0
- package/dist/admin/index.js.map +1 -0
- package/dist/bounce-handler.d.ts +65 -0
- package/dist/bounce-handler.d.ts.map +1 -0
- package/dist/bounce-handler.js +171 -0
- package/dist/bounce-handler.js.map +1 -0
- package/dist/client.d.ts +113 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +213 -0
- package/dist/client.js.map +1 -0
- package/dist/dispatcher.d.ts +95 -0
- package/dist/dispatcher.d.ts.map +1 -0
- package/dist/dispatcher.js +251 -0
- package/dist/dispatcher.js.map +1 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +66 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/email-noop.d.ts +30 -0
- package/dist/providers/email-noop.d.ts.map +1 -0
- package/dist/providers/email-noop.js +39 -0
- package/dist/providers/email-noop.js.map +1 -0
- package/dist/providers/email-ses.d.ts +39 -0
- package/dist/providers/email-ses.d.ts.map +1 -0
- package/dist/providers/email-ses.js +114 -0
- package/dist/providers/email-ses.js.map +1 -0
- package/dist/providers/index.d.ts +20 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.js +20 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/push-noop.d.ts +12 -0
- package/dist/providers/push-noop.d.ts.map +1 -0
- package/dist/providers/push-noop.js +30 -0
- package/dist/providers/push-noop.js.map +1 -0
- package/dist/providers/sms-noop.d.ts +21 -0
- package/dist/providers/sms-noop.d.ts.map +1 -0
- package/dist/providers/sms-noop.js +42 -0
- package/dist/providers/sms-noop.js.map +1 -0
- package/dist/providers/types.d.ts +95 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +40 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/providers/whatsapp-meta.d.ts +63 -0
- package/dist/providers/whatsapp-meta.d.ts.map +1 -0
- package/dist/providers/whatsapp-meta.js +160 -0
- package/dist/providers/whatsapp-meta.js.map +1 -0
- package/dist/store/index.d.ts +6 -0
- package/dist/store/index.d.ts.map +1 -0
- package/dist/store/index.js +5 -0
- package/dist/store/index.js.map +1 -0
- package/dist/store/postgres.d.ts +36 -0
- package/dist/store/postgres.d.ts.map +1 -0
- package/dist/store/postgres.js +354 -0
- package/dist/store/postgres.js.map +1 -0
- package/dist/store/types.d.ts +139 -0
- package/dist/store/types.d.ts.map +1 -0
- package/dist/store/types.js +14 -0
- package/dist/store/types.js.map +1 -0
- package/dist/templates/registry.d.ts +55 -0
- package/dist/templates/registry.d.ts.map +1 -0
- package/dist/templates/registry.js +94 -0
- package/dist/templates/registry.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +12 -0
- package/dist/types.js.map +1 -0
- package/migrations/vk_notify_001_init.sql +109 -0
- package/package.json +109 -0
- package/runtime/bounce-handler-stub.ts +35 -0
- package/runtime/dispatcher-stub.ts +63 -0
- package/runtime/tsconfig.json +19 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @venturekit/notify — admin route helpers
|
|
3
|
+
*
|
|
4
|
+
* Building blocks for the `/admin/notifications/*` API surface. Phase 1
|
|
5
|
+
* exposes pure functions consumers wire into their own route handlers
|
|
6
|
+
* (so they can pick their own auth/scope conventions); a future phase
|
|
7
|
+
* may ship a fully-baked `mountAdminRoutes(api, scope)` helper.
|
|
8
|
+
*
|
|
9
|
+
* Each helper takes a {@link NotifyClient} (which carries the store
|
|
10
|
+
* and templates) plus the request input, and returns a JSON-ready
|
|
11
|
+
* value. The HTTP shell (status code, scope check) stays in the
|
|
12
|
+
* consumer's hands.
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* // src/routes/admin/notifications/list/get.ts
|
|
16
|
+
* import { handler } from '@venturekit/runtime';
|
|
17
|
+
* import { adminListNotifications } from '@venturekit/notify/admin';
|
|
18
|
+
* import { notify } from '@/lib/notify';
|
|
19
|
+
*
|
|
20
|
+
* export const main = handler(async (ctx) => {
|
|
21
|
+
* ctx.requireScope('admin.notifications.read');
|
|
22
|
+
* const url = new URL(ctx.request.url);
|
|
23
|
+
* return adminListNotifications(notify, {
|
|
24
|
+
* limit: Number(url.searchParams.get('limit') ?? 50),
|
|
25
|
+
* offset: Number(url.searchParams.get('offset') ?? 0),
|
|
26
|
+
* channel: url.searchParams.get('channel') as never,
|
|
27
|
+
* status: url.searchParams.get('status') as never,
|
|
28
|
+
* search: url.searchParams.get('q') ?? undefined,
|
|
29
|
+
* });
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
/**
|
|
34
|
+
* List outbox rows (admin view). Accepts every filter from
|
|
35
|
+
* {@link AdminListOptions}. Always returns a `total` count so the UI
|
|
36
|
+
* can render pagination controls.
|
|
37
|
+
*/
|
|
38
|
+
export async function adminListNotifications(client, options = {}) {
|
|
39
|
+
const { rows, total } = await client.store.adminList(options);
|
|
40
|
+
return {
|
|
41
|
+
rows,
|
|
42
|
+
total,
|
|
43
|
+
limit: options.limit ?? 50,
|
|
44
|
+
offset: options.offset ?? 0,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// ── Single row ──────────────────────────────────────────────────────
|
|
48
|
+
export async function adminGetNotification(client, id) {
|
|
49
|
+
return client.store.findById(id);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Admin-driven send. Equivalent to `client.enqueue(...)` — exposed as a
|
|
53
|
+
* named admin helper so route handlers can audit-log the operator who
|
|
54
|
+
* triggered it without reaching into the client API directly.
|
|
55
|
+
*/
|
|
56
|
+
export async function adminSendNotification(client, input) {
|
|
57
|
+
const result = await client.enqueue(input);
|
|
58
|
+
return {
|
|
59
|
+
id: result.id,
|
|
60
|
+
channel: result.channel,
|
|
61
|
+
suppressed: result.suppressed,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
// ── Retry / cancel ──────────────────────────────────────────────────
|
|
65
|
+
/**
|
|
66
|
+
* Move a `'failed'` / `'bounced'` row back to `'pending'`. Returns
|
|
67
|
+
* `null` when the row isn't found or wasn't in a retryable state.
|
|
68
|
+
*/
|
|
69
|
+
export async function adminRetryNotification(client, id) {
|
|
70
|
+
return client.store.retry(id);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Cancel a `'pending'` row before the dispatcher claims it. Returns
|
|
74
|
+
* `null` when the row isn't found or has already left `'pending'`.
|
|
75
|
+
*/
|
|
76
|
+
export async function adminCancelNotification(client, id) {
|
|
77
|
+
return client.store.cancel(id);
|
|
78
|
+
}
|
|
79
|
+
// ── Suppression list ────────────────────────────────────────────────
|
|
80
|
+
/**
|
|
81
|
+
* Lift a hard-bounce / complaint suppression. Use sparingly — SES
|
|
82
|
+
* complaint feedback is reliable and re-sending after a complaint is
|
|
83
|
+
* a fast path to deliverability problems.
|
|
84
|
+
*/
|
|
85
|
+
export async function adminClearSuppression(client, channel, address) {
|
|
86
|
+
await client.store.clearSuppression(channel, address);
|
|
87
|
+
}
|
|
88
|
+
// ── Preferences ─────────────────────────────────────────────────────
|
|
89
|
+
/** List a user's stored opt-in/opt-out preferences. */
|
|
90
|
+
export async function listUserPreferences(client, userId) {
|
|
91
|
+
return client.store.listPreferences(userId);
|
|
92
|
+
}
|
|
93
|
+
/** Upsert a single `(user, channel, category)` preference. */
|
|
94
|
+
export async function upsertUserPreference(client, input) {
|
|
95
|
+
return client.store.upsertPreference(input);
|
|
96
|
+
}
|
|
97
|
+
// ── Per-user inbox view ─────────────────────────────────────────────
|
|
98
|
+
/**
|
|
99
|
+
* List a user's notifications (newest first). Used by the buyer-portal
|
|
100
|
+
* inbox in projects like Groupi.
|
|
101
|
+
*/
|
|
102
|
+
export async function listNotificationsForUser(client, userId, options = {}) {
|
|
103
|
+
return client.store.listForUser(userId, options);
|
|
104
|
+
}
|
|
105
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/admin/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AAyBH;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,MAAoB,EACpB,UAA4B,EAAE;IAE9B,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,MAAM,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC9D,OAAO;QACL,IAAI;QACJ,KAAK;QACL,KAAK,EAAE,OAAO,CAAC,KAAK,IAAI,EAAE;QAC1B,MAAM,EAAE,OAAO,CAAC,MAAM,IAAI,CAAC;KAC5B,CAAC;AACJ,CAAC;AAED,uEAAuE;AAEvE,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAoB,EACpB,EAAU;IAEV,OAAO,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC;AACnC,CAAC;AAMD;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,MAAoB,EACpB,KAAqB;IAErB,MAAM,MAAM,GAAG,MAAM,MAAM,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;IAC3C,OAAO;QACL,EAAE,EAAE,MAAM,CAAC,EAAE;QACb,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,UAAU,EAAE,MAAM,CAAC,UAAU;KAC9B,CAAC;AACJ,CAAC;AAED,uEAAuE;AAEvE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,MAAoB,EACpB,EAAU;IAEV,OAAO,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;AAChC,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,uBAAuB,CAC3C,MAAoB,EACpB,EAAU;IAEV,OAAO,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;AACjC,CAAC;AAED,uEAAuE;AAEvE;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,MAAoB,EACpB,OAAsB,EACtB,OAAe;IAEf,MAAM,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;AACxD,CAAC;AAED,uEAAuE;AAEvE,uDAAuD;AACvD,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACvC,MAAoB,EACpB,MAAc;IAEd,OAAO,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,MAAM,CAAC,CAAC;AAC9C,CAAC;AAED,8DAA8D;AAC9D,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,MAAoB,EACpB,KAAsD;IAEtD,OAAO,MAAM,CAAC,KAAK,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC;AAC9C,CAAC;AAED,uEAAuE;AAEvE;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,wBAAwB,CAC5C,MAAoB,EACpB,MAAc,EACd,UAA8B,EAAE;IAEhC,OAAO,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AACnD,CAAC"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @venturekit/notify — SES bounce / complaint event handler
|
|
3
|
+
*
|
|
4
|
+
* Subscribed to the SNS topic that receives event-destination
|
|
5
|
+
* notifications from the SES configuration set
|
|
6
|
+
* (`createNotify()` in `@venturekit/infra` wires this).
|
|
7
|
+
*
|
|
8
|
+
* Event shapes follow SES v2 publishing format:
|
|
9
|
+
*
|
|
10
|
+
* ```json
|
|
11
|
+
* {
|
|
12
|
+
* "eventType": "Bounce" | "Complaint" | "Delivery" | "Open" | "Click",
|
|
13
|
+
* "mail": {
|
|
14
|
+
* "messageId": "...",
|
|
15
|
+
* "tags": { "venturekit_notification_id": ["notif_..."] }
|
|
16
|
+
* },
|
|
17
|
+
* "bounce": { "bounceType": "Permanent" | "Transient", "bouncedRecipients": [...] },
|
|
18
|
+
* "complaint": { "complainedRecipients": [...] }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The handler maps each event into a `notification_event_log` row with
|
|
23
|
+
* `suppress = true` for `Bounce.Permanent` and `Complaint`. Future
|
|
24
|
+
* sends to that address are then short-circuited by
|
|
25
|
+
* `NotifyStore.isAddressSuppressed`.
|
|
26
|
+
*
|
|
27
|
+
* Tolerates malformed events (missing fields, unknown event types):
|
|
28
|
+
* we log + skip rather than throw, because SNS retries failed
|
|
29
|
+
* deliveries which would only re-enter the same failure mode.
|
|
30
|
+
*/
|
|
31
|
+
import type { NotifyClient } from './client.js';
|
|
32
|
+
/** SNS event the Lambda receives from the SES events topic. */
|
|
33
|
+
interface SnsEvent {
|
|
34
|
+
Records: Array<{
|
|
35
|
+
EventSource?: string;
|
|
36
|
+
Sns?: {
|
|
37
|
+
Message: string;
|
|
38
|
+
MessageId?: string;
|
|
39
|
+
Subject?: string;
|
|
40
|
+
};
|
|
41
|
+
}>;
|
|
42
|
+
}
|
|
43
|
+
/** Per-record outcome — useful for tests + log inspection. */
|
|
44
|
+
export interface BounceHandlerOutcome {
|
|
45
|
+
ok: boolean;
|
|
46
|
+
/** Reason for skipping when `ok === false`. */
|
|
47
|
+
reason?: string;
|
|
48
|
+
/** Address recorded (when `ok === true`). */
|
|
49
|
+
address?: string;
|
|
50
|
+
/** Event type recorded. */
|
|
51
|
+
eventType?: string;
|
|
52
|
+
/** Whether `suppress` was set on the row. */
|
|
53
|
+
suppressed?: boolean;
|
|
54
|
+
/** SES message id. */
|
|
55
|
+
providerMessageId?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Build the bounce-handler Lambda function. The infra package wires
|
|
59
|
+
* this via a synth-time stub file.
|
|
60
|
+
*/
|
|
61
|
+
export declare function makeBounceHandler(getClient: () => NotifyClient | Promise<NotifyClient>): (event: SnsEvent) => Promise<{
|
|
62
|
+
outcomes: BounceHandlerOutcome[];
|
|
63
|
+
}>;
|
|
64
|
+
export {};
|
|
65
|
+
//# sourceMappingURL=bounce-handler.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounce-handler.d.ts","sourceRoot":"","sources":["../src/bounce-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,+DAA+D;AAC/D,UAAU,QAAQ;IAChB,OAAO,EAAE,KAAK,CAAC;QACb,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,GAAG,CAAC,EAAE;YACJ,OAAO,EAAE,MAAM,CAAC;YAChB,SAAS,CAAC,EAAE,MAAM,CAAC;YACnB,OAAO,CAAC,EAAE,MAAM,CAAC;SAClB,CAAC;KACH,CAAC,CAAC;CACJ;AAkBD,8DAA8D;AAC9D,MAAM,WAAW,oBAAoB;IACnC,EAAE,EAAE,OAAO,CAAC;IACZ,+CAA+C;IAC/C,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,6CAA6C;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2BAA2B;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6CAA6C;IAC7C,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,sBAAsB;IACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,SAAS,EAAE,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,CAAC,GACpD,CAAC,KAAK,EAAE,QAAQ,KAAK,OAAO,CAAC;IAAE,QAAQ,EAAE,oBAAoB,EAAE,CAAA;CAAE,CAAC,CA2IpE"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @venturekit/notify — SES bounce / complaint event handler
|
|
3
|
+
*
|
|
4
|
+
* Subscribed to the SNS topic that receives event-destination
|
|
5
|
+
* notifications from the SES configuration set
|
|
6
|
+
* (`createNotify()` in `@venturekit/infra` wires this).
|
|
7
|
+
*
|
|
8
|
+
* Event shapes follow SES v2 publishing format:
|
|
9
|
+
*
|
|
10
|
+
* ```json
|
|
11
|
+
* {
|
|
12
|
+
* "eventType": "Bounce" | "Complaint" | "Delivery" | "Open" | "Click",
|
|
13
|
+
* "mail": {
|
|
14
|
+
* "messageId": "...",
|
|
15
|
+
* "tags": { "venturekit_notification_id": ["notif_..."] }
|
|
16
|
+
* },
|
|
17
|
+
* "bounce": { "bounceType": "Permanent" | "Transient", "bouncedRecipients": [...] },
|
|
18
|
+
* "complaint": { "complainedRecipients": [...] }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* The handler maps each event into a `notification_event_log` row with
|
|
23
|
+
* `suppress = true` for `Bounce.Permanent` and `Complaint`. Future
|
|
24
|
+
* sends to that address are then short-circuited by
|
|
25
|
+
* `NotifyStore.isAddressSuppressed`.
|
|
26
|
+
*
|
|
27
|
+
* Tolerates malformed events (missing fields, unknown event types):
|
|
28
|
+
* we log + skip rather than throw, because SNS retries failed
|
|
29
|
+
* deliveries which would only re-enter the same failure mode.
|
|
30
|
+
*/
|
|
31
|
+
/**
|
|
32
|
+
* Build the bounce-handler Lambda function. The infra package wires
|
|
33
|
+
* this via a synth-time stub file.
|
|
34
|
+
*/
|
|
35
|
+
export function makeBounceHandler(getClient) {
|
|
36
|
+
return async (event) => {
|
|
37
|
+
const client = await getClient();
|
|
38
|
+
const outcomes = [];
|
|
39
|
+
for (const record of event.Records ?? []) {
|
|
40
|
+
const sns = record.Sns;
|
|
41
|
+
if (!sns?.Message) {
|
|
42
|
+
outcomes.push({ ok: false, reason: 'missing-sns-message' });
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
let parsed;
|
|
46
|
+
try {
|
|
47
|
+
parsed = JSON.parse(sns.Message);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
outcomes.push({ ok: false, reason: 'sns-message-not-json' });
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const eventType = parsed.eventType;
|
|
54
|
+
if (!eventType) {
|
|
55
|
+
outcomes.push({ ok: false, reason: 'missing-eventType' });
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const providerMessageId = parsed.mail?.messageId ?? null;
|
|
59
|
+
const channel = 'email';
|
|
60
|
+
// Pull the outbox row id back out of the SES tags. SES wraps
|
|
61
|
+
// every tag value in a single-element array.
|
|
62
|
+
const notificationId = parsed.mail?.tags?.['venturekit_notification_id']?.[0] ?? null;
|
|
63
|
+
switch (eventType) {
|
|
64
|
+
case 'Bounce': {
|
|
65
|
+
const recipients = parsed.bounce?.bouncedRecipients ?? [];
|
|
66
|
+
const isPermanent = parsed.bounce?.bounceType === 'Permanent';
|
|
67
|
+
for (const r of recipients) {
|
|
68
|
+
if (!r.emailAddress)
|
|
69
|
+
continue;
|
|
70
|
+
await client.store.recordEvent({
|
|
71
|
+
channel,
|
|
72
|
+
address: r.emailAddress,
|
|
73
|
+
eventType: isPermanent ? 'bounce' : 'soft-bounce',
|
|
74
|
+
providerMessageId,
|
|
75
|
+
suppress: isPermanent,
|
|
76
|
+
raw: parsed,
|
|
77
|
+
});
|
|
78
|
+
// Update the originating outbox row when we can correlate
|
|
79
|
+
// it. SES sometimes delivers the event before the original
|
|
80
|
+
// SendEmail's response landed in the outbox row, so a
|
|
81
|
+
// missing row is not necessarily an error.
|
|
82
|
+
if (notificationId && isPermanent) {
|
|
83
|
+
await client.store
|
|
84
|
+
.updateStatus({
|
|
85
|
+
id: notificationId,
|
|
86
|
+
status: 'bounced',
|
|
87
|
+
error: `hard bounce on ${r.emailAddress}`,
|
|
88
|
+
})
|
|
89
|
+
.catch(() => null);
|
|
90
|
+
}
|
|
91
|
+
outcomes.push({
|
|
92
|
+
ok: true,
|
|
93
|
+
address: r.emailAddress,
|
|
94
|
+
eventType: isPermanent ? 'bounce' : 'soft-bounce',
|
|
95
|
+
suppressed: isPermanent,
|
|
96
|
+
providerMessageId: providerMessageId ?? undefined,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
case 'Complaint': {
|
|
102
|
+
const recipients = parsed.complaint?.complainedRecipients ?? [];
|
|
103
|
+
for (const r of recipients) {
|
|
104
|
+
if (!r.emailAddress)
|
|
105
|
+
continue;
|
|
106
|
+
await client.store.recordEvent({
|
|
107
|
+
channel,
|
|
108
|
+
address: r.emailAddress,
|
|
109
|
+
eventType: 'complaint',
|
|
110
|
+
providerMessageId,
|
|
111
|
+
suppress: true,
|
|
112
|
+
raw: parsed,
|
|
113
|
+
});
|
|
114
|
+
if (notificationId) {
|
|
115
|
+
await client.store
|
|
116
|
+
.updateStatus({
|
|
117
|
+
id: notificationId,
|
|
118
|
+
status: 'bounced',
|
|
119
|
+
error: `complaint reported by ${r.emailAddress}`,
|
|
120
|
+
})
|
|
121
|
+
.catch(() => null);
|
|
122
|
+
}
|
|
123
|
+
outcomes.push({
|
|
124
|
+
ok: true,
|
|
125
|
+
address: r.emailAddress,
|
|
126
|
+
eventType: 'complaint',
|
|
127
|
+
suppressed: true,
|
|
128
|
+
providerMessageId: providerMessageId ?? undefined,
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
case 'Delivery':
|
|
134
|
+
case 'Open':
|
|
135
|
+
case 'Click': {
|
|
136
|
+
// Informational events — record without suppressing.
|
|
137
|
+
// We log them per-recipient when the data is available; in
|
|
138
|
+
// practice SES only includes one recipient per event for these.
|
|
139
|
+
const lcEvent = eventType.toLowerCase();
|
|
140
|
+
// Address may come from `delivery.recipients[0]` etc., but
|
|
141
|
+
// shapes vary by event type. Fall back to omitting address
|
|
142
|
+
// when the parser didn't catch it — the row still records
|
|
143
|
+
// the event for observability.
|
|
144
|
+
await client.store.recordEvent({
|
|
145
|
+
channel,
|
|
146
|
+
address: parsed.mail?.tags?.['ses_recipient']?.[0] ?? '',
|
|
147
|
+
eventType: lcEvent,
|
|
148
|
+
providerMessageId,
|
|
149
|
+
suppress: false,
|
|
150
|
+
raw: parsed,
|
|
151
|
+
});
|
|
152
|
+
outcomes.push({
|
|
153
|
+
ok: true,
|
|
154
|
+
eventType: lcEvent,
|
|
155
|
+
suppressed: false,
|
|
156
|
+
providerMessageId: providerMessageId ?? undefined,
|
|
157
|
+
});
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
default:
|
|
161
|
+
outcomes.push({
|
|
162
|
+
ok: false,
|
|
163
|
+
reason: `unknown event type '${eventType}'`,
|
|
164
|
+
eventType,
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return { outcomes };
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=bounce-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bounce-handler.js","sourceRoot":"","sources":["../src/bounce-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAgDH;;;GAGG;AACH,MAAM,UAAU,iBAAiB,CAC/B,SAAqD;IAErD,OAAO,KAAK,EAAE,KAAe,EAAE,EAAE;QAC/B,MAAM,MAAM,GAAG,MAAM,SAAS,EAAE,CAAC;QACjC,MAAM,QAAQ,GAA2B,EAAE,CAAC;QAE5C,KAAK,MAAM,MAAM,IAAI,KAAK,CAAC,OAAO,IAAI,EAAE,EAAE,CAAC;YACzC,MAAM,GAAG,GAAG,MAAM,CAAC,GAAG,CAAC;YACvB,IAAI,CAAC,GAAG,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AAiB,EAAE,iBAAiB,IAAI,SAAS;qBAClD,CAAC,CAAC;oBACH,MAAM;gBACR,CAAC;gBACD;oBACE,QAAQ,CAAC,IAAI,CAAC;wBACZ,EAAE,EAAE,KAAK;wBACT,MAAM,EAAE,uBAAuB,SAAS,GAAG;wBAC3C,SAAS;qBACV,CAAC,CAAC;YACP,CAAC;QACH,CAAC;QAED,OAAO,EAAE,QAAQ,EAAE,CAAC;IACtB,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @venturekit/notify — runtime client
|
|
3
|
+
*
|
|
4
|
+
* The runtime API consumers use:
|
|
5
|
+
* - `notify.enqueue({ recipientUserId, channel, templateKey, payload })`
|
|
6
|
+
* writes a row to the outbox.
|
|
7
|
+
* - `notify.registerTemplate({...})` adds a render function.
|
|
8
|
+
* - `notify.registerProvider(provider)` swaps in a custom provider
|
|
9
|
+
* for a given channel (e.g. wire MailHog locally, Twilio for SMS).
|
|
10
|
+
*
|
|
11
|
+
* The client never sends synchronously — the dispatcher cron picks rows
|
|
12
|
+
* up. Synchronous send is technically possible (skip the outbox by
|
|
13
|
+
* calling provider.send directly) but Phase 1 deliberately doesn't
|
|
14
|
+
* expose that path: the outbox is what makes retries, audit logging,
|
|
15
|
+
* and bounce-suppression work, so bypassing it loses too much value.
|
|
16
|
+
*
|
|
17
|
+
* For a project that genuinely doesn't need an outbox (small marketing
|
|
18
|
+
* site, dev preview), set `NotifyIntent.tracking = 'none'`. The infra
|
|
19
|
+
* package wires a memory-backed store in that mode and the dispatcher
|
|
20
|
+
* is provisioned but does nothing — the next-tick re-pickup window
|
|
21
|
+
* collapses to "synchronous-ish" because each call ends up dispatching
|
|
22
|
+
* within seconds.
|
|
23
|
+
*/
|
|
24
|
+
import { type TemplateRegistry } from './templates/registry.js';
|
|
25
|
+
import type { NotifyStore } from './store/types.js';
|
|
26
|
+
import type { NotifyProvider } from './providers/types.js';
|
|
27
|
+
import type { EnqueueInput, EnqueueResult, NotifyChannel, NotifyTemplate } from './types.js';
|
|
28
|
+
/**
|
|
29
|
+
* Resolves a logical user id to a channel-specific address. Consumers
|
|
30
|
+
* provide this when they keep contact info in their own `users` table
|
|
31
|
+
* (the common case). The dispatcher calls it on every send for which
|
|
32
|
+
* `recipientUserId` is set and `address` is null.
|
|
33
|
+
*
|
|
34
|
+
* Returning `null` means "this user has no address on this channel" —
|
|
35
|
+
* the dispatcher marks the row `'failed'` with reason
|
|
36
|
+
* `'no-address'` and never retries.
|
|
37
|
+
*/
|
|
38
|
+
export type AddressResolver = (userId: string, channel: NotifyChannel) => Promise<string | null>;
|
|
39
|
+
/** Construction options for {@link createNotifyClient}. */
|
|
40
|
+
export interface CreateNotifyClientOptions {
|
|
41
|
+
/** Outbox + preferences + suppression store. */
|
|
42
|
+
store: NotifyStore;
|
|
43
|
+
/** Default `From:` address for emails. */
|
|
44
|
+
defaultFrom?: string;
|
|
45
|
+
/** Provider factories keyed by channel. The dispatcher resolves one
|
|
46
|
+
* provider per channel; providers are constructed lazily on first
|
|
47
|
+
* use. */
|
|
48
|
+
providers?: NotifyProvider[];
|
|
49
|
+
/** User-id -> address resolver (see {@link AddressResolver}). */
|
|
50
|
+
addressResolver?: AddressResolver;
|
|
51
|
+
/** Pre-populated template registry (rare — usually you call
|
|
52
|
+
* `client.registerTemplate(...)` after construction). */
|
|
53
|
+
templates?: TemplateRegistry;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The user-facing client surface. Returned by
|
|
57
|
+
* {@link createNotifyClient} and re-exposed at module scope as
|
|
58
|
+
* `notify` for the common single-instance case.
|
|
59
|
+
*/
|
|
60
|
+
export interface NotifyClient {
|
|
61
|
+
/**
|
|
62
|
+
* Persist a new outbox row. The dispatcher will pick it up on the
|
|
63
|
+
* next cron tick. Returns synchronously — provider calls happen
|
|
64
|
+
* later, asynchronously. Use {@link NotifyClient.enqueue} for the
|
|
65
|
+
* 99% case (transactional notifications).
|
|
66
|
+
*/
|
|
67
|
+
enqueue(input: EnqueueInput): Promise<EnqueueResult>;
|
|
68
|
+
/** Register a template at boot time. */
|
|
69
|
+
registerTemplate<P>(template: NotifyTemplate<P>): void;
|
|
70
|
+
/** Register many templates at once. */
|
|
71
|
+
registerTemplates(templates: NotifyTemplate<any>[]): void;
|
|
72
|
+
/** Register / override a channel provider. */
|
|
73
|
+
registerProvider(provider: NotifyProvider): void;
|
|
74
|
+
/** Read access to the configured store (for admin handlers). */
|
|
75
|
+
store: NotifyStore;
|
|
76
|
+
/** Read access to the template registry. */
|
|
77
|
+
templates: TemplateRegistry;
|
|
78
|
+
/** Read access to the provider map. */
|
|
79
|
+
providers: ReadonlyMap<NotifyChannel, NotifyProvider>;
|
|
80
|
+
/** Default `From:` address, if any. */
|
|
81
|
+
defaultFrom?: string;
|
|
82
|
+
/** Address resolver, if registered. */
|
|
83
|
+
addressResolver?: AddressResolver;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Create a notify client. Most projects construct one at module scope:
|
|
87
|
+
*
|
|
88
|
+
* ```ts
|
|
89
|
+
* // src/lib/notify.ts
|
|
90
|
+
* import { createNotifyClient, createPostgresNotifyStore,
|
|
91
|
+
* createSesEmailProvider, createMetaWhatsAppProvider,
|
|
92
|
+
* createSecretsTokenResolver } from '@venturekit/notify';
|
|
93
|
+
*
|
|
94
|
+
* export const notify = createNotifyClient({
|
|
95
|
+
* store: createPostgresNotifyStore(),
|
|
96
|
+
* defaultFrom: process.env.VENTURE_NOTIFY_FROM,
|
|
97
|
+
* providers: [
|
|
98
|
+
* createSesEmailProvider({
|
|
99
|
+
* region: process.env.AWS_REGION!,
|
|
100
|
+
* configurationSetName: process.env.VENTURE_NOTIFY_CONFIG_SET,
|
|
101
|
+
* }),
|
|
102
|
+
* createMetaWhatsAppProvider({
|
|
103
|
+
* phoneNumberId: process.env.VENTURE_NOTIFY_WHATSAPP_PHONE_ID!,
|
|
104
|
+
* tokenResolver: createSecretsTokenResolver({
|
|
105
|
+
* secretArn: process.env.VENTURE_NOTIFY_WHATSAPP_TOKEN_SECRET!,
|
|
106
|
+
* }),
|
|
107
|
+
* }),
|
|
108
|
+
* ],
|
|
109
|
+
* });
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
export declare function createNotifyClient(options: CreateNotifyClientOptions): NotifyClient;
|
|
113
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,OAAO,EAEL,KAAK,gBAAgB,EACtB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,KAAK,EACV,YAAY,EACZ,aAAa,EACb,aAAa,EACb,cAAc,EAEf,MAAM,YAAY,CAAC;AAEpB;;;;;;;;;GASG;AACH,MAAM,MAAM,eAAe,GAAG,CAC5B,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,aAAa,KACnB,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;AAE5B,2DAA2D;AAC3D,MAAM,WAAW,yBAAyB;IACxC,gDAAgD;IAChD,KAAK,EAAE,WAAW,CAAC;IACnB,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB;;eAEW;IACX,SAAS,CAAC,EAAE,cAAc,EAAE,CAAC;IAC7B,iEAAiE;IACjE,eAAe,CAAC,EAAE,eAAe,CAAC;IAClC;8DAC0D;IAC1D,SAAS,CAAC,EAAE,gBAAgB,CAAC;CAC9B;AAED;;;;GAIG;AACH,MAAM,WAAW,YAAY;IAC3B;;;;;OAKG;IACH,OAAO,CAAC,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC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