@lovable.dev/email-js 0.3.0 → 0.4.0
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 +25 -0
- package/dist/index.cjs +9 -1
- package/dist/index.d.cts +28 -2
- package/dist/index.d.ts +28 -2
- package/dist/index.js +9 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -204,6 +204,7 @@ import { createAuthEmailHandler } from "@lovable.dev/email-js";
|
|
|
204
204
|
const handler = createAuthEmailHandler({
|
|
205
205
|
apiKey: process.env.LOVABLE_API_KEY!,
|
|
206
206
|
from: { name: "My App", address: "noreply@notify.example.com" }, // sender domain derived from the address
|
|
207
|
+
replyTo: { name: "My App Support", address: "support@example.com" },
|
|
207
208
|
sendUrl: process.env.LOVABLE_SEND_URL,
|
|
208
209
|
emails: {
|
|
209
210
|
signup: { subject: "Confirm your email", render: (data) => <SignupEmail confirmationUrl={data.url} /> },
|
|
@@ -216,6 +217,30 @@ const handler = createAuthEmailHandler({
|
|
|
216
217
|
});
|
|
217
218
|
```
|
|
218
219
|
|
|
220
|
+
Projects on Lovable Auth pass `provider: "lovable_auth"`. Lovable Auth sends
|
|
221
|
+
one-time codes rather than links, so the `emails` map is total over
|
|
222
|
+
`signup_confirmation`, `sign_in_otp` and `recovery_otp`, and `render` receives
|
|
223
|
+
`code` and `expires_at` (ISO 8601) instead of `url` and `token`. The default
|
|
224
|
+
provider is `"lovable_cloud"`, the handler shown above.
|
|
225
|
+
|
|
226
|
+
```tsx
|
|
227
|
+
const handler = createAuthEmailHandler({
|
|
228
|
+
provider: "lovable_auth",
|
|
229
|
+
apiKey: process.env.LOVABLE_API_KEY!,
|
|
230
|
+
from: { name: "My App", address: "noreply@notify.example.com" },
|
|
231
|
+
sendUrl: process.env.LOVABLE_SEND_URL,
|
|
232
|
+
emails: {
|
|
233
|
+
signup_confirmation: { subject: "Confirm your email", render: (data) => <CodeEmail code={data.code} expiresAt={data.expires_at} /> },
|
|
234
|
+
sign_in_otp: { subject: "Your sign-in code", render: (data) => <CodeEmail code={data.code} expiresAt={data.expires_at} /> },
|
|
235
|
+
recovery_otp: { subject: "Reset your password", render: (data) => <CodeEmail code={data.code} expiresAt={data.expires_at} /> },
|
|
236
|
+
},
|
|
237
|
+
});
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
`replyTo` takes the same string-or-parts form as `from` and sets the Reply-To
|
|
241
|
+
header, so a user who replies to a confirmation email reaches a monitored
|
|
242
|
+
inbox instead of the noreply sender. Omit it and replies go to `from`.
|
|
243
|
+
|
|
219
244
|
`render` may be async — look up personalization data first, and fall back
|
|
220
245
|
(`?? "friend"`) instead of throwing when it's missing: auth emails are
|
|
221
246
|
load-bearing. A per-type entry can also be a single async function returning
|
package/dist/index.cjs
CHANGED
|
@@ -170,9 +170,14 @@ function domainOfAddress(from) {
|
|
|
170
170
|
return /@([^@\s<>]+)>?\s*$/.exec(from.trim())?.[1];
|
|
171
171
|
}
|
|
172
172
|
function createAuthEmailHandler(options) {
|
|
173
|
-
|
|
173
|
+
if (options.provider === "lovable_auth") return createHandler(options, options.emails, (data) => typeof data.code === "string" && data.code !== "" ? void 0 : "Missing code");
|
|
174
|
+
return createHandler(options, options.emails, () => void 0);
|
|
175
|
+
}
|
|
176
|
+
function createHandler(options, emails, rejectReason) {
|
|
177
|
+
const { apiKey } = options;
|
|
174
178
|
if (!apiKey) throw new Error("Missing Lovable API key");
|
|
175
179
|
const from = formatEmailFrom(options.from);
|
|
180
|
+
const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
|
|
176
181
|
const senderDomain = options.senderDomain ?? domainOfAddress(from);
|
|
177
182
|
if (!senderDomain) throw new Error(`Cannot derive a sender domain from "${from}"; pass senderDomain`);
|
|
178
183
|
return async (req) => {
|
|
@@ -197,6 +202,8 @@ function createAuthEmailHandler(options) {
|
|
|
197
202
|
const actionType = event.data.action_type;
|
|
198
203
|
const definition = Object.prototype.hasOwnProperty.call(emails, actionType) ? emails[actionType] : void 0;
|
|
199
204
|
if (!definition) return Response.json({ error: `Unknown auth email action type: ${actionType}` }, { status: 400 });
|
|
205
|
+
const reason = rejectReason(event.data);
|
|
206
|
+
if (reason) return Response.json({ error: reason }, { status: 400 });
|
|
200
207
|
try {
|
|
201
208
|
const { render } = await import("@react-email/render");
|
|
202
209
|
const { subject, element } = typeof definition === "function" ? await definition(event.data) : {
|
|
@@ -210,6 +217,7 @@ function createAuthEmailHandler(options) {
|
|
|
210
217
|
to: event.data.email,
|
|
211
218
|
from,
|
|
212
219
|
sender_domain: senderDomain,
|
|
220
|
+
reply_to: replyTo,
|
|
213
221
|
subject,
|
|
214
222
|
html,
|
|
215
223
|
text,
|
package/dist/index.d.cts
CHANGED
|
@@ -132,14 +132,40 @@ type AuthEmailDefinition = {
|
|
|
132
132
|
render: (data: AuthEmailHookData) => ReactElement | Promise<ReactElement>;
|
|
133
133
|
} | ((data: AuthEmailHookData) => AuthEmailContent | Promise<AuthEmailContent>);
|
|
134
134
|
type AuthEmailDefinitions = Record<AuthEmailActionType, AuthEmailDefinition>;
|
|
135
|
-
|
|
135
|
+
type LovableAuthEmailActionType = "signup_confirmation" | "sign_in_otp" | "recovery_otp";
|
|
136
|
+
interface LovableAuthEmailHookData {
|
|
137
|
+
action_type: LovableAuthEmailActionType;
|
|
138
|
+
email: string;
|
|
139
|
+
code: string;
|
|
140
|
+
expires_at: string;
|
|
141
|
+
url: string | null;
|
|
142
|
+
site_url: string | null;
|
|
143
|
+
callback_url?: string;
|
|
144
|
+
}
|
|
145
|
+
type LovableAuthEmailWebhookPayload = WebhookEnvelope<"auth", LovableAuthEmailHookData>;
|
|
146
|
+
type LovableAuthEmailDefinition = {
|
|
147
|
+
subject: string;
|
|
148
|
+
render: (data: LovableAuthEmailHookData) => ReactElement | Promise<ReactElement>;
|
|
149
|
+
} | ((data: LovableAuthEmailHookData) => AuthEmailContent | Promise<AuthEmailContent>);
|
|
150
|
+
type LovableAuthEmailDefinitions = Record<LovableAuthEmailActionType, LovableAuthEmailDefinition>;
|
|
151
|
+
interface AuthEmailHandlerBaseOptions {
|
|
136
152
|
apiKey: string;
|
|
137
153
|
from: EmailFrom;
|
|
138
154
|
/** Defaults to the domain of `from`. */
|
|
139
155
|
senderDomain?: string;
|
|
156
|
+
/** Reply-To header. Replies go to `from` when omitted. */
|
|
157
|
+
replyTo?: EmailFrom;
|
|
140
158
|
sendUrl?: string | undefined;
|
|
159
|
+
}
|
|
160
|
+
interface LovableCloudAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
161
|
+
provider?: "lovable_cloud";
|
|
141
162
|
emails: AuthEmailDefinitions;
|
|
142
163
|
}
|
|
164
|
+
interface LovableAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
165
|
+
provider: "lovable_auth";
|
|
166
|
+
emails: LovableAuthEmailDefinitions;
|
|
167
|
+
}
|
|
168
|
+
type AuthEmailHandlerOptions = LovableCloudAuthEmailHandlerOptions | LovableAuthEmailHandlerOptions;
|
|
143
169
|
//#endregion
|
|
144
170
|
//#region src/auth-email-handler.d.ts
|
|
145
171
|
declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -174,4 +200,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
|
|
|
174
200
|
//#region src/webhook-handler.d.ts
|
|
175
201
|
declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
|
|
176
202
|
//#endregion
|
|
177
|
-
export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
|
|
203
|
+
export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type LovableAuthEmailActionType, type LovableAuthEmailDefinition, type LovableAuthEmailDefinitions, type LovableAuthEmailHandlerOptions, type LovableAuthEmailHookData, type LovableAuthEmailWebhookPayload, type LovableCloudAuthEmailHandlerOptions, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
|
package/dist/index.d.ts
CHANGED
|
@@ -132,14 +132,40 @@ type AuthEmailDefinition = {
|
|
|
132
132
|
render: (data: AuthEmailHookData) => ReactElement | Promise<ReactElement>;
|
|
133
133
|
} | ((data: AuthEmailHookData) => AuthEmailContent | Promise<AuthEmailContent>);
|
|
134
134
|
type AuthEmailDefinitions = Record<AuthEmailActionType, AuthEmailDefinition>;
|
|
135
|
-
|
|
135
|
+
type LovableAuthEmailActionType = "signup_confirmation" | "sign_in_otp" | "recovery_otp";
|
|
136
|
+
interface LovableAuthEmailHookData {
|
|
137
|
+
action_type: LovableAuthEmailActionType;
|
|
138
|
+
email: string;
|
|
139
|
+
code: string;
|
|
140
|
+
expires_at: string;
|
|
141
|
+
url: string | null;
|
|
142
|
+
site_url: string | null;
|
|
143
|
+
callback_url?: string;
|
|
144
|
+
}
|
|
145
|
+
type LovableAuthEmailWebhookPayload = WebhookEnvelope<"auth", LovableAuthEmailHookData>;
|
|
146
|
+
type LovableAuthEmailDefinition = {
|
|
147
|
+
subject: string;
|
|
148
|
+
render: (data: LovableAuthEmailHookData) => ReactElement | Promise<ReactElement>;
|
|
149
|
+
} | ((data: LovableAuthEmailHookData) => AuthEmailContent | Promise<AuthEmailContent>);
|
|
150
|
+
type LovableAuthEmailDefinitions = Record<LovableAuthEmailActionType, LovableAuthEmailDefinition>;
|
|
151
|
+
interface AuthEmailHandlerBaseOptions {
|
|
136
152
|
apiKey: string;
|
|
137
153
|
from: EmailFrom;
|
|
138
154
|
/** Defaults to the domain of `from`. */
|
|
139
155
|
senderDomain?: string;
|
|
156
|
+
/** Reply-To header. Replies go to `from` when omitted. */
|
|
157
|
+
replyTo?: EmailFrom;
|
|
140
158
|
sendUrl?: string | undefined;
|
|
159
|
+
}
|
|
160
|
+
interface LovableCloudAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
161
|
+
provider?: "lovable_cloud";
|
|
141
162
|
emails: AuthEmailDefinitions;
|
|
142
163
|
}
|
|
164
|
+
interface LovableAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
165
|
+
provider: "lovable_auth";
|
|
166
|
+
emails: LovableAuthEmailDefinitions;
|
|
167
|
+
}
|
|
168
|
+
type AuthEmailHandlerOptions = LovableCloudAuthEmailHandlerOptions | LovableAuthEmailHandlerOptions;
|
|
143
169
|
//#endregion
|
|
144
170
|
//#region src/auth-email-handler.d.ts
|
|
145
171
|
declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -174,4 +200,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
|
|
|
174
200
|
//#region src/webhook-handler.d.ts
|
|
175
201
|
declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
|
|
176
202
|
//#endregion
|
|
177
|
-
export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
|
|
203
|
+
export { type AuthEmailActionType, type AuthEmailContent, type AuthEmailDefinition, type AuthEmailDefinitions, type AuthEmailHandlerOptions, type AuthEmailHookData, type AuthEmailWebhookPayload, EmailAPIError, type EmailClientOptions, type EmailEvent, type EmailFrom, type EmailFromParts, type EmailLogsPagination, type EmailSendRequest, type EmailSendResponse, type EmailUnsubscribeResponse, type EmailWebhookEvent, type EmailWebhookEventType, type EmailWebhookHandlerOptions, type EmailWebhookHandlers, type EmailWebhookPayload, type GetEmailUnsubscribeRequest, type ListEmailLogsFilters, type ListEmailLogsResponse, type LovableAuthEmailActionType, type LovableAuthEmailDefinition, type LovableAuthEmailDefinitions, type LovableAuthEmailHandlerOptions, type LovableAuthEmailHookData, type LovableAuthEmailWebhookPayload, type LovableCloudAuthEmailHandlerOptions, type SendEmailOptions, type SetEmailUnsubscribeRequest, createAuthEmailHandler, createEmailWebhookHandler, formatEmailFrom, getEmailUnsubscribe, listEmailLogs, parseEmailWebhookPayload, sendLovableEmail, setEmailUnsubscribe };
|
package/dist/index.js
CHANGED
|
@@ -169,9 +169,14 @@ function domainOfAddress(from) {
|
|
|
169
169
|
return /@([^@\s<>]+)>?\s*$/.exec(from.trim())?.[1];
|
|
170
170
|
}
|
|
171
171
|
function createAuthEmailHandler(options) {
|
|
172
|
-
|
|
172
|
+
if (options.provider === "lovable_auth") return createHandler(options, options.emails, (data) => typeof data.code === "string" && data.code !== "" ? void 0 : "Missing code");
|
|
173
|
+
return createHandler(options, options.emails, () => void 0);
|
|
174
|
+
}
|
|
175
|
+
function createHandler(options, emails, rejectReason) {
|
|
176
|
+
const { apiKey } = options;
|
|
173
177
|
if (!apiKey) throw new Error("Missing Lovable API key");
|
|
174
178
|
const from = formatEmailFrom(options.from);
|
|
179
|
+
const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
|
|
175
180
|
const senderDomain = options.senderDomain ?? domainOfAddress(from);
|
|
176
181
|
if (!senderDomain) throw new Error(`Cannot derive a sender domain from "${from}"; pass senderDomain`);
|
|
177
182
|
return async (req) => {
|
|
@@ -196,6 +201,8 @@ function createAuthEmailHandler(options) {
|
|
|
196
201
|
const actionType = event.data.action_type;
|
|
197
202
|
const definition = Object.prototype.hasOwnProperty.call(emails, actionType) ? emails[actionType] : void 0;
|
|
198
203
|
if (!definition) return Response.json({ error: `Unknown auth email action type: ${actionType}` }, { status: 400 });
|
|
204
|
+
const reason = rejectReason(event.data);
|
|
205
|
+
if (reason) return Response.json({ error: reason }, { status: 400 });
|
|
199
206
|
try {
|
|
200
207
|
const { render } = await import("@react-email/render");
|
|
201
208
|
const { subject, element } = typeof definition === "function" ? await definition(event.data) : {
|
|
@@ -209,6 +216,7 @@ function createAuthEmailHandler(options) {
|
|
|
209
216
|
to: event.data.email,
|
|
210
217
|
from,
|
|
211
218
|
sender_domain: senderDomain,
|
|
219
|
+
reply_to: replyTo,
|
|
212
220
|
subject,
|
|
213
221
|
html,
|
|
214
222
|
text,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lovable.dev/email-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Lovable Email JS",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"license": "MIT",
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@lovable.dev/webhooks-js": "0.0.
|
|
24
|
+
"@lovable.dev/webhooks-js": "0.0.3"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
27
|
"@react-email/render": ">=1.0.0",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"build": "tsdown",
|
|
56
56
|
"check:package": "tmp=$(mktemp -d) && pnpm pack --out \"$tmp/pkg.tgz\" && tar -xzf \"$tmp/pkg.tgz\" -C \"$tmp\" && publint \"$tmp/package\" --strict && attw \"$tmp/pkg.tgz\"",
|
|
57
57
|
"dev": "tsdown --watch",
|
|
58
|
-
"test": "vitest run",
|
|
58
|
+
"test": "pnpm --filter @lovable.dev/webhooks-js run build && vitest run",
|
|
59
59
|
"clean": "rimraf dist",
|
|
60
|
-
"typecheck": "
|
|
60
|
+
"typecheck": "pnpm --filter @lovable.dev/webhooks-js run build && tsc --noEmit"
|
|
61
61
|
}
|
|
62
62
|
}
|