@lovable.dev/email-js 0.0.3 → 0.1.0-rc.1
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 +71 -1
- package/dist/index.cjs +107 -16
- package/dist/index.d.cts +55 -3
- package/dist/index.d.ts +55 -3
- package/dist/index.js +103 -15
- package/package.json +16 -11
package/README.md
CHANGED
|
@@ -10,6 +10,11 @@ npm install @lovable.dev/email-js
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
+
### Sending with a `run_id` (build-context emails)
|
|
14
|
+
|
|
15
|
+
When sending from a webhook where a build run exists, pass the `run_id` from
|
|
16
|
+
the payload. You can override the idempotency key (defaults to `run_id`).
|
|
17
|
+
|
|
13
18
|
```ts
|
|
14
19
|
import { parseEmailWebhookPayload, sendLovableEmail } from "@lovable.dev/email-js";
|
|
15
20
|
import { verifyWebhookRequest, type EmailWebhookPayload } from "@lovable.dev/webhooks-js";
|
|
@@ -50,4 +55,69 @@ export async function handleWebhook(req: Request) {
|
|
|
50
55
|
}
|
|
51
56
|
```
|
|
52
57
|
|
|
53
|
-
|
|
58
|
+
### Sending without a `run_id` (runtime-triggered emails)
|
|
59
|
+
|
|
60
|
+
For transactional emails sent outside a build context (e.g., webhook handlers,
|
|
61
|
+
admin actions, database triggers), `run_id` is not required. Provide an
|
|
62
|
+
`idempotency_key` with `purpose: "transactional"` and the API creates a
|
|
63
|
+
transactional run automatically:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
await sendLovableEmail(
|
|
67
|
+
{
|
|
68
|
+
to: "user@example.com",
|
|
69
|
+
from: "App <noreply@notify.example.com>",
|
|
70
|
+
sender_domain: "notify.example.com",
|
|
71
|
+
subject: "Your request was approved",
|
|
72
|
+
html: "<p>Approved!</p>",
|
|
73
|
+
text: "Approved!",
|
|
74
|
+
purpose: "transactional",
|
|
75
|
+
idempotency_key: `approval-${requestId}`,
|
|
76
|
+
unsubscribe_token: token,
|
|
77
|
+
},
|
|
78
|
+
{ apiKey },
|
|
79
|
+
);
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
All clients take the client options (`{ apiKey, apiBaseUrl?, authHeader? }`)
|
|
83
|
+
as their last argument.
|
|
84
|
+
|
|
85
|
+
### Listing email logs
|
|
86
|
+
|
|
87
|
+
`listEmailLogs` returns recent delivery events for the calling project.
|
|
88
|
+
`since`/`until` are RFC3339 timestamps, clamped server-side to the workspace
|
|
89
|
+
plan's retention window. Results are cursor-paginated (max 100 per page).
|
|
90
|
+
|
|
91
|
+
```ts
|
|
92
|
+
import { listEmailLogs } from "@lovable.dev/email-js";
|
|
93
|
+
|
|
94
|
+
const { data, pagination } = await listEmailLogs(
|
|
95
|
+
{ recipient: "user@example.com", event_type: "delivered", since: "2026-06-01T00:00:00Z", limit: 50 },
|
|
96
|
+
{ apiKey },
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
if (pagination.next_cursor) {
|
|
100
|
+
const nextPage = await listEmailLogs({ cursor: pagination.next_cursor }, { apiKey });
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Managing unsubscribes
|
|
105
|
+
|
|
106
|
+
Unsubscribe state is scoped to the recipient and the sender domain's apex.
|
|
107
|
+
Setting `subscribed: true` lifts a sender-driven unsubscribe; recipients who
|
|
108
|
+
filed a spam complaint cannot be resubscribed — the API responds 409 and the
|
|
109
|
+
thrown `EmailAPIError` has `code === "complaint_not_liftable"`.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import { getEmailUnsubscribe, setEmailUnsubscribe } from "@lovable.dev/email-js";
|
|
113
|
+
|
|
114
|
+
const state = await getEmailUnsubscribe(
|
|
115
|
+
{ recipient: "user@example.com", domain: "notify.example.com" },
|
|
116
|
+
{ apiKey },
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
await setEmailUnsubscribe(
|
|
120
|
+
{ recipient: "user@example.com", domain: "notify.example.com", subscribed: false },
|
|
121
|
+
{ apiKey },
|
|
122
|
+
);
|
|
123
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -21,21 +21,27 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
23
|
EmailAPIError: () => EmailAPIError,
|
|
24
|
+
getEmailUnsubscribe: () => getEmailUnsubscribe,
|
|
25
|
+
listEmailLogs: () => listEmailLogs,
|
|
24
26
|
parseEmailWebhookPayload: () => parseEmailWebhookPayload,
|
|
25
|
-
sendLovableEmail: () => sendLovableEmail
|
|
27
|
+
sendLovableEmail: () => sendLovableEmail,
|
|
28
|
+
setEmailUnsubscribe: () => setEmailUnsubscribe
|
|
26
29
|
});
|
|
27
30
|
module.exports = __toCommonJS(src_exports);
|
|
28
31
|
|
|
29
|
-
// src/
|
|
32
|
+
// src/http.ts
|
|
30
33
|
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
31
34
|
var DEFAULT_API_BASE_URL = "https://api.lovable.dev";
|
|
32
|
-
|
|
35
|
+
function resolveApiBaseUrl(apiBaseUrl) {
|
|
36
|
+
return (apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/$/, "");
|
|
37
|
+
}
|
|
33
38
|
var EmailAPIError = class extends Error {
|
|
34
|
-
constructor(status, message, retryAfterSeconds) {
|
|
39
|
+
constructor(status, message, retryAfterSeconds, code = null) {
|
|
35
40
|
super(message);
|
|
36
41
|
this.name = "EmailAPIError";
|
|
37
42
|
this.status = status;
|
|
38
43
|
this.retryAfterSeconds = retryAfterSeconds;
|
|
44
|
+
this.code = code;
|
|
39
45
|
}
|
|
40
46
|
get retryable() {
|
|
41
47
|
return this.status === 429 || this.status >= 500 && this.status < 600;
|
|
@@ -56,6 +62,50 @@ function parseRetryAfter(header) {
|
|
|
56
62
|
function buildAuthHeaderValue(apiKey) {
|
|
57
63
|
return `Bearer ${apiKey}`;
|
|
58
64
|
}
|
|
65
|
+
function parseErrorCode(body) {
|
|
66
|
+
try {
|
|
67
|
+
const parsed = JSON.parse(body);
|
|
68
|
+
if (parsed && typeof parsed === "object" && "type" in parsed && typeof parsed.type === "string") {
|
|
69
|
+
return parsed.type;
|
|
70
|
+
}
|
|
71
|
+
} catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
async function throwIfNotOk(response) {
|
|
77
|
+
if (response.ok)
|
|
78
|
+
return;
|
|
79
|
+
const errorText = await response.text();
|
|
80
|
+
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
81
|
+
throw new EmailAPIError(
|
|
82
|
+
response.status,
|
|
83
|
+
`Email API error: ${response.status} ${safeErrorText}`,
|
|
84
|
+
parseRetryAfter(response.headers.get("Retry-After")),
|
|
85
|
+
parseErrorCode(errorText)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
async function emailApiRequest(options, path, init = { method: "GET" }) {
|
|
89
|
+
if (!options.apiKey) {
|
|
90
|
+
throw new Error("Missing Lovable API key");
|
|
91
|
+
}
|
|
92
|
+
const headers = {
|
|
93
|
+
[options.authHeader ?? DEFAULT_AUTH_HEADER]: buildAuthHeaderValue(options.apiKey)
|
|
94
|
+
};
|
|
95
|
+
if (init.body !== void 0) {
|
|
96
|
+
headers["Content-Type"] = "application/json";
|
|
97
|
+
}
|
|
98
|
+
const response = await fetch(`${resolveApiBaseUrl(options.apiBaseUrl)}${path}`, {
|
|
99
|
+
method: init.method,
|
|
100
|
+
headers,
|
|
101
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
102
|
+
});
|
|
103
|
+
await throwIfNotOk(response);
|
|
104
|
+
return await response.json();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/email.ts
|
|
108
|
+
var DEFAULT_SEND_PATH = "/v1/messaging/email/send";
|
|
59
109
|
async function sendLovableEmail(payload, options) {
|
|
60
110
|
const apiKey = options.apiKey;
|
|
61
111
|
if (!apiKey) {
|
|
@@ -63,8 +113,7 @@ async function sendLovableEmail(payload, options) {
|
|
|
63
113
|
}
|
|
64
114
|
const authHeader = options.authHeader ?? DEFAULT_AUTH_HEADER;
|
|
65
115
|
const sendUrl = options.sendUrl;
|
|
66
|
-
const
|
|
67
|
-
const url = sendUrl || `${apiBaseUrl.replace(/\/$/, "")}${DEFAULT_SEND_PATH}`;
|
|
116
|
+
const url = sendUrl || `${resolveApiBaseUrl(options.apiBaseUrl)}${DEFAULT_SEND_PATH}`;
|
|
68
117
|
const idempotencyKey = options.idempotencyKey ?? payload.idempotency_key ?? payload.run_id;
|
|
69
118
|
const headers = {
|
|
70
119
|
[authHeader]: buildAuthHeaderValue(apiKey),
|
|
@@ -78,18 +127,57 @@ async function sendLovableEmail(payload, options) {
|
|
|
78
127
|
headers,
|
|
79
128
|
body: JSON.stringify(payload)
|
|
80
129
|
});
|
|
81
|
-
|
|
82
|
-
const errorText = await response.text();
|
|
83
|
-
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
84
|
-
throw new EmailAPIError(
|
|
85
|
-
response.status,
|
|
86
|
-
`Email API error: ${response.status} ${safeErrorText}`,
|
|
87
|
-
parseRetryAfter(response.headers.get("Retry-After"))
|
|
88
|
-
);
|
|
89
|
-
}
|
|
130
|
+
await throwIfNotOk(response);
|
|
90
131
|
return await response.json();
|
|
91
132
|
}
|
|
92
133
|
|
|
134
|
+
// src/logs.ts
|
|
135
|
+
var LOGS_PATH = "/v1/messaging/email/logs";
|
|
136
|
+
function buildLogsQuery(filters) {
|
|
137
|
+
const params = new URLSearchParams();
|
|
138
|
+
if (filters.recipient)
|
|
139
|
+
params.set("recipient", filters.recipient);
|
|
140
|
+
if (filters.event_type)
|
|
141
|
+
params.set("event_type", filters.event_type);
|
|
142
|
+
if (filters.status)
|
|
143
|
+
params.set("status", filters.status);
|
|
144
|
+
if (filters.tag)
|
|
145
|
+
params.set("tag", filters.tag);
|
|
146
|
+
if (filters.since)
|
|
147
|
+
params.set("since", filters.since);
|
|
148
|
+
if (filters.until)
|
|
149
|
+
params.set("until", filters.until);
|
|
150
|
+
if (filters.limit !== void 0)
|
|
151
|
+
params.set("limit", String(filters.limit));
|
|
152
|
+
if (filters.cursor)
|
|
153
|
+
params.set("cursor", filters.cursor);
|
|
154
|
+
const query = params.toString();
|
|
155
|
+
return query ? `?${query}` : "";
|
|
156
|
+
}
|
|
157
|
+
async function listEmailLogs(filters, options) {
|
|
158
|
+
const body = await emailApiRequest(options, `${LOGS_PATH}${buildLogsQuery(filters)}`);
|
|
159
|
+
return { ...body, data: body.data ?? [] };
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/unsubscribe.ts
|
|
163
|
+
var UNSUBSCRIBES_PATH = "/v1/messaging/email/unsubscribes";
|
|
164
|
+
function buildUnsubscribePath(recipient, domain) {
|
|
165
|
+
if (!recipient.includes("@")) {
|
|
166
|
+
throw new Error("Invalid recipient");
|
|
167
|
+
}
|
|
168
|
+
const query = new URLSearchParams({ domain }).toString();
|
|
169
|
+
return `${UNSUBSCRIBES_PATH}/${encodeURIComponent(recipient)}?${query}`;
|
|
170
|
+
}
|
|
171
|
+
async function getEmailUnsubscribe(request, options) {
|
|
172
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain));
|
|
173
|
+
}
|
|
174
|
+
async function setEmailUnsubscribe(request, options) {
|
|
175
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain), {
|
|
176
|
+
method: "PUT",
|
|
177
|
+
body: { subscribed: request.subscribed }
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
93
181
|
// src/webhook.ts
|
|
94
182
|
function parseEmailWebhookPayload(body) {
|
|
95
183
|
const parsed = JSON.parse(body);
|
|
@@ -105,6 +193,9 @@ function parseEmailWebhookPayload(body) {
|
|
|
105
193
|
// Annotate the CommonJS export names for ESM import in node:
|
|
106
194
|
0 && (module.exports = {
|
|
107
195
|
EmailAPIError,
|
|
196
|
+
getEmailUnsubscribe,
|
|
197
|
+
listEmailLogs,
|
|
108
198
|
parseEmailWebhookPayload,
|
|
109
|
-
sendLovableEmail
|
|
199
|
+
sendLovableEmail,
|
|
200
|
+
setEmailUnsubscribe
|
|
110
201
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -31,15 +31,67 @@ interface SendEmailOptions {
|
|
|
31
31
|
sendUrl?: string;
|
|
32
32
|
idempotencyKey?: string;
|
|
33
33
|
}
|
|
34
|
+
interface EmailClientOptions {
|
|
35
|
+
apiKey: string;
|
|
36
|
+
authHeader?: string;
|
|
37
|
+
apiBaseUrl?: string;
|
|
38
|
+
}
|
|
39
|
+
interface ListEmailLogsFilters {
|
|
40
|
+
recipient?: string;
|
|
41
|
+
event_type?: string;
|
|
42
|
+
status?: string;
|
|
43
|
+
tag?: string;
|
|
44
|
+
since?: string;
|
|
45
|
+
until?: string;
|
|
46
|
+
limit?: number;
|
|
47
|
+
cursor?: string;
|
|
48
|
+
}
|
|
49
|
+
interface EmailEvent {
|
|
50
|
+
timestamp: string;
|
|
51
|
+
recipient: string;
|
|
52
|
+
event_type: string;
|
|
53
|
+
status?: string;
|
|
54
|
+
message_id?: string;
|
|
55
|
+
tags?: string[] | null;
|
|
56
|
+
}
|
|
57
|
+
interface EmailLogsPagination {
|
|
58
|
+
next_cursor: string | null;
|
|
59
|
+
has_more: boolean;
|
|
60
|
+
}
|
|
61
|
+
interface ListEmailLogsResponse {
|
|
62
|
+
data: EmailEvent[];
|
|
63
|
+
pagination: EmailLogsPagination;
|
|
64
|
+
}
|
|
65
|
+
interface GetEmailUnsubscribeRequest {
|
|
66
|
+
recipient: string;
|
|
67
|
+
domain: string;
|
|
68
|
+
}
|
|
69
|
+
interface SetEmailUnsubscribeRequest {
|
|
70
|
+
recipient: string;
|
|
71
|
+
domain: string;
|
|
72
|
+
subscribed: boolean;
|
|
73
|
+
}
|
|
74
|
+
interface EmailUnsubscribeResponse {
|
|
75
|
+
recipient: string;
|
|
76
|
+
domain: string;
|
|
77
|
+
subscribed: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
34
81
|
|
|
35
82
|
declare class EmailAPIError extends Error {
|
|
36
83
|
readonly status: number;
|
|
37
84
|
readonly retryAfterSeconds: number | null;
|
|
38
|
-
|
|
85
|
+
readonly code: string | null;
|
|
86
|
+
constructor(status: number, message: string, retryAfterSeconds: number | null, code?: string | null);
|
|
39
87
|
get retryable(): boolean;
|
|
40
88
|
}
|
|
41
|
-
|
|
89
|
+
|
|
90
|
+
declare function listEmailLogs(filters: ListEmailLogsFilters, options: EmailClientOptions): Promise<ListEmailLogsResponse>;
|
|
91
|
+
|
|
92
|
+
declare function getEmailUnsubscribe(request: GetEmailUnsubscribeRequest, options: EmailClientOptions): Promise<EmailUnsubscribeResponse>;
|
|
93
|
+
declare function setEmailUnsubscribe(request: SetEmailUnsubscribeRequest, options: EmailClientOptions): Promise<EmailUnsubscribeResponse>;
|
|
42
94
|
|
|
43
95
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
44
96
|
|
|
45
|
-
export { EmailAPIError, EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
|
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 };
|
package/dist/index.d.ts
CHANGED
|
@@ -31,15 +31,67 @@ interface SendEmailOptions {
|
|
|
31
31
|
sendUrl?: string;
|
|
32
32
|
idempotencyKey?: string;
|
|
33
33
|
}
|
|
34
|
+
interface EmailClientOptions {
|
|
35
|
+
apiKey: string;
|
|
36
|
+
authHeader?: string;
|
|
37
|
+
apiBaseUrl?: string;
|
|
38
|
+
}
|
|
39
|
+
interface ListEmailLogsFilters {
|
|
40
|
+
recipient?: string;
|
|
41
|
+
event_type?: string;
|
|
42
|
+
status?: string;
|
|
43
|
+
tag?: string;
|
|
44
|
+
since?: string;
|
|
45
|
+
until?: string;
|
|
46
|
+
limit?: number;
|
|
47
|
+
cursor?: string;
|
|
48
|
+
}
|
|
49
|
+
interface EmailEvent {
|
|
50
|
+
timestamp: string;
|
|
51
|
+
recipient: string;
|
|
52
|
+
event_type: string;
|
|
53
|
+
status?: string;
|
|
54
|
+
message_id?: string;
|
|
55
|
+
tags?: string[] | null;
|
|
56
|
+
}
|
|
57
|
+
interface EmailLogsPagination {
|
|
58
|
+
next_cursor: string | null;
|
|
59
|
+
has_more: boolean;
|
|
60
|
+
}
|
|
61
|
+
interface ListEmailLogsResponse {
|
|
62
|
+
data: EmailEvent[];
|
|
63
|
+
pagination: EmailLogsPagination;
|
|
64
|
+
}
|
|
65
|
+
interface GetEmailUnsubscribeRequest {
|
|
66
|
+
recipient: string;
|
|
67
|
+
domain: string;
|
|
68
|
+
}
|
|
69
|
+
interface SetEmailUnsubscribeRequest {
|
|
70
|
+
recipient: string;
|
|
71
|
+
domain: string;
|
|
72
|
+
subscribed: boolean;
|
|
73
|
+
}
|
|
74
|
+
interface EmailUnsubscribeResponse {
|
|
75
|
+
recipient: string;
|
|
76
|
+
domain: string;
|
|
77
|
+
subscribed: boolean;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
34
81
|
|
|
35
82
|
declare class EmailAPIError extends Error {
|
|
36
83
|
readonly status: number;
|
|
37
84
|
readonly retryAfterSeconds: number | null;
|
|
38
|
-
|
|
85
|
+
readonly code: string | null;
|
|
86
|
+
constructor(status: number, message: string, retryAfterSeconds: number | null, code?: string | null);
|
|
39
87
|
get retryable(): boolean;
|
|
40
88
|
}
|
|
41
|
-
|
|
89
|
+
|
|
90
|
+
declare function listEmailLogs(filters: ListEmailLogsFilters, options: EmailClientOptions): Promise<ListEmailLogsResponse>;
|
|
91
|
+
|
|
92
|
+
declare function getEmailUnsubscribe(request: GetEmailUnsubscribeRequest, options: EmailClientOptions): Promise<EmailUnsubscribeResponse>;
|
|
93
|
+
declare function setEmailUnsubscribe(request: SetEmailUnsubscribeRequest, options: EmailClientOptions): Promise<EmailUnsubscribeResponse>;
|
|
42
94
|
|
|
43
95
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
44
96
|
|
|
45
|
-
export { EmailAPIError, EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
// src/
|
|
1
|
+
// src/http.ts
|
|
2
2
|
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
3
3
|
var DEFAULT_API_BASE_URL = "https://api.lovable.dev";
|
|
4
|
-
|
|
4
|
+
function resolveApiBaseUrl(apiBaseUrl) {
|
|
5
|
+
return (apiBaseUrl ?? DEFAULT_API_BASE_URL).replace(/\/$/, "");
|
|
6
|
+
}
|
|
5
7
|
var EmailAPIError = class extends Error {
|
|
6
|
-
constructor(status, message, retryAfterSeconds) {
|
|
8
|
+
constructor(status, message, retryAfterSeconds, code = null) {
|
|
7
9
|
super(message);
|
|
8
10
|
this.name = "EmailAPIError";
|
|
9
11
|
this.status = status;
|
|
10
12
|
this.retryAfterSeconds = retryAfterSeconds;
|
|
13
|
+
this.code = code;
|
|
11
14
|
}
|
|
12
15
|
get retryable() {
|
|
13
16
|
return this.status === 429 || this.status >= 500 && this.status < 600;
|
|
@@ -28,6 +31,50 @@ function parseRetryAfter(header) {
|
|
|
28
31
|
function buildAuthHeaderValue(apiKey) {
|
|
29
32
|
return `Bearer ${apiKey}`;
|
|
30
33
|
}
|
|
34
|
+
function parseErrorCode(body) {
|
|
35
|
+
try {
|
|
36
|
+
const parsed = JSON.parse(body);
|
|
37
|
+
if (parsed && typeof parsed === "object" && "type" in parsed && typeof parsed.type === "string") {
|
|
38
|
+
return parsed.type;
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
async function throwIfNotOk(response) {
|
|
46
|
+
if (response.ok)
|
|
47
|
+
return;
|
|
48
|
+
const errorText = await response.text();
|
|
49
|
+
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
50
|
+
throw new EmailAPIError(
|
|
51
|
+
response.status,
|
|
52
|
+
`Email API error: ${response.status} ${safeErrorText}`,
|
|
53
|
+
parseRetryAfter(response.headers.get("Retry-After")),
|
|
54
|
+
parseErrorCode(errorText)
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
async function emailApiRequest(options, path, init = { method: "GET" }) {
|
|
58
|
+
if (!options.apiKey) {
|
|
59
|
+
throw new Error("Missing Lovable API key");
|
|
60
|
+
}
|
|
61
|
+
const headers = {
|
|
62
|
+
[options.authHeader ?? DEFAULT_AUTH_HEADER]: buildAuthHeaderValue(options.apiKey)
|
|
63
|
+
};
|
|
64
|
+
if (init.body !== void 0) {
|
|
65
|
+
headers["Content-Type"] = "application/json";
|
|
66
|
+
}
|
|
67
|
+
const response = await fetch(`${resolveApiBaseUrl(options.apiBaseUrl)}${path}`, {
|
|
68
|
+
method: init.method,
|
|
69
|
+
headers,
|
|
70
|
+
body: init.body === void 0 ? void 0 : JSON.stringify(init.body)
|
|
71
|
+
});
|
|
72
|
+
await throwIfNotOk(response);
|
|
73
|
+
return await response.json();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// src/email.ts
|
|
77
|
+
var DEFAULT_SEND_PATH = "/v1/messaging/email/send";
|
|
31
78
|
async function sendLovableEmail(payload, options) {
|
|
32
79
|
const apiKey = options.apiKey;
|
|
33
80
|
if (!apiKey) {
|
|
@@ -35,8 +82,7 @@ async function sendLovableEmail(payload, options) {
|
|
|
35
82
|
}
|
|
36
83
|
const authHeader = options.authHeader ?? DEFAULT_AUTH_HEADER;
|
|
37
84
|
const sendUrl = options.sendUrl;
|
|
38
|
-
const
|
|
39
|
-
const url = sendUrl || `${apiBaseUrl.replace(/\/$/, "")}${DEFAULT_SEND_PATH}`;
|
|
85
|
+
const url = sendUrl || `${resolveApiBaseUrl(options.apiBaseUrl)}${DEFAULT_SEND_PATH}`;
|
|
40
86
|
const idempotencyKey = options.idempotencyKey ?? payload.idempotency_key ?? payload.run_id;
|
|
41
87
|
const headers = {
|
|
42
88
|
[authHeader]: buildAuthHeaderValue(apiKey),
|
|
@@ -50,18 +96,57 @@ async function sendLovableEmail(payload, options) {
|
|
|
50
96
|
headers,
|
|
51
97
|
body: JSON.stringify(payload)
|
|
52
98
|
});
|
|
53
|
-
|
|
54
|
-
const errorText = await response.text();
|
|
55
|
-
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
56
|
-
throw new EmailAPIError(
|
|
57
|
-
response.status,
|
|
58
|
-
`Email API error: ${response.status} ${safeErrorText}`,
|
|
59
|
-
parseRetryAfter(response.headers.get("Retry-After"))
|
|
60
|
-
);
|
|
61
|
-
}
|
|
99
|
+
await throwIfNotOk(response);
|
|
62
100
|
return await response.json();
|
|
63
101
|
}
|
|
64
102
|
|
|
103
|
+
// src/logs.ts
|
|
104
|
+
var LOGS_PATH = "/v1/messaging/email/logs";
|
|
105
|
+
function buildLogsQuery(filters) {
|
|
106
|
+
const params = new URLSearchParams();
|
|
107
|
+
if (filters.recipient)
|
|
108
|
+
params.set("recipient", filters.recipient);
|
|
109
|
+
if (filters.event_type)
|
|
110
|
+
params.set("event_type", filters.event_type);
|
|
111
|
+
if (filters.status)
|
|
112
|
+
params.set("status", filters.status);
|
|
113
|
+
if (filters.tag)
|
|
114
|
+
params.set("tag", filters.tag);
|
|
115
|
+
if (filters.since)
|
|
116
|
+
params.set("since", filters.since);
|
|
117
|
+
if (filters.until)
|
|
118
|
+
params.set("until", filters.until);
|
|
119
|
+
if (filters.limit !== void 0)
|
|
120
|
+
params.set("limit", String(filters.limit));
|
|
121
|
+
if (filters.cursor)
|
|
122
|
+
params.set("cursor", filters.cursor);
|
|
123
|
+
const query = params.toString();
|
|
124
|
+
return query ? `?${query}` : "";
|
|
125
|
+
}
|
|
126
|
+
async function listEmailLogs(filters, options) {
|
|
127
|
+
const body = await emailApiRequest(options, `${LOGS_PATH}${buildLogsQuery(filters)}`);
|
|
128
|
+
return { ...body, data: body.data ?? [] };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// src/unsubscribe.ts
|
|
132
|
+
var UNSUBSCRIBES_PATH = "/v1/messaging/email/unsubscribes";
|
|
133
|
+
function buildUnsubscribePath(recipient, domain) {
|
|
134
|
+
if (!recipient.includes("@")) {
|
|
135
|
+
throw new Error("Invalid recipient");
|
|
136
|
+
}
|
|
137
|
+
const query = new URLSearchParams({ domain }).toString();
|
|
138
|
+
return `${UNSUBSCRIBES_PATH}/${encodeURIComponent(recipient)}?${query}`;
|
|
139
|
+
}
|
|
140
|
+
async function getEmailUnsubscribe(request, options) {
|
|
141
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain));
|
|
142
|
+
}
|
|
143
|
+
async function setEmailUnsubscribe(request, options) {
|
|
144
|
+
return emailApiRequest(options, buildUnsubscribePath(request.recipient, request.domain), {
|
|
145
|
+
method: "PUT",
|
|
146
|
+
body: { subscribed: request.subscribed }
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
|
|
65
150
|
// src/webhook.ts
|
|
66
151
|
function parseEmailWebhookPayload(body) {
|
|
67
152
|
const parsed = JSON.parse(body);
|
|
@@ -76,6 +161,9 @@ function parseEmailWebhookPayload(body) {
|
|
|
76
161
|
}
|
|
77
162
|
export {
|
|
78
163
|
EmailAPIError,
|
|
164
|
+
getEmailUnsubscribe,
|
|
165
|
+
listEmailLogs,
|
|
79
166
|
parseEmailWebhookPayload,
|
|
80
|
-
sendLovableEmail
|
|
167
|
+
sendLovableEmail,
|
|
168
|
+
setEmailUnsubscribe
|
|
81
169
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovable.dev/email-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.1.0-rc.1",
|
|
4
4
|
"description": "Lovable Email JS",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/lovablelabs/lovable.git",
|
|
8
|
+
"directory": "npm-packages/email-js"
|
|
9
|
+
},
|
|
5
10
|
"main": "./dist/index.cjs",
|
|
6
11
|
"module": "./dist/index.js",
|
|
7
12
|
"types": "./dist/index.d.ts",
|
|
@@ -9,14 +14,6 @@
|
|
|
9
14
|
"files": [
|
|
10
15
|
"dist"
|
|
11
16
|
],
|
|
12
|
-
"scripts": {
|
|
13
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --outDir dist",
|
|
14
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
15
|
-
"test": "vitest run",
|
|
16
|
-
"prepublishOnly": "npm run clean && npm run build",
|
|
17
|
-
"clean": "rimraf dist",
|
|
18
|
-
"typecheck": "tsgo --noEmit"
|
|
19
|
-
},
|
|
20
17
|
"keywords": [
|
|
21
18
|
"lovable",
|
|
22
19
|
"email",
|
|
@@ -31,6 +28,14 @@
|
|
|
31
28
|
"rimraf": "^5.0.0",
|
|
32
29
|
"tsup": "^7.2.0",
|
|
33
30
|
"typescript": "^5",
|
|
34
|
-
"vitest": "^
|
|
31
|
+
"vitest": "^4.1.5",
|
|
32
|
+
"@lovable/api-utils": "1.0.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --outDir dist",
|
|
36
|
+
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
37
|
+
"test": "vitest run",
|
|
38
|
+
"clean": "rimraf dist",
|
|
39
|
+
"typecheck": "tsgo --noEmit"
|
|
35
40
|
}
|
|
36
|
-
}
|
|
41
|
+
}
|