@lovable.dev/email-js 0.1.0-rc.1 → 0.1.0-rc.2

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/README.md CHANGED
@@ -121,3 +121,35 @@ await setEmailUnsubscribe(
121
121
  { apiKey },
122
122
  );
123
123
  ```
124
+
125
+ ### Handling email webhooks
126
+
127
+ `createEmailWebhookHandler` verifies the `X-Lovable-Signature` on each delivery
128
+ (signed with the project's `LOVABLE_API_KEY`) and dispatches to the handler
129
+ registered for the event type. Write only handler bodies — verification stays
130
+ inside the package.
131
+
132
+ ```ts
133
+ import { createEmailWebhookHandler } from "@lovable.dev/email-js";
134
+
135
+ const handler = createEmailWebhookHandler({
136
+ apiKey: Deno.env.get("LOVABLE_API_KEY")!,
137
+ on: {
138
+ "email.bounced": async (event) => {
139
+ await markEmailUndeliverable(event.data.recipient);
140
+ },
141
+ "email.unsubscribed": async (event) => {
142
+ await recordOptOut(event.data.recipient);
143
+ },
144
+ },
145
+ });
146
+
147
+ Deno.serve(handler);
148
+ ```
149
+
150
+ Event types: `email.bounced`, `email.complaint`, `email.unsubscribed`,
151
+ `email.resubscribed` (defined but not yet emitted). Events without a
152
+ registered handler are acknowledged with 200. A handler that throws returns
153
+ 500 and the delivery is retried (up to 5 attempts in total, with backoff) —
154
+ use `event.event_id` (also sent as the `X-Lovable-Delivery` header) to
155
+ deduplicate redeliveries.
package/dist/index.cjs CHANGED
@@ -21,6 +21,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var src_exports = {};
22
22
  __export(src_exports, {
23
23
  EmailAPIError: () => EmailAPIError,
24
+ createEmailWebhookHandler: () => createEmailWebhookHandler,
24
25
  getEmailUnsubscribe: () => getEmailUnsubscribe,
25
26
  listEmailLogs: () => listEmailLogs,
26
27
  parseEmailWebhookPayload: () => parseEmailWebhookPayload,
@@ -190,9 +191,60 @@ function parseEmailWebhookPayload(body) {
190
191
  }
191
192
  return payload;
192
193
  }
194
+
195
+ // src/webhook-handler.ts
196
+ var import_webhooks_js = require("@lovable.dev/webhooks-js");
197
+ var WEBHOOK_ERROR_STATUS = {
198
+ missing_secret: 401,
199
+ missing_timestamp: 401,
200
+ invalid_timestamp: 401,
201
+ stale_timestamp: 401,
202
+ invalid_signature: 401,
203
+ body_too_large: 400,
204
+ invalid_json: 400,
205
+ invalid_payload: 400
206
+ };
207
+ function parseEmailWebhookEvent(body) {
208
+ return parseEmailWebhookPayload(body);
209
+ }
210
+ function createEmailWebhookHandler(options) {
211
+ const { apiKey, on } = options;
212
+ if (!apiKey) {
213
+ throw new Error("Missing Lovable API key");
214
+ }
215
+ return async (req) => {
216
+ if (req.method !== "POST") {
217
+ return Response.json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } });
218
+ }
219
+ let event;
220
+ try {
221
+ ({ payload: event } = await (0, import_webhooks_js.verifyWebhookRequest)({ req, secret: apiKey, parser: parseEmailWebhookEvent }));
222
+ } catch (error) {
223
+ if (error instanceof import_webhooks_js.WebhookError) {
224
+ return Response.json({ error: error.message }, { status: WEBHOOK_ERROR_STATUS[error.code] });
225
+ }
226
+ console.error("[email-js] webhook verification failed:", error);
227
+ return Response.json({ error: "Webhook verification failed" }, { status: 500 });
228
+ }
229
+ if (event.version !== "1") {
230
+ return Response.json({ received: true });
231
+ }
232
+ const handler = on[event.type];
233
+ if (handler) {
234
+ try {
235
+ await handler(event);
236
+ } catch (error) {
237
+ console.error("[email-js] webhook handler failed:", error);
238
+ return Response.json({ error: "Handler failed" }, { status: 500 });
239
+ }
240
+ }
241
+ return Response.json({ received: true });
242
+ };
243
+ }
193
244
  // Annotate the CommonJS export names for ESM import in node:
194
245
  0 && (module.exports = {
195
246
  EmailAPIError,
247
+ createEmailWebhookHandler,
196
248
  getEmailUnsubscribe,
197
249
  listEmailLogs,
198
250
  parseEmailWebhookPayload,
package/dist/index.d.cts CHANGED
@@ -76,6 +76,29 @@ interface EmailUnsubscribeResponse {
76
76
  domain: string;
77
77
  subscribed: boolean;
78
78
  }
79
+ type EmailWebhookEventType = "email.bounced" | "email.complaint" | "email.unsubscribed" | "email.resubscribed";
80
+ interface EmailWebhookEvent<T extends EmailWebhookEventType = EmailWebhookEventType> {
81
+ event_id: string;
82
+ run_id: string;
83
+ type: T;
84
+ project_id: string;
85
+ environment: "dev" | "prod";
86
+ data: {
87
+ event: T;
88
+ recipient: string;
89
+ message_id: string;
90
+ };
91
+ timestamp: string;
92
+ version: string;
93
+ replay_id?: string;
94
+ }
95
+ type EmailWebhookHandlers = {
96
+ [T in EmailWebhookEventType]?: (event: EmailWebhookEvent<T>) => void | Promise<void>;
97
+ };
98
+ interface EmailWebhookHandlerOptions {
99
+ apiKey: string;
100
+ on: EmailWebhookHandlers;
101
+ }
79
102
 
80
103
  declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
81
104
 
@@ -94,4 +117,6 @@ declare function setEmailUnsubscribe(request: SetEmailUnsubscribeRequest, option
94
117
 
95
118
  declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
96
119
 
97
- export { EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
120
+ declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
121
+
122
+ export { EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createEmailWebhookHandler, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
package/dist/index.d.ts CHANGED
@@ -76,6 +76,29 @@ interface EmailUnsubscribeResponse {
76
76
  domain: string;
77
77
  subscribed: boolean;
78
78
  }
79
+ type EmailWebhookEventType = "email.bounced" | "email.complaint" | "email.unsubscribed" | "email.resubscribed";
80
+ interface EmailWebhookEvent<T extends EmailWebhookEventType = EmailWebhookEventType> {
81
+ event_id: string;
82
+ run_id: string;
83
+ type: T;
84
+ project_id: string;
85
+ environment: "dev" | "prod";
86
+ data: {
87
+ event: T;
88
+ recipient: string;
89
+ message_id: string;
90
+ };
91
+ timestamp: string;
92
+ version: string;
93
+ replay_id?: string;
94
+ }
95
+ type EmailWebhookHandlers = {
96
+ [T in EmailWebhookEventType]?: (event: EmailWebhookEvent<T>) => void | Promise<void>;
97
+ };
98
+ interface EmailWebhookHandlerOptions {
99
+ apiKey: string;
100
+ on: EmailWebhookHandlers;
101
+ }
79
102
 
80
103
  declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
81
104
 
@@ -94,4 +117,6 @@ declare function setEmailUnsubscribe(request: SetEmailUnsubscribeRequest, option
94
117
 
95
118
  declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
96
119
 
97
- export { EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
120
+ declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
121
+
122
+ export { EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createEmailWebhookHandler, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
package/dist/index.js CHANGED
@@ -159,8 +159,59 @@ function parseEmailWebhookPayload(body) {
159
159
  }
160
160
  return payload;
161
161
  }
162
+
163
+ // src/webhook-handler.ts
164
+ import { WebhookError, verifyWebhookRequest } from "@lovable.dev/webhooks-js";
165
+ var WEBHOOK_ERROR_STATUS = {
166
+ missing_secret: 401,
167
+ missing_timestamp: 401,
168
+ invalid_timestamp: 401,
169
+ stale_timestamp: 401,
170
+ invalid_signature: 401,
171
+ body_too_large: 400,
172
+ invalid_json: 400,
173
+ invalid_payload: 400
174
+ };
175
+ function parseEmailWebhookEvent(body) {
176
+ return parseEmailWebhookPayload(body);
177
+ }
178
+ function createEmailWebhookHandler(options) {
179
+ const { apiKey, on } = options;
180
+ if (!apiKey) {
181
+ throw new Error("Missing Lovable API key");
182
+ }
183
+ return async (req) => {
184
+ if (req.method !== "POST") {
185
+ return Response.json({ error: "Method not allowed" }, { status: 405, headers: { Allow: "POST" } });
186
+ }
187
+ let event;
188
+ try {
189
+ ({ payload: event } = await verifyWebhookRequest({ req, secret: apiKey, parser: parseEmailWebhookEvent }));
190
+ } catch (error) {
191
+ if (error instanceof WebhookError) {
192
+ return Response.json({ error: error.message }, { status: WEBHOOK_ERROR_STATUS[error.code] });
193
+ }
194
+ console.error("[email-js] webhook verification failed:", error);
195
+ return Response.json({ error: "Webhook verification failed" }, { status: 500 });
196
+ }
197
+ if (event.version !== "1") {
198
+ return Response.json({ received: true });
199
+ }
200
+ const handler = on[event.type];
201
+ if (handler) {
202
+ try {
203
+ await handler(event);
204
+ } catch (error) {
205
+ console.error("[email-js] webhook handler failed:", error);
206
+ return Response.json({ error: "Handler failed" }, { status: 500 });
207
+ }
208
+ }
209
+ return Response.json({ received: true });
210
+ };
211
+ }
162
212
  export {
163
213
  EmailAPIError,
214
+ createEmailWebhookHandler,
164
215
  getEmailUnsubscribe,
165
216
  listEmailLogs,
166
217
  parseEmailWebhookPayload,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lovable.dev/email-js",
3
- "version": "0.1.0-rc.1",
3
+ "version": "0.1.0-rc.2",
4
4
  "description": "Lovable Email JS",
5
5
  "repository": {
6
6
  "type": "git",