better-auth 0.0.8-beta.2 → 0.0.8-beta.21

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.
Files changed (58) hide show
  1. package/dist/access.d.ts +3 -4
  2. package/dist/access.js +3 -13
  3. package/dist/access.js.map +1 -1
  4. package/dist/cli.d.ts +1 -2
  5. package/dist/cli.js +36 -44
  6. package/dist/cli.js.map +1 -1
  7. package/dist/client/plugins.d.ts +97 -2407
  8. package/dist/client/plugins.js +143 -203
  9. package/dist/client/plugins.js.map +1 -1
  10. package/dist/client.d.ts +159 -1213
  11. package/dist/client.js +125 -527
  12. package/dist/client.js.map +1 -1
  13. package/dist/{helper-B5_2Vzba.d.ts → helper-D8dhRz72.d.ts} +1 -4
  14. package/dist/{index-Dg4eEXZW.d.ts → index-B9jOjqnF.d.ts} +1 -1
  15. package/dist/{schema-BOszzrbQ.d.ts → index-CcxejJTH.d.ts} +172 -142
  16. package/dist/{client-CaF9eUcv.d.ts → index-Dwhjsk4l.d.ts} +2014 -1971
  17. package/dist/index.d.ts +1124 -6
  18. package/dist/index.js +737 -707
  19. package/dist/index.js.map +1 -1
  20. package/dist/internal-adapter-CVKQ4XR9.d.ts +637 -0
  21. package/dist/next-js.d.ts +17 -7
  22. package/dist/next-js.js +20 -3
  23. package/dist/next-js.js.map +1 -1
  24. package/dist/plugins.d.ts +12 -883
  25. package/dist/plugins.js +743 -679
  26. package/dist/plugins.js.map +1 -1
  27. package/dist/react.d.ts +312 -12
  28. package/dist/react.js +138 -148
  29. package/dist/react.js.map +1 -1
  30. package/dist/social.d.ts +2 -2
  31. package/dist/social.js +179 -151
  32. package/dist/social.js.map +1 -1
  33. package/dist/solid-start.d.ts +7 -6
  34. package/dist/solid-start.js +3 -3
  35. package/dist/solid-start.js.map +1 -1
  36. package/dist/solid.d.ts +91 -2713
  37. package/dist/solid.js +130 -139
  38. package/dist/solid.js.map +1 -1
  39. package/dist/{statement-COylZd3J.d.ts → statement-D6SPoYOh.d.ts} +7 -7
  40. package/dist/svelte-kit.d.ts +6 -5
  41. package/dist/svelte-kit.js +10 -9
  42. package/dist/svelte-kit.js.map +1 -1
  43. package/dist/svelte.d.ts +89 -2713
  44. package/dist/svelte.js +124 -138
  45. package/dist/svelte.js.map +1 -1
  46. package/dist/types-D4WrjKeJ.d.ts +81 -0
  47. package/dist/types.d.ts +31 -5
  48. package/dist/types.js +2 -0
  49. package/dist/types.js.map +1 -1
  50. package/dist/vue.d.ts +313 -12
  51. package/dist/vue.js +131 -145
  52. package/dist/vue.js.map +1 -1
  53. package/package.json +8 -3
  54. package/dist/index-CGeV0d2g.d.ts +0 -1498
  55. package/dist/preact.d.ts +0 -8
  56. package/dist/preact.js +0 -291
  57. package/dist/preact.js.map +0 -1
  58. package/dist/type-tYx_kmry.d.ts +0 -5724
package/dist/preact.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import { useStore } from '@nanostores/react';
2
- import { BetterFetchOption } from '@better-fetch/fetch';
3
-
4
- declare const useAuthStore: typeof useStore;
5
-
6
- declare const createAuthClient: (options?: BetterFetchOption) => any;
7
-
8
- export { createAuthClient, useAuthStore };
package/dist/preact.js DELETED
@@ -1,291 +0,0 @@
1
- // src/client/preact.ts
2
- import { useStore } from "@nanostores/react";
3
-
4
- // src/client/base.ts
5
- import { createFetch } from "@better-fetch/fetch";
6
-
7
- // src/error/better-auth-error.ts
8
- var BetterAuthError = class extends Error {
9
- constructor(message) {
10
- super(message);
11
- }
12
- };
13
-
14
- // src/utils/base-url.ts
15
- function checkHasPath(url) {
16
- try {
17
- const parsedUrl = new URL(url);
18
- return parsedUrl.pathname !== "/";
19
- } catch (error) {
20
- console.error("Invalid URL:", error);
21
- return false;
22
- }
23
- }
24
- function withPath(url, path = "/api/auth") {
25
- const hasPath = checkHasPath(url);
26
- if (hasPath) {
27
- return {
28
- baseURL: new URL(url).origin,
29
- withPath: url
30
- };
31
- }
32
- path = path.startsWith("/") ? path : `/${path}`;
33
- return {
34
- baseURL: url,
35
- withPath: `${url}${path}`
36
- };
37
- }
38
- function getBaseURL(url, path) {
39
- if (url) {
40
- return withPath(url, path);
41
- }
42
- const env = typeof process !== "undefined" ? process.env : {};
43
- const fromEnv = env.BETTER_AUTH_URL || env.AUTH_URL || env.NEXT_PUBLIC_AUTH_URL || env.NEXT_PUBLIC_BETTER_AUTH_URL || env.PUBLIC_AUTH_URL || env.PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_BETTER_AUTH_URL || env.NUXT_PUBLIC_AUTH_URL;
44
- if (fromEnv) {
45
- return withPath(fromEnv, path);
46
- }
47
- const isDev = !fromEnv && (env.NODE_ENV === "development" || env.NODE_ENV === "test");
48
- if (isDev) {
49
- return {
50
- baseURL: "http://localhost:3000",
51
- withPath: "http://localhost:3000/api/auth"
52
- };
53
- }
54
- throw new BetterAuthError(
55
- "Could not infer baseURL from environment variables"
56
- );
57
- }
58
-
59
- // src/client/fetch-plugins.ts
60
- import { betterFetch } from "@better-fetch/fetch";
61
- var redirectPlugin = {
62
- id: "redirect",
63
- name: "Redirect",
64
- hooks: {
65
- onSuccess(context) {
66
- if (context.data?.url && context.data?.redirect) {
67
- window.location.href = context.data.url;
68
- }
69
- }
70
- }
71
- };
72
- var addCurrentURL = {
73
- id: "add-current-url",
74
- name: "Add current URL",
75
- hooks: {
76
- onRequest(context) {
77
- if (typeof window !== "undefined") {
78
- const url = new URL(context.url);
79
- url.searchParams.set("currentURL", window.location.href);
80
- context.url = url;
81
- }
82
- return context;
83
- }
84
- }
85
- };
86
- var csrfPlugin = {
87
- id: "csrf",
88
- name: "CSRF Check",
89
- async init(url, options) {
90
- if (options?.method !== "GET") {
91
- options = options || {};
92
- const { data, error } = await betterFetch("/csrf", {
93
- body: void 0,
94
- baseURL: options.baseURL,
95
- plugins: [],
96
- method: "GET",
97
- credentials: "include"
98
- });
99
- if (error?.status === 404) {
100
- throw new BetterAuthError(
101
- "Route not found. Make sure the server is running and the base URL is correct and includes the path (e.g. http://localhost:3000/api/auth)."
102
- );
103
- }
104
- if (error) {
105
- throw new BetterAuthError(error.message || "Failed to get CSRF token.");
106
- }
107
- options.body = {
108
- ...options?.body,
109
- csrfToken: data.csrfToken
110
- };
111
- }
112
- options.credentials = "include";
113
- return { url, options };
114
- }
115
- };
116
-
117
- // src/client/proxy.ts
118
- function getMethod(path, knownPathMethods, args) {
119
- const method = knownPathMethods[path];
120
- const { options, query, ...body } = args || {};
121
- if (method) {
122
- return method;
123
- }
124
- if (options?.method) {
125
- return options.method;
126
- }
127
- if (body && Object.keys(body).length > 0) {
128
- return "POST";
129
- }
130
- return "GET";
131
- }
132
- function createDynamicPathProxy(routes, client, knownPathMethods, $signal, $signals) {
133
- function createProxy(path = []) {
134
- return new Proxy(function() {
135
- }, {
136
- get(target, prop) {
137
- const fullPath = [...path, prop];
138
- let current = routes;
139
- for (const segment of fullPath) {
140
- if (current && typeof current === "object" && segment in current) {
141
- current = current[segment];
142
- } else {
143
- current = void 0;
144
- break;
145
- }
146
- }
147
- if (typeof current === "function") {
148
- return current;
149
- }
150
- return createProxy(fullPath);
151
- },
152
- apply: async (_, __, args) => {
153
- const routePath = "/" + path.map(
154
- (segment) => segment.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
155
- ).join("/");
156
- const arg = args[0] || {};
157
- const method = getMethod(routePath, knownPathMethods, arg);
158
- const { query, options, ...body } = arg;
159
- return await client(routePath, {
160
- ...options,
161
- body: method === "GET" ? void 0 : body,
162
- query,
163
- method,
164
- async onSuccess(context) {
165
- const signal = $signal?.find((s) => s.matcher(routePath));
166
- if (!signal) return;
167
- const signalAtom = $signals?.[signal.atom];
168
- if (!signalAtom) return;
169
- signalAtom.set(!signalAtom.get());
170
- await options?.onSuccess?.(context);
171
- }
172
- });
173
- }
174
- });
175
- }
176
- return createProxy();
177
- }
178
-
179
- // src/client/session-atom.ts
180
- import { atom, computed, task } from "nanostores";
181
- function getSessionAtom(client) {
182
- const $signal = atom(false);
183
- const $session = computed(
184
- $signal,
185
- () => task(async () => {
186
- const session = await client("/session", {
187
- credentials: "include",
188
- method: "GET"
189
- });
190
- return session.data;
191
- })
192
- );
193
- return { $session, $sessionSignal: $signal };
194
- }
195
-
196
- // src/client/base.ts
197
- var createAuthFetch = (options) => {
198
- const $baseFetch = createFetch();
199
- return createFetch({
200
- method: "GET",
201
- ...options,
202
- baseURL: getBaseURL(options?.baseURL).withPath,
203
- plugins: [
204
- ...options?.plugins || [],
205
- ...options?.authPlugins?.flatMap((plugin) => plugin($baseFetch).fetchPlugins).filter((plugin) => plugin !== void 0) || [],
206
- ...options?.csrfPlugin !== false ? [csrfPlugin] : [],
207
- redirectPlugin,
208
- addCurrentURL
209
- ]
210
- });
211
- };
212
- var createAuthClient = (options, additionalActions = {}) => {
213
- const $fetch = createAuthFetch(options);
214
- const { $session, $sessionSignal } = getSessionAtom($fetch);
215
- let pluginsActions = {};
216
- const pluginProxySignals = [];
217
- let pluginSignals = {};
218
- let pluginPathMethods = {};
219
- for (const plugin of options?.authPlugins || []) {
220
- const pl = plugin($fetch);
221
- if (pl.authProxySignal) {
222
- pluginProxySignals.push(...pl.authProxySignal);
223
- }
224
- if (pl.actions) {
225
- pluginsActions = {
226
- ...pluginsActions,
227
- ...pl.actions
228
- };
229
- }
230
- if (pl.signals) {
231
- pluginSignals = {
232
- ...pluginSignals,
233
- ...pl.signals
234
- };
235
- }
236
- if (pl.pathMethods) {
237
- pluginPathMethods = {
238
- ...pluginPathMethods,
239
- ...pl.pathMethods
240
- };
241
- }
242
- }
243
- const actions = {
244
- $atoms: {
245
- $session
246
- },
247
- $fetch,
248
- ...pluginsActions,
249
- ...additionalActions
250
- };
251
- const proxy = createDynamicPathProxy(
252
- actions,
253
- $fetch,
254
- {
255
- ...pluginPathMethods,
256
- "/sign-out": "POST"
257
- },
258
- [
259
- {
260
- matcher: (path) => path === "/organization/create",
261
- atom: "$listOrg"
262
- },
263
- {
264
- matcher: (path) => path.startsWith("/organization"),
265
- atom: "$activeOrgSignal"
266
- },
267
- {
268
- matcher: (path) => path === "/sign-out" || path.startsWith("/sign-up") || path.startsWith("/sign-in"),
269
- atom: "$sessionSignal"
270
- },
271
- ...pluginProxySignals
272
- ],
273
- {
274
- $sessionSignal,
275
- ...pluginSignals
276
- }
277
- );
278
- return proxy;
279
- };
280
-
281
- // src/client/preact.ts
282
- var useAuthStore = useStore;
283
- var createAuthClient2 = (options) => {
284
- const client = createAuthClient(options);
285
- return Object.assign(client);
286
- };
287
- export {
288
- createAuthClient2 as createAuthClient,
289
- useAuthStore
290
- };
291
- //# sourceMappingURL=preact.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/client/preact.ts","../src/client/base.ts","../src/error/better-auth-error.ts","../src/utils/base-url.ts","../src/client/fetch-plugins.ts","../src/client/proxy.ts","../src/client/session-atom.ts"],"sourcesContent":["import { useStore } from \"@nanostores/react\";\nexport const useAuthStore = useStore;\n\nimport type { BetterFetchOption } from \"@better-fetch/fetch\";\nimport { createAuthClient as createVanillaClient } from \"./base\";\n\nexport const createAuthClient = (options?: BetterFetchOption) => {\n\tconst client = createVanillaClient(options);\n\treturn Object.assign(client);\n};\n","import { createFetch } from \"@better-fetch/fetch\";\nimport type { Auth } from \"../auth\";\nimport { getBaseURL } from \"../utils/base-url\";\nimport { addCurrentURL, csrfPlugin, redirectPlugin } from \"./fetch-plugins\";\nimport type { InferRoutes } from \"./path-to-object\";\nimport { createDynamicPathProxy, type AuthProxySignal } from \"./proxy\";\nimport { getSessionAtom } from \"./session-atom\";\nimport type { AuthPlugin, ClientOptions } from \"./type\";\nimport type { UnionToIntersection } from \"../types/helper\";\nimport type { PreinitializedWritableAtom } from \"nanostores\";\nimport type { BetterAuthPlugin } from \"../types/plugins\";\n\n/**\n * used for plugins only\n */\n\nexport const createAuthFetch = (options?: ClientOptions) => {\n\tconst $baseFetch = createFetch();\n\treturn createFetch({\n\t\tmethod: \"GET\",\n\t\t...options,\n\t\tbaseURL: getBaseURL(options?.baseURL).withPath,\n\t\tplugins: [\n\t\t\t...(options?.plugins || []),\n\t\t\t...(options?.authPlugins\n\t\t\t\t?.flatMap((plugin) => plugin($baseFetch).fetchPlugins)\n\t\t\t\t.filter((plugin) => plugin !== undefined) || []),\n\t\t\t...(options?.csrfPlugin !== false ? [csrfPlugin] : []),\n\t\t\tredirectPlugin,\n\t\t\taddCurrentURL,\n\t\t],\n\t});\n};\n\nexport const createAuthClient = <\n\tO extends ClientOptions = ClientOptions,\n\tAT extends Record<string, any> = {},\n>(\n\toptions?: O,\n\tadditionalActions = {} as AT,\n) => {\n\ttype API = O[\"authPlugins\"] extends Array<any>\n\t\t? (O[\"authPlugins\"] extends Array<infer Pl>\n\t\t\t\t? UnionToIntersection<\n\t\t\t\t\t\t//@ts-expect-error\n\t\t\t\t\t\tReturnType<Pl> extends {\n\t\t\t\t\t\t\tplugin: infer Plug;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t\t? Plug extends BetterAuthPlugin\n\t\t\t\t\t\t\t\t? Plug[\"endpoints\"]\n\t\t\t\t\t\t\t\t: {}\n\t\t\t\t\t\t\t: {}\n\t\t\t\t\t>\n\t\t\t\t: {}) &\n\t\t\t\tAuth[\"api\"]\n\t\t: Auth[\"api\"];\n\n\tconst $fetch = createAuthFetch(options);\n\n\ttype Plugins = O[\"authPlugins\"] extends Array<AuthPlugin>\n\t\t? Array<ReturnType<O[\"authPlugins\"][number]>[\"plugin\"]>\n\t\t: undefined;\n\t//@ts-expect-error\n\tconst { $session, $sessionSignal } = getSessionAtom<{\n\t\thandler: any;\n\t\tapi: any;\n\t\toptions: {\n\t\t\tdatabase: any;\n\t\t\tplugins: Plugins;\n\t\t};\n\t}>($fetch);\n\n\tlet pluginsActions = {} as Record<string, any>;\n\ttype PluginActions = UnionToIntersection<\n\t\tO[\"authPlugins\"] extends Array<infer Pl>\n\t\t\t? //@ts-expect-error\n\t\t\t\tReturnType<Pl> extends {\n\t\t\t\t\tactions?: infer R;\n\t\t\t\t}\n\t\t\t\t? R\n\t\t\t\t: {}\n\t\t\t: {}\n\t>;\n\n\tconst pluginProxySignals: AuthProxySignal[] = [];\n\tlet pluginSignals: Record<string, PreinitializedWritableAtom<boolean>> = {};\n\tlet pluginPathMethods: Record<string, \"POST\" | \"GET\"> = {};\n\n\tfor (const plugin of options?.authPlugins || []) {\n\t\tconst pl = plugin($fetch);\n\t\tif (pl.authProxySignal) {\n\t\t\tpluginProxySignals.push(...pl.authProxySignal);\n\t\t}\n\t\tif (pl.actions) {\n\t\t\tpluginsActions = {\n\t\t\t\t...pluginsActions,\n\t\t\t\t...pl.actions,\n\t\t\t};\n\t\t}\n\t\tif (pl.signals) {\n\t\t\tpluginSignals = {\n\t\t\t\t...pluginSignals,\n\t\t\t\t...pl.signals,\n\t\t\t};\n\t\t}\n\t\tif (pl.pathMethods) {\n\t\t\tpluginPathMethods = {\n\t\t\t\t...pluginPathMethods,\n\t\t\t\t...pl.pathMethods,\n\t\t\t};\n\t\t}\n\t}\n\n\tconst actions = {\n\t\t$atoms: {\n\t\t\t$session,\n\t\t},\n\t\t$fetch,\n\t\t...(pluginsActions as object),\n\t\t...additionalActions,\n\t};\n\n\ttype Actions = typeof actions & PluginActions;\n\n\tconst proxy = createDynamicPathProxy(\n\t\tactions,\n\t\t$fetch,\n\t\t{\n\t\t\t...pluginPathMethods,\n\t\t\t\"/sign-out\": \"POST\",\n\t\t},\n\t\t[\n\t\t\t{\n\t\t\t\tmatcher: (path) => path === \"/organization/create\",\n\t\t\t\tatom: \"$listOrg\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tmatcher: (path) => path.startsWith(\"/organization\"),\n\t\t\t\tatom: \"$activeOrgSignal\",\n\t\t\t},\n\t\t\t{\n\t\t\t\tmatcher: (path) =>\n\t\t\t\t\tpath === \"/sign-out\" ||\n\t\t\t\t\tpath.startsWith(\"/sign-up\") ||\n\t\t\t\t\tpath.startsWith(\"/sign-in\"),\n\t\t\t\tatom: \"$sessionSignal\",\n\t\t\t},\n\t\t\t...pluginProxySignals,\n\t\t],\n\t\t{\n\t\t\t$sessionSignal,\n\t\t\t...pluginSignals,\n\t\t},\n\t) as unknown as InferRoutes<API> & Actions;\n\treturn proxy;\n};\n","export class BetterAuthError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t}\n}\n","import { BetterAuthError } from \"../error/better-auth-error\";\n\nfunction checkHasPath(url: string): boolean {\n\ttry {\n\t\tconst parsedUrl = new URL(url);\n\t\treturn parsedUrl.pathname !== \"/\";\n\t} catch (error) {\n\t\tconsole.error(\"Invalid URL:\", error);\n\t\treturn false;\n\t}\n}\n\nfunction withPath(url: string, path = \"/api/auth\") {\n\tconst hasPath = checkHasPath(url);\n\tif (hasPath) {\n\t\treturn {\n\t\t\tbaseURL: new URL(url).origin,\n\t\t\twithPath: url,\n\t\t};\n\t}\n\tpath = path.startsWith(\"/\") ? path : `/${path}`;\n\treturn {\n\t\tbaseURL: url,\n\t\twithPath: `${url}${path}`,\n\t};\n}\n\nexport function getBaseURL(url?: string, path?: string) {\n\tif (url) {\n\t\treturn withPath(url, path);\n\t}\n\tconst env: any = typeof process !== \"undefined\" ? process.env : {};\n\tconst fromEnv =\n\t\tenv.BETTER_AUTH_URL ||\n\t\tenv.AUTH_URL ||\n\t\tenv.NEXT_PUBLIC_AUTH_URL ||\n\t\tenv.NEXT_PUBLIC_BETTER_AUTH_URL ||\n\t\tenv.PUBLIC_AUTH_URL ||\n\t\tenv.PUBLIC_BETTER_AUTH_URL ||\n\t\tenv.NUXT_PUBLIC_BETTER_AUTH_URL ||\n\t\tenv.NUXT_PUBLIC_AUTH_URL;\n\tif (fromEnv) {\n\t\treturn withPath(fromEnv, path);\n\t}\n\n\tconst isDev =\n\t\t!fromEnv && (env.NODE_ENV === \"development\" || env.NODE_ENV === \"test\");\n\tif (isDev) {\n\t\treturn {\n\t\t\tbaseURL: \"http://localhost:3000\",\n\t\t\twithPath: \"http://localhost:3000/api/auth\",\n\t\t};\n\t}\n\tthrow new BetterAuthError(\n\t\t\"Could not infer baseURL from environment variables\",\n\t);\n}\n","import { type BetterFetchPlugin, betterFetch } from \"@better-fetch/fetch\";\nimport { BetterAuthError } from \"../error/better-auth-error\";\n\nexport const redirectPlugin = {\n\tid: \"redirect\",\n\tname: \"Redirect\",\n\thooks: {\n\t\tonSuccess(context) {\n\t\t\tif (context.data?.url && context.data?.redirect) {\n\t\t\t\twindow.location.href = context.data.url;\n\t\t\t}\n\t\t},\n\t},\n} satisfies BetterFetchPlugin;\n\nexport const addCurrentURL = {\n\tid: \"add-current-url\",\n\tname: \"Add current URL\",\n\thooks: {\n\t\tonRequest(context) {\n\t\t\tif (typeof window !== \"undefined\") {\n\t\t\t\tconst url = new URL(context.url);\n\t\t\t\turl.searchParams.set(\"currentURL\", window.location.href);\n\t\t\t\tcontext.url = url;\n\t\t\t}\n\t\t\treturn context;\n\t\t},\n\t},\n} satisfies BetterFetchPlugin;\n\nexport const csrfPlugin = {\n\tid: \"csrf\",\n\tname: \"CSRF Check\",\n\tasync init(url, options) {\n\t\tif (options?.method !== \"GET\") {\n\t\t\toptions = options || {};\n\t\t\tconst { data, error } = await betterFetch<{\n\t\t\t\tcsrfToken: string;\n\t\t\t}>(\"/csrf\", {\n\t\t\t\tbody: undefined,\n\t\t\t\tbaseURL: options.baseURL,\n\t\t\t\tplugins: [],\n\t\t\t\tmethod: \"GET\",\n\t\t\t\tcredentials: \"include\",\n\t\t\t});\n\t\t\tif (error?.status === 404) {\n\t\t\t\tthrow new BetterAuthError(\n\t\t\t\t\t\"Route not found. Make sure the server is running and the base URL is correct and includes the path (e.g. http://localhost:3000/api/auth).\",\n\t\t\t\t);\n\t\t\t}\n\t\t\tif (error) {\n\t\t\t\tthrow new BetterAuthError(error.message || \"Failed to get CSRF token.\");\n\t\t\t}\n\t\t\toptions.body = {\n\t\t\t\t...options?.body,\n\t\t\t\tcsrfToken: data.csrfToken,\n\t\t\t};\n\t\t}\n\t\toptions.credentials = \"include\";\n\t\treturn { url, options };\n\t},\n} satisfies BetterFetchPlugin;\n","import type { BetterFetch } from \"@better-fetch/fetch\";\nimport type { PreinitializedWritableAtom } from \"nanostores\";\nimport type { ProxyRequest } from \"./path-to-object\";\nimport type { LiteralUnion } from \"../types/helper\";\n\nfunction getMethod(\n\tpath: string,\n\tknownPathMethods: Record<string, \"POST\" | \"GET\">,\n\targs?: ProxyRequest,\n) {\n\tconst method = knownPathMethods[path];\n\tconst { options, query, ...body } = args || {};\n\tif (method) {\n\t\treturn method;\n\t}\n\tif (options?.method) {\n\t\treturn options.method;\n\t}\n\tif (body && Object.keys(body).length > 0) {\n\t\treturn \"POST\";\n\t}\n\treturn \"GET\";\n}\n\nexport type AuthProxySignal = {\n\tatom: LiteralUnion<string, \"$sessionSignal\">;\n\tmatcher: (path: string) => boolean;\n};\n\nexport function createDynamicPathProxy<T extends Record<string, any>>(\n\troutes: T,\n\tclient: BetterFetch,\n\tknownPathMethods: Record<string, \"POST\" | \"GET\">,\n\t$signal?: AuthProxySignal[],\n\t$signals?: Record<string, PreinitializedWritableAtom<boolean>>,\n): T {\n\tfunction createProxy(path: string[] = []): any {\n\t\treturn new Proxy(function () {}, {\n\t\t\tget(target, prop: string) {\n\t\t\t\tconst fullPath = [...path, prop];\n\t\t\t\tlet current: any = routes;\n\t\t\t\tfor (const segment of fullPath) {\n\t\t\t\t\tif (current && typeof current === \"object\" && segment in current) {\n\t\t\t\t\t\tcurrent = current[segment];\n\t\t\t\t\t} else {\n\t\t\t\t\t\tcurrent = undefined;\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (typeof current === \"function\") {\n\t\t\t\t\treturn current;\n\t\t\t\t}\n\n\t\t\t\treturn createProxy(fullPath);\n\t\t\t},\n\t\t\tapply: async (_, __, args) => {\n\t\t\t\tconst routePath =\n\t\t\t\t\t\"/\" +\n\t\t\t\t\tpath\n\t\t\t\t\t\t.map((segment) =>\n\t\t\t\t\t\t\tsegment.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`),\n\t\t\t\t\t\t)\n\t\t\t\t\t\t.join(\"/\");\n\n\t\t\t\tconst arg = (args[0] || {}) as ProxyRequest;\n\t\t\t\tconst method = getMethod(routePath, knownPathMethods, arg);\n\t\t\t\tconst { query, options, ...body } = arg;\n\n\t\t\t\treturn await client(routePath, {\n\t\t\t\t\t...options,\n\t\t\t\t\tbody: method === \"GET\" ? undefined : body,\n\t\t\t\t\tquery: query,\n\t\t\t\t\tmethod,\n\t\t\t\t\tasync onSuccess(context) {\n\t\t\t\t\t\tconst signal = $signal?.find((s) => s.matcher(routePath));\n\t\t\t\t\t\tif (!signal) return;\n\t\t\t\t\t\tconst signalAtom = $signals?.[signal.atom];\n\t\t\t\t\t\tif (!signalAtom) return;\n\t\t\t\t\t\tsignalAtom.set(!signalAtom.get());\n\t\t\t\t\t\tawait options?.onSuccess?.(context);\n\t\t\t\t\t},\n\t\t\t\t});\n\t\t\t},\n\t\t});\n\t}\n\n\treturn createProxy() as T;\n}\n","import type { BetterFetch } from \"@better-fetch/fetch\";\nimport { atom, computed, task } from \"nanostores\";\nimport type { Auth as BetterAuth } from \"../auth\";\nimport type { Prettify } from \"../types/helper\";\nimport type { InferSession, InferUser } from \"../types/models\";\n\nexport function getSessionAtom<Auth extends BetterAuth>(client: BetterFetch) {\n\ttype UserWithAdditionalFields = InferUser<Auth[\"options\"]>;\n\ttype SessionWithAdditionalFields = InferSession<Auth[\"options\"]>;\n\tconst $signal = atom<boolean>(false);\n\tconst $session = computed($signal, () =>\n\t\ttask(async () => {\n\t\t\tconst session = await client(\"/session\", {\n\t\t\t\tcredentials: \"include\",\n\t\t\t\tmethod: \"GET\",\n\t\t\t});\n\t\t\treturn session.data as {\n\t\t\t\tuser: Prettify<UserWithAdditionalFields>;\n\t\t\t\tsession: Prettify<SessionWithAdditionalFields>;\n\t\t\t} | null;\n\t\t}),\n\t);\n\treturn { $session, $sessionSignal: $signal };\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;;;ACA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