@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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@better-auth/infra",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Dashboard and analytics plugin for Better Auth",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",
@@ -10,15 +10,18 @@
10
10
  "exports": {
11
11
  ".": {
12
12
  "types": "./dist/index.d.mts",
13
- "import": "./dist/index.mjs"
13
+ "import": "./dist/index.mjs",
14
+ "default": "./dist/index.mjs"
14
15
  },
15
16
  "./client": {
16
17
  "types": "./dist/client.d.mts",
17
- "import": "./dist/client.mjs"
18
+ "import": "./dist/client.mjs",
19
+ "default": "./dist/client.mjs"
18
20
  },
19
21
  "./email": {
20
22
  "types": "./dist/email.d.mts",
21
- "import": "./dist/email.mjs"
23
+ "import": "./dist/email.mjs",
24
+ "default": "./dist/email.mjs"
22
25
  }
23
26
  },
24
27
  "files": [
@@ -54,16 +57,16 @@
54
57
  "zod": "beta"
55
58
  },
56
59
  "dependencies": {
57
- "@better-auth/core": "beta",
58
- "@better-auth/sso": "beta",
59
- "@better-fetch/fetch": "beta",
60
- "better-call": "beta",
60
+ "@better-fetch/fetch": "^1.1.21",
61
+ "better-call": "^1.3.3",
61
62
  "jose": "^6.1.0",
62
63
  "libphonenumber-js": "^1.12.36"
63
64
  },
64
65
  "peerDependencies": {
65
- "better-auth": ">=1.4.15",
66
- "zod": ">=4.1.12"
66
+ "better-auth": ">=1.4.0",
67
+ "zod": ">=4.1.12",
68
+ "@better-auth/core": ">=1.4.0",
69
+ "@better-auth/sso": ">=1.4.0"
67
70
  },
68
71
  "scripts": {
69
72
  "build": "tsdown ",
@@ -1,195 +0,0 @@
1
- import { logger } from "better-auth";
2
- import { env } from "@better-auth/core/env";
3
-
4
- //#region src/constants.ts
5
- /**
6
- * Infrastructure API URL
7
- * Can be overridden via plugin config or BETTER_AUTH_API_URL env var for local development
8
- */
9
- const INFRA_API_URL = env.BETTER_AUTH_API_URL || "https://beta.better-auth.com";
10
- /**
11
- * KV Storage URL
12
- * Can be overridden via plugin config or BETTER_AUTH_KV_URL env var for local development
13
- */
14
- const INFRA_KV_URL = env.BETTER_AUTH_KV_URL || "https://kv.better-auth.com";
15
-
16
- //#endregion
17
- //#region src/email.ts
18
- /**
19
- * Email sending module for @better-auth/infra
20
- *
21
- * This module provides email sending functionality that integrates with
22
- * Better Auth Infra's template system.
23
- */
24
- /**
25
- * Email template definitions with their required variables
26
- */
27
- const EMAIL_TEMPLATES = {
28
- "verify-email": { variables: {} },
29
- "reset-password": { variables: {} },
30
- "change-email": { variables: {} },
31
- "sign-in-otp": { variables: {} },
32
- "verify-email-otp": { variables: {} },
33
- "reset-password-otp": { variables: {} },
34
- "magic-link": { variables: {} },
35
- "two-factor": { variables: {} },
36
- invitation: { variables: {} },
37
- "application-invite": { variables: {} },
38
- "delete-account": { variables: {} },
39
- "stale-account-user": { variables: {} },
40
- "stale-account-admin": { variables: {} }
41
- };
42
- /**
43
- * Create an email sender instance
44
- */
45
- function createEmailSender(config) {
46
- const baseUrl = config?.apiUrl || INFRA_API_URL;
47
- const apiUrl = baseUrl.endsWith("/api") ? baseUrl : `${baseUrl}/api`;
48
- const apiKey = config?.apiKey || env.BETTER_AUTH_API_KEY || "";
49
- if (!apiKey) logger.warn("[Dash] No API key provided for email sending. Set BETTER_AUTH_API_KEY environment variable or pass apiKey in config.");
50
- /**
51
- * Send an email using a template from Better Auth Infra
52
- */
53
- async function send(options) {
54
- if (!apiKey) return {
55
- success: false,
56
- error: "API key not configured"
57
- };
58
- try {
59
- const response = await fetch(`${apiUrl}/v1/email/send`, {
60
- method: "POST",
61
- headers: {
62
- "Content-Type": "application/json",
63
- Authorization: `Bearer ${apiKey}`
64
- },
65
- body: JSON.stringify({
66
- template: options.template,
67
- to: options.to,
68
- variables: options.variables || {},
69
- subject: options.subject
70
- })
71
- });
72
- if (!response.ok) return {
73
- success: false,
74
- error: (await response.json().catch(() => ({ message: "Unknown error" }))).message || `HTTP ${response.status}`
75
- };
76
- return {
77
- success: true,
78
- messageId: (await response.json()).messageId
79
- };
80
- } catch (error) {
81
- logger.warn("[Dash] Email send failed:", error);
82
- return {
83
- success: false,
84
- error: error instanceof Error ? error.message : "Failed to send email"
85
- };
86
- }
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
- /**
134
- * Get available email templates
135
- */
136
- async function getTemplates() {
137
- if (!apiKey) return [];
138
- try {
139
- const response = await fetch(`${apiUrl}/v1/email/templates`, { headers: { Authorization: `Bearer ${apiKey}` } });
140
- if (!response.ok) return [];
141
- return response.json();
142
- } catch (error) {
143
- logger.warn("[Dash] Failed to fetch email templates:", error);
144
- return [];
145
- }
146
- }
147
- return {
148
- send,
149
- sendBulk,
150
- getTemplates
151
- };
152
- }
153
- /**
154
- * Send an email using the Better Auth dashboard's email templates.
155
- *
156
- * @example
157
- * ```ts
158
- * import { sendEmail } from "@better-auth/infra";
159
- *
160
- * // Type-safe - variables are inferred from template
161
- * await sendEmail({
162
- * template: "reset-password",
163
- * to: "user@example.com",
164
- * variables: {
165
- * resetLink: "https://...",
166
- * userEmail: "user@example.com",
167
- * },
168
- * });
169
- * ```
170
- */
171
- async function sendEmail(options, config) {
172
- return createEmailSender(config).send(options);
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
- }
193
-
194
- //#endregion
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 };