better-auth 0.2.2 → 0.2.3-beta.10

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.ts CHANGED
@@ -3,16 +3,14 @@ import * as nanostores from 'nanostores';
3
3
  import { PreinitializedWritableAtom } from 'nanostores';
4
4
  import * as _better_fetch_fetch from '@better-fetch/fetch';
5
5
  import { BetterFetch, BetterFetchError, BetterFetchOption } from '@better-fetch/fetch';
6
- import { B as BetterAuthPlugin, F as FieldAttribute, I as InferFieldOutput } from './index-D_ohe9r9.js';
6
+ import { B as BetterAuthPlugin, F as FieldAttribute, I as InferFieldOutput } from './index-gO-yM4kI.js';
7
7
  import { U as UnionToIntersection, P as Prettify } from './helper-C1ihmerM.js';
8
8
  import { ClientOptions, InferClientAPI, InferActions, BetterAuthClientPlugin, InferSessionFromClient, InferUserFromClient, IsSignal } from './types.js';
9
9
  export { AtomListener, InferPluginsFromClient } from './types.js';
10
10
  import 'kysely';
11
- import './schema-D9o3OF80.js';
12
- import 'better-call';
13
- import './social.js';
11
+ import './index-CE92ti2Z.js';
14
12
  import 'arctic';
15
- import './adapter-D-m9-hQp.js';
13
+ import 'better-call';
16
14
 
17
15
  type InferResolvedHooks<O extends ClientOptions> = O["plugins"] extends Array<infer Plugin> ? Plugin extends BetterAuthClientPlugin ? Plugin["getAtoms"] extends (fetch: any) => infer Atoms ? Atoms extends Record<string, any> ? {
18
16
  [key in keyof Atoms as IsSignal<key> extends true ? never : key extends string ? `use${Capitalize<key>}` : never]: Atoms[key];
package/dist/client.js CHANGED
@@ -1 +1 @@
1
- var z=Object.defineProperty,X=Object.defineProperties;var D=Object.getOwnPropertyDescriptors;var R=Object.getOwnPropertySymbols;var L=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var S=(e,t,r)=>t in e?z(e,t,{enumerable:!0,configurable:!0,writable:!0,value:r}):e[t]=r,l=(e,t)=>{for(var r in t||(t={}))L.call(t,r)&&S(e,r,t[r]);if(R)for(var r of R(t))x.call(t,r)&&S(e,r,t[r]);return e},m=(e,t)=>X(e,D(t));var O=(e,t)=>{var r={};for(var i in e)L.call(e,i)&&t.indexOf(i)<0&&(r[i]=e[i]);if(e!=null&&R)for(var i of R(e))t.indexOf(i)<0&&x.call(e,i)&&(r[i]=e[i]);return r};var y=(e,t,r)=>new Promise((i,n)=>{var d=f=>{try{s(r.next(f))}catch(u){n(u)}},c=f=>{try{s(r.throw(f))}catch(u){n(u)}},s=f=>f.done?i(f.value):Promise.resolve(f.value).then(d,c);s((r=r.apply(e,t)).next())});import{createFetch as J}from"@better-fetch/fetch";var p=class extends Error{constructor(t,r,i){super(t),this.name="BetterAuthError",this.message=t,this.cause=r}};function K(e){try{return new URL(e).pathname!=="/"}catch(t){throw new p(`Invalid base URL: ${e}. Please provide a valid base URL.`)}}function B(e,t="/api/auth"){return K(e)?e:(t=t.startsWith("/")?t:`/${t}`,`${e}${t}`)}function w(e,t){if(e)return B(e,t);let r=(process==null?void 0:process.env)||{},i=r.BETTER_AUTH_URL||r.NEXT_PUBLIC_BETTER_AUTH_URL||r.PUBLIC_BETTER_AUTH_URL||r.NUXT_PUBLIC_BETTER_AUTH_URL||r.NUXT_PUBLIC_AUTH_URL||(r.BASE_URL!=="/"?r.BASE_URL:void 0);if(i)return B(i,t);if(typeof window!="undefined")return B(window.location.origin,t)}import"nanostores";import{betterFetch as Q}from"@better-fetch/fetch";var E={id:"redirect",name:"Redirect",hooks:{onSuccess(e){var t,r;(t=e.data)!=null&&t.url&&((r=e.data)!=null&&r.redirect)&&typeof window!="undefined"&&(window.location.href=e.data.url)}}},_={id:"add-current-url",name:"Add current URL",hooks:{onRequest(e){if(typeof window!="undefined"){let t=new URL(e.url);t.searchParams.set("currentURL",window.location.href),e.url=t}return e}}},I=new Map,k={id:"csrf",name:"CSRF Check",init(e,t){return y(this,null,function*(){if(!(t!=null&&t.baseURL))throw new p("API Base URL on the auth client isn't configured. Please pass it directly to the client `baseURL`");if((t==null?void 0:t.method)!=="GET"){t=t||{};let r=I.get("CSRF_TOKEN");if(!r){let{data:i,error:n}=yield Q("/csrf",{body:void 0,baseURL:t.baseURL,plugins:[],method:"GET",credentials:"include",customFetchImpl:t.customFetchImpl});if(n){if(n.status===404)throw new p("CSRF 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).");if(n.status===429)return new Response(JSON.stringify({message:"Too many requests. Please try again later."}),{status:429,statusText:"Too Many Requests"});throw new p("Failed to fetch CSRF token: "+n.message)}I.set("CSRF_TOKEN",i.csrfToken)}t.body=m(l({},t==null?void 0:t.body),{csrfToken:r})}return t.credentials="include",{url:e,options:t}})}};var v=e=>{var s,f,u,o,h,P;let t=J(m(l({baseURL:w(((s=e==null?void 0:e.fetchOptions)==null?void 0:s.baseURL)||(e==null?void 0:e.baseURL)),credentials:"include"},e==null?void 0:e.fetchOptions),{plugins:[k,E,_,...((u=(f=e==null?void 0:e.fetchOptions)==null?void 0:f.plugins)==null?void 0:u.filter(a=>a!==void 0))||[],...((o=e==null?void 0:e.plugins)==null?void 0:o.flatMap(a=>a.fetchPlugins).filter(a=>a!==void 0))||[]]})),r=(e==null?void 0:e.plugins)||[],i={},n={},d={"/sign-out":"POST","/user/revoke-sessions":"POST"},c=[{signal:"_sessionSignal",matcher(a){return a==="/sign-out"||a==="sign-up/email"||a==="/user/update"}}];for(let a of r)a.getActions&&Object.assign(i,(h=a.getActions)==null?void 0:h.call(a,t)),a.getAtoms&&Object.assign(n,(P=a.getAtoms)==null?void 0:P.call(a,t)),a.pathMethods&&Object.assign(d,a.pathMethods),a.atomListeners&&c.push(...a.atomListeners);return{pluginsActions:i,pluginsAtoms:n,pluginPathMethods:d,atomListeners:c,$fetch:t}};function $(e){return e.charAt(0).toUpperCase()+e.slice(1)}function V(e,t,r){let i=t[e],s=r||{},{fetchOptions:n,query:d}=s,c=O(s,["fetchOptions","query"]);return i||(n!=null&&n.method?n.method:c&&Object.keys(c).length>0?"POST":"GET")}function q(e,t,r,i,n){function d(c=[]){return new Proxy(function(){},{get(s,f){let u=[...c,f],o=e;for(let h of u)if(o&&typeof o=="object"&&h in o)o=o[h];else{o=void 0;break}return typeof o=="function"?o:d(u)},apply:(s,f,u)=>y(this,null,function*(){let o="/"+c.map(T=>T.replace(/[A-Z]/g,A=>`-${A.toLowerCase()}`)).join("/"),h=u[0]||{},P=u[1]||{},F=h,{query:a,fetchOptions:H}=F,j=O(F,["query","fetchOptions"]),g=l(l({},P),H),b=V(o,r,h);return yield t(o,m(l({},g),{body:b==="GET"?void 0:l(l({},j),(g==null?void 0:g.body)||{}),query:a||(g==null?void 0:g.query),method:b,onSuccess(T){return y(this,null,function*(){var C;yield(C=g==null?void 0:g.onSuccess)==null?void 0:C.call(g,T);let A=n==null?void 0:n.find(N=>N.matcher(o));if(!A)return;let U=i[A.signal];if(!U)return;let G=U.get();setTimeout(()=>{U.set(!G)},10)})}}))})})}return d()}import{atom as ee}from"nanostores";import"@better-fetch/fetch";import{atom as Z,onMount as Y}from"nanostores";var W=(e,t,r,i)=>{let n=Z({data:null,error:null,isPending:!1}),d=()=>{let s=typeof i=="function"?i({data:n.get().data,error:n.get().error,isPending:n.get().isPending}):i;return r(t,m(l({},s),{onSuccess:u=>y(void 0,null,function*(){var o;n.set({data:u.data,error:null,isPending:!1}),yield(o=s==null?void 0:s.onSuccess)==null?void 0:o.call(s,u)}),onError(u){return y(this,null,function*(){var o;n.set({error:u.error,data:null,isPending:!1}),yield(o=s==null?void 0:s.onError)==null?void 0:o.call(s,u)})},onRequest(u){return y(this,null,function*(){var h;let o=n.get();n.set({isPending:!0,data:o.data,error:o.error}),yield(h=s==null?void 0:s.onRequest)==null?void 0:h.call(s,u)})}}))};e=Array.isArray(e)?e:[e];let c=!1;for(let s of e)s.subscribe(()=>{c?d():Y(n,()=>(d(),c=!0,()=>{n.off(),s.off()}))});return n};function M(e){let t=ee(!1);return{$session:W(t,"/session",e,{method:"GET"}),_sessionSignal:t,$Infer:{}}}function xe(e){let{pluginPathMethods:t,pluginsActions:r,pluginsAtoms:i,$fetch:n,atomListeners:d}=v(e),c={};for(let[h,P]of Object.entries(i))c[`use${$(h)}`]=P;let{$session:s,_sessionSignal:f}=M(n),u=m(l(l({},r),c),{$fetch:n,useSession:s});return q(u,n,t,m(l({},i),{_sessionSignal:f}),d)}export{xe as createAuthClient,W as useAuthQuery};
1
+ import{createFetch as $}from"@better-fetch/fetch";var f=class extends Error{constructor(t,s,i){super(t),this.name="BetterAuthError",this.message=t,this.cause=s}};function k(e){try{return new URL(e).pathname!=="/"}catch{throw new f(`Invalid base URL: ${e}. Please provide a valid base URL.`)}}function P(e,t="/api/auth"){return k(e)?e:(t=t.startsWith("/")?t:`/${t}`,`${e}${t}`)}function R(e,t){if(e)return P(e,t);let s=process?.env||{},i=s.BETTER_AUTH_URL||s.NEXT_PUBLIC_BETTER_AUTH_URL||s.PUBLIC_BETTER_AUTH_URL||s.NUXT_PUBLIC_BETTER_AUTH_URL||s.NUXT_PUBLIC_AUTH_URL||(s.BASE_URL!=="/"?s.BASE_URL:void 0);if(i)return P(i,t);if(typeof window<"u")return P(window.location.origin,t)}import"nanostores";import{betterFetch as v}from"@better-fetch/fetch";var U={id:"redirect",name:"Redirect",hooks:{onSuccess(e){e.data?.url&&e.data?.redirect&&typeof window<"u"&&(window.location.href=e.data.url)}}},O={id:"add-current-url",name:"Add current URL",hooks:{onRequest(e){if(typeof window<"u"){let t=new URL(e.url);t.searchParams.set("currentURL",window.location.href),e.url=t}return e}}},T=new Map,B={id:"csrf",name:"CSRF Check",async init(e,t){if(!t?.baseURL)throw new f("API Base URL on the auth client isn't configured. Please pass it directly to the client `baseURL`");if(t?.method!=="GET"){t=t||{};let s=T.get("CSRF_TOKEN");if(!s){let{data:i,error:r}=await v("/csrf",{body:void 0,baseURL:t.baseURL,plugins:[],method:"GET",credentials:"include",customFetchImpl:t.customFetchImpl});if(r){if(r.status===404)throw new f("CSRF 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).");if(r.status===429)return new Response(JSON.stringify({message:"Too many requests. Please try again later."}),{status:429,statusText:"Too Many Requests"});throw new f("Failed to fetch CSRF token: "+r.message)}T.set("CSRF_TOKEN",i.csrfToken)}t.body={...t?.body,csrfToken:s}}return t.credentials="include",{url:e,options:t}}};var b=e=>{let t=$({baseURL:R(e?.fetchOptions?.baseURL||e?.baseURL),credentials:"include",...e?.fetchOptions,plugins:[B,U,O,...e?.fetchOptions?.plugins?.filter(n=>n!==void 0)||[],...e?.plugins?.flatMap(n=>n.fetchPlugins).filter(n=>n!==void 0)||[]]}),s=e?.plugins||[],i={},r={},c={"/sign-out":"POST","/user/revoke-sessions":"POST"},o=[{signal:"_sessionSignal",matcher(n){return n==="/sign-out"||n==="sign-up/email"||n==="/user/update"}}];for(let n of s)n.getActions&&Object.assign(i,n.getActions?.(t)),n.getAtoms&&Object.assign(r,n.getAtoms?.(t)),n.pathMethods&&Object.assign(c,n.pathMethods),n.atomListeners&&o.push(...n.atomListeners);return{pluginsActions:i,pluginsAtoms:r,pluginPathMethods:c,atomListeners:o,$fetch:t}};function F(e){return e.charAt(0).toUpperCase()+e.slice(1)}function q(e,t,s){let i=t[e],{fetchOptions:r,query:c,...o}=s||{};return i||(r?.method?r.method:o&&Object.keys(o).length>0?"POST":"GET")}function L(e,t,s,i,r){function c(o=[]){return new Proxy(function(){},{get(n,u){let l=[...o,u],a=e;for(let d of l)if(a&&typeof a=="object"&&d in a)a=a[d];else{a=void 0;break}return typeof a=="function"?a:c(l)},apply:async(n,u,l)=>{let a="/"+o.map(m=>m.replace(/[A-Z]/g,h=>`-${h.toLowerCase()}`)).join("/"),d=l[0]||{},g=l[1]||{},{query:x,fetchOptions:w,...I}=d,p={...g,...w},A=q(a,s,d);return await t(a,{...p,body:A==="GET"?void 0:{...I,...p?.body||{}},query:x||p?.query,method:A,async onSuccess(m){await p?.onSuccess?.(m);let h=r?.find(_=>_.matcher(a));if(!h)return;let y=i[h.signal];if(!y)return;let E=y.get();setTimeout(()=>{y.set(!E)},10)}})}})}return c()}import{atom as H}from"nanostores";import"@better-fetch/fetch";import{atom as W,onMount as M}from"nanostores";var C=(e,t,s,i)=>{let r=W({data:null,error:null,isPending:!1}),c=()=>{let n=typeof i=="function"?i({data:r.get().data,error:r.get().error,isPending:r.get().isPending}):i;return s(t,{...n,onSuccess:async u=>{r.set({data:u.data,error:null,isPending:!1}),await n?.onSuccess?.(u)},async onError(u){r.set({error:u.error,data:null,isPending:!1}),await n?.onError?.(u)},async onRequest(u){let l=r.get();r.set({isPending:!0,data:l.data,error:l.error}),await n?.onRequest?.(u)}})};e=Array.isArray(e)?e:[e];let o=!1;for(let n of e)n.subscribe(()=>{o?c():M(r,()=>(c(),o=!0,()=>{r.off(),n.off()}))});return r};function S(e){let t=H(!1);return{$session:C(t,"/session",e,{method:"GET"}),_sessionSignal:t,$Infer:{}}}function de(e){let{pluginPathMethods:t,pluginsActions:s,pluginsAtoms:i,$fetch:r,atomListeners:c}=b(e),o={};for(let[d,g]of Object.entries(i))o[`use${F(d)}`]=g;let{$session:n,_sessionSignal:u}=S(r),l={...s,...o,$fetch:r,useSession:n};return L(l,r,t,{...i,_sessionSignal:u},c)}export{de as createAuthClient,C as useAuthQuery};