samva 0.29.1 → 0.31.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.
Files changed (185) hide show
  1. package/AGENTS.md +23 -5
  2. package/README.md +166 -133
  3. package/dist/_virtual/_rolldown/runtime.js +1 -2
  4. package/dist/client/client/client.gen.js +1 -2
  5. package/dist/client/client/utils.gen.js +1 -2
  6. package/dist/client/client.gen.js +1 -2
  7. package/dist/client/core/auth.gen.js +1 -2
  8. package/dist/client/core/bodySerializer.gen.js +1 -2
  9. package/dist/client/core/params.gen.js +1 -2
  10. package/dist/client/core/pathSerializer.gen.js +1 -2
  11. package/dist/client/core/serverSentEvents.gen.js +1 -2
  12. package/dist/client/core/utils.gen.js +1 -2
  13. package/dist/client/sdk.gen.d.ts +218 -192
  14. package/dist/client/sdk.gen.js +2397 -1154
  15. package/dist/client/types.gen.d.ts +767 -376
  16. package/dist/client-attribution.js +1 -2
  17. package/dist/effect/agents.d.ts +33 -11
  18. package/dist/effect/agents.js +1 -2
  19. package/dist/effect/analytics.d.ts +6 -6
  20. package/dist/effect/analytics.js +1 -2
  21. package/dist/effect/api-keys.d.ts +8 -8
  22. package/dist/effect/api-keys.js +1 -2
  23. package/dist/effect/attachments.d.ts +4 -4
  24. package/dist/effect/attachments.js +1 -2
  25. package/dist/effect/brands.d.ts +6 -6
  26. package/dist/effect/brands.js +1 -2
  27. package/dist/effect/campaigns.d.ts +15 -16
  28. package/dist/effect/campaigns.js +1 -2
  29. package/dist/effect/categories.js +1 -2
  30. package/dist/effect/client.js +1 -2
  31. package/dist/effect/contact-groups.d.ts +10 -10
  32. package/dist/effect/contact-groups.js +1 -2
  33. package/dist/effect/contacts.d.ts +23 -24
  34. package/dist/effect/contacts.js +1 -2
  35. package/dist/effect/conversations.d.ts +16 -13
  36. package/dist/effect/conversations.js +1 -2
  37. package/dist/effect/custom-fields.d.ts +4 -4
  38. package/dist/effect/custom-fields.js +1 -2
  39. package/dist/effect/email.d.ts +61 -63
  40. package/dist/effect/email.js +1 -2
  41. package/dist/effect/events.d.ts +2 -4
  42. package/dist/effect/events.js +1 -2
  43. package/dist/effect/extension-installations.d.ts +9 -9
  44. package/dist/effect/extension-installations.js +1 -2
  45. package/dist/effect/extensions.d.ts +5 -5
  46. package/dist/effect/extensions.js +1 -2
  47. package/dist/effect/feedback.d.ts +1 -1
  48. package/dist/effect/feedback.js +1 -2
  49. package/dist/effect/generated/agents.d.ts +264 -22
  50. package/dist/effect/generated/agents.js +2 -3
  51. package/dist/effect/generated/analytics.js +1 -2
  52. package/dist/effect/generated/api-keys.js +1 -2
  53. package/dist/effect/generated/attachments.js +1 -2
  54. package/dist/effect/generated/brands.js +1 -2
  55. package/dist/effect/generated/campaigns.d.ts +160 -155
  56. package/dist/effect/generated/campaigns.js +7 -9
  57. package/dist/effect/generated/contact-events.js +1 -2
  58. package/dist/effect/generated/contact-groups.d.ts +26 -26
  59. package/dist/effect/generated/contact-groups.js +22 -19
  60. package/dist/effect/generated/contacts.d.ts +119 -90
  61. package/dist/effect/generated/contacts.js +32 -32
  62. package/dist/effect/generated/conversations.d.ts +26 -23
  63. package/dist/effect/generated/conversations.js +10 -10
  64. package/dist/effect/generated/custom-fields.d.ts +6 -6
  65. package/dist/effect/generated/custom-fields.js +3 -4
  66. package/dist/effect/generated/email.d.ts +38 -38
  67. package/dist/effect/generated/email.js +13 -14
  68. package/dist/effect/generated/events.d.ts +18 -9
  69. package/dist/effect/generated/events.js +8 -6
  70. package/dist/effect/generated/extension-installations.d.ts +2 -2
  71. package/dist/effect/generated/extension-installations.js +6 -3
  72. package/dist/effect/generated/extensions.js +1 -2
  73. package/dist/effect/generated/feedback.js +1 -2
  74. package/dist/effect/generated/index.d.ts +7 -5
  75. package/dist/effect/generated/journeys.js +1 -2
  76. package/dist/effect/generated/mailboxes.d.ts +44 -44
  77. package/dist/effect/generated/mailboxes.js +17 -18
  78. package/dist/effect/generated/media.js +1 -2
  79. package/dist/effect/generated/messages.d.ts +42 -42
  80. package/dist/effect/generated/messages.js +3 -4
  81. package/dist/effect/generated/namespaces.js +1 -2
  82. package/dist/effect/generated/{publication-domains.d.ts → newsletter-domains.d.ts} +304 -80
  83. package/dist/effect/generated/newsletter-domains.js +218 -0
  84. package/dist/effect/generated/{topic-issues.d.ts → newsletter-issues.d.ts} +330 -288
  85. package/dist/effect/generated/{topic-issues.js → newsletter-issues.js} +255 -246
  86. package/dist/effect/generated/newsletters.d.ts +741 -0
  87. package/dist/effect/generated/newsletters.js +143 -0
  88. package/dist/effect/generated/operations.js +1 -2
  89. package/dist/effect/generated/organizations.d.ts +9 -9
  90. package/dist/effect/generated/organizations.js +4 -5
  91. package/dist/effect/generated/scheduled-messages.d.ts +174 -169
  92. package/dist/effect/generated/scheduled-messages.js +7 -7
  93. package/dist/effect/generated/shared.d.ts +276 -208
  94. package/dist/effect/generated/shared.js +50 -36
  95. package/dist/effect/generated/subscriptions.js +1 -2
  96. package/dist/effect/generated/templates.d.ts +118 -67
  97. package/dist/effect/generated/templates.js +43 -35
  98. package/dist/effect/generated/topic-forms.d.ts +79 -19
  99. package/dist/effect/generated/topic-forms.js +13 -14
  100. package/dist/effect/generated/topics.d.ts +27 -65
  101. package/dist/effect/generated/topics.js +13 -20
  102. package/dist/effect/generated/webhooks.d.ts +9 -9
  103. package/dist/effect/generated/webhooks.js +4 -5
  104. package/dist/effect/generated/whoami.js +1 -2
  105. package/dist/effect/generated-metadata.d.ts +475 -70
  106. package/dist/effect/generated-metadata.js +642 -432
  107. package/dist/effect/index.d.ts +3 -2
  108. package/dist/effect/index.js +4 -3
  109. package/dist/effect/internal/client.d.ts +5 -0
  110. package/dist/effect/internal/client.js +9 -5
  111. package/dist/effect/internal/email-sender.d.ts +1 -1
  112. package/dist/effect/internal/email-sender.js +4 -3
  113. package/dist/effect/internal/mailbox-events.js +1 -2
  114. package/dist/effect/internal/operation.d.ts +4 -15
  115. package/dist/effect/internal/operation.js +21 -33
  116. package/dist/effect/internal/send.js +2 -3
  117. package/dist/effect/journeys.d.ts +14 -14
  118. package/dist/effect/journeys.js +1 -2
  119. package/dist/effect/mailboxes.d.ts +60 -70
  120. package/dist/effect/mailboxes.js +1 -2
  121. package/dist/effect/media.d.ts +5 -5
  122. package/dist/effect/media.js +1 -2
  123. package/dist/effect/messages.d.ts +9 -9
  124. package/dist/effect/messages.js +1 -2
  125. package/dist/effect/namespaces.d.ts +8 -8
  126. package/dist/effect/namespaces.js +1 -2
  127. package/dist/effect/newsletter-domains.d.ts +71 -0
  128. package/dist/effect/newsletter-domains.js +18 -0
  129. package/dist/effect/{topic-issues.d.ts → newsletter-issues.d.ts} +11 -15
  130. package/dist/effect/newsletter-issues.js +32 -0
  131. package/dist/effect/newsletters.d.ts +22 -0
  132. package/dist/effect/newsletters.js +20 -0
  133. package/dist/effect/operations.d.ts +3 -3
  134. package/dist/effect/operations.js +1 -2
  135. package/dist/effect/organizations.d.ts +8 -10
  136. package/dist/effect/organizations.js +1 -2
  137. package/dist/effect/pagination.js +1 -2
  138. package/dist/effect/retry.d.ts +4 -4
  139. package/dist/effect/retry.js +5 -6
  140. package/dist/effect/runtime.js +1 -2
  141. package/dist/effect/scheduled-messages.d.ts +5 -6
  142. package/dist/effect/scheduled-messages.js +1 -2
  143. package/dist/effect/subscriptions.d.ts +1 -1
  144. package/dist/effect/subscriptions.js +1 -2
  145. package/dist/effect/templates.d.ts +34 -43
  146. package/dist/effect/templates.js +1 -2
  147. package/dist/effect/topic-forms.d.ts +5 -5
  148. package/dist/effect/topic-forms.js +1 -2
  149. package/dist/effect/topics.d.ts +9 -13
  150. package/dist/effect/topics.js +1 -2
  151. package/dist/effect/webhooks.d.ts +11 -11
  152. package/dist/effect/webhooks.js +1 -2
  153. package/dist/effect/whoami.d.ts +1 -1
  154. package/dist/effect/whoami.js +1 -2
  155. package/dist/index.js +2 -2
  156. package/dist/inline-attachments.js +1 -2
  157. package/dist/mailbox/dispatch.d.ts +10 -0
  158. package/dist/mailbox/dispatch.js +66 -0
  159. package/dist/mailbox/index.d.ts +17 -0
  160. package/dist/mailbox/index.js +30 -0
  161. package/dist/mailbox/test.d.ts +25 -0
  162. package/dist/mailbox/test.js +174 -0
  163. package/dist/mailbox/types.d.ts +184 -0
  164. package/dist/mailbox/webhook.d.ts +10 -0
  165. package/dist/mailbox/webhook.js +41 -0
  166. package/dist/mailbox-events.js +1 -2
  167. package/dist/normalize.js +1 -2
  168. package/dist/package.js +2 -3
  169. package/dist/promises/client.d.ts +215 -0
  170. package/dist/promises/client.js +324 -0
  171. package/dist/promises/errors.gen.js +1 -2
  172. package/dist/promises/index.d.ts +2 -196
  173. package/dist/promises/index.js +3 -295
  174. package/dist/promises/request.d.ts +15 -0
  175. package/dist/promises/request.js +75 -0
  176. package/dist/promises/types.gen.d.ts +1 -1
  177. package/dist/request-policy.d.ts +3 -0
  178. package/dist/request-policy.js +16 -0
  179. package/dist/service-map.d.ts +78 -47
  180. package/dist/service-map.js +52 -32
  181. package/package.json +34 -18
  182. package/dist/effect/generated/publication-domains.js +0 -163
  183. package/dist/effect/publication-domains.d.ts +0 -14
  184. package/dist/effect/publication-domains.js +0 -17
  185. package/dist/effect/topic-issues.js +0 -33
@@ -0,0 +1,30 @@
1
+ import { createBoundClient } from "../promises/client.js";
2
+ import { dispatchEvent, errorMessage } from "./dispatch.js";
3
+ // src/mailbox/index.ts
4
+ function defineExtension(definition) {
5
+ for (const key of Object.keys(definition.on)) if (!definition.manifest.events.includes(key)) throw new Error(`Handler ${key} is not listed in manifest.events`);
6
+ if (!definition.manifest.permissions.includes("read")) throw new Error("An extension requires the read permission");
7
+ return {
8
+ ...definition,
9
+ async fetch(request, env) {
10
+ try {
11
+ if (request.method !== "POST") throw new Error("Hosted invocations require POST");
12
+ const invocation = await request.json();
13
+ const client = createBoundClient({
14
+ fetch: env.SAMVA.fetch.bind(env.SAMVA),
15
+ baseUrl: "https://api.samva.dev"
16
+ });
17
+ return Response.json(await dispatchEvent(definition, invocation, client));
18
+ } catch (error) {
19
+ return Response.json({
20
+ ok: false,
21
+ error: errorMessage(error),
22
+ logs: []
23
+ });
24
+ }
25
+ }
26
+ };
27
+ }
28
+ export { defineExtension, dispatchEvent };
29
+
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,25 @@
1
+ import { type SamvaClient } from "../promises/client";
2
+ import type { EventData, EventType, ExtensionDefinition, ExtensionEvent } from "./types";
3
+ export interface RecordedCall {
4
+ method: string;
5
+ path: string;
6
+ body: unknown;
7
+ idempotencyKey: string | null;
8
+ }
9
+ /** Canned responses are keyed by `METHOD /path`. */
10
+ export declare function createTestClient(responses?: Record<string, unknown>): SamvaClient & {
11
+ calls: RecordedCall[];
12
+ };
13
+ export declare function runExtension(extension: ExtensionDefinition, event: ExtensionEvent, options?: {
14
+ client?: SamvaClient & {
15
+ calls?: RecordedCall[];
16
+ };
17
+ attempt?: number;
18
+ }): Promise<{
19
+ ok: boolean;
20
+ logs: readonly string[];
21
+ error: string | undefined;
22
+ calls: RecordedCall[];
23
+ }>;
24
+ /** Top-level data overrides replace the corresponding fixture field. */
25
+ export declare function mailboxEvent<T extends EventType>(type: T, overrides?: Partial<EventData[T]>): ExtensionEvent<T>;
@@ -0,0 +1,174 @@
1
+ import { createBoundClient } from "../promises/client.js";
2
+ import { dispatchEvent } from "./dispatch.js";
3
+ // src/mailbox/test.ts
4
+ /** Canned responses are keyed by `METHOD /path`. */
5
+ function createTestClient(responses = {}) {
6
+ const calls = [];
7
+ const client = createBoundClient({
8
+ baseUrl: "https://api.samva.dev",
9
+ async fetch(input, init) {
10
+ const request = new Request(input, init);
11
+ const url = new URL(request.url);
12
+ const path = url.pathname + url.search;
13
+ const text = await request.text();
14
+ calls.push({
15
+ method: request.method,
16
+ path,
17
+ body: text ? JSON.parse(text) : void 0,
18
+ idempotencyKey: request.headers.get("idempotency-key")
19
+ });
20
+ const response = responses[`${request.method} ${path}`];
21
+ return Response.json(response ?? {});
22
+ }
23
+ });
24
+ return Object.assign(client, { calls });
25
+ }
26
+ async function runExtension(extension, event, options = {}) {
27
+ const client = options.client ?? createTestClient();
28
+ const start = client.calls?.length ?? 0;
29
+ const { id, ...body } = event;
30
+ const result = await dispatchEvent(extension, {
31
+ eventId: id,
32
+ attempt: options.attempt ?? 1,
33
+ body: JSON.stringify(body)
34
+ }, client);
35
+ return {
36
+ ...result,
37
+ error: result.error,
38
+ calls: client.calls?.slice(start) ?? []
39
+ };
40
+ }
41
+ const timestamp = "2026-01-01T00:00:00.000Z";
42
+ const message = {
43
+ id: "msg_0000000000000000",
44
+ channel: "email",
45
+ threadId: "thread_0000000000000000",
46
+ from: { address: "sender@example.com" },
47
+ to: ["inbox@example.com"],
48
+ cc: [],
49
+ subject: "Hello",
50
+ text: "Hello",
51
+ replyText: "Hello",
52
+ attachments: [],
53
+ occurredAt: timestamp
54
+ };
55
+ const actor = {
56
+ kind: "extension",
57
+ id: "exti_0000000000000000",
58
+ source: "extension"
59
+ };
60
+ const thread = {
61
+ id: message.threadId,
62
+ endpointId: "mbxep_0000000000000000",
63
+ subject: "Hello",
64
+ preview: "Hello"
65
+ };
66
+ const common = {
67
+ mailboxId: "mbx_0000000000000000",
68
+ namespaceId: null
69
+ };
70
+ const fixtures = {
71
+ "mailbox.message.received": {
72
+ ...common,
73
+ message,
74
+ thread
75
+ },
76
+ "mailbox.message.sent": {
77
+ ...common,
78
+ state: "completed",
79
+ acceptedAt: timestamp,
80
+ message,
81
+ actor,
82
+ actionId: "mbxact_0000000000000000"
83
+ },
84
+ "mailbox.action.pending_approval": {
85
+ ...common,
86
+ action: {
87
+ state: "approvalRequired",
88
+ decision: {
89
+ outcome: "requireApproval",
90
+ reason: "Approval required",
91
+ policyId: null,
92
+ source: "installation",
93
+ decidedAt: timestamp
94
+ },
95
+ id: "mbxact_0000000000000000",
96
+ type: "sendMessage",
97
+ threadId: message.threadId,
98
+ reason: null,
99
+ proposedBy: actor,
100
+ createdAt: timestamp
101
+ },
102
+ draft: {
103
+ to: message.to,
104
+ cc: [],
105
+ subject: "Hello"
106
+ }
107
+ },
108
+ "mailbox.action.resolved": {
109
+ ...common,
110
+ action: {
111
+ id: "mbxact_0000000000000000",
112
+ type: "sendMessage",
113
+ threadId: message.threadId,
114
+ proposedBy: actor,
115
+ createdAt: timestamp
116
+ },
117
+ resolution: {
118
+ outcome: "approved",
119
+ actor: {
120
+ kind: "user",
121
+ id: "user_0000000000000000",
122
+ source: "humanUi"
123
+ },
124
+ resolvedAt: timestamp,
125
+ reason: null
126
+ }
127
+ },
128
+ "mailbox.message.quarantined": {
129
+ ...common,
130
+ disposition: "quarantined",
131
+ reason: "spam",
132
+ source: "receipt",
133
+ message: {
134
+ id: message.id,
135
+ from: message.from,
136
+ subject: message.subject,
137
+ receivedAt: timestamp
138
+ }
139
+ },
140
+ "mailbox.message.released": {
141
+ ...common,
142
+ message,
143
+ thread,
144
+ releasedBy: actor,
145
+ reason: "spam"
146
+ },
147
+ "mailbox.message.discarded": {
148
+ ...common,
149
+ discardedBy: actor,
150
+ disposition: "quarantined",
151
+ reason: "spam",
152
+ message: {
153
+ id: message.id,
154
+ from: message.from,
155
+ subject: message.subject,
156
+ receivedAt: timestamp
157
+ }
158
+ }
159
+ };
160
+ /** Top-level data overrides replace the corresponding fixture field. */
161
+ function mailboxEvent(type, overrides = {}) {
162
+ return {
163
+ id: "whevt_0000000000000000",
164
+ type,
165
+ timestamp,
166
+ data: {
167
+ ...structuredClone(fixtures[type]),
168
+ ...overrides
169
+ }
170
+ };
171
+ }
172
+ export { createTestClient, mailboxEvent, runExtension };
173
+
174
+ //# sourceMappingURL=test.js.map
@@ -0,0 +1,184 @@
1
+ import type { SamvaClient } from "../promises/client";
2
+ type SendResult = Awaited<ReturnType<SamvaClient["mailboxes"]["reply"]>>;
3
+ export type ExtensionManifest = {
4
+ readonly name: string;
5
+ readonly displayName: string;
6
+ readonly description?: string | undefined;
7
+ readonly version: string;
8
+ readonly runtime: "hosted" | "webhook";
9
+ readonly events: readonly EventType[];
10
+ readonly permissions: readonly ("read" | "update" | "send" | "quarantine.read")[];
11
+ };
12
+ export type Invocation = {
13
+ readonly eventId: string;
14
+ readonly attempt: number;
15
+ readonly body: string;
16
+ };
17
+ export type DispatchResult = {
18
+ readonly ok: boolean;
19
+ readonly error?: string | undefined;
20
+ readonly logs: readonly string[];
21
+ };
22
+ export type MailboxActor = {
23
+ readonly kind: "user" | "apiKey" | "extension" | "externalAgent" | "samvaAgent" | "system";
24
+ readonly id: string;
25
+ readonly displayName?: string | undefined;
26
+ };
27
+ type WebhookActor = MailboxActor & {
28
+ readonly source: "humanUi" | "externalApi" | "externalAgent" | "extension" | "samvaManagedAgent" | "system";
29
+ };
30
+ /** How a held action's approval ended, and who ended it. */
31
+ export type ActionResolution = {
32
+ readonly outcome: "approved" | "denied" | "cancelled";
33
+ readonly actor: WebhookActor;
34
+ readonly resolvedAt: string;
35
+ /** The denial reason; null for an approval or a cancellation. */
36
+ readonly reason: string | null;
37
+ };
38
+ export type CampaignAttribution = {
39
+ readonly campaignId: string;
40
+ readonly runId: string;
41
+ };
42
+ type WebhookMessage = {
43
+ readonly campaign?: CampaignAttribution | undefined;
44
+ readonly id: string;
45
+ /** Deliveries recorded before the field existed omit it; they are email. */
46
+ readonly channel?: "email" | "whatsapp" | "sms";
47
+ readonly threadId: string;
48
+ readonly from: {
49
+ readonly address: string;
50
+ readonly name?: string | undefined;
51
+ };
52
+ readonly to: readonly string[];
53
+ readonly cc: readonly string[];
54
+ readonly subject: string | null;
55
+ readonly text: string | null;
56
+ readonly replyText: string | null;
57
+ readonly attachments: readonly {
58
+ readonly id: string;
59
+ readonly filename: string;
60
+ readonly contentType: string;
61
+ readonly size: number;
62
+ }[];
63
+ readonly occurredAt: string;
64
+ };
65
+ type WebhookThread = {
66
+ readonly id: string;
67
+ /** The endpoint the message arrived at; null once it is removed. */
68
+ readonly endpointId?: string | null;
69
+ readonly subject: string | null;
70
+ readonly preview: string | null;
71
+ };
72
+ type MailboxEventScope = {
73
+ readonly mailboxId: string;
74
+ readonly namespaceId: string | null;
75
+ };
76
+ type QuarantineReason = "spam" | "dmarc" | "virus" | "virus_unscanned" | "phishing" | "fraud" | "prompt_injection";
77
+ export type EventData = {
78
+ "mailbox.message.received": MailboxEventScope & {
79
+ readonly message: WebhookMessage;
80
+ readonly thread: WebhookThread;
81
+ };
82
+ "mailbox.message.sent": MailboxEventScope & {
83
+ readonly state?: "completed";
84
+ readonly acceptedAt?: string;
85
+ readonly actionId: string;
86
+ readonly actor: WebhookActor;
87
+ readonly message: WebhookMessage;
88
+ };
89
+ "mailbox.action.pending_approval": MailboxEventScope & {
90
+ readonly action: {
91
+ readonly state?: "approvalRequired";
92
+ readonly decision?: Readonly<NonNullable<SendResult["decision"]>>;
93
+ readonly id: string;
94
+ readonly type: "sendMessage" | "reply" | "updateDraft" | "createNote" | "assignThread" | "changeThreadState" | "call" | "extensionRequest" | "toolCall";
95
+ readonly threadId: string | null;
96
+ readonly reason: string | null;
97
+ readonly proposedBy: WebhookActor;
98
+ readonly createdAt: string;
99
+ };
100
+ /** Who a held send goes to; null for a held tool call. */
101
+ readonly draft: {
102
+ readonly to: readonly string[];
103
+ readonly cc: readonly string[];
104
+ readonly subject: string | null;
105
+ } | null;
106
+ /** The tool a held tool call runs and its arguments; null for a held send. */
107
+ readonly tool?: {
108
+ readonly name: string;
109
+ readonly argumentsHash: string;
110
+ readonly arguments: {
111
+ readonly [key: string]: unknown;
112
+ };
113
+ } | null;
114
+ };
115
+ "mailbox.action.resolved": MailboxEventScope & {
116
+ readonly action: {
117
+ readonly id: string;
118
+ readonly type: EventData["mailbox.action.pending_approval"]["action"]["type"];
119
+ readonly threadId: string | null;
120
+ readonly proposedBy: WebhookActor;
121
+ readonly createdAt: string;
122
+ };
123
+ readonly resolution: ActionResolution;
124
+ };
125
+ "mailbox.message.quarantined": MailboxEventScope & {
126
+ readonly disposition: "quarantined" | "isolated";
127
+ readonly reason: QuarantineReason;
128
+ readonly source: "receipt" | "classifier";
129
+ readonly message: {
130
+ readonly id: string;
131
+ readonly from: {
132
+ readonly address: string;
133
+ readonly name?: string | undefined;
134
+ };
135
+ readonly subject: string | null;
136
+ readonly receivedAt: string;
137
+ };
138
+ };
139
+ "mailbox.message.released": MailboxEventScope & {
140
+ readonly releasedBy: WebhookActor;
141
+ readonly reason: QuarantineReason;
142
+ readonly message: WebhookMessage;
143
+ readonly thread: WebhookThread;
144
+ };
145
+ "mailbox.message.discarded": MailboxEventScope & {
146
+ readonly discardedBy: WebhookActor;
147
+ readonly disposition: "quarantined" | "isolated";
148
+ readonly reason: QuarantineReason;
149
+ readonly message: {
150
+ readonly id: string;
151
+ readonly from: {
152
+ readonly address: string;
153
+ readonly name?: string | undefined;
154
+ };
155
+ readonly subject: string | null;
156
+ readonly receivedAt: string;
157
+ };
158
+ };
159
+ };
160
+ export type EventType = keyof EventData;
161
+ export type ExtensionEvent<T extends EventType = EventType> = {
162
+ [K in T]: {
163
+ id: string;
164
+ type: K;
165
+ timestamp: string;
166
+ data: EventData[K];
167
+ };
168
+ }[T];
169
+ export interface HandlerContext<T extends EventType = EventType> {
170
+ samva: SamvaClient;
171
+ log: (...args: unknown[]) => void;
172
+ event: ExtensionEvent<T>;
173
+ attempt: number;
174
+ }
175
+ export type ExtensionHandlers<T extends EventType = EventType> = {
176
+ [K in T]?: (event: ExtensionEvent<K>, context: HandlerContext<K>) => void | Promise<void>;
177
+ };
178
+ export interface ExtensionDefinition<T extends EventType = EventType> {
179
+ manifest: ExtensionManifest & {
180
+ events: readonly T[];
181
+ };
182
+ on: ExtensionHandlers<T>;
183
+ }
184
+ export {};
@@ -0,0 +1,10 @@
1
+ import { createClient } from "../promises/client";
2
+ import type { ExtensionDefinition } from "./types";
3
+ type Fetch = NonNullable<Parameters<typeof createClient>[0]["fetch"]>;
4
+ export declare function createWebhookHandler(extension: ExtensionDefinition, options: {
5
+ apiKey: string;
6
+ webhookSecret: string;
7
+ apiOrigin?: string;
8
+ fetch?: Fetch;
9
+ }): (request: Request) => Promise<Response>;
10
+ export {};
@@ -0,0 +1,41 @@
1
+ import { createClient } from "../promises/client.js";
2
+ import { dispatchEvent, errorMessage } from "./dispatch.js";
3
+ import { verifyRequest } from "@samva/webhooks";
4
+ // src/mailbox/webhook.ts
5
+ function createWebhookHandler(extension, options) {
6
+ const client = createClient({
7
+ fetch: options.fetch ?? globalThis.fetch.bind(globalThis),
8
+ baseUrl: options.apiOrigin ?? "https://api.samva.dev",
9
+ apiKey: options.apiKey
10
+ });
11
+ return async (request) => {
12
+ let eventId;
13
+ try {
14
+ eventId = (await verifyRequest(request, options.webhookSecret)).id;
15
+ } catch (error) {
16
+ return Response.json({
17
+ ok: false,
18
+ error: errorMessage(error)
19
+ }, { status: 401 });
20
+ }
21
+ try {
22
+ const result = await dispatchEvent(extension, {
23
+ eventId,
24
+ attempt: 1,
25
+ body: await request.text()
26
+ }, client);
27
+ return Response.json(result.ok ? { ok: true } : {
28
+ ok: false,
29
+ error: result.error
30
+ }, { status: result.ok ? 200 : 500 });
31
+ } catch (error) {
32
+ return Response.json({
33
+ ok: false,
34
+ error: errorMessage(error)
35
+ }, { status: 500 });
36
+ }
37
+ };
38
+ }
39
+ export { createWebhookHandler };
40
+
41
+ //# sourceMappingURL=webhook.js.map
@@ -1,4 +1,4 @@
1
- //#region src/mailbox-events.ts
1
+ // src/mailbox-events.ts
2
2
  /**
3
3
  * The mailbox event stream: the `mailbox.*` events a webhook endpoint receives, delivered over a
4
4
  * WebSocket to a program that has no public URL for webhooks to reach.
@@ -653,7 +653,6 @@ const readRefusalBody = async (response) => {
653
653
  return;
654
654
  }
655
655
  };
656
- //#endregion
657
656
  export { MAILBOX_EVENT_STREAM_IDLE_MILLIS, MAILBOX_EVENT_STREAM_PATH, MAILBOX_EVENT_STREAM_PING_FRAME, MAILBOX_EVENT_STREAM_PING_MILLIS, MailboxEventStream, MailboxEventStreamCloseCode, MailboxEventStreamError, mailboxEventsUrl };
658
657
 
659
658
  //# sourceMappingURL=mailbox-events.js.map
package/dist/normalize.js CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/normalize.ts
1
+ // src/normalize.ts
2
2
  function toRecipientArray(recipient) {
3
3
  return Array.isArray(recipient) ? recipient : [recipient];
4
4
  }
@@ -66,7 +66,6 @@ function buildMessagesSendPayload(input) {
66
66
  email: normalizeEmailContent(input.email ?? {})
67
67
  };
68
68
  }
69
- //#endregion
70
69
  export { buildEmailSendPayload, buildMessagesSendPayload, replyToEmailsPayload };
71
70
 
72
71
  //# sourceMappingURL=normalize.js.map
package/dist/package.js CHANGED
@@ -1,6 +1,5 @@
1
- //#region package.json
2
- var version = "0.29.1";
3
- //#endregion
1
+ // package.json
2
+ var version = "0.31.0";
4
3
  export { version };
5
4
 
6
5
  //# sourceMappingURL=package.js.map