@lovable.dev/email-js 0.0.2 → 0.0.4
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 +28 -1
- package/dist/index.cjs +30 -1
- package/dist/index.d.cts +8 -2
- package/dist/index.d.ts +8 -2
- package/dist/index.js +29 -1
- package/package.json +1 -1
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,26 @@ 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
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
EmailAPIError: () => EmailAPIError,
|
|
23
24
|
parseEmailWebhookPayload: () => parseEmailWebhookPayload,
|
|
24
25
|
sendLovableEmail: () => sendLovableEmail
|
|
25
26
|
});
|
|
@@ -29,6 +30,29 @@ module.exports = __toCommonJS(src_exports);
|
|
|
29
30
|
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
30
31
|
var DEFAULT_API_BASE_URL = "https://api.lovable.dev";
|
|
31
32
|
var DEFAULT_SEND_PATH = "/v1/messaging/email/send";
|
|
33
|
+
var EmailAPIError = class extends Error {
|
|
34
|
+
constructor(status, message, retryAfterSeconds) {
|
|
35
|
+
super(message);
|
|
36
|
+
this.name = "EmailAPIError";
|
|
37
|
+
this.status = status;
|
|
38
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
39
|
+
}
|
|
40
|
+
get retryable() {
|
|
41
|
+
return this.status === 429 || this.status >= 500 && this.status < 600;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
function parseRetryAfter(header) {
|
|
45
|
+
if (!header)
|
|
46
|
+
return null;
|
|
47
|
+
const parsed = Number(header);
|
|
48
|
+
if (!Number.isNaN(parsed))
|
|
49
|
+
return parsed;
|
|
50
|
+
const date = new Date(header);
|
|
51
|
+
if (!Number.isNaN(date.getTime())) {
|
|
52
|
+
return Math.max(0, Math.ceil((date.getTime() - Date.now()) / 1e3));
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
32
56
|
function buildAuthHeaderValue(apiKey) {
|
|
33
57
|
return `Bearer ${apiKey}`;
|
|
34
58
|
}
|
|
@@ -57,7 +81,11 @@ async function sendLovableEmail(payload, options) {
|
|
|
57
81
|
if (!response.ok) {
|
|
58
82
|
const errorText = await response.text();
|
|
59
83
|
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
60
|
-
throw new
|
|
84
|
+
throw new EmailAPIError(
|
|
85
|
+
response.status,
|
|
86
|
+
`Email API error: ${response.status} ${safeErrorText}`,
|
|
87
|
+
parseRetryAfter(response.headers.get("Retry-After"))
|
|
88
|
+
);
|
|
61
89
|
}
|
|
62
90
|
return await response.json();
|
|
63
91
|
}
|
|
@@ -76,6 +104,7 @@ function parseEmailWebhookPayload(body) {
|
|
|
76
104
|
}
|
|
77
105
|
// Annotate the CommonJS export names for ESM import in node:
|
|
78
106
|
0 && (module.exports = {
|
|
107
|
+
EmailAPIError,
|
|
79
108
|
parseEmailWebhookPayload,
|
|
80
109
|
sendLovableEmail
|
|
81
110
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -14,9 +14,9 @@ interface EmailSendRequest {
|
|
|
14
14
|
identity_id?: string;
|
|
15
15
|
test_mode?: boolean;
|
|
16
16
|
label?: string;
|
|
17
|
-
external_id?: string;
|
|
18
17
|
idempotency_key?: string;
|
|
19
18
|
unsubscribe_token?: string;
|
|
19
|
+
message_id?: string;
|
|
20
20
|
}
|
|
21
21
|
interface EmailSendResponse {
|
|
22
22
|
success: boolean;
|
|
@@ -32,8 +32,14 @@ interface SendEmailOptions {
|
|
|
32
32
|
idempotencyKey?: string;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
declare class EmailAPIError extends Error {
|
|
36
|
+
readonly status: number;
|
|
37
|
+
readonly retryAfterSeconds: number | null;
|
|
38
|
+
constructor(status: number, message: string, retryAfterSeconds: number | null);
|
|
39
|
+
get retryable(): boolean;
|
|
40
|
+
}
|
|
35
41
|
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
36
42
|
|
|
37
43
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
38
44
|
|
|
39
|
-
export {
|
|
45
|
+
export { EmailAPIError, EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,9 +14,9 @@ interface EmailSendRequest {
|
|
|
14
14
|
identity_id?: string;
|
|
15
15
|
test_mode?: boolean;
|
|
16
16
|
label?: string;
|
|
17
|
-
external_id?: string;
|
|
18
17
|
idempotency_key?: string;
|
|
19
18
|
unsubscribe_token?: string;
|
|
19
|
+
message_id?: string;
|
|
20
20
|
}
|
|
21
21
|
interface EmailSendResponse {
|
|
22
22
|
success: boolean;
|
|
@@ -32,8 +32,14 @@ interface SendEmailOptions {
|
|
|
32
32
|
idempotencyKey?: string;
|
|
33
33
|
}
|
|
34
34
|
|
|
35
|
+
declare class EmailAPIError extends Error {
|
|
36
|
+
readonly status: number;
|
|
37
|
+
readonly retryAfterSeconds: number | null;
|
|
38
|
+
constructor(status: number, message: string, retryAfterSeconds: number | null);
|
|
39
|
+
get retryable(): boolean;
|
|
40
|
+
}
|
|
35
41
|
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
36
42
|
|
|
37
43
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
38
44
|
|
|
39
|
-
export {
|
|
45
|
+
export { EmailAPIError, EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
package/dist/index.js
CHANGED
|
@@ -2,6 +2,29 @@
|
|
|
2
2
|
var DEFAULT_AUTH_HEADER = "Authorization";
|
|
3
3
|
var DEFAULT_API_BASE_URL = "https://api.lovable.dev";
|
|
4
4
|
var DEFAULT_SEND_PATH = "/v1/messaging/email/send";
|
|
5
|
+
var EmailAPIError = class extends Error {
|
|
6
|
+
constructor(status, message, retryAfterSeconds) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = "EmailAPIError";
|
|
9
|
+
this.status = status;
|
|
10
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
11
|
+
}
|
|
12
|
+
get retryable() {
|
|
13
|
+
return this.status === 429 || this.status >= 500 && this.status < 600;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function parseRetryAfter(header) {
|
|
17
|
+
if (!header)
|
|
18
|
+
return null;
|
|
19
|
+
const parsed = Number(header);
|
|
20
|
+
if (!Number.isNaN(parsed))
|
|
21
|
+
return parsed;
|
|
22
|
+
const date = new Date(header);
|
|
23
|
+
if (!Number.isNaN(date.getTime())) {
|
|
24
|
+
return Math.max(0, Math.ceil((date.getTime() - Date.now()) / 1e3));
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
5
28
|
function buildAuthHeaderValue(apiKey) {
|
|
6
29
|
return `Bearer ${apiKey}`;
|
|
7
30
|
}
|
|
@@ -30,7 +53,11 @@ async function sendLovableEmail(payload, options) {
|
|
|
30
53
|
if (!response.ok) {
|
|
31
54
|
const errorText = await response.text();
|
|
32
55
|
const safeErrorText = errorText.length > 500 ? `${errorText.slice(0, 500)}...` : errorText;
|
|
33
|
-
throw new
|
|
56
|
+
throw new EmailAPIError(
|
|
57
|
+
response.status,
|
|
58
|
+
`Email API error: ${response.status} ${safeErrorText}`,
|
|
59
|
+
parseRetryAfter(response.headers.get("Retry-After"))
|
|
60
|
+
);
|
|
34
61
|
}
|
|
35
62
|
return await response.json();
|
|
36
63
|
}
|
|
@@ -48,6 +75,7 @@ function parseEmailWebhookPayload(body) {
|
|
|
48
75
|
return payload;
|
|
49
76
|
}
|
|
50
77
|
export {
|
|
78
|
+
EmailAPIError,
|
|
51
79
|
parseEmailWebhookPayload,
|
|
52
80
|
sendLovableEmail
|
|
53
81
|
};
|