@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.
Files changed (75) hide show
  1. package/LICENSE +191 -0
  2. package/README.md +304 -0
  3. package/dist/admin/index.d.ts +88 -0
  4. package/dist/admin/index.d.ts.map +1 -0
  5. package/dist/admin/index.js +105 -0
  6. package/dist/admin/index.js.map +1 -0
  7. package/dist/bounce-handler.d.ts +65 -0
  8. package/dist/bounce-handler.d.ts.map +1 -0
  9. package/dist/bounce-handler.js +171 -0
  10. package/dist/bounce-handler.js.map +1 -0
  11. package/dist/client.d.ts +113 -0
  12. package/dist/client.d.ts.map +1 -0
  13. package/dist/client.js +213 -0
  14. package/dist/client.js.map +1 -0
  15. package/dist/dispatcher.d.ts +95 -0
  16. package/dist/dispatcher.d.ts.map +1 -0
  17. package/dist/dispatcher.js +251 -0
  18. package/dist/dispatcher.js.map +1 -0
  19. package/dist/index.d.ts +60 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +66 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/providers/email-noop.d.ts +30 -0
  24. package/dist/providers/email-noop.d.ts.map +1 -0
  25. package/dist/providers/email-noop.js +39 -0
  26. package/dist/providers/email-noop.js.map +1 -0
  27. package/dist/providers/email-ses.d.ts +39 -0
  28. package/dist/providers/email-ses.d.ts.map +1 -0
  29. package/dist/providers/email-ses.js +114 -0
  30. package/dist/providers/email-ses.js.map +1 -0
  31. package/dist/providers/index.d.ts +20 -0
  32. package/dist/providers/index.d.ts.map +1 -0
  33. package/dist/providers/index.js +20 -0
  34. package/dist/providers/index.js.map +1 -0
  35. package/dist/providers/push-noop.d.ts +12 -0
  36. package/dist/providers/push-noop.d.ts.map +1 -0
  37. package/dist/providers/push-noop.js +30 -0
  38. package/dist/providers/push-noop.js.map +1 -0
  39. package/dist/providers/sms-noop.d.ts +21 -0
  40. package/dist/providers/sms-noop.d.ts.map +1 -0
  41. package/dist/providers/sms-noop.js +42 -0
  42. package/dist/providers/sms-noop.js.map +1 -0
  43. package/dist/providers/types.d.ts +95 -0
  44. package/dist/providers/types.d.ts.map +1 -0
  45. package/dist/providers/types.js +40 -0
  46. package/dist/providers/types.js.map +1 -0
  47. package/dist/providers/whatsapp-meta.d.ts +63 -0
  48. package/dist/providers/whatsapp-meta.d.ts.map +1 -0
  49. package/dist/providers/whatsapp-meta.js +160 -0
  50. package/dist/providers/whatsapp-meta.js.map +1 -0
  51. package/dist/store/index.d.ts +6 -0
  52. package/dist/store/index.d.ts.map +1 -0
  53. package/dist/store/index.js +5 -0
  54. package/dist/store/index.js.map +1 -0
  55. package/dist/store/postgres.d.ts +36 -0
  56. package/dist/store/postgres.d.ts.map +1 -0
  57. package/dist/store/postgres.js +354 -0
  58. package/dist/store/postgres.js.map +1 -0
  59. package/dist/store/types.d.ts +139 -0
  60. package/dist/store/types.d.ts.map +1 -0
  61. package/dist/store/types.js +14 -0
  62. package/dist/store/types.js.map +1 -0
  63. package/dist/templates/registry.d.ts +55 -0
  64. package/dist/templates/registry.d.ts.map +1 -0
  65. package/dist/templates/registry.js +94 -0
  66. package/dist/templates/registry.js.map +1 -0
  67. package/dist/types.d.ts +214 -0
  68. package/dist/types.d.ts.map +1 -0
  69. package/dist/types.js +12 -0
  70. package/dist/types.js.map +1 -0
  71. package/migrations/vk_notify_001_init.sql +109 -0
  72. package/package.json +109 -0
  73. package/runtime/bounce-handler-stub.ts +35 -0
  74. package/runtime/dispatcher-stub.ts +63 -0
  75. package/runtime/tsconfig.json +19 -0
@@ -0,0 +1,109 @@
1
+ -- @venturekit/notify — initial schema
2
+ --
3
+ -- Tables created by this migration:
4
+ -- notifications — outbox / audit log
5
+ -- notification_preferences — per-(user, channel, category) opt-in
6
+ -- notification_event_log — bounce / complaint / delivery events
7
+ --
8
+ -- The migration is idempotent (CREATE … IF NOT EXISTS / EXCEPTION
9
+ -- WHEN duplicate_object) so re-applying after a partial failure or
10
+ -- a Phase 3 rename never breaks the deploy.
11
+ --
12
+ -- Naming: prefixed with `vk_notify_001_` so the bundled-migration
13
+ -- runner (which sorts alphabetically) interleaves VentureKit-shipped
14
+ -- migrations after the project's own `0xx_*.sql` files. Project SQL
15
+ -- using these table names will collide; consumers either let the
16
+ -- runner own the schema (recommended) or rename their own tables.
17
+
18
+ -- Channel enum -------------------------------------------------------
19
+ DO $$ BEGIN
20
+ CREATE TYPE notify_channel AS ENUM ('email', 'whatsapp', 'sms', 'push');
21
+ EXCEPTION
22
+ WHEN duplicate_object THEN NULL;
23
+ END $$;
24
+
25
+ -- Notification status enum ------------------------------------------
26
+ DO $$ BEGIN
27
+ CREATE TYPE notify_status AS ENUM (
28
+ 'pending',
29
+ 'sent',
30
+ 'failed',
31
+ 'bounced',
32
+ 'cancelled'
33
+ );
34
+ EXCEPTION
35
+ WHEN duplicate_object THEN NULL;
36
+ END $$;
37
+
38
+ -- Outbox -------------------------------------------------------------
39
+ CREATE TABLE IF NOT EXISTS notifications (
40
+ id TEXT PRIMARY KEY,
41
+ recipient_user_id TEXT,
42
+ address TEXT,
43
+ channel notify_channel NOT NULL,
44
+ template_key TEXT NOT NULL,
45
+ payload JSONB NOT NULL DEFAULT '{}'::jsonb,
46
+ status notify_status NOT NULL DEFAULT 'pending',
47
+ scheduled_for TIMESTAMPTZ,
48
+ sent_at TIMESTAMPTZ,
49
+ related_kind TEXT,
50
+ related_id TEXT,
51
+ provider_message_id TEXT,
52
+ error TEXT,
53
+ attempts INTEGER NOT NULL DEFAULT 0,
54
+ created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
55
+ -- One of recipient_user_id or address must be set; both is allowed
56
+ -- (address overrides resolver lookup).
57
+ CONSTRAINT notifications_addressable_chk CHECK (
58
+ recipient_user_id IS NOT NULL OR address IS NOT NULL
59
+ )
60
+ );
61
+
62
+ -- Outbox scanner hot path: claimPending() with status='pending' AND
63
+ -- (scheduled_for IS NULL OR scheduled_for <= NOW()).
64
+ CREATE INDEX IF NOT EXISTS notifications_pending_idx
65
+ ON notifications (created_at ASC)
66
+ WHERE status = 'pending';
67
+
68
+ -- Per-user inbox view.
69
+ CREATE INDEX IF NOT EXISTS notifications_recipient_idx
70
+ ON notifications (recipient_user_id, created_at DESC)
71
+ WHERE recipient_user_id IS NOT NULL;
72
+
73
+ -- Bounce/complaint correlation by provider message id.
74
+ CREATE INDEX IF NOT EXISTS notifications_provider_msg_idx
75
+ ON notifications (provider_message_id)
76
+ WHERE provider_message_id IS NOT NULL;
77
+
78
+ -- Preferences (§6.3) -------------------------------------------------
79
+ CREATE TABLE IF NOT EXISTS notification_preferences (
80
+ user_id TEXT NOT NULL,
81
+ channel notify_channel NOT NULL,
82
+ category TEXT NOT NULL,
83
+ opted_in BOOLEAN NOT NULL DEFAULT TRUE,
84
+ updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
85
+ PRIMARY KEY (user_id, channel, category)
86
+ );
87
+
88
+ -- Bounce / complaint / delivery event log ---------------------------
89
+ CREATE TABLE IF NOT EXISTS notification_event_log (
90
+ id TEXT PRIMARY KEY,
91
+ channel notify_channel NOT NULL,
92
+ address TEXT NOT NULL,
93
+ event_type TEXT NOT NULL,
94
+ provider_message_id TEXT,
95
+ suppress BOOLEAN NOT NULL DEFAULT FALSE,
96
+ raw JSONB NOT NULL DEFAULT '{}'::jsonb,
97
+ received_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
98
+ );
99
+
100
+ -- Suppression lookup hot path: isAddressSuppressed(channel, address).
101
+ -- A partial unique index ensures one active suppression per
102
+ -- (channel, address) without restricting historical event rows.
103
+ CREATE INDEX IF NOT EXISTS notification_event_log_suppress_idx
104
+ ON notification_event_log (channel, address)
105
+ WHERE suppress = TRUE;
106
+
107
+ CREATE INDEX IF NOT EXISTS notification_event_log_provider_msg_idx
108
+ ON notification_event_log (provider_message_id)
109
+ WHERE provider_message_id IS NOT NULL;
package/package.json ADDED
@@ -0,0 +1,109 @@
1
+ {
2
+ "name": "@venturekit/notify",
3
+ "version": "0.0.0-dev.20260507015349",
4
+ "description": "Transactional notifications for VentureKit — email (SES), WhatsApp (Meta Cloud), with a Postgres outbox + cron dispatcher.",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "migrations",
11
+ "runtime"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/venturekit-dev/venturekit.private.git",
16
+ "directory": "packages/notify"
17
+ },
18
+ "publishConfig": {
19
+ "registry": "https://registry.npmjs.org",
20
+ "access": "public"
21
+ },
22
+ "license": "Apache-2.0",
23
+ "exports": {
24
+ ".": {
25
+ "import": "./dist/index.js",
26
+ "types": "./dist/index.d.ts"
27
+ },
28
+ "./dispatcher": {
29
+ "import": "./dist/dispatcher.js",
30
+ "types": "./dist/dispatcher.d.ts"
31
+ },
32
+ "./bounce-handler": {
33
+ "import": "./dist/bounce-handler.js",
34
+ "types": "./dist/bounce-handler.d.ts"
35
+ },
36
+ "./admin": {
37
+ "import": "./dist/admin/index.js",
38
+ "types": "./dist/admin/index.d.ts"
39
+ },
40
+ "./providers/email-ses": {
41
+ "import": "./dist/providers/email-ses.js",
42
+ "types": "./dist/providers/email-ses.d.ts"
43
+ },
44
+ "./providers/whatsapp-meta": {
45
+ "import": "./dist/providers/whatsapp-meta.js",
46
+ "types": "./dist/providers/whatsapp-meta.d.ts"
47
+ },
48
+ "./store/postgres": {
49
+ "import": "./dist/store/postgres.js",
50
+ "types": "./dist/store/postgres.d.ts"
51
+ },
52
+ "./migrations": {
53
+ "default": "./migrations"
54
+ },
55
+ "./runtime/dispatcher-stub": {
56
+ "default": "./runtime/dispatcher-stub.ts"
57
+ },
58
+ "./runtime/bounce-handler-stub": {
59
+ "default": "./runtime/bounce-handler-stub.ts"
60
+ }
61
+ },
62
+ "keywords": [
63
+ "venturekit",
64
+ "saas",
65
+ "notifications",
66
+ "email",
67
+ "whatsapp",
68
+ "ses",
69
+ "outbox"
70
+ ],
71
+ "dependencies": {
72
+ "@venturekit/core": "0.0.0-dev.20260507015349"
73
+ },
74
+ "peerDependencies": {
75
+ "@venturekit/data": "0.0.0-dev.20260507015349",
76
+ "@venturekit/runtime": "0.0.0-dev.20260507015349",
77
+ "@aws-sdk/client-sesv2": "^3.668.0",
78
+ "@aws-sdk/client-secrets-manager": "^3.668.0"
79
+ },
80
+ "peerDependenciesMeta": {
81
+ "@venturekit/data": {
82
+ "optional": true
83
+ },
84
+ "@venturekit/runtime": {
85
+ "optional": true
86
+ },
87
+ "@aws-sdk/client-sesv2": {
88
+ "optional": true
89
+ },
90
+ "@aws-sdk/client-secrets-manager": {
91
+ "optional": true
92
+ }
93
+ },
94
+ "devDependencies": {
95
+ "@venturekit/data": "0.0.0-dev.20260507015349",
96
+ "@venturekit/runtime": "0.0.0-dev.20260507015349",
97
+ "@aws-sdk/client-sesv2": "^3.668.0",
98
+ "@aws-sdk/client-secrets-manager": "^3.668.0",
99
+ "@types/aws-lambda": "^8.10.131",
100
+ "@types/node": "^25.6.0",
101
+ "typescript": "^5.3.0",
102
+ "vitest": "^4.1.5"
103
+ },
104
+ "scripts": {
105
+ "build": "tsc",
106
+ "dev": "tsc --watch",
107
+ "clean": "rm -rf dist"
108
+ }
109
+ }
@@ -0,0 +1,35 @@
1
+ /**
2
+ * @venturekit/notify — bounce-handler Lambda stub
3
+ *
4
+ * Same shape as `dispatcher-stub.ts` — see that file for the rationale
5
+ * behind `VENTURE_NOTIFY_CLIENT_MODULE`.
6
+ *
7
+ * Subscribed to the SES events SNS topic; receives bounce / complaint
8
+ * / delivery / open / click events and writes them to
9
+ * `notification_event_log` via the consumer's `NotifyClient`.
10
+ */
11
+
12
+ import { makeBounceHandler, type NotifyClient } from '@venturekit/notify';
13
+
14
+ let cachedClient: NotifyClient | null = null;
15
+
16
+ async function getClient(): Promise<NotifyClient> {
17
+ if (cachedClient) return cachedClient;
18
+
19
+ const modulePath = process.env.VENTURE_NOTIFY_CLIENT_MODULE;
20
+ if (!modulePath) {
21
+ throw new Error(
22
+ 'VENTURE_NOTIFY_CLIENT_MODULE env var is not set — cannot resolve notify client.',
23
+ );
24
+ }
25
+ const mod = (await import(modulePath)) as { notify?: NotifyClient };
26
+ if (!mod.notify) {
27
+ throw new Error(
28
+ `Module at '${modulePath}' does not export a 'notify' NotifyClient instance.`,
29
+ );
30
+ }
31
+ cachedClient = mod.notify;
32
+ return cachedClient;
33
+ }
34
+
35
+ export const main = makeBounceHandler(getClient);
@@ -0,0 +1,63 @@
1
+ /**
2
+ * @venturekit/notify — dispatcher Lambda stub
3
+ *
4
+ * `@venturekit/infra` writes a copy of this file into a temporary
5
+ * directory at synth-time, runs esbuild against it, and uses the
6
+ * bundled output as the cron dispatcher Lambda's code.
7
+ *
8
+ * The stub deliberately depends on a single environment variable —
9
+ * `VENTURE_NOTIFY_CLIENT_MODULE` — pointing at the consumer module
10
+ * that exports a `notify` object (a `NotifyClient`). The consumer is
11
+ * responsible for shaping that module however they want; the only
12
+ * contract is `export const notify = createNotifyClient(...)`.
13
+ *
14
+ * Why an env-var module path rather than a build-time import:
15
+ *
16
+ * - Avoids forcing every consumer to put their notify config in a
17
+ * single hard-coded path (`src/lib/notify.ts`). Some projects
18
+ * already have a `lib/email.ts` / `notify/index.ts` convention.
19
+ * - Lets the synth-time stub stay portable across projects without
20
+ * templating per-project paths.
21
+ * - The dynamic import resolves at cold-start once and is cached for
22
+ * the warm container's lifetime, so the cost is one extra file
23
+ * read on cold start — negligible against the SES/Meta calls the
24
+ * handler performs.
25
+ */
26
+
27
+ import { makeDispatchHandler, type NotifyClient } from '@venturekit/notify';
28
+
29
+ let cachedClient: NotifyClient | null = null;
30
+
31
+ async function getClient(): Promise<NotifyClient> {
32
+ if (cachedClient) return cachedClient;
33
+
34
+ const modulePath = process.env.VENTURE_NOTIFY_CLIENT_MODULE;
35
+ if (!modulePath) {
36
+ throw new Error(
37
+ 'VENTURE_NOTIFY_CLIENT_MODULE env var is not set — cannot resolve notify client. ' +
38
+ 'The infra package sets this when wiring the dispatcher cron; if you are seeing ' +
39
+ 'this in a non-VK Lambda, set the env var to the path of the module that exports ' +
40
+ '`notify` (e.g. "./lib/notify.js").',
41
+ );
42
+ }
43
+
44
+ // Use a dynamic import — relative paths resolve against the bundled
45
+ // Lambda's working directory.
46
+ const mod = (await import(modulePath)) as { notify?: NotifyClient };
47
+ if (!mod.notify) {
48
+ throw new Error(
49
+ `Module at '${modulePath}' does not export a 'notify' NotifyClient instance. ` +
50
+ 'Either export `notify` from that module, or point VENTURE_NOTIFY_CLIENT_MODULE ' +
51
+ 'at the right file.',
52
+ );
53
+ }
54
+ cachedClient = mod.notify;
55
+ return cachedClient;
56
+ }
57
+
58
+ export const main = makeDispatchHandler(getClient, {
59
+ // Default batch size mirrors NotifyIntent.dispatchBatchSize default.
60
+ // The infra package overrides via env var when the operator picks a
61
+ // different value.
62
+ batchSize: parseInt(process.env.VENTURE_NOTIFY_BATCH_SIZE ?? '50', 10) || 50,
63
+ });
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2022",
4
+ "module": "NodeNext",
5
+ "moduleResolution": "NodeNext",
6
+ "lib": ["ES2022"],
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "types": ["node"],
11
+ "noEmit": true,
12
+ "allowImportingTsExtensions": false,
13
+ "paths": {
14
+ "@venturekit/notify": ["../src/index.ts"]
15
+ }
16
+ },
17
+ "include": ["./**/*.ts"],
18
+ "exclude": ["node_modules"]
19
+ }