@better-auth/infra 0.1.7 → 0.1.8
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/client.mjs +3 -1
- package/dist/{email-BGxJ96Ky.mjs → email-D2dL1i3c.mjs} +69 -2
- package/dist/email.d.mts +53 -1
- package/dist/email.mjs +2 -2
- package/dist/index.d.mts +516 -258
- package/dist/index.mjs +663 -231
- package/package.json +14 -13
package/dist/client.mjs
CHANGED
|
@@ -417,7 +417,9 @@ async function sendIdentify(identifyUrl) {
|
|
|
417
417
|
headers: { "Content-Type": "application/json" },
|
|
418
418
|
body: JSON.stringify(payload)
|
|
419
419
|
});
|
|
420
|
-
} catch
|
|
420
|
+
} catch (error) {
|
|
421
|
+
console.warn("[Dash] Identify request failed:", error);
|
|
422
|
+
} finally {
|
|
421
423
|
identifyCompleteResolve?.();
|
|
422
424
|
}
|
|
423
425
|
}
|
|
@@ -78,6 +78,7 @@ function createEmailSender(config) {
|
|
|
78
78
|
messageId: (await response.json()).messageId
|
|
79
79
|
};
|
|
80
80
|
} catch (error) {
|
|
81
|
+
logger.warn("[Dash] Email send failed:", error);
|
|
81
82
|
return {
|
|
82
83
|
success: false,
|
|
83
84
|
error: error instanceof Error ? error.message : "Failed to send email"
|
|
@@ -85,6 +86,51 @@ function createEmailSender(config) {
|
|
|
85
86
|
}
|
|
86
87
|
}
|
|
87
88
|
/**
|
|
89
|
+
* Send bulk emails using a template from Better Auth Infra
|
|
90
|
+
*/
|
|
91
|
+
async function sendBulk(options) {
|
|
92
|
+
if (!apiKey) return {
|
|
93
|
+
success: false,
|
|
94
|
+
failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: "API key not configured" }]]))
|
|
95
|
+
};
|
|
96
|
+
try {
|
|
97
|
+
const response = await fetch(`${apiUrl}/v1/email/send-bulk`, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: {
|
|
100
|
+
"Content-Type": "application/json",
|
|
101
|
+
Authorization: `Bearer ${apiKey}`
|
|
102
|
+
},
|
|
103
|
+
body: JSON.stringify({
|
|
104
|
+
template: options.template,
|
|
105
|
+
emails: options.emails.map((e) => ({
|
|
106
|
+
to: e.to,
|
|
107
|
+
variables: e.variables || {}
|
|
108
|
+
})),
|
|
109
|
+
subject: options.subject,
|
|
110
|
+
variables: options.variables || {}
|
|
111
|
+
})
|
|
112
|
+
});
|
|
113
|
+
if (!response.ok) {
|
|
114
|
+
const error = await response.json().catch(() => ({ message: "Unknown error" }));
|
|
115
|
+
return {
|
|
116
|
+
success: false,
|
|
117
|
+
failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: error.message || `HTTP ${response.status}` }]]))
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
const result = await response.json();
|
|
121
|
+
return {
|
|
122
|
+
success: result.success,
|
|
123
|
+
failures: result.failures
|
|
124
|
+
};
|
|
125
|
+
} catch (error) {
|
|
126
|
+
logger.warn("[Dash] Bulk email send failed:", error);
|
|
127
|
+
return {
|
|
128
|
+
success: false,
|
|
129
|
+
failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: error instanceof Error ? error.message : "Failed to send bulk emails" }]]))
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
88
134
|
* Get available email templates
|
|
89
135
|
*/
|
|
90
136
|
async function getTemplates() {
|
|
@@ -93,12 +139,14 @@ function createEmailSender(config) {
|
|
|
93
139
|
const response = await fetch(`${apiUrl}/v1/email/templates`, { headers: { Authorization: `Bearer ${apiKey}` } });
|
|
94
140
|
if (!response.ok) return [];
|
|
95
141
|
return response.json();
|
|
96
|
-
} catch {
|
|
142
|
+
} catch (error) {
|
|
143
|
+
logger.warn("[Dash] Failed to fetch email templates:", error);
|
|
97
144
|
return [];
|
|
98
145
|
}
|
|
99
146
|
}
|
|
100
147
|
return {
|
|
101
148
|
send,
|
|
149
|
+
sendBulk,
|
|
102
150
|
getTemplates
|
|
103
151
|
};
|
|
104
152
|
}
|
|
@@ -123,6 +171,25 @@ function createEmailSender(config) {
|
|
|
123
171
|
async function sendEmail(options, config) {
|
|
124
172
|
return createEmailSender(config).send(options);
|
|
125
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Send bulk emails using the Better Auth dashboard's email templates.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* import { sendBulkEmails } from "@better-auth/infra";
|
|
180
|
+
*
|
|
181
|
+
* const result = await sendBulkEmails({
|
|
182
|
+
* template: "reset-password",
|
|
183
|
+
* emails: [
|
|
184
|
+
* { to: "user1@example.com", variables: { resetLink: "...", userEmail: "user1@example.com" } },
|
|
185
|
+
* { to: "user2@example.com", variables: { resetLink: "...", userEmail: "user2@example.com" } },
|
|
186
|
+
* ],
|
|
187
|
+
* });
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
async function sendBulkEmails(options, config) {
|
|
191
|
+
return createEmailSender(config).sendBulk(options);
|
|
192
|
+
}
|
|
126
193
|
|
|
127
194
|
//#endregion
|
|
128
|
-
export {
|
|
195
|
+
export { INFRA_API_URL as a, sendEmail as i, createEmailSender as n, INFRA_KV_URL as o, sendBulkEmails as r, EMAIL_TEMPLATES as t };
|
package/dist/email.d.mts
CHANGED
|
@@ -168,11 +168,46 @@ type SendEmailOptions<T extends EmailTemplateId = EmailTemplateId> = {
|
|
|
168
168
|
*/
|
|
169
169
|
subject?: string;
|
|
170
170
|
};
|
|
171
|
+
/**
|
|
172
|
+
* Options for sending bulk emails
|
|
173
|
+
*/
|
|
174
|
+
type SendBulkEmailsOptions<T extends EmailTemplateId = EmailTemplateId> = {
|
|
175
|
+
/**
|
|
176
|
+
* The template ID to use for all emails
|
|
177
|
+
*/
|
|
178
|
+
template: T;
|
|
179
|
+
/**
|
|
180
|
+
* Array of recipients with their template variables
|
|
181
|
+
*/
|
|
182
|
+
emails: Array<{
|
|
183
|
+
to: string;
|
|
184
|
+
variables?: EmailTemplateVariables<T>;
|
|
185
|
+
}>;
|
|
186
|
+
/**
|
|
187
|
+
* Optional subject override (shared across all emails)
|
|
188
|
+
*/
|
|
189
|
+
subject?: string;
|
|
190
|
+
/**
|
|
191
|
+
* Optional shared variables applied to all emails (per-recipient variables override these)
|
|
192
|
+
*/
|
|
193
|
+
variables?: Record<string, string>;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Result of a bulk email send operation
|
|
197
|
+
*/
|
|
198
|
+
interface SendBulkEmailsResult {
|
|
199
|
+
success: boolean;
|
|
200
|
+
failures?: Record<string, {
|
|
201
|
+
error?: string;
|
|
202
|
+
messageId?: string;
|
|
203
|
+
}[]>;
|
|
204
|
+
}
|
|
171
205
|
/**
|
|
172
206
|
* Create an email sender instance
|
|
173
207
|
*/
|
|
174
208
|
declare function createEmailSender(config?: EmailConfig): {
|
|
175
209
|
send: <T extends EmailTemplateId>(options: SendEmailOptions<T>) => Promise<SendEmailResult>;
|
|
210
|
+
sendBulk: <T extends EmailTemplateId>(options: SendBulkEmailsOptions<T>) => Promise<SendBulkEmailsResult>;
|
|
176
211
|
getTemplates: () => Promise<{
|
|
177
212
|
id: string;
|
|
178
213
|
name: string;
|
|
@@ -198,5 +233,22 @@ declare function createEmailSender(config?: EmailConfig): {
|
|
|
198
233
|
* ```
|
|
199
234
|
*/
|
|
200
235
|
declare function sendEmail<T extends EmailTemplateId>(options: SendEmailOptions<T>, config?: EmailConfig): Promise<SendEmailResult>;
|
|
236
|
+
/**
|
|
237
|
+
* Send bulk emails using the Better Auth dashboard's email templates.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```ts
|
|
241
|
+
* import { sendBulkEmails } from "@better-auth/infra";
|
|
242
|
+
*
|
|
243
|
+
* const result = await sendBulkEmails({
|
|
244
|
+
* template: "reset-password",
|
|
245
|
+
* emails: [
|
|
246
|
+
* { to: "user1@example.com", variables: { resetLink: "...", userEmail: "user1@example.com" } },
|
|
247
|
+
* { to: "user2@example.com", variables: { resetLink: "...", userEmail: "user2@example.com" } },
|
|
248
|
+
* ],
|
|
249
|
+
* });
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
declare function sendBulkEmails<T extends EmailTemplateId>(options: SendBulkEmailsOptions<T>, config?: EmailConfig): Promise<SendBulkEmailsResult>;
|
|
201
253
|
//#endregion
|
|
202
|
-
export { EMAIL_TEMPLATES, EmailConfig, EmailTemplateId, EmailTemplateVariables, SendEmailOptions, SendEmailResult, createEmailSender, sendEmail };
|
|
254
|
+
export { EMAIL_TEMPLATES, EmailConfig, EmailTemplateId, EmailTemplateVariables, SendBulkEmailsOptions, SendBulkEmailsResult, SendEmailOptions, SendEmailResult, createEmailSender, sendBulkEmails, sendEmail };
|
package/dist/email.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { n as createEmailSender, r as
|
|
1
|
+
import { i as sendEmail, n as createEmailSender, r as sendBulkEmails, t as EMAIL_TEMPLATES } from "./email-D2dL1i3c.mjs";
|
|
2
2
|
|
|
3
|
-
export { EMAIL_TEMPLATES, createEmailSender, sendEmail };
|
|
3
|
+
export { EMAIL_TEMPLATES, createEmailSender, sendBulkEmails, sendEmail };
|