@lovable.dev/email-js 0.3.1 → 0.5.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 +24 -1
- package/dist/index.cjs +12 -2
- package/dist/index.d.cts +26 -2
- package/dist/index.d.ts +26 -2
- package/dist/index.js +12 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -217,6 +217,26 @@ const handler = createAuthEmailHandler({
|
|
|
217
217
|
});
|
|
218
218
|
```
|
|
219
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
|
+
|
|
220
240
|
`replyTo` takes the same string-or-parts form as `from` and sets the Reply-To
|
|
221
241
|
header, so a user who replies to a confirmation email reaches a monitored
|
|
222
242
|
inbox instead of the noreply sender. Omit it and replies go to `from`.
|
|
@@ -233,4 +253,7 @@ run) instead of being acked, and a render failure or retryable send failure
|
|
|
233
253
|
returns 500 so the delivery is retried — the payload's `run_id` doubles as
|
|
234
254
|
the send idempotency key, so retries cannot double-send. Permanent send
|
|
235
255
|
rejections (for example an expired run) also return 400 rather than burning
|
|
236
|
-
retries on a deterministic failure.
|
|
256
|
+
retries on a deterministic failure. The 400 for an unknown action type carries
|
|
257
|
+
`{ "error": "unsupported_action_type", "action_type": "<type>", "supported_action_types": [...] }`,
|
|
258
|
+
the keys of your `emails` map, so the caller can tell a missing template from
|
|
259
|
+
any other rejection.
|
package/dist/index.cjs
CHANGED
|
@@ -170,7 +170,11 @@ 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);
|
|
176
180
|
const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
|
|
@@ -197,7 +201,13 @@ function createAuthEmailHandler(options) {
|
|
|
197
201
|
if (event.version !== "1") return Response.json({ error: `Unsupported payload version: ${event.version}` }, { status: 400 });
|
|
198
202
|
const actionType = event.data.action_type;
|
|
199
203
|
const definition = Object.prototype.hasOwnProperty.call(emails, actionType) ? emails[actionType] : void 0;
|
|
200
|
-
if (!definition) return Response.json({
|
|
204
|
+
if (!definition) return Response.json({
|
|
205
|
+
error: "unsupported_action_type",
|
|
206
|
+
action_type: actionType,
|
|
207
|
+
supported_action_types: Object.keys(emails)
|
|
208
|
+
}, { status: 400 });
|
|
209
|
+
const reason = rejectReason(event.data);
|
|
210
|
+
if (reason) return Response.json({ error: reason }, { status: 400 });
|
|
201
211
|
try {
|
|
202
212
|
const { render } = await import("@react-email/render");
|
|
203
213
|
const { subject, element } = typeof definition === "function" ? await definition(event.data) : {
|
package/dist/index.d.cts
CHANGED
|
@@ -132,7 +132,23 @@ 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`. */
|
|
@@ -140,8 +156,16 @@ interface AuthEmailHandlerOptions {
|
|
|
140
156
|
/** Reply-To header. Replies go to `from` when omitted. */
|
|
141
157
|
replyTo?: EmailFrom;
|
|
142
158
|
sendUrl?: string | undefined;
|
|
159
|
+
}
|
|
160
|
+
interface LovableCloudAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
161
|
+
provider?: "lovable_cloud";
|
|
143
162
|
emails: AuthEmailDefinitions;
|
|
144
163
|
}
|
|
164
|
+
interface LovableAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
165
|
+
provider: "lovable_auth";
|
|
166
|
+
emails: LovableAuthEmailDefinitions;
|
|
167
|
+
}
|
|
168
|
+
type AuthEmailHandlerOptions = LovableCloudAuthEmailHandlerOptions | LovableAuthEmailHandlerOptions;
|
|
145
169
|
//#endregion
|
|
146
170
|
//#region src/auth-email-handler.d.ts
|
|
147
171
|
declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -176,4 +200,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
|
|
|
176
200
|
//#region src/webhook-handler.d.ts
|
|
177
201
|
declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
|
|
178
202
|
//#endregion
|
|
179
|
-
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,7 +132,23 @@ 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`. */
|
|
@@ -140,8 +156,16 @@ interface AuthEmailHandlerOptions {
|
|
|
140
156
|
/** Reply-To header. Replies go to `from` when omitted. */
|
|
141
157
|
replyTo?: EmailFrom;
|
|
142
158
|
sendUrl?: string | undefined;
|
|
159
|
+
}
|
|
160
|
+
interface LovableCloudAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
161
|
+
provider?: "lovable_cloud";
|
|
143
162
|
emails: AuthEmailDefinitions;
|
|
144
163
|
}
|
|
164
|
+
interface LovableAuthEmailHandlerOptions extends AuthEmailHandlerBaseOptions {
|
|
165
|
+
provider: "lovable_auth";
|
|
166
|
+
emails: LovableAuthEmailDefinitions;
|
|
167
|
+
}
|
|
168
|
+
type AuthEmailHandlerOptions = LovableCloudAuthEmailHandlerOptions | LovableAuthEmailHandlerOptions;
|
|
145
169
|
//#endregion
|
|
146
170
|
//#region src/auth-email-handler.d.ts
|
|
147
171
|
declare function createAuthEmailHandler(options: AuthEmailHandlerOptions): (req: Request) => Promise<Response>;
|
|
@@ -176,4 +200,4 @@ declare function parseEmailWebhookPayload(body: string): EmailWebhookPayload$1;
|
|
|
176
200
|
//#region src/webhook-handler.d.ts
|
|
177
201
|
declare function createEmailWebhookHandler(options: EmailWebhookHandlerOptions): (req: Request) => Promise<Response>;
|
|
178
202
|
//#endregion
|
|
179
|
-
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,7 +169,11 @@ 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);
|
|
175
179
|
const replyTo = options.replyTo === void 0 ? void 0 : formatEmailFrom(options.replyTo);
|
|
@@ -196,7 +200,13 @@ function createAuthEmailHandler(options) {
|
|
|
196
200
|
if (event.version !== "1") return Response.json({ error: `Unsupported payload version: ${event.version}` }, { status: 400 });
|
|
197
201
|
const actionType = event.data.action_type;
|
|
198
202
|
const definition = Object.prototype.hasOwnProperty.call(emails, actionType) ? emails[actionType] : void 0;
|
|
199
|
-
if (!definition) return Response.json({
|
|
203
|
+
if (!definition) return Response.json({
|
|
204
|
+
error: "unsupported_action_type",
|
|
205
|
+
action_type: actionType,
|
|
206
|
+
supported_action_types: Object.keys(emails)
|
|
207
|
+
}, { status: 400 });
|
|
208
|
+
const reason = rejectReason(event.data);
|
|
209
|
+
if (reason) return Response.json({ error: reason }, { status: 400 });
|
|
200
210
|
try {
|
|
201
211
|
const { render } = await import("@react-email/render");
|
|
202
212
|
const { subject, element } = typeof definition === "function" ? await definition(event.data) : {
|