@lovable.dev/email-js 0.1.1-rc.15 → 0.1.1-rc.16
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/dist/index.cjs +220 -313
- package/dist/index.d.cts +116 -112
- package/dist/index.d.ts +116 -112
- package/dist/index.js +218 -275
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -1,352 +1,259 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
|
|
30
|
-
// src/index.ts
|
|
31
|
-
var src_exports = {};
|
|
32
|
-
__export(src_exports, {
|
|
33
|
-
EmailAPIError: () => EmailAPIError,
|
|
34
|
-
createAuthEmailHandler: () => createAuthEmailHandler,
|
|
35
|
-
createEmailWebhookHandler: () => createEmailWebhookHandler,
|
|
36
|
-
getEmailUnsubscribe: () => getEmailUnsubscribe,
|
|
37
|
-
listEmailLogs: () => listEmailLogs,
|
|
38
|
-
parseEmailWebhookPayload: () => parseEmailWebhookPayload,
|
|
39
|
-
sendLovableEmail: () => sendLovableEmail,
|
|
40
|
-
setEmailUnsubscribe: () => setEmailUnsubscribe
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(src_exports);
|
|
43
|
-
|
|
44
|
-
// src/auth-email-handler.ts
|
|
45
|
-
var import_webhooks_js2 = require("@lovable.dev/webhooks-js");
|
|
46
|
-
|
|
47
|
-
// src/http.ts
|
|
48
|
-
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
49
|
-
var DEFAULT_API_BASE_URL = "https://api.lovable.dev";
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let _lovable_dev_webhooks_js = require("@lovable.dev/webhooks-js");
|
|
50
3
|
function resolveApiBaseUrl(apiBaseUrl) {
|
|
51
|
-
|
|
4
|
+
return (apiBaseUrl ?? "https://api.lovable.dev").replace(/\/$/, "");
|
|
52
5
|
}
|
|
53
6
|
var EmailAPIError = class extends Error {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
7
|
+
constructor(status, message, retryAfterSeconds, code = null) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = "EmailAPIError";
|
|
10
|
+
this.status = status;
|
|
11
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
12
|
+
this.code = code;
|
|
13
|
+
}
|
|
14
|
+
get retryable() {
|
|
15
|
+
return this.status === 429 || this.status >= 500 && this.status < 600;
|
|
16
|
+
}
|
|
64
17
|
};
|
|
65
18
|
function parseRetryAfter(header) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
if (!Number.isNaN(date.getTime())) {
|
|
73
|
-
return Math.max(0, Math.ceil((date.getTime() - Date.now()) / 1e3));
|
|
74
|
-
}
|
|
75
|
-
return null;
|
|
19
|
+
if (!header) return null;
|
|
20
|
+
const parsed = Number(header);
|
|
21
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
22
|
+
const date = new Date(header);
|
|
23
|
+
if (!Number.isNaN(date.getTime())) return Math.max(0, Math.ceil((date.getTime() - Date.now()) / 1e3));
|
|
24
|
+
return null;
|
|
76
25
|
}
|
|
77
26
|
function buildAuthHeaderValue(apiKey) {
|
|
78
|
-
|
|
27
|
+
return `Bearer ${apiKey}`;
|
|
79
28
|
}
|
|
80
29
|
function parseErrorCode(body) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
}
|
|
89
|
-
return null;
|
|
30
|
+
try {
|
|
31
|
+
const parsed = JSON.parse(body);
|
|
32
|
+
if (parsed && typeof parsed === "object" && "type" in parsed && typeof parsed.type === "string") return parsed.type;
|
|
33
|
+
} catch {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
90
37
|
}
|
|
91
38
|
async function throwIfNotOk(response) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
`Email API error: ${response.status} ${safeErrorText}`,
|
|
104
|
-
retryAfterSeconds,
|
|
105
|
-
code
|
|
106
|
-
);
|
|
39
|
+
if (response.ok) return;
|
|
40
|
+
const errorText = await response.text();
|
|
41
|
+
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
42
|
+
const retryAfterSeconds = parseRetryAfter(response.headers.get("Retry-After"));
|
|
43
|
+
const code = parseErrorCode(errorText);
|
|
44
|
+
if (response.status === 429) console.error("[email-js] rate limited:", {
|
|
45
|
+
status: response.status,
|
|
46
|
+
code,
|
|
47
|
+
retryAfterSeconds
|
|
48
|
+
});
|
|
49
|
+
throw new EmailAPIError(response.status, `Email API error: ${response.status} ${safeErrorText}`, retryAfterSeconds, code);
|
|
107
50
|
}
|
|
108
51
|
async function emailApiRequest(options, path, init = { method: "GET" }) {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
method: init.method,
|
|
120
|
-
headers,
|
|
121
|
-
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
122
|
-
});
|
|
123
|
-
await throwIfNotOk(response);
|
|
124
|
-
return await response.json();
|
|
52
|
+
if (!options.apiKey) throw new Error("Missing Lovable API key");
|
|
53
|
+
const headers = { [options.authHeader ?? "Authorization"]: buildAuthHeaderValue(options.apiKey) };
|
|
54
|
+
if (init.body !== void 0) headers["Content-Type"] = "application/json";
|
|
55
|
+
const response = await fetch(`${resolveApiBaseUrl(options.apiBaseUrl)}${path}`, {
|
|
56
|
+
method: init.method,
|
|
57
|
+
headers,
|
|
58
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
59
|
+
});
|
|
60
|
+
await throwIfNotOk(response);
|
|
61
|
+
return await response.json();
|
|
125
62
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/email.ts
|
|
65
|
+
const DEFAULT_SEND_PATH = "/v1/messaging/email/send";
|
|
129
66
|
async function sendLovableEmail(payload, options) {
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
headers,
|
|
148
|
-
body: JSON.stringify(payload)
|
|
149
|
-
});
|
|
150
|
-
await throwIfNotOk(response);
|
|
151
|
-
return await response.json();
|
|
67
|
+
const apiKey = options.apiKey;
|
|
68
|
+
if (!apiKey) throw new Error("Missing Lovable API key");
|
|
69
|
+
const authHeader = options.authHeader ?? "Authorization";
|
|
70
|
+
const url = options.sendUrl || `${resolveApiBaseUrl(options.apiBaseUrl)}${DEFAULT_SEND_PATH}`;
|
|
71
|
+
const idempotencyKey = options.idempotencyKey ?? payload.idempotency_key ?? payload.run_id;
|
|
72
|
+
const headers = {
|
|
73
|
+
[authHeader]: buildAuthHeaderValue(apiKey),
|
|
74
|
+
"Content-Type": "application/json"
|
|
75
|
+
};
|
|
76
|
+
if (idempotencyKey) headers["Idempotency-Key"] = idempotencyKey;
|
|
77
|
+
const response = await fetch(url, {
|
|
78
|
+
method: "POST",
|
|
79
|
+
headers,
|
|
80
|
+
body: JSON.stringify(payload)
|
|
81
|
+
});
|
|
82
|
+
await throwIfNotOk(response);
|
|
83
|
+
return await response.json();
|
|
152
84
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
var import_webhooks_js = require("@lovable.dev/webhooks-js");
|
|
156
|
-
|
|
157
|
-
// src/webhook.ts
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/webhook.ts
|
|
158
87
|
function parseEmailWebhookPayload(body) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (typeof payload.version !== "string" || typeof payload.type !== "string") {
|
|
165
|
-
throw new Error("Invalid email webhook payload: missing version or type");
|
|
166
|
-
}
|
|
167
|
-
return payload;
|
|
88
|
+
const parsed = JSON.parse(body);
|
|
89
|
+
if (!parsed || typeof parsed !== "object") throw new Error("Invalid email webhook payload: missing version or type");
|
|
90
|
+
const payload = parsed;
|
|
91
|
+
if (typeof payload.version !== "string" || typeof payload.type !== "string") throw new Error("Invalid email webhook payload: missing version or type");
|
|
92
|
+
return payload;
|
|
168
93
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
94
|
+
//#endregion
|
|
95
|
+
//#region src/webhook-handler.ts
|
|
96
|
+
const WEBHOOK_ERROR_STATUS = {
|
|
97
|
+
missing_secret: 401,
|
|
98
|
+
missing_timestamp: 401,
|
|
99
|
+
invalid_timestamp: 401,
|
|
100
|
+
stale_timestamp: 401,
|
|
101
|
+
invalid_signature: 401,
|
|
102
|
+
body_too_large: 400,
|
|
103
|
+
invalid_json: 400,
|
|
104
|
+
invalid_payload: 400
|
|
180
105
|
};
|
|
181
106
|
function parseEmailWebhookEvent(body) {
|
|
182
|
-
|
|
107
|
+
return parseEmailWebhookPayload(body);
|
|
183
108
|
}
|
|
184
109
|
function createEmailWebhookHandler(options) {
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
}
|
|
215
|
-
return Response.json({ received: true });
|
|
216
|
-
};
|
|
110
|
+
const { apiKey, on } = options;
|
|
111
|
+
if (!apiKey) throw new Error("Missing Lovable API key");
|
|
112
|
+
return async (req) => {
|
|
113
|
+
if (req.method !== "POST") return Response.json({ error: "Method not allowed" }, {
|
|
114
|
+
status: 405,
|
|
115
|
+
headers: { Allow: "POST" }
|
|
116
|
+
});
|
|
117
|
+
let event;
|
|
118
|
+
try {
|
|
119
|
+
({payload: event} = await (0, _lovable_dev_webhooks_js.verifyWebhookRequest)({
|
|
120
|
+
req,
|
|
121
|
+
secret: apiKey,
|
|
122
|
+
parser: parseEmailWebhookEvent
|
|
123
|
+
}));
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (error instanceof _lovable_dev_webhooks_js.WebhookError) return Response.json({ error: error.message }, { status: WEBHOOK_ERROR_STATUS[error.code] });
|
|
126
|
+
console.error("[email-js] webhook verification failed:", error);
|
|
127
|
+
return Response.json({ error: "Webhook verification failed" }, { status: 500 });
|
|
128
|
+
}
|
|
129
|
+
if (event.version !== "1") return Response.json({ received: true });
|
|
130
|
+
const handler = on[event.type];
|
|
131
|
+
if (handler) try {
|
|
132
|
+
await handler(event);
|
|
133
|
+
} catch (error) {
|
|
134
|
+
console.error("[email-js] webhook handler failed:", error);
|
|
135
|
+
return Response.json({ error: "Handler failed" }, { status: 500 });
|
|
136
|
+
}
|
|
137
|
+
return Response.json({ received: true });
|
|
138
|
+
};
|
|
217
139
|
}
|
|
218
|
-
|
|
219
|
-
|
|
140
|
+
//#endregion
|
|
141
|
+
//#region src/auth-email-handler.ts
|
|
220
142
|
function parseAuthEmailWebhookPayload(body) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
return payload;
|
|
143
|
+
const parsed = JSON.parse(body);
|
|
144
|
+
const payload = parsed;
|
|
145
|
+
if (!parsed || typeof parsed !== "object" || typeof payload.version !== "string" || payload.type !== "auth" || !payload.data || typeof payload.data !== "object" || typeof payload.data.action_type !== "string" || typeof payload.data.email !== "string") throw new Error("Invalid auth email webhook payload");
|
|
146
|
+
return payload;
|
|
227
147
|
}
|
|
228
148
|
function domainOfAddress(from) {
|
|
229
|
-
|
|
230
|
-
return match?.[1];
|
|
149
|
+
return /@([^@\s<>]+)>?\s*$/.exec(from.trim())?.[1];
|
|
231
150
|
}
|
|
232
151
|
function createAuthEmailHandler(options) {
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
}
|
|
292
|
-
return Response.json({ success: true, sent: true });
|
|
293
|
-
};
|
|
152
|
+
const { apiKey, from, emails } = options;
|
|
153
|
+
if (!apiKey) throw new Error("Missing Lovable API key");
|
|
154
|
+
const senderDomain = options.senderDomain ?? domainOfAddress(from);
|
|
155
|
+
if (!senderDomain) throw new Error(`Cannot derive a sender domain from "${from}"; pass senderDomain`);
|
|
156
|
+
return async (req) => {
|
|
157
|
+
if (req.method !== "POST") return Response.json({ error: "Method not allowed" }, {
|
|
158
|
+
status: 405,
|
|
159
|
+
headers: { Allow: "POST" }
|
|
160
|
+
});
|
|
161
|
+
let event;
|
|
162
|
+
try {
|
|
163
|
+
({payload: event} = await (0, _lovable_dev_webhooks_js.verifyWebhookRequest)({
|
|
164
|
+
req,
|
|
165
|
+
secret: apiKey,
|
|
166
|
+
parser: parseAuthEmailWebhookPayload
|
|
167
|
+
}));
|
|
168
|
+
} catch (error) {
|
|
169
|
+
if (error instanceof _lovable_dev_webhooks_js.WebhookError) return Response.json({ error: error.message }, { status: WEBHOOK_ERROR_STATUS[error.code] });
|
|
170
|
+
console.error("[email-js] auth webhook verification failed:", error);
|
|
171
|
+
return Response.json({ error: "Webhook verification failed" }, { status: 500 });
|
|
172
|
+
}
|
|
173
|
+
if (!event.run_id) return Response.json({ error: "Missing run_id" }, { status: 400 });
|
|
174
|
+
if (event.version !== "1") return Response.json({ error: `Unsupported payload version: ${event.version}` }, { status: 400 });
|
|
175
|
+
const actionType = event.data.action_type;
|
|
176
|
+
const definition = Object.prototype.hasOwnProperty.call(emails, actionType) ? emails[actionType] : void 0;
|
|
177
|
+
if (!definition) return Response.json({ error: `Unknown auth email action type: ${actionType}` }, { status: 400 });
|
|
178
|
+
try {
|
|
179
|
+
const { render } = await import("@react-email/render");
|
|
180
|
+
const { subject, element } = typeof definition === "function" ? await definition(event.data) : {
|
|
181
|
+
subject: definition.subject,
|
|
182
|
+
element: await definition.render(event.data)
|
|
183
|
+
};
|
|
184
|
+
const html = await render(element);
|
|
185
|
+
const text = await render(element, { plainText: true });
|
|
186
|
+
await sendLovableEmail({
|
|
187
|
+
run_id: event.run_id,
|
|
188
|
+
to: event.data.email,
|
|
189
|
+
from,
|
|
190
|
+
sender_domain: senderDomain,
|
|
191
|
+
subject,
|
|
192
|
+
html,
|
|
193
|
+
text,
|
|
194
|
+
purpose: "transactional",
|
|
195
|
+
label: actionType
|
|
196
|
+
}, {
|
|
197
|
+
apiKey,
|
|
198
|
+
sendUrl: options.sendUrl
|
|
199
|
+
});
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.error("[email-js] auth email send failed:", error);
|
|
202
|
+
if (error instanceof EmailAPIError && !error.retryable) return Response.json({ error: "Email send rejected" }, { status: 400 });
|
|
203
|
+
return Response.json({ error: "Failed to send email" }, { status: 500 });
|
|
204
|
+
}
|
|
205
|
+
return Response.json({
|
|
206
|
+
success: true,
|
|
207
|
+
sent: true
|
|
208
|
+
});
|
|
209
|
+
};
|
|
294
210
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region src/logs.ts
|
|
213
|
+
const LOGS_PATH = "/v1/messaging/email/logs";
|
|
298
214
|
function buildLogsQuery(filters) {
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
if (filters.until)
|
|
311
|
-
params.set("until", filters.until);
|
|
312
|
-
if (filters.limit !== void 0)
|
|
313
|
-
params.set("limit", String(filters.limit));
|
|
314
|
-
if (filters.cursor)
|
|
315
|
-
params.set("cursor", filters.cursor);
|
|
316
|
-
const query = params.toString();
|
|
317
|
-
return query ? `?${query}` : "";
|
|
215
|
+
const params = new URLSearchParams();
|
|
216
|
+
if (filters.recipient) params.set("recipient", filters.recipient);
|
|
217
|
+
if (filters.event_type) params.set("event_type", filters.event_type);
|
|
218
|
+
if (filters.status) params.set("status", filters.status);
|
|
219
|
+
if (filters.tag) params.set("tag", filters.tag);
|
|
220
|
+
if (filters.since) params.set("since", filters.since);
|
|
221
|
+
if (filters.until) params.set("until", filters.until);
|
|
222
|
+
if (filters.limit !== void 0) params.set("limit", String(filters.limit));
|
|
223
|
+
if (filters.cursor) params.set("cursor", filters.cursor);
|
|
224
|
+
const query = params.toString();
|
|
225
|
+
return query ? `?${query}` : "";
|
|
318
226
|
}
|
|
319
227
|
async function listEmailLogs(filters, options) {
|
|
320
|
-
|
|
321
|
-
|
|
228
|
+
const body = await emailApiRequest(options, `${LOGS_PATH}${buildLogsQuery(filters)}`);
|
|
229
|
+
return {
|
|
230
|
+
...body,
|
|
231
|
+
data: body.data ?? []
|
|
232
|
+
};
|
|
322
233
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/unsubscribe.ts
|
|
236
|
+
const UNSUBSCRIBES_PATH = "/v1/messaging/email/unsubscribes";
|
|
326
237
|
function buildUnsubscribePath(recipient, domain) {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
const query = new URLSearchParams({ domain }).toString();
|
|
331
|
-
return `${UNSUBSCRIBES_PATH}/${encodeURIComponent(recipient)}?${query}`;
|
|
238
|
+
if (!recipient.includes("@")) throw new Error("Invalid recipient");
|
|
239
|
+
const query = new URLSearchParams({ domain }).toString();
|
|
240
|
+
return `${UNSUBSCRIBES_PATH}/${encodeURIComponent(recipient)}?${query}`;
|
|
332
241
|
}
|
|
333
242
|
async function getEmailUnsubscribe(request, options) {
|
|
334
|
-
|
|
243
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain));
|
|
335
244
|
}
|
|
336
245
|
async function setEmailUnsubscribe(request, options) {
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
246
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain), {
|
|
247
|
+
method: "PUT",
|
|
248
|
+
body: { subscribed: request.subscribed }
|
|
249
|
+
});
|
|
341
250
|
}
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
setEmailUnsubscribe
|
|
352
|
-
});
|
|
251
|
+
//#endregion
|
|
252
|
+
exports.EmailAPIError = EmailAPIError;
|
|
253
|
+
exports.createAuthEmailHandler = createAuthEmailHandler;
|
|
254
|
+
exports.createEmailWebhookHandler = createEmailWebhookHandler;
|
|
255
|
+
exports.getEmailUnsubscribe = getEmailUnsubscribe;
|
|
256
|
+
exports.listEmailLogs = listEmailLogs;
|
|
257
|
+
exports.parseEmailWebhookPayload = parseEmailWebhookPayload;
|
|
258
|
+
exports.sendLovableEmail = sendLovableEmail;
|
|
259
|
+
exports.setEmailUnsubscribe = setEmailUnsubscribe;
|