@better-auth/infra 0.1.8 → 0.1.9

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-B-e0_Nsv.mjs";
1
2
  import { env } from "@better-auth/core/env";
2
3
 
3
4
  //#region src/client.ts
@@ -415,7 +416,8 @@ 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
422
  } catch (error) {
421
423
  console.warn("[Dash] Identify request failed:", error);
@@ -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://dash.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.mjs CHANGED
@@ -1,3 +1,183 @@
1
- import { i as sendEmail, n as createEmailSender, r as sendBulkEmails, t as EMAIL_TEMPLATES } from "./email-D2dL1i3c.mjs";
1
+ import { t as INFRA_API_URL } from "./constants-B-e0_Nsv.mjs";
2
+ import { logger } from "better-auth";
3
+ import { env } from "@better-auth/core/env";
2
4
 
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
3
183
  export { EMAIL_TEMPLATES, createEmailSender, sendBulkEmails, sendEmail };