@lovable.dev/email-js 0.0.1 → 0.0.3
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 +31 -2
- package/dist/index.d.cts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +30 -2
- package/package.json +11 -10
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
|
}
|
|
@@ -41,7 +65,7 @@ async function sendLovableEmail(payload, options) {
|
|
|
41
65
|
const sendUrl = options.sendUrl;
|
|
42
66
|
const apiBaseUrl = options.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
43
67
|
const url = sendUrl || `${apiBaseUrl.replace(/\/$/, "")}${DEFAULT_SEND_PATH}`;
|
|
44
|
-
const idempotencyKey = options.idempotencyKey ?? payload.run_id;
|
|
68
|
+
const idempotencyKey = options.idempotencyKey ?? payload.idempotency_key ?? payload.run_id;
|
|
45
69
|
const headers = {
|
|
46
70
|
[authHeader]: buildAuthHeaderValue(apiKey),
|
|
47
71
|
"Content-Type": "application/json"
|
|
@@ -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
|
@@ -2,7 +2,7 @@ import { EmailWebhookPayload } from '@lovable.dev/webhooks-js';
|
|
|
2
2
|
export { EmailWebhookPayload } from '@lovable.dev/webhooks-js';
|
|
3
3
|
|
|
4
4
|
interface EmailSendRequest {
|
|
5
|
-
run_id
|
|
5
|
+
run_id?: string;
|
|
6
6
|
to: string;
|
|
7
7
|
from: string;
|
|
8
8
|
sender_domain?: string;
|
|
@@ -13,6 +13,10 @@ interface EmailSendRequest {
|
|
|
13
13
|
reply_to?: string;
|
|
14
14
|
identity_id?: string;
|
|
15
15
|
test_mode?: boolean;
|
|
16
|
+
label?: string;
|
|
17
|
+
idempotency_key?: string;
|
|
18
|
+
unsubscribe_token?: string;
|
|
19
|
+
message_id?: string;
|
|
16
20
|
}
|
|
17
21
|
interface EmailSendResponse {
|
|
18
22
|
success: boolean;
|
|
@@ -28,8 +32,14 @@ interface SendEmailOptions {
|
|
|
28
32
|
idempotencyKey?: string;
|
|
29
33
|
}
|
|
30
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
|
+
}
|
|
31
41
|
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
32
42
|
|
|
33
43
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
34
44
|
|
|
35
|
-
export { EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
|
45
|
+
export { EmailAPIError, EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { EmailWebhookPayload } from '@lovable.dev/webhooks-js';
|
|
|
2
2
|
export { EmailWebhookPayload } from '@lovable.dev/webhooks-js';
|
|
3
3
|
|
|
4
4
|
interface EmailSendRequest {
|
|
5
|
-
run_id
|
|
5
|
+
run_id?: string;
|
|
6
6
|
to: string;
|
|
7
7
|
from: string;
|
|
8
8
|
sender_domain?: string;
|
|
@@ -13,6 +13,10 @@ interface EmailSendRequest {
|
|
|
13
13
|
reply_to?: string;
|
|
14
14
|
identity_id?: string;
|
|
15
15
|
test_mode?: boolean;
|
|
16
|
+
label?: string;
|
|
17
|
+
idempotency_key?: string;
|
|
18
|
+
unsubscribe_token?: string;
|
|
19
|
+
message_id?: string;
|
|
16
20
|
}
|
|
17
21
|
interface EmailSendResponse {
|
|
18
22
|
success: boolean;
|
|
@@ -28,8 +32,14 @@ interface SendEmailOptions {
|
|
|
28
32
|
idempotencyKey?: string;
|
|
29
33
|
}
|
|
30
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
|
+
}
|
|
31
41
|
declare function sendLovableEmail(payload: EmailSendRequest, options: SendEmailOptions): Promise<EmailSendResponse>;
|
|
32
42
|
|
|
33
43
|
declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload;
|
|
34
44
|
|
|
35
|
-
export { EmailSendRequest, EmailSendResponse, SendEmailOptions, parseEmailWebhookPayload, sendLovableEmail };
|
|
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
|
}
|
|
@@ -14,7 +37,7 @@ async function sendLovableEmail(payload, options) {
|
|
|
14
37
|
const sendUrl = options.sendUrl;
|
|
15
38
|
const apiBaseUrl = options.apiBaseUrl ?? DEFAULT_API_BASE_URL;
|
|
16
39
|
const url = sendUrl || `${apiBaseUrl.replace(/\/$/, "")}${DEFAULT_SEND_PATH}`;
|
|
17
|
-
const idempotencyKey = options.idempotencyKey ?? payload.run_id;
|
|
40
|
+
const idempotencyKey = options.idempotencyKey ?? payload.idempotency_key ?? payload.run_id;
|
|
18
41
|
const headers = {
|
|
19
42
|
[authHeader]: buildAuthHeaderValue(apiKey),
|
|
20
43
|
"Content-Type": "application/json"
|
|
@@ -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
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovable.dev/email-js",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Lovable Email JS",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -9,6 +9,14 @@
|
|
|
9
9
|
"files": [
|
|
10
10
|
"dist"
|
|
11
11
|
],
|
|
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
|
+
},
|
|
12
20
|
"keywords": [
|
|
13
21
|
"lovable",
|
|
14
22
|
"email",
|
|
@@ -16,7 +24,7 @@
|
|
|
16
24
|
],
|
|
17
25
|
"license": "MIT",
|
|
18
26
|
"dependencies": {
|
|
19
|
-
"@lovable.dev/webhooks-js": "
|
|
27
|
+
"@lovable.dev/webhooks-js": "0.0.1"
|
|
20
28
|
},
|
|
21
29
|
"devDependencies": {
|
|
22
30
|
"@types/node": "^22",
|
|
@@ -24,12 +32,5 @@
|
|
|
24
32
|
"tsup": "^7.2.0",
|
|
25
33
|
"typescript": "^5",
|
|
26
34
|
"vitest": "^3"
|
|
27
|
-
},
|
|
28
|
-
"scripts": {
|
|
29
|
-
"build": "tsup src/index.ts --format cjs,esm --dts --outDir dist",
|
|
30
|
-
"dev": "tsup src/index.ts --format cjs,esm --dts --watch",
|
|
31
|
-
"test": "vitest run",
|
|
32
|
-
"typecheck": "tsc --noEmit",
|
|
33
|
-
"clean": "rimraf dist"
|
|
34
35
|
}
|
|
35
|
-
}
|
|
36
|
+
}
|