@better-auth/infra 0.1.7 → 0.1.9-beta.1

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.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import * as better_auth_client0 from "better-auth/client";
1
+ import * as _better_fetch_fetch0 from "@better-fetch/fetch";
2
2
 
3
3
  //#region src/client.d.ts
4
4
  interface DashAuditLog {
@@ -49,7 +49,7 @@ interface DashClientOptions {
49
49
  }
50
50
  declare const dashClient: (options?: DashClientOptions) => {
51
51
  id: "dash";
52
- getActions: ($fetch: better_auth_client0.BetterFetch) => {
52
+ getActions: ($fetch: _better_fetch_fetch0.BetterFetch) => {
53
53
  dash: {
54
54
  getAuditLogs: (input?: DashGetAuditLogsInput) => Promise<{
55
55
  data: DashAuditLogsResponse;
@@ -97,15 +97,15 @@ declare const sentinelClient: (options?: SentinelClientOptions) => {
97
97
  id: string;
98
98
  name: string;
99
99
  hooks: {
100
- onRequest<T extends Record<string, any>>(context: better_auth_client0.RequestContext<T>): Promise<better_auth_client0.RequestContext<T>>;
100
+ onRequest<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>): Promise<_better_fetch_fetch0.RequestContext<T>>;
101
101
  onResponse?: undefined;
102
102
  };
103
103
  } | {
104
104
  id: string;
105
105
  name: string;
106
106
  hooks: {
107
- onResponse(context: better_auth_client0.ResponseContext): Promise<better_auth_client0.ResponseContext>;
108
- onRequest<T extends Record<string, any>>(context: better_auth_client0.RequestContext<T>): Promise<better_auth_client0.RequestContext<T>>;
107
+ onResponse(context: _better_fetch_fetch0.ResponseContext): Promise<_better_fetch_fetch0.ResponseContext>;
108
+ onRequest<T extends Record<string, any>>(context: _better_fetch_fetch0.RequestContext<T>): Promise<_better_fetch_fetch0.RequestContext<T>>;
109
109
  };
110
110
  })[];
111
111
  };
package/dist/client.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import { r as KV_TIMEOUT_MS } from "./constants-DWl1utFw.mjs";
1
2
  import { env } from "@better-auth/core/env";
2
3
 
3
4
  //#region src/client.ts
@@ -415,9 +416,12 @@ async function sendIdentify(identifyUrl) {
415
416
  await fetch(`${identifyUrl}/identify`, {
416
417
  method: "POST",
417
418
  headers: { "Content-Type": "application/json" },
418
- body: JSON.stringify(payload)
419
+ body: JSON.stringify(payload),
420
+ signal: AbortSignal.timeout(KV_TIMEOUT_MS)
419
421
  });
420
- } catch {} finally {
422
+ } catch (error) {
423
+ console.warn("[Dash] Identify request failed:", error);
424
+ } finally {
421
425
  identifyCompleteResolve?.();
422
426
  }
423
427
  }
@@ -0,0 +1,18 @@
1
+ import { env } from "@better-auth/core/env";
2
+
3
+ //#region src/constants.ts
4
+ /**
5
+ * Infrastructure API URL
6
+ * Can be overridden via plugin config or BETTER_AUTH_API_URL env var for local development
7
+ */
8
+ const INFRA_API_URL = env.BETTER_AUTH_API_URL || "https://beta.better-auth.com";
9
+ /**
10
+ * KV Storage URL
11
+ * Can be overridden via plugin config or BETTER_AUTH_KV_URL env var for local development
12
+ */
13
+ const INFRA_KV_URL = env.BETTER_AUTH_KV_URL || "https://kv.better-auth.com";
14
+ /** Timeout for KV HTTP operations (ms) */
15
+ const KV_TIMEOUT_MS = 5e3;
16
+
17
+ //#endregion
18
+ export { INFRA_KV_URL as n, KV_TIMEOUT_MS as r, INFRA_API_URL 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,183 @@
1
- import { n as createEmailSender, r as sendEmail, t as EMAIL_TEMPLATES } from "./email-BGxJ96Ky.mjs";
1
+ import { t as INFRA_API_URL } from "./constants-DWl1utFw.mjs";
2
+ import { logger } from "better-auth";
3
+ import { env } from "@better-auth/core/env";
2
4
 
3
- export { EMAIL_TEMPLATES, createEmailSender, sendEmail };
5
+ //#region src/email.ts
6
+ /**
7
+ * Email sending module for @better-auth/infra
8
+ *
9
+ * This module provides email sending functionality that integrates with
10
+ * Better Auth Infra's template system.
11
+ */
12
+ /**
13
+ * Email template definitions with their required variables
14
+ */
15
+ const EMAIL_TEMPLATES = {
16
+ "verify-email": { variables: {} },
17
+ "reset-password": { variables: {} },
18
+ "change-email": { variables: {} },
19
+ "sign-in-otp": { variables: {} },
20
+ "verify-email-otp": { variables: {} },
21
+ "reset-password-otp": { variables: {} },
22
+ "magic-link": { variables: {} },
23
+ "two-factor": { variables: {} },
24
+ invitation: { variables: {} },
25
+ "application-invite": { variables: {} },
26
+ "delete-account": { variables: {} },
27
+ "stale-account-user": { variables: {} },
28
+ "stale-account-admin": { variables: {} }
29
+ };
30
+ /**
31
+ * Create an email sender instance
32
+ */
33
+ function createEmailSender(config) {
34
+ const baseUrl = config?.apiUrl || INFRA_API_URL;
35
+ const apiUrl = baseUrl.endsWith("/api") ? baseUrl : `${baseUrl}/api`;
36
+ const apiKey = config?.apiKey || env.BETTER_AUTH_API_KEY || "";
37
+ if (!apiKey) logger.warn("[Dash] No API key provided for email sending. Set BETTER_AUTH_API_KEY environment variable or pass apiKey in config.");
38
+ /**
39
+ * Send an email using a template from Better Auth Infra
40
+ */
41
+ async function send(options) {
42
+ if (!apiKey) return {
43
+ success: false,
44
+ error: "API key not configured"
45
+ };
46
+ try {
47
+ const response = await fetch(`${apiUrl}/v1/email/send`, {
48
+ method: "POST",
49
+ headers: {
50
+ "Content-Type": "application/json",
51
+ Authorization: `Bearer ${apiKey}`
52
+ },
53
+ body: JSON.stringify({
54
+ template: options.template,
55
+ to: options.to,
56
+ variables: options.variables || {},
57
+ subject: options.subject
58
+ })
59
+ });
60
+ if (!response.ok) return {
61
+ success: false,
62
+ error: (await response.json().catch(() => ({ message: "Unknown error" }))).message || `HTTP ${response.status}`
63
+ };
64
+ return {
65
+ success: true,
66
+ messageId: (await response.json()).messageId
67
+ };
68
+ } catch (error) {
69
+ logger.warn("[Dash] Email send failed:", error);
70
+ return {
71
+ success: false,
72
+ error: error instanceof Error ? error.message : "Failed to send email"
73
+ };
74
+ }
75
+ }
76
+ /**
77
+ * Send bulk emails using a template from Better Auth Infra
78
+ */
79
+ async function sendBulk(options) {
80
+ if (!apiKey) return {
81
+ success: false,
82
+ failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: "API key not configured" }]]))
83
+ };
84
+ try {
85
+ const response = await fetch(`${apiUrl}/v1/email/send-bulk`, {
86
+ method: "POST",
87
+ headers: {
88
+ "Content-Type": "application/json",
89
+ Authorization: `Bearer ${apiKey}`
90
+ },
91
+ body: JSON.stringify({
92
+ template: options.template,
93
+ emails: options.emails.map((e) => ({
94
+ to: e.to,
95
+ variables: e.variables || {}
96
+ })),
97
+ subject: options.subject,
98
+ variables: options.variables || {}
99
+ })
100
+ });
101
+ if (!response.ok) {
102
+ const error = await response.json().catch(() => ({ message: "Unknown error" }));
103
+ return {
104
+ success: false,
105
+ failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: error.message || `HTTP ${response.status}` }]]))
106
+ };
107
+ }
108
+ const result = await response.json();
109
+ return {
110
+ success: result.success,
111
+ failures: result.failures
112
+ };
113
+ } catch (error) {
114
+ logger.warn("[Dash] Bulk email send failed:", error);
115
+ return {
116
+ success: false,
117
+ failures: Object.fromEntries(options.emails.map((e) => [e.to, [{ error: error instanceof Error ? error.message : "Failed to send bulk emails" }]]))
118
+ };
119
+ }
120
+ }
121
+ /**
122
+ * Get available email templates
123
+ */
124
+ async function getTemplates() {
125
+ if (!apiKey) return [];
126
+ try {
127
+ const response = await fetch(`${apiUrl}/v1/email/templates`, { headers: { Authorization: `Bearer ${apiKey}` } });
128
+ if (!response.ok) return [];
129
+ return response.json();
130
+ } catch (error) {
131
+ logger.warn("[Dash] Failed to fetch email templates:", error);
132
+ return [];
133
+ }
134
+ }
135
+ return {
136
+ send,
137
+ sendBulk,
138
+ getTemplates
139
+ };
140
+ }
141
+ /**
142
+ * Send an email using the Better Auth dashboard's email templates.
143
+ *
144
+ * @example
145
+ * ```ts
146
+ * import { sendEmail } from "@better-auth/infra";
147
+ *
148
+ * // Type-safe - variables are inferred from template
149
+ * await sendEmail({
150
+ * template: "reset-password",
151
+ * to: "user@example.com",
152
+ * variables: {
153
+ * resetLink: "https://...",
154
+ * userEmail: "user@example.com",
155
+ * },
156
+ * });
157
+ * ```
158
+ */
159
+ async function sendEmail(options, config) {
160
+ return createEmailSender(config).send(options);
161
+ }
162
+ /**
163
+ * Send bulk emails using the Better Auth dashboard's email templates.
164
+ *
165
+ * @example
166
+ * ```ts
167
+ * import { sendBulkEmails } from "@better-auth/infra";
168
+ *
169
+ * const result = await sendBulkEmails({
170
+ * template: "reset-password",
171
+ * emails: [
172
+ * { to: "user1@example.com", variables: { resetLink: "...", userEmail: "user1@example.com" } },
173
+ * { to: "user2@example.com", variables: { resetLink: "...", userEmail: "user2@example.com" } },
174
+ * ],
175
+ * });
176
+ * ```
177
+ */
178
+ async function sendBulkEmails(options, config) {
179
+ return createEmailSender(config).sendBulk(options);
180
+ }
181
+
182
+ //#endregion
183
+ export { EMAIL_TEMPLATES, createEmailSender, sendBulkEmails, sendEmail };