@symbo.ls/fetch 3.14.595 → 3.14.599

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/CHANGELOG.md CHANGED
@@ -1,10 +1,22 @@
1
1
  # @symbo.ls/fetch
2
2
 
3
+ ## 3.14.599
4
+
5
+ ### Patch Changes
6
+
7
+ - Manual patch bump triggered via workflow_dispatch (scope: all).
8
+ No source change behind this bump — released to refresh dist or
9
+ coordinate a cross-package version line.
10
+ - Updated dependencies
11
+ - @symbo.ls/utils@3.14.599
12
+
3
13
  ## 3.14.595
4
14
 
5
15
  ### Patch Changes
6
16
 
7
17
  - Auto-generated cross-repo patch release.
18
+ - Updated dependencies
19
+ - @symbo.ls/utils@3.14.595
8
20
 
9
21
  ## 3.14.589
10
22
 
package/adapters/local.js CHANGED
@@ -118,6 +118,33 @@ export const localAdapter = (initialData = {}, options = {}) => {
118
118
  return { data: updated, error: null }
119
119
  },
120
120
 
121
+ // Update-if-matched, else insert. Each row matches on explicit `params`
122
+ // when given, falling back to matching by the row's own `id` (the
123
+ // conventional upsert key) so `upsert({ from, data: { id, ... } })` works
124
+ // without callers having to also pass redundant params.
125
+ upsert: async ({ from, data, params }) => {
126
+ const table = getTable(from)
127
+ const items = Array.isArray(data) ? data : [data]
128
+ const results = []
129
+ for (const item of items) {
130
+ const matchParams = params || (item.id !== undefined ? { id: item.id } : null)
131
+ const idx = matchParams ? table.findIndex(row => matchesParams(row, matchParams)) : -1
132
+ if (idx > -1) {
133
+ const old = { ...table[idx] }
134
+ Object.assign(table[idx], item)
135
+ results.push(table[idx])
136
+ notify(from, 'UPDATE', table[idx], old)
137
+ } else {
138
+ const row = { id: item.id || ++idCounter, ...item }
139
+ table.push(row)
140
+ results.push(row)
141
+ notify(from, 'INSERT', row, null)
142
+ }
143
+ }
144
+ save(from)
145
+ return { data: Array.isArray(data) ? results : results[0], error: null }
146
+ },
147
+
121
148
  delete: async ({ from, params }) => {
122
149
  const table = getTable(from)
123
150
  const removed = []
package/adapters/rest.js CHANGED
@@ -133,6 +133,21 @@ export const restAdapter = (baseUrl, defaultHeaders = {}, authConfig, defaultFet
133
133
  }))
134
134
  },
135
135
 
136
+ // Create-or-replace by id. PUT-by-id is the REST-idiomatic upsert (unlike
137
+ // POST/PATCH, it's conventionally idempotent and backend routers commonly
138
+ // treat "PUT to an id that doesn't exist yet" as a create). Callers that
139
+ // need different upsert semantics for their backend can still pass
140
+ // `method` to override.
141
+ upsert: ({ from, data, params, method, headers, baseUrl: fromBase }) => {
142
+ const id = params?.id || data?.id
143
+ const path = id ? `${from}/${id}` : from
144
+ return request(resolveUrl(path, fromBase), getFetchOptions({
145
+ method: method || 'PUT',
146
+ headers: getJsonHeaders(headers),
147
+ body: JSON.stringify(data)
148
+ }))
149
+ },
150
+
136
151
  delete: ({ from, params, headers, baseUrl: fromBase }) => {
137
152
  const id = params?.id
138
153
  const path = id ? `${from}/${id}` : from
@@ -1 +1 @@
1
- "use strict";var h=Object.defineProperty;var x=Object.getOwnPropertyDescriptor;var v=Object.getOwnPropertyNames;var E=Object.prototype.hasOwnProperty;var O=(r,n)=>{for(var t in n)h(r,t,{get:n[t],enumerable:!0})},T=(r,n,t,s)=>{if(n&&typeof n=="object"||typeof n=="function")for(let l of v(n))!E.call(r,l)&&l!==t&&h(r,l,{get:()=>n[l],enumerable:!(s=x(n,l))||s.enumerable});return r};var w=r=>T(h({},"__esModule",{value:!0}),r);var j={};O(j,{localAdapter:()=>S,setup:()=>N});module.exports=w(j);const A=(r,n)=>{if(!n)return!0;for(const t in n){const s=n[t];if(s===null){if(r[t]!==null&&r[t]!==void 0)return!1}else if(Array.isArray(s)){if(!s.includes(r[t]))return!1}else if(typeof s=="object"){for(const l in s)if(l==="gt"&&!(r[t]>s[l])||l==="gte"&&!(r[t]>=s[l])||l==="lt"&&!(r[t]<s[l])||l==="lte"&&!(r[t]<=s[l])||l==="neq"&&r[t]===s[l]||l==="like"&&!String(r[t]).includes(s[l]))return!1}else if(r[t]!==s)return!1}return!0},I=(r,{order:n,limit:t,offset:s,single:l})=>{let a=[...r];if(n){const y=typeof n=="string"?n:n.by,p=n.asc!==!1;a.sort((b,g)=>b[y]<g[y]?p?-1:1:b[y]>g[y]?p?1:-1:0)}return s&&(a=a.slice(s)),t&&(a=a.slice(0,t)),l?{data:a[0]||null,error:null}:{data:a,error:null}},N=async({data:r,...n})=>S(r,n),S=(r={},n={})=>{const t={},s={},l=n.persist!==!1&&typeof localStorage<"u",a=n.prefix||"smbls_db_",y=e=>{if(!t[e])if(l)try{const u=localStorage.getItem(a+e);t[e]=u?JSON.parse(u):r[e]||[]}catch{t[e]=r[e]||[]}else t[e]=r[e]||[];return t[e]},p=e=>{if(l)try{localStorage.setItem(a+e,JSON.stringify(t[e]))}catch{}},b=(e,u,o,f)=>{const c=s[e];if(c)for(const i of c)i(o,f,{event:u,table:e})};let g=Date.now();return{name:"local",store:t,select:async({from:e,params:u,...o})=>{const c=y(e).filter(i=>A(i,u));return I(c,o)},insert:async({from:e,data:u})=>{const o=y(e),c=(Array.isArray(u)?u:[u]).map(i=>{const d={id:i.id||++g,...i};return o.push(d),d});p(e);for(const i of c)b(e,"INSERT",i,null);return{data:Array.isArray(u)?c:c[0],error:null}},update:async({from:e,data:u,params:o})=>{const f=y(e),c=[];for(let i=0;i<f.length;i++)if(A(f[i],o)){const d={...f[i]};Object.assign(f[i],u),c.push(f[i]),b(e,"UPDATE",f[i],d)}return p(e),{data:c,error:null}},delete:async({from:e,params:u})=>{const o=y(e),f=[];for(let c=o.length-1;c>=0;c--)A(o[c],u)&&(f.push(o[c]),o.splice(c,1),b(e,"DELETE",null,f[f.length-1]));return p(e),{data:f,error:null}},subscribe:({from:e,on:u},o)=>{s[e]||(s[e]=[]);const f=(c,i,d)=>{u&&u!=="*"&&u!==d.event||o(c,i,d)};return s[e].push(f),()=>{const c=s[e].indexOf(f);c>-1&&s[e].splice(c,1)}}}};
1
+ "use strict";var S=Object.defineProperty;var v=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var O=Object.prototype.hasOwnProperty;var w=(s,l)=>{for(var t in l)S(s,t,{get:l[t],enumerable:!0})},N=(s,l,t,r)=>{if(l&&typeof l=="object"||typeof l=="function")for(let u of I(l))!O.call(s,u)&&u!==t&&S(s,u,{get:()=>l[u],enumerable:!(r=v(l,u))||r.enumerable});return s};var P=s=>N(S({},"__esModule",{value:!0}),s);var D={};w(D,{localAdapter:()=>T,setup:()=>k});module.exports=P(D);const x=(s,l)=>{if(!l)return!0;for(const t in l){const r=l[t];if(r===null){if(s[t]!==null&&s[t]!==void 0)return!1}else if(Array.isArray(r)){if(!r.includes(s[t]))return!1}else if(typeof r=="object"){for(const u in r)if(u==="gt"&&!(s[t]>r[u])||u==="gte"&&!(s[t]>=r[u])||u==="lt"&&!(s[t]<r[u])||u==="lte"&&!(s[t]<=r[u])||u==="neq"&&s[t]===r[u]||u==="like"&&!String(s[t]).includes(r[u]))return!1}else if(s[t]!==r)return!1}return!0},j=(s,{order:l,limit:t,offset:r,single:u})=>{let d=[...s];if(l){const y=typeof l=="string"?l:l.by,b=l.asc!==!1;d.sort((p,h)=>p[y]<h[y]?b?-1:1:p[y]>h[y]?b?1:-1:0)}return r&&(d=d.slice(r)),t&&(d=d.slice(0,t)),u?{data:d[0]||null,error:null}:{data:d,error:null}},k=async({data:s,...l})=>T(s,l),T=(s={},l={})=>{const t={},r={},u=l.persist!==!1&&typeof localStorage<"u",d=l.prefix||"smbls_db_",y=e=>{if(!t[e])if(u)try{const c=localStorage.getItem(d+e);t[e]=c?JSON.parse(c):s[e]||[]}catch{t[e]=s[e]||[]}else t[e]=s[e]||[];return t[e]},b=e=>{if(u)try{localStorage.setItem(d+e,JSON.stringify(t[e]))}catch{}},p=(e,c,f,n)=>{const i=r[e];if(i)for(const o of i)o(f,n,{event:c,table:e})};let h=Date.now();return{name:"local",store:t,select:async({from:e,params:c,...f})=>{const i=y(e).filter(o=>x(o,c));return j(i,f)},insert:async({from:e,data:c})=>{const f=y(e),i=(Array.isArray(c)?c:[c]).map(o=>{const a={id:o.id||++h,...o};return f.push(a),a});b(e);for(const o of i)p(e,"INSERT",o,null);return{data:Array.isArray(c)?i:i[0],error:null}},update:async({from:e,data:c,params:f})=>{const n=y(e),i=[];for(let o=0;o<n.length;o++)if(x(n[o],f)){const a={...n[o]};Object.assign(n[o],c),i.push(n[o]),p(e,"UPDATE",n[o],a)}return b(e),{data:i,error:null}},upsert:async({from:e,data:c,params:f})=>{const n=y(e),i=Array.isArray(c)?c:[c],o=[];for(const a of i){const E=f||(a.id!==void 0?{id:a.id}:null),A=E?n.findIndex(g=>x(g,E)):-1;if(A>-1){const g={...n[A]};Object.assign(n[A],a),o.push(n[A]),p(e,"UPDATE",n[A],g)}else{const g={id:a.id||++h,...a};n.push(g),o.push(g),p(e,"INSERT",g,null)}}return b(e),{data:Array.isArray(c)?o:o[0],error:null}},delete:async({from:e,params:c})=>{const f=y(e),n=[];for(let i=f.length-1;i>=0;i--)x(f[i],c)&&(n.push(f[i]),f.splice(i,1),p(e,"DELETE",null,n[n.length-1]));return b(e),{data:n,error:null}},subscribe:({from:e,on:c},f)=>{r[e]||(r[e]=[]);const n=(i,o,a)=>{c&&c!=="*"&&c!==a.event||f(i,o,a)};return r[e].push(n),()=>{const i=r[e].indexOf(n);i>-1&&r[e].splice(i,1)}}}};
@@ -1 +1 @@
1
- "use strict";var O=Object.defineProperty;var T=Object.getOwnPropertyDescriptor;var k=Object.getOwnPropertyNames;var P=Object.prototype.hasOwnProperty;var J=(a,r)=>{for(var e in r)O(a,e,{get:r[e],enumerable:!0})},j=(a,r,e,c)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of k(r))!P.call(a,s)&&s!==e&&O(a,s,{get:()=>r[s],enumerable:!(c=T(r,s))||c.enumerable});return a};var A=a=>j(O({},"__esModule",{value:!0}),a);var x={};J(x,{restAdapter:()=>w,setup:()=>N});module.exports=A(x);const E=(a,r,e)=>{const c=r&&!r.startsWith("/")?"/":"",s=new URL(`${a}${c}${r}`);if(e)for(const u in e){const d=e[u];d!=null&&s.searchParams.set(u,typeof d=="object"?JSON.stringify(d):d)}return s.toString()},p=async(a,r)=>{const e=await globalThis.fetch(a,r),c=e.headers.get("content-type"),s=c&&c.includes("json")?await e.json():await e.text();return{data:s,error:e.ok?null:s?.message||s?.error||e.statusText,status:e.status}},N=async({url:a,headers:r,auth:e,fetchOptions:c})=>{if(!a)throw new Error("@symbo.ls/fetch rest: url is required");return w(a,r,e,c)},w=(a,r={},e,c={})=>{let s=e?.token||null;const u=t=>{const n={...r,...t};return s&&(n.Authorization=`Bearer ${s}`),n},d=t=>({...u(t),"Content-Type":"application/json"}),h=t=>({...c,...t}),b=(t,n)=>{const i=n||a;if(t&&(t.startsWith("http://")||t.startsWith("https://")))return t;const l=t&&!t.startsWith("/")?"/":"";return`${i}${l}${t||""}`};return{name:"rest",setToken:t=>{s=t},getSession:async()=>{if(!e?.sessionUrl)return s?{token:s}:null;const t=await p(b(e.sessionUrl,e.baseUrl),h({headers:u()}));return t.error?null:t.data},signIn:async t=>{if(!e?.signInUrl)throw new Error("rest: auth.signInUrl not configured");const n=await p(b(e.signInUrl,e.baseUrl),h({method:"POST",headers:d(),body:JSON.stringify(t)}));return n.data?.token&&(s=n.data.token),n},signOut:async()=>(e?.signOutUrl&&await p(b(e.signOutUrl,e.baseUrl),h({method:"POST",headers:u()})),s=null,{error:null}),select:({from:t,params:n,select:i,limit:l,offset:U,order:o,single:$,headers:g,baseUrl:S})=>{const y={...n};return i&&(y.select=i),l&&(y.limit=l),U&&(y.offset=U),$&&(y.single=!0),o&&(typeof o=="string"?y.order=o:Array.isArray(o)?y.order=o.map(f=>`${f.by}:${f.asc===!1?"desc":"asc"}`).join(","):o.by&&(y.order=`${o.by}:${o.asc===!1?"desc":"asc"}`)),p(E(S||a,t||"",y),h({headers:u(g)}))},rpc:({from:t,params:n,headers:i,baseUrl:l})=>p(b(`rpc/${t}`,l),h({method:"POST",headers:d(i),body:JSON.stringify(n)})),insert:({from:t,data:n,headers:i,baseUrl:l})=>p(b(t,l),h({method:"POST",headers:d(i),body:JSON.stringify(n)})),update:({from:t,data:n,params:i,method:l,headers:U,baseUrl:o})=>{const $=i?.id||n?.id,g=$?`${t}/${$}`:t;return p(b(g,o),h({method:l||"PATCH",headers:d(U),body:JSON.stringify(n)}))},delete:({from:t,params:n,headers:i,baseUrl:l})=>{const U=n?.id,o=U?`${t}/${U}`:t;return p(b(o,l),h({method:"DELETE",headers:u(i)}))}}};
1
+ "use strict";var O=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var P=Object.getOwnPropertyNames;var k=Object.prototype.hasOwnProperty;var J=(a,n)=>{for(var e in n)O(a,e,{get:n[e],enumerable:!0})},N=(a,n,e,c)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of P(n))!k.call(a,r)&&r!==e&&O(a,r,{get:()=>n[r],enumerable:!(c=w(n,r))||c.enumerable});return a};var j=a=>N(O({},"__esModule",{value:!0}),a);var x={};J(x,{restAdapter:()=>S,setup:()=>E});module.exports=j(x);const A=(a,n,e)=>{const c=n&&!n.startsWith("/")?"/":"",r=new URL(`${a}${c}${n}`);if(e)for(const p in e){const d=e[p];d!=null&&r.searchParams.set(p,typeof d=="object"?JSON.stringify(d):d)}return r.toString()},y=async(a,n)=>{const e=await globalThis.fetch(a,n),c=e.headers.get("content-type"),r=c&&c.includes("json")?await e.json():await e.text();return{data:r,error:e.ok?null:r?.message||r?.error||e.statusText,status:e.status}},E=async({url:a,headers:n,auth:e,fetchOptions:c})=>{if(!a)throw new Error("@symbo.ls/fetch rest: url is required");return S(a,n,e,c)},S=(a,n={},e,c={})=>{let r=e?.token||null;const p=t=>{const s={...n,...t};return r&&(s.Authorization=`Bearer ${r}`),s},d=t=>({...p(t),"Content-Type":"application/json"}),u=t=>({...c,...t}),b=(t,s)=>{const i=s||a;if(t&&(t.startsWith("http://")||t.startsWith("https://")))return t;const o=t&&!t.startsWith("/")?"/":"";return`${i}${o}${t||""}`};return{name:"rest",setToken:t=>{r=t},getSession:async()=>{if(!e?.sessionUrl)return r?{token:r}:null;const t=await y(b(e.sessionUrl,e.baseUrl),u({headers:p()}));return t.error?null:t.data},signIn:async t=>{if(!e?.signInUrl)throw new Error("rest: auth.signInUrl not configured");const s=await y(b(e.signInUrl,e.baseUrl),u({method:"POST",headers:d(),body:JSON.stringify(t)}));return s.data?.token&&(r=s.data.token),s},signOut:async()=>(e?.signOutUrl&&await y(b(e.signOutUrl,e.baseUrl),u({method:"POST",headers:p()})),r=null,{error:null}),select:({from:t,params:s,select:i,limit:o,offset:h,order:l,single:$,headers:g,baseUrl:T})=>{const U={...s};return i&&(U.select=i),o&&(U.limit=o),h&&(U.offset=h),$&&(U.single=!0),l&&(typeof l=="string"?U.order=l:Array.isArray(l)?U.order=l.map(f=>`${f.by}:${f.asc===!1?"desc":"asc"}`).join(","):l.by&&(U.order=`${l.by}:${l.asc===!1?"desc":"asc"}`)),y(A(T||a,t||"",U),u({headers:p(g)}))},rpc:({from:t,params:s,headers:i,baseUrl:o})=>y(b(`rpc/${t}`,o),u({method:"POST",headers:d(i),body:JSON.stringify(s)})),insert:({from:t,data:s,headers:i,baseUrl:o})=>y(b(t,o),u({method:"POST",headers:d(i),body:JSON.stringify(s)})),update:({from:t,data:s,params:i,method:o,headers:h,baseUrl:l})=>{const $=i?.id||s?.id,g=$?`${t}/${$}`:t;return y(b(g,l),u({method:o||"PATCH",headers:d(h),body:JSON.stringify(s)}))},upsert:({from:t,data:s,params:i,method:o,headers:h,baseUrl:l})=>{const $=i?.id||s?.id,g=$?`${t}/${$}`:t;return y(b(g,l),u({method:o||"PUT",headers:d(h),body:JSON.stringify(s)}))},delete:({from:t,params:s,headers:i,baseUrl:o})=>{const h=s?.id,l=h?`${t}/${h}`:t;return y(b(l,o),u({method:"DELETE",headers:p(i)}))}}};
package/dist/cjs/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var Nt=Object.create;var rt=Object.defineProperty;var It=Object.getOwnPropertyDescriptor;var Ot=Object.getOwnPropertyNames;var qt=Object.getPrototypeOf,Ut=Object.prototype.hasOwnProperty;var zt=(e,t)=>{for(var s in t)rt(e,s,{get:t[s],enumerable:!0})},Tt=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ot(t))!Ut.call(e,o)&&o!==s&&rt(e,o,{get:()=>t[o],enumerable:!(r=It(t,o))||r.enumerable});return e};var pt=(e,t,s)=>(s=e!=null?Nt(qt(e)):{},Tt(t||!e||!e.__esModule?rt(s,"default",{value:e,enumerable:!0}):s,e)),jt=e=>Tt(rt({},"__esModule",{value:!0}),e);var oe={};zt(oe,{createAdapter:()=>Yt,default:()=>ae,executeFetch:()=>wt,fetchPlugin:()=>re,initAdapterAuth:()=>ct,parseDuration:()=>W,queryClient:()=>se,registerAdapter:()=>Kt,resolveDb:()=>kt});module.exports=jt(oe);var f=require("@symbo.ls/utils");const Gt=["select","rpc","insert","update","delete","subscribe"],Qt={supabase:()=>import("./adapters/supabase.js"),rest:()=>import("./adapters/rest.js"),local:()=>import("./adapters/local.js")},at=Symbol.for("@symbo.ls/fetch:CUSTOM_ADAPTERS"),Y=(()=>{if(typeof globalThis<"u"&&globalThis[at])return globalThis[at];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,at,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[at]}catch{}return e})(),ot=Symbol.for("@symbo.ls/fetch:PENDING_ADAPTERS"),_t=(()=>{if(typeof globalThis<"u"&&globalThis[ot])return globalThis[ot];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,ot,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[ot]}catch{}return e})(),xt=5e3,Wt=e=>new Promise(t=>{if(Y[e])return t(Y[e]);const s=_t[e]||=[];let r=!1;const o=()=>{r||(r=!0,t(Y[e]||null))};s.push(o),setTimeout(o,xt)}),Kt=(e,t)=>{Y[e]=t;const s=_t[e];if(s&&s.length){_t[e]=[];for(const r of s)r()}},Yt=e=>{const t={};for(const s of Gt)e[s]&&(t[s]=e[s]);return t.name=e.name||"custom",t},kt=async e=>{if(!e)return null;if(typeof e.select=="function")return e;const{adapter:t,...s}=e,r=typeof t=="string"?t:typeof e=="string"?e:null;if(!r)return null;let o=Y[r]||Qt[r];if(!o&&(o=await Wt(r),!o))throw new Error(`Unknown db adapter: "${r}". Use registerAdapter() for adapters with optional deps.`);const a=await o();if(!a||typeof a.setup!="function")throw new Error(`Adapter "${r}" loader returned a module without a setup() function. Got keys: [${Object.keys(a||{}).join(", ")}]. Likely a bundler chunk-resolution issue.`);return a.setup(s)},W=e=>{if(!e)return 0;if(typeof e=="number")return e;const t=e.match(/^(\d+)(ms|s|m|h)$/);if(!t)return 0;const s=parseInt(t[1]),r=t[2];return r==="ms"?s:r==="s"?s*1e3:r==="m"?s*6e4:r==="h"?s*36e5:0},w=new Map,U=new Map,B=new Map,Bt=(e,t,s)=>{if(e.cache?.key)return e.cache.key;const r=Vt(e.params),o=s?`${s}:`:"",a=`${e.from}:${e.method}:${o}${JSON.stringify(r||"")}`;return t?`${a}:${t}`:a},J=e=>w.get(e)||null,H=(e,t,s)=>{const r=w.get(e),o={data:t,error:s,time:Date.now(),stale:!1};w.set(e,o);const a=U.get(e);if(a)for(const i of a)(0,f.isFunction)(i)&&i(o);return o},V=e=>{if(e){const t=w.get(e);t&&(t.stale=!0);return}for(const[,t]of w)t.stale=!0},it=e=>{if(e){w.delete(e);return}w.clear()},ce=(e,t)=>(U.has(e)||U.set(e,new Set),U.get(e).add(t),()=>{const s=U.get(e);s&&(s.delete(t),s.size===0&&U.delete(e))}),yt=e=>!e&&e!==!1?{staleTime:6e4,gcTime:3e5}:e===!1?null:e===!0?{staleTime:6e4,gcTime:3e5}:typeof e=="number"?{staleTime:e,gcTime:Math.max(e*5,3e5)}:(0,f.isString)(e)?{staleTime:W(e),gcTime:3e5}:{staleTime:W(e.stale||e.staleTime)||6e4,gcTime:W(e.gc||e.gcTime||e.expire)||3e5,key:e.key};let nt=null;const Jt=()=>{nt||(nt=setInterval(()=>{const e=Date.now();for(const[t,s]of w){const r=U.get(t);!(r&&r.size>0)&&e-s.time>(s.gcTime||3e5)&&w.delete(t)}},3e4),nt.unref&&nt.unref())};Jt();const Ft=3,gt=e=>Math.min(1e3*2**e,3e4),Et=e=>{const t=e.retry;return t===!1?{count:0}:t===!0||t===void 0?{count:Ft,delay:gt}:typeof t=="number"?{count:t,delay:gt}:{count:t.count??Ft,delay:(0,f.isFunction)(t.delay)?t.delay:typeof t.delay=="number"?()=>t.delay:gt}},Dt=async(e,t)=>{const{count:s,delay:r}=t;let o;for(let a=0;a<=s;a++)try{const i=await e();if(i?.error){if(o=i.error,a<s){const c=(0,f.isFunction)(r)?r(a,o):r;await new Promise(n=>setTimeout(n,c));continue}return i}return i}catch(i){if(o=i,a<s){const c=(0,f.isFunction)(r)?r(a,i):r;await new Promise(n=>setTimeout(n,c));continue}throw i}return{data:null,error:o}},x={focus:new Set,online:new Set};let Ct=!1;const Ht=()=>{if(Ct||typeof window>"u")return;Ct=!0;const e=()=>{for(const s of x.focus)s()},t=()=>{for(const s of x.online)s()};window.addEventListener("visibilitychange",()=>{document.visibilityState==="visible"&&e()}),window.addEventListener("focus",e),window.addEventListener("online",t)},Pt=(e,t)=>{const s=t.__ref;if(e===!0)return{from:s.__state||t.key,method:"select",query:!0,on:"create"};if((0,f.isString)(e))return{from:e,method:"select",query:!0,on:"create"};if((0,f.isObject)(e)){const r={...e};return r.from=r.from||s.__state||t.key,r.method=r.method||"select",r.on=r.on||"create",r.query===void 0&&(r.query=!0),r}return null},Vt=e=>(!e||(0,f.isFunction)(e),e),ft=(e,t)=>{const s=e?.state?.root||t?.state?.root;if(s?.lang)return s.lang},At=(e,t)=>{const r=(e?.state?.root||t?.state?.root)?.__fetchScope;return r?String(r):void 0},Lt=(e,t,s)=>{let r;if(!e)r=void 0;else if((0,f.isFunction)(e))r=e(t,t.state);else{r={};for(const o in e){const a=e[o];r[o]=(0,f.isFunction)(a)?a(t,t.state):a}}return r},ct=async(e,t)=>{if(!e||e.__authInitialized||(e.__authInitialized=!0,!e.getSession))return;const s=(r,o)=>{const a=t.state?.root;a?.update&&a.update({auth:{user:r,session:o,loading:!1}})};try{const r=await e.getSession();s(r?.user||null,r)}catch(r){console.warn("[fetch] Failed to restore auth session:",r.message)}e.onAuthStateChange&&e.onAuthStateChange((r,o)=>{s(o?.user||null,o)})},St=async(e,t)=>{if((0,f.isFunction)(e.select))return e.auth!==!1&&await ct(e,t),e;if(e.__resolved)return e.auth!==!1&&await ct(e.__resolved,t),e.__resolved;if(e.__resolving)return e.__resolving;e.__resolving=kt(e);const s=await e.__resolving;return delete e.__resolving,s?(s.auth=e.auth,s.url=e.url,s.key=e.key,e.__resolved=s,t.fetch=s,e.auth!==!1&&await ct(s,t),s):null},T=(e,t,...s)=>{const r=(0,f.isFunction)(e[t])?e[t]:null;r&&r.call(e,...s,e,e.state,e.context)},C=(e,t,s,...r)=>{const o=e&&(0,f.isFunction)(e[s])?e[s]:null;o&&o.call(t,...r,t,t.state,t.context)},X=e=>{const t={},s=e.node;if(!s)return t;if(s.tagName==="FORM"){const o=new FormData(s);for(const[a,i]of o.entries())t[a]=i;return t}const r=s.querySelectorAll("input, textarea, select");for(let o=0;o<r.length;o++){const a=r[o],i=a.name||a.getAttribute("name");i&&(a.type==="checkbox"?t[i]=a.checked:a.type==="file"?t[i]=a.files:t[i]=a.value)}return t},z=(e,t,s,r={})=>{const o=s&&s.startsWith("~/"),a=o&&e.state?.root||e.state;if(a?.update)if(o){const i=s.slice(2);t&&typeof t=="object"&&!Array.isArray(t)&&i in t?a.update(t,{preventFetch:!0,...r}):a.update({[i]:t},{preventFetch:!0,...r})}else{const i=s?{[s]:t}:t;a.update(i,{preventFetch:!0,...r})}},k=(e,t)=>{const s=e?.__ref;s&&(s.__fetchStatus=t,s.__fetching=t.isFetching,s.__fetchError=t.error)},I=async(e,t,s,r={})=>{const o=s?.fetch;if(!o||!t||!t.__ref||e.enabled===!1||(0,f.isFunction)(e.enabled)&&!e.enabled(t,t.state)||e.skip===!0||(0,f.isFunction)(e.skip)&&e.skip(t,t.state))return;const a=await St(o,s);if(!a||!t.__ref)return;const i=t.__ref,{from:c,method:n,query:_,params:m,cache:Z,transform:F,single:ut,auth:tt,fields:R,as:O,limit:K,offset:et,order:j,headers:N,baseUrl:y,page:g,cursor:v,getNextPageParam:G,getPreviousPageParam:S,infinite:P,placeholderData:q,initialData:Q,select:A,keepPreviousData:$}=e;let E=(0,f.isString)(A)?A:void 0;if(_&&(0,f.isFunction)(t.getQuery)){const d=t.getQuery(a.name||"paths");d&&(E=d.select||d.length&&d.join(",")||E)}const L=Lt(m,t,s),D=yt(Z),st=Et(e),lt=ft(t,s),mt=At(t,s),$t=lt?`:${lt}`:"",Mt=mt?`${mt}:`:"",p=D?Z?.key||`${c}:${n}:${Mt}${JSON.stringify(L||"")}${P?":infinite":""}${g?":p"+JSON.stringify(g):""}${$t}`:null;if(!i.__fetchInitialized&&p&&(i.__fetchInitialized=!0,Q!==void 0)){const d=(0,f.isFunction)(Q)?Q():Q;H(p,d,null)}if((n==="select"||n==="rpc")&&p&&D){const d=J(p);if(d&&!d.error){const l=Date.now()-d.time,h=d.stale||l>(D.staleTime||0);let u=d.data;if((0,f.isFunction)(A)&&(u=A(u,t,t.state)),F&&(u=F(u,t,t.state)),z(t,u,O),!h){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"});return}}else if(q!==void 0){const l=(0,f.isFunction)(q)?q(t,t.state):q;l!==void 0&&z(t,l,O)}}if(p&&B.has(p)&&!r.force){const d=B.get(p);try{const l=await Promise.race([d,new Promise((h,u)=>setTimeout(()=>u("DEDUP_TIMEOUT"),15e3))]);if(l?.data!==void 0){let h=l.data;(0,f.isFunction)(A)&&(h=A(h,t,t.state)),F&&(h=F(h,t,t.state)),z(t,h,O),T(t,"onFetchComplete",h),C(e,t,"onFetchComplete",h)}}catch{}return}const Rt=!J(p)?.data;k(t,{isFetching:!0,isLoading:Rt,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const M=s?.analyze?.emitNetwork,dt=M?typeof performance<"u"?performance.now():Date.now():0;M&&M({phase:"start",mode:"query",from:c,method:n,cacheKey:p,lang:lt});const vt=Dt(async()=>{if(tt!==!1&&a.auth!==!1&&a.getSession){const u=await a.getSession();if(tt===!0&&!u){const b={message:"Not authenticated"};return k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:b,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",b),C(e,t,"onFetchError",b),{data:null,error:b}}}const d=a[n];if(!(0,f.isFunction)(d))return{data:null,error:{message:`Method "${n}" not found on adapter`}};const l={from:c,select:E,params:L,single:ut,limit:K,offset:et,order:j,headers:N,baseUrl:y},h=ft(t,s);if(h&&(l.headers={...l.headers,"Accept-Language":h}),g!==void 0){if((0,f.isObject)(g))g.offset!==void 0&&(l.offset=g.offset),g.limit!==void 0&&(l.limit=g.limit),g.cursor!==void 0&&(l.params={...l.params,cursor:g.cursor});else if(typeof g=="number"){const u=e.pageSize||K||20;l.offset=(g-1)*u,l.limit=u}}if(v!==void 0&&(l.params={...l.params,cursor:v}),n==="insert"||n==="update"||n==="upsert"){let u;if(R===!0||e.on==="submit")u=X(t);else if((0,f.isArray)(R)){const b=X(t);u={};for(const ht of R)u[ht]=b[ht]}else t.state?.parse?u=t.state.parse():(0,f.isObject)(t.state)&&(u={...t.state});F&&(u=F(u,t,t.state)),l.data=u}if(n==="rpc"){let u=L;if(e.on==="submit"){const b=X(t);u=e.transformParams?e.transformParams(b,t,t.state):b}l.params=u}if(n==="subscribe"){const u=a.subscribe({from:c,params:L,on:e.subscribeOn},(b,ht,ne)=>{z(t,b,O),T(t,"onFetchComplete",b),C(e,t,"onFetchComplete",b)});return i.__unsubscribe=u,{data:null,error:null,subscribed:!0}}return d(l)},st);p&&B.set(p,vt);try{const d=await vt;if(d?.subscribed)return;const{data:l,error:h}=d||{};if(h){p&&H(p,null,h),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:h,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",h),C(e,t,"onFetchError",h),M&&M({phase:"error",mode:"query",from:c,method:n,cacheKey:p,error:h.message||h,durationMs:(typeof performance<"u"?performance.now():Date.now())-dt});return}if(l!==void 0){if((n==="select"||n==="rpc")&&p){const b=H(p,l,null);b.gcTime=D?.gcTime||3e5}let u=l;P&&(0,f.isArray)(l)&&(i.__pages||(i.__pages=[]),r.direction==="previous"?i.__pages.unshift(l):i.__pages.push(l),u=i.__pages.flat(),G&&(i.__nextPageParam=G(l,i.__pages),i.__hasNextPage=i.__nextPageParam!=null),S&&(i.__prevPageParam=S(l,i.__pages),i.__hasPreviousPage=i.__prevPageParam!=null)),(0,f.isFunction)(A)&&(u=A(u,t,t.state)),F&&(u=F(u,t,t.state)),(!$||u!==void 0)&&z(t,u,O),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",u),C(e,t,"onFetchComplete",u),M&&M({phase:"success",mode:"query",ok:!0,from:c,method:n,cacheKey:p,durationMs:(typeof performance<"u"?performance.now():Date.now())-dt})}}catch(d){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:d,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",d),C(e,t,"onFetchError",d),M&&M({phase:"error",mode:"query",from:c,method:n,cacheKey:p,error:d.message||String(d),durationMs:(typeof performance<"u"?performance.now():Date.now())-dt})}finally{p&&B.delete(p)}},Xt=(e,t,s)=>{const r=e.on,o=t.__ref;o.__fetchListeners||(o.__fetchListeners=[]);const a=e.__runner||I;if(r==="submit"){const i=n=>{n.preventDefault(),a(e,t,s)},c=t.node;c&&(c.addEventListener("submit",i),o.__fetchListeners.push(()=>c.removeEventListener("submit",i)))}else if(r==="click"){const i=()=>a(e,t,s),c=t.node;c&&(c.addEventListener("click",i),o.__fetchListeners.push(()=>c.removeEventListener("click",i)))}else r==="stateChange"&&(o.__fetchOnStateChange=()=>a(e,t,s))},Zt=(e,t,s)=>{const r=t.__ref;r.__fetchCleanups||(r.__fetchCleanups=[]),Ht();const o=e.refetchInterval;if(o){const a=typeof o=="number"?o:W(o);if(a>0){const i=setInterval(()=>{(e.refetchIntervalInBackground||typeof document>"u"||document.visibilityState==="visible")&&I(e,t,s,{force:!0})},a);r.__fetchCleanups.push(()=>clearInterval(i))}}if(e.refetchOnWindowFocus!==!1){const a=()=>{const i=ft(t,s),c=At(t,s),n=Bt(e,i,c),_=J(n),m=yt(e.cache);(!_||_.stale||Date.now()-_.time>(m?.staleTime||0))&&I(e,t,s,{force:!0})};x.focus.add(a),r.__fetchCleanups.push(()=>x.focus.delete(a))}if(e.refetchOnReconnect!==!1){const a=()=>I(e,t,s,{force:!0});x.online.add(a),r.__fetchCleanups.push(()=>x.online.delete(a))}},te=(e,t,s)=>{if(!t.optimistic)return null;const r=e.__ref,o=t.as,a=e.state?.parse?e.state.parse():(0,f.isObject)(e.state)?{...e.state}:e.state;r.__optimisticSnapshot=a;const i=(0,f.isFunction)(t.optimistic)?t.optimistic(s,a,e):t.optimistic;return i!==void 0&&z(e,i,o),a},bt=(e,t)=>{const s=e.__ref,r=s.__optimisticSnapshot;r!==void 0&&(z(e,r,t.as),delete s.__optimisticSnapshot)},ee=async(e,t,s)=>{const r=s?.fetch;if(!r)return;const o=await St(r,s);if(!o)return;const a=t.__ref,{method:i,from:c,fields:n,transform:_,as:m,on:Z,auth:F,headers:ut,baseUrl:tt,invalidates:R,optimistic:O,onMutate:K,onSuccess:et,onError:j,onSettled:N}=e;let y;if(n===!0||Z==="submit")y=X(t);else if((0,f.isArray)(n)){const S=X(t);y={};for(const P of n)y[P]=S[P]}else t.state?.parse?y=t.state.parse():(0,f.isObject)(t.state)&&(y={...t.state});_&&(y=_(y,t,t.state)),(0,f.isFunction)(K)&&K(y,t,t.state);const g=O?te(t,e,y):null;a.__fetching=!0,k(t,{isFetching:!0,isLoading:!1,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const v=s?.analyze?.emitNetwork,G=v?typeof performance<"u"?performance.now():Date.now():0;v&&v({phase:"start",mode:"mutation",from:c,method:i});try{if(F!==!1&&o.auth!==!1&&o.getSession){const L=await o.getSession();if(F===!0&&!L){g!==void 0&&bt(t,e);const D={message:"Not authenticated"};T(t,"onFetchError",D),C(e,t,"onFetchError",D);return}}const S=o[i];if(!(0,f.isFunction)(S))return;const P={from:c,data:y,headers:ut,baseUrl:tt};e.params&&(P.params=Lt(e.params,t,s));const q=ft(t,s);q&&(P.headers={...P.headers,"Accept-Language":q});const Q=Et(e),A=await Dt(()=>S(P),Q),{data:$,error:E}=A||{};if(E){g!==void 0&&bt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:E,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",E),(0,f.isFunction)(j)&&j(E,y,t),(0,f.isFunction)(N)&&N(null,E,y,t),v&&v({phase:"error",mode:"mutation",from:c,method:i,error:E.message||String(E),durationMs:(typeof performance<"u"?performance.now():Date.now())-G});return}if(delete a.__optimisticSnapshot,$!==void 0){const L=m?{[m]:$}:$;t.state?.update&&t.state.update(L,{preventFetch:!0})}if(k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",$),C(e,t,"onFetchComplete",$),(0,f.isFunction)(et)&&et($,y,t),(0,f.isFunction)(N)&&N($,null,y,t),v&&v({phase:"success",mode:"mutation",ok:!0,from:c,method:i,durationMs:(typeof performance<"u"?performance.now():Date.now())-G}),R){const L=(0,f.isArray)(R)?R:[R];for(const D of L)if(D===!0||D==="*")for(const[st]of w)st.startsWith(c+":")&&V(st);else V(D)}}catch(S){g!==void 0&&bt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:S,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",S),C(e,t,"onFetchError",S),(0,f.isFunction)(j)&&j(S,y,t),(0,f.isFunction)(N)&&N(null,S,y,t),v&&v({phase:"error",mode:"mutation",from:c,method:i,error:S.message||String(S),durationMs:(typeof performance<"u"?performance.now():Date.now())-G})}finally{a.__fetching=!1}},wt=(e,t,s,r)=>{if(!e||!r?.fetch)return;const a=(0,f.exec)(e,t);if(!a)return;const i=(0,f.isArray)(a)?a.map(n=>Pt((0,f.exec)(n,t),t)).filter(Boolean):[Pt(a,t)].filter(Boolean);for(const n of i){const _=n.method==="insert"||n.method==="update"||n.method==="upsert"||n.method==="delete",m=_?ee:I;n.on==="create"?m(n,t,r):Promise.resolve().then(()=>{Xt({...n,__runner:m},t,r)}),!_&&n.on==="create"&&(n.refetchInterval||n.refetchOnWindowFocus!==!1||n.refetchOnReconnect!==!1)&&Zt(n,t,r)}const c=t.__ref;c.refetch=n=>{for(const _ of i)I(_,t,r,{force:!0,...n})},c.fetchNextPage=()=>{const n=i[0];if(!n||!n.infinite)return;const _=c.__nextPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"next",force:!0})},c.fetchPreviousPage=()=>{const n=i[0];if(!n||!n.infinite)return;const _=c.__prevPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"previous",force:!0})}},se={invalidateQueries:e=>{if(!e){V();return}if((0,f.isString)(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&V(t);else if((0,f.isArray)(e)){const t=e.join(":");for(const[s]of w)s.includes(t)&&V(s)}},removeQueries:e=>{if(!e){it();return}if((0,f.isString)(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&it(t)},getQueryData:e=>J(e)?.data??void 0,setQueryData:(e,t)=>{const s=J(e),r=(0,f.isFunction)(t)?t(s?.data):t;H(e,r,null)},prefetchQuery:async(e,t)=>{const s=t?.fetch;if(!s)return;const r=await St(s,t);if(!r)return;const o=e.cache?.key||`${e.from}:${e.method||"select"}:${JSON.stringify(e.params||"")}`,a=r[e.method||"select"];if(!(0,f.isFunction)(a))return;const i=await a({from:e.from,params:e.params,limit:e.limit,offset:e.offset,order:e.order,single:e.single});if(i?.data!==void 0&&!i?.error){const c=H(o,i.data,null);c.gcTime=yt(e.cache)?.gcTime||3e5}return i},getCache:()=>w,clear:()=>it(),dropNonLang:e=>{if(!e)return;const t=`:${e}`;for(const[s]of w)s.match(/:([a-z]{2,3})$/i)&&(s.endsWith(t)||it(s))}},re={name:"fetch",create(e){const t=e.fetch;t&&(e.parent||B.clear(),wt(t,e,e.state,e.context))}};var ae=wt;
1
+ "use strict";var Nt=Object.create;var rt=Object.defineProperty;var It=Object.getOwnPropertyDescriptor;var Ot=Object.getOwnPropertyNames;var qt=Object.getPrototypeOf,Ut=Object.prototype.hasOwnProperty;var zt=(e,t)=>{for(var s in t)rt(e,s,{get:t[s],enumerable:!0})},Tt=(e,t,s,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of Ot(t))!Ut.call(e,o)&&o!==s&&rt(e,o,{get:()=>t[o],enumerable:!(r=It(t,o))||r.enumerable});return e};var pt=(e,t,s)=>(s=e!=null?Nt(qt(e)):{},Tt(t||!e||!e.__esModule?rt(s,"default",{value:e,enumerable:!0}):s,e)),jt=e=>Tt(rt({},"__esModule",{value:!0}),e);var oe={};zt(oe,{createAdapter:()=>Yt,default:()=>ae,executeFetch:()=>wt,fetchPlugin:()=>re,initAdapterAuth:()=>ct,parseDuration:()=>W,queryClient:()=>se,registerAdapter:()=>Kt,resolveDb:()=>kt});module.exports=jt(oe);var f=require("@symbo.ls/utils");const Gt=["select","rpc","insert","update","upsert","delete","subscribe"],Qt={supabase:()=>import("./adapters/supabase.js"),rest:()=>import("./adapters/rest.js"),local:()=>import("./adapters/local.js")},at=Symbol.for("@symbo.ls/fetch:CUSTOM_ADAPTERS"),Y=(()=>{if(typeof globalThis<"u"&&globalThis[at])return globalThis[at];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,at,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[at]}catch{}return e})(),ot=Symbol.for("@symbo.ls/fetch:PENDING_ADAPTERS"),_t=(()=>{if(typeof globalThis<"u"&&globalThis[ot])return globalThis[ot];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,ot,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[ot]}catch{}return e})(),xt=5e3,Wt=e=>new Promise(t=>{if(Y[e])return t(Y[e]);const s=_t[e]||=[];let r=!1;const o=()=>{r||(r=!0,t(Y[e]||null))};s.push(o),setTimeout(o,xt)}),Kt=(e,t)=>{Y[e]=t;const s=_t[e];if(s&&s.length){_t[e]=[];for(const r of s)r()}},Yt=e=>{const t={};for(const s of Gt)e[s]&&(t[s]=e[s]);return t.name=e.name||"custom",t},kt=async e=>{if(!e)return null;if(typeof e.select=="function")return e;const{adapter:t,...s}=e,r=typeof t=="string"?t:typeof e=="string"?e:null;if(!r)return null;let o=Y[r]||Qt[r];if(!o&&(o=await Wt(r),!o))throw new Error(`Unknown db adapter: "${r}". Use registerAdapter() for adapters with optional deps.`);const a=await o();if(!a||typeof a.setup!="function")throw new Error(`Adapter "${r}" loader returned a module without a setup() function. Got keys: [${Object.keys(a||{}).join(", ")}]. Likely a bundler chunk-resolution issue.`);return a.setup(s)},W=e=>{if(!e)return 0;if(typeof e=="number")return e;const t=e.match(/^(\d+)(ms|s|m|h)$/);if(!t)return 0;const s=parseInt(t[1]),r=t[2];return r==="ms"?s:r==="s"?s*1e3:r==="m"?s*6e4:r==="h"?s*36e5:0},w=new Map,U=new Map,B=new Map,Bt=(e,t,s)=>{if(e.cache?.key)return e.cache.key;const r=Vt(e.params),o=s?`${s}:`:"",a=`${e.from}:${e.method}:${o}${JSON.stringify(r||"")}`;return t?`${a}:${t}`:a},J=e=>w.get(e)||null,H=(e,t,s)=>{const r=w.get(e),o={data:t,error:s,time:Date.now(),stale:!1};w.set(e,o);const a=U.get(e);if(a)for(const i of a)(0,f.isFunction)(i)&&i(o);return o},V=e=>{if(e){const t=w.get(e);t&&(t.stale=!0);return}for(const[,t]of w)t.stale=!0},it=e=>{if(e){w.delete(e);return}w.clear()},ce=(e,t)=>(U.has(e)||U.set(e,new Set),U.get(e).add(t),()=>{const s=U.get(e);s&&(s.delete(t),s.size===0&&U.delete(e))}),yt=e=>!e&&e!==!1?{staleTime:6e4,gcTime:3e5}:e===!1?null:e===!0?{staleTime:6e4,gcTime:3e5}:typeof e=="number"?{staleTime:e,gcTime:Math.max(e*5,3e5)}:(0,f.isString)(e)?{staleTime:W(e),gcTime:3e5}:{staleTime:W(e.stale||e.staleTime)||6e4,gcTime:W(e.gc||e.gcTime||e.expire)||3e5,key:e.key};let nt=null;const Jt=()=>{nt||(nt=setInterval(()=>{const e=Date.now();for(const[t,s]of w){const r=U.get(t);!(r&&r.size>0)&&e-s.time>(s.gcTime||3e5)&&w.delete(t)}},3e4),nt.unref&&nt.unref())};Jt();const Ft=3,gt=e=>Math.min(1e3*2**e,3e4),Et=e=>{const t=e.retry;return t===!1?{count:0}:t===!0||t===void 0?{count:Ft,delay:gt}:typeof t=="number"?{count:t,delay:gt}:{count:t.count??Ft,delay:(0,f.isFunction)(t.delay)?t.delay:typeof t.delay=="number"?()=>t.delay:gt}},Dt=async(e,t)=>{const{count:s,delay:r}=t;let o;for(let a=0;a<=s;a++)try{const i=await e();if(i?.error){if(o=i.error,a<s){const c=(0,f.isFunction)(r)?r(a,o):r;await new Promise(n=>setTimeout(n,c));continue}return i}return i}catch(i){if(o=i,a<s){const c=(0,f.isFunction)(r)?r(a,i):r;await new Promise(n=>setTimeout(n,c));continue}throw i}return{data:null,error:o}},x={focus:new Set,online:new Set};let Ct=!1;const Ht=()=>{if(Ct||typeof window>"u")return;Ct=!0;const e=()=>{for(const s of x.focus)s()},t=()=>{for(const s of x.online)s()};window.addEventListener("visibilitychange",()=>{document.visibilityState==="visible"&&e()}),window.addEventListener("focus",e),window.addEventListener("online",t)},Pt=(e,t)=>{const s=t.__ref;if(e===!0)return{from:s.__state||t.key,method:"select",query:!0,on:"create"};if((0,f.isString)(e))return{from:e,method:"select",query:!0,on:"create"};if((0,f.isObject)(e)){const r={...e};return r.from=r.from||s.__state||t.key,r.method=r.method||"select",r.on=r.on||"create",r.query===void 0&&(r.query=!0),r}return null},Vt=e=>(!e||(0,f.isFunction)(e),e),ft=(e,t)=>{const s=e?.state?.root||t?.state?.root;if(s?.lang)return s.lang},At=(e,t)=>{const r=(e?.state?.root||t?.state?.root)?.__fetchScope;return r?String(r):void 0},Lt=(e,t,s)=>{let r;if(!e)r=void 0;else if((0,f.isFunction)(e))r=e(t,t.state);else{r={};for(const o in e){const a=e[o];r[o]=(0,f.isFunction)(a)?a(t,t.state):a}}return r},ct=async(e,t)=>{if(!e||e.__authInitialized||(e.__authInitialized=!0,!e.getSession))return;const s=(r,o)=>{const a=t.state?.root;a?.update&&a.update({auth:{user:r,session:o,loading:!1}})};try{const r=await e.getSession();s(r?.user||null,r)}catch(r){console.warn("[fetch] Failed to restore auth session:",r.message)}e.onAuthStateChange&&e.onAuthStateChange((r,o)=>{s(o?.user||null,o)})},St=async(e,t)=>{if((0,f.isFunction)(e.select))return e.auth!==!1&&await ct(e,t),e;if(e.__resolved)return e.auth!==!1&&await ct(e.__resolved,t),e.__resolved;if(e.__resolving)return e.__resolving;e.__resolving=kt(e);const s=await e.__resolving;return delete e.__resolving,s?(s.auth=e.auth,s.url=e.url,s.key=e.key,e.__resolved=s,t.fetch=s,e.auth!==!1&&await ct(s,t),s):null},T=(e,t,...s)=>{const r=(0,f.isFunction)(e[t])?e[t]:null;r&&r.call(e,...s,e,e.state,e.context)},C=(e,t,s,...r)=>{const o=e&&(0,f.isFunction)(e[s])?e[s]:null;o&&o.call(t,...r,t,t.state,t.context)},X=e=>{const t={},s=e.node;if(!s)return t;if(s.tagName==="FORM"){const o=new FormData(s);for(const[a,i]of o.entries())t[a]=i;return t}const r=s.querySelectorAll("input, textarea, select");for(let o=0;o<r.length;o++){const a=r[o],i=a.name||a.getAttribute("name");i&&(a.type==="checkbox"?t[i]=a.checked:a.type==="file"?t[i]=a.files:t[i]=a.value)}return t},z=(e,t,s,r={})=>{const o=s&&s.startsWith("~/"),a=o&&e.state?.root||e.state;if(a?.update)if(o){const i=s.slice(2);t&&typeof t=="object"&&!Array.isArray(t)&&i in t?a.update(t,{preventFetch:!0,...r}):a.update({[i]:t},{preventFetch:!0,...r})}else{const i=s?{[s]:t}:t;a.update(i,{preventFetch:!0,...r})}},k=(e,t)=>{const s=e?.__ref;s&&(s.__fetchStatus=t,s.__fetching=t.isFetching,s.__fetchError=t.error)},I=async(e,t,s,r={})=>{const o=s?.fetch;if(!o||!t||!t.__ref||e.enabled===!1||(0,f.isFunction)(e.enabled)&&!e.enabled(t,t.state)||e.skip===!0||(0,f.isFunction)(e.skip)&&e.skip(t,t.state))return;const a=await St(o,s);if(!a||!t.__ref)return;const i=t.__ref,{from:c,method:n,query:_,params:m,cache:Z,transform:F,single:ut,auth:tt,fields:R,as:O,limit:K,offset:et,order:j,headers:N,baseUrl:y,page:g,cursor:v,getNextPageParam:G,getPreviousPageParam:S,infinite:P,placeholderData:q,initialData:Q,select:A,keepPreviousData:$}=e;let E=(0,f.isString)(A)?A:void 0;if(_&&(0,f.isFunction)(t.getQuery)){const d=t.getQuery(a.name||"paths");d&&(E=d.select||d.length&&d.join(",")||E)}const L=Lt(m,t,s),D=yt(Z),st=Et(e),lt=ft(t,s),mt=At(t,s),$t=lt?`:${lt}`:"",Mt=mt?`${mt}:`:"",p=D?Z?.key||`${c}:${n}:${Mt}${JSON.stringify(L||"")}${P?":infinite":""}${g?":p"+JSON.stringify(g):""}${$t}`:null;if(!i.__fetchInitialized&&p&&(i.__fetchInitialized=!0,Q!==void 0)){const d=(0,f.isFunction)(Q)?Q():Q;H(p,d,null)}if((n==="select"||n==="rpc")&&p&&D){const d=J(p);if(d&&!d.error){const l=Date.now()-d.time,h=d.stale||l>(D.staleTime||0);let u=d.data;if((0,f.isFunction)(A)&&(u=A(u,t,t.state)),F&&(u=F(u,t,t.state)),z(t,u,O),!h){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"});return}}else if(q!==void 0){const l=(0,f.isFunction)(q)?q(t,t.state):q;l!==void 0&&z(t,l,O)}}if(p&&B.has(p)&&!r.force){const d=B.get(p);try{const l=await Promise.race([d,new Promise((h,u)=>setTimeout(()=>u("DEDUP_TIMEOUT"),15e3))]);if(l?.data!==void 0){let h=l.data;(0,f.isFunction)(A)&&(h=A(h,t,t.state)),F&&(h=F(h,t,t.state)),z(t,h,O),T(t,"onFetchComplete",h),C(e,t,"onFetchComplete",h)}}catch{}return}const Rt=!J(p)?.data;k(t,{isFetching:!0,isLoading:Rt,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const M=s?.analyze?.emitNetwork,dt=M?typeof performance<"u"?performance.now():Date.now():0;M&&M({phase:"start",mode:"query",from:c,method:n,cacheKey:p,lang:lt});const vt=Dt(async()=>{if(tt!==!1&&a.auth!==!1&&a.getSession){const u=await a.getSession();if(tt===!0&&!u){const b={message:"Not authenticated"};return k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:b,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",b),C(e,t,"onFetchError",b),{data:null,error:b}}}const d=a[n];if(!(0,f.isFunction)(d))return{data:null,error:{message:`Method "${n}" not found on adapter`}};const l={from:c,select:E,params:L,single:ut,limit:K,offset:et,order:j,headers:N,baseUrl:y},h=ft(t,s);if(h&&(l.headers={...l.headers,"Accept-Language":h}),g!==void 0){if((0,f.isObject)(g))g.offset!==void 0&&(l.offset=g.offset),g.limit!==void 0&&(l.limit=g.limit),g.cursor!==void 0&&(l.params={...l.params,cursor:g.cursor});else if(typeof g=="number"){const u=e.pageSize||K||20;l.offset=(g-1)*u,l.limit=u}}if(v!==void 0&&(l.params={...l.params,cursor:v}),n==="insert"||n==="update"||n==="upsert"){let u;if(R===!0||e.on==="submit")u=X(t);else if((0,f.isArray)(R)){const b=X(t);u={};for(const ht of R)u[ht]=b[ht]}else t.state?.parse?u=t.state.parse():(0,f.isObject)(t.state)&&(u={...t.state});F&&(u=F(u,t,t.state)),l.data=u}if(n==="rpc"){let u=L;if(e.on==="submit"){const b=X(t);u=e.transformParams?e.transformParams(b,t,t.state):b}l.params=u}if(n==="subscribe"){const u=a.subscribe({from:c,params:L,on:e.subscribeOn},(b,ht,ne)=>{z(t,b,O),T(t,"onFetchComplete",b),C(e,t,"onFetchComplete",b)});return i.__unsubscribe=u,{data:null,error:null,subscribed:!0}}return d(l)},st);p&&B.set(p,vt);try{const d=await vt;if(d?.subscribed)return;const{data:l,error:h}=d||{};if(h){p&&H(p,null,h),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:h,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",h),C(e,t,"onFetchError",h),M&&M({phase:"error",mode:"query",from:c,method:n,cacheKey:p,error:h.message||h,durationMs:(typeof performance<"u"?performance.now():Date.now())-dt});return}if(l!==void 0){if((n==="select"||n==="rpc")&&p){const b=H(p,l,null);b.gcTime=D?.gcTime||3e5}let u=l;P&&(0,f.isArray)(l)&&(i.__pages||(i.__pages=[]),r.direction==="previous"?i.__pages.unshift(l):i.__pages.push(l),u=i.__pages.flat(),G&&(i.__nextPageParam=G(l,i.__pages),i.__hasNextPage=i.__nextPageParam!=null),S&&(i.__prevPageParam=S(l,i.__pages),i.__hasPreviousPage=i.__prevPageParam!=null)),(0,f.isFunction)(A)&&(u=A(u,t,t.state)),F&&(u=F(u,t,t.state)),(!$||u!==void 0)&&z(t,u,O),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",u),C(e,t,"onFetchComplete",u),M&&M({phase:"success",mode:"query",ok:!0,from:c,method:n,cacheKey:p,durationMs:(typeof performance<"u"?performance.now():Date.now())-dt})}}catch(d){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:d,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",d),C(e,t,"onFetchError",d),M&&M({phase:"error",mode:"query",from:c,method:n,cacheKey:p,error:d.message||String(d),durationMs:(typeof performance<"u"?performance.now():Date.now())-dt})}finally{p&&B.delete(p)}},Xt=(e,t,s)=>{const r=e.on,o=t.__ref;o.__fetchListeners||(o.__fetchListeners=[]);const a=e.__runner||I;if(r==="submit"){const i=n=>{n.preventDefault(),a(e,t,s)},c=t.node;c&&(c.addEventListener("submit",i),o.__fetchListeners.push(()=>c.removeEventListener("submit",i)))}else if(r==="click"){const i=()=>a(e,t,s),c=t.node;c&&(c.addEventListener("click",i),o.__fetchListeners.push(()=>c.removeEventListener("click",i)))}else r==="stateChange"&&(o.__fetchOnStateChange=()=>a(e,t,s))},Zt=(e,t,s)=>{const r=t.__ref;r.__fetchCleanups||(r.__fetchCleanups=[]),Ht();const o=e.refetchInterval;if(o){const a=typeof o=="number"?o:W(o);if(a>0){const i=setInterval(()=>{(e.refetchIntervalInBackground||typeof document>"u"||document.visibilityState==="visible")&&I(e,t,s,{force:!0})},a);r.__fetchCleanups.push(()=>clearInterval(i))}}if(e.refetchOnWindowFocus!==!1){const a=()=>{const i=ft(t,s),c=At(t,s),n=Bt(e,i,c),_=J(n),m=yt(e.cache);(!_||_.stale||Date.now()-_.time>(m?.staleTime||0))&&I(e,t,s,{force:!0})};x.focus.add(a),r.__fetchCleanups.push(()=>x.focus.delete(a))}if(e.refetchOnReconnect!==!1){const a=()=>I(e,t,s,{force:!0});x.online.add(a),r.__fetchCleanups.push(()=>x.online.delete(a))}},te=(e,t,s)=>{if(!t.optimistic)return null;const r=e.__ref,o=t.as,a=e.state?.parse?e.state.parse():(0,f.isObject)(e.state)?{...e.state}:e.state;r.__optimisticSnapshot=a;const i=(0,f.isFunction)(t.optimistic)?t.optimistic(s,a,e):t.optimistic;return i!==void 0&&z(e,i,o),a},bt=(e,t)=>{const s=e.__ref,r=s.__optimisticSnapshot;r!==void 0&&(z(e,r,t.as),delete s.__optimisticSnapshot)},ee=async(e,t,s)=>{const r=s?.fetch;if(!r)return;const o=await St(r,s);if(!o)return;const a=t.__ref,{method:i,from:c,fields:n,transform:_,as:m,on:Z,auth:F,headers:ut,baseUrl:tt,invalidates:R,optimistic:O,onMutate:K,onSuccess:et,onError:j,onSettled:N}=e;let y;if(n===!0||Z==="submit")y=X(t);else if((0,f.isArray)(n)){const S=X(t);y={};for(const P of n)y[P]=S[P]}else t.state?.parse?y=t.state.parse():(0,f.isObject)(t.state)&&(y={...t.state});_&&(y=_(y,t,t.state)),(0,f.isFunction)(K)&&K(y,t,t.state);const g=O?te(t,e,y):null;a.__fetching=!0,k(t,{isFetching:!0,isLoading:!1,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const v=s?.analyze?.emitNetwork,G=v?typeof performance<"u"?performance.now():Date.now():0;v&&v({phase:"start",mode:"mutation",from:c,method:i});try{if(F!==!1&&o.auth!==!1&&o.getSession){const L=await o.getSession();if(F===!0&&!L){g!==void 0&&bt(t,e);const D={message:"Not authenticated"};T(t,"onFetchError",D),C(e,t,"onFetchError",D);return}}const S=o[i];if(!(0,f.isFunction)(S))return;const P={from:c,data:y,headers:ut,baseUrl:tt};e.params&&(P.params=Lt(e.params,t,s));const q=ft(t,s);q&&(P.headers={...P.headers,"Accept-Language":q});const Q=Et(e),A=await Dt(()=>S(P),Q),{data:$,error:E}=A||{};if(E){g!==void 0&&bt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:E,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",E),(0,f.isFunction)(j)&&j(E,y,t),(0,f.isFunction)(N)&&N(null,E,y,t),v&&v({phase:"error",mode:"mutation",from:c,method:i,error:E.message||String(E),durationMs:(typeof performance<"u"?performance.now():Date.now())-G});return}if(delete a.__optimisticSnapshot,$!==void 0){const L=m?{[m]:$}:$;t.state?.update&&t.state.update(L,{preventFetch:!0})}if(k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",$),C(e,t,"onFetchComplete",$),(0,f.isFunction)(et)&&et($,y,t),(0,f.isFunction)(N)&&N($,null,y,t),v&&v({phase:"success",mode:"mutation",ok:!0,from:c,method:i,durationMs:(typeof performance<"u"?performance.now():Date.now())-G}),R){const L=(0,f.isArray)(R)?R:[R];for(const D of L)if(D===!0||D==="*")for(const[st]of w)st.startsWith(c+":")&&V(st);else V(D)}}catch(S){g!==void 0&&bt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:S,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",S),C(e,t,"onFetchError",S),(0,f.isFunction)(j)&&j(S,y,t),(0,f.isFunction)(N)&&N(null,S,y,t),v&&v({phase:"error",mode:"mutation",from:c,method:i,error:S.message||String(S),durationMs:(typeof performance<"u"?performance.now():Date.now())-G})}finally{a.__fetching=!1}},wt=(e,t,s,r)=>{if(!e||!r?.fetch)return;const a=(0,f.exec)(e,t);if(!a)return;const i=(0,f.isArray)(a)?a.map(n=>Pt((0,f.exec)(n,t),t)).filter(Boolean):[Pt(a,t)].filter(Boolean);for(const n of i){const _=n.method==="insert"||n.method==="update"||n.method==="upsert"||n.method==="delete",m=_?ee:I;n.on==="create"?m(n,t,r):Promise.resolve().then(()=>{Xt({...n,__runner:m},t,r)}),!_&&n.on==="create"&&(n.refetchInterval||n.refetchOnWindowFocus!==!1||n.refetchOnReconnect!==!1)&&Zt(n,t,r)}const c=t.__ref;c.refetch=n=>{for(const _ of i)I(_,t,r,{force:!0,...n})},c.fetchNextPage=()=>{const n=i[0];if(!n||!n.infinite)return;const _=c.__nextPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"next",force:!0})},c.fetchPreviousPage=()=>{const n=i[0];if(!n||!n.infinite)return;const _=c.__prevPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"previous",force:!0})}},se={invalidateQueries:e=>{if(!e){V();return}if((0,f.isString)(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&V(t);else if((0,f.isArray)(e)){const t=e.join(":");for(const[s]of w)s.includes(t)&&V(s)}},removeQueries:e=>{if(!e){it();return}if((0,f.isString)(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&it(t)},getQueryData:e=>J(e)?.data??void 0,setQueryData:(e,t)=>{const s=J(e),r=(0,f.isFunction)(t)?t(s?.data):t;H(e,r,null)},prefetchQuery:async(e,t)=>{const s=t?.fetch;if(!s)return;const r=await St(s,t);if(!r)return;const o=e.cache?.key||`${e.from}:${e.method||"select"}:${JSON.stringify(e.params||"")}`,a=r[e.method||"select"];if(!(0,f.isFunction)(a))return;const i=await a({from:e.from,params:e.params,limit:e.limit,offset:e.offset,order:e.order,single:e.single});if(i?.data!==void 0&&!i?.error){const c=H(o,i.data,null);c.gcTime=yt(e.cache)?.gcTime||3e5}return i},getCache:()=>w,clear:()=>it(),dropNonLang:e=>{if(!e)return;const t=`:${e}`;for(const[s]of w)s.match(/:([a-z]{2,3})$/i)&&(s.endsWith(t)||it(s))}},re={name:"fetch",create(e){const t=e.fetch;t&&(e.parent||B.clear(),wt(t,e,e.state,e.context))}};var ae=wt;
@@ -1 +1 @@
1
- const h=(l,i)=>{if(!i)return!0;for(const t in i){const r=i[t];if(r===null){if(l[t]!==null&&l[t]!==void 0)return!1}else if(Array.isArray(r)){if(!r.includes(l[t]))return!1}else if(typeof r=="object"){for(const f in r)if(f==="gt"&&!(l[t]>r[f])||f==="gte"&&!(l[t]>=r[f])||f==="lt"&&!(l[t]<r[f])||f==="lte"&&!(l[t]<=r[f])||f==="neq"&&l[t]===r[f]||f==="like"&&!String(l[t]).includes(r[f]))return!1}else if(l[t]!==r)return!1}return!0},A=(l,{order:i,limit:t,offset:r,single:f})=>{let a=[...l];if(i){const y=typeof i=="string"?i:i.by,p=i.asc!==!1;a.sort((b,g)=>b[y]<g[y]?p?-1:1:b[y]>g[y]?p?1:-1:0)}return r&&(a=a.slice(r)),t&&(a=a.slice(0,t)),f?{data:a[0]||null,error:null}:{data:a,error:null}},x=async({data:l,...i})=>S(l,i),S=(l={},i={})=>{const t={},r={},f=i.persist!==!1&&typeof localStorage<"u",a=i.prefix||"smbls_db_",y=e=>{if(!t[e])if(f)try{const n=localStorage.getItem(a+e);t[e]=n?JSON.parse(n):l[e]||[]}catch{t[e]=l[e]||[]}else t[e]=l[e]||[];return t[e]},p=e=>{if(f)try{localStorage.setItem(a+e,JSON.stringify(t[e]))}catch{}},b=(e,n,o,c)=>{const s=r[e];if(s)for(const u of s)u(o,c,{event:n,table:e})};let g=Date.now();return{name:"local",store:t,select:async({from:e,params:n,...o})=>{const s=y(e).filter(u=>h(u,n));return A(s,o)},insert:async({from:e,data:n})=>{const o=y(e),s=(Array.isArray(n)?n:[n]).map(u=>{const d={id:u.id||++g,...u};return o.push(d),d});p(e);for(const u of s)b(e,"INSERT",u,null);return{data:Array.isArray(n)?s:s[0],error:null}},update:async({from:e,data:n,params:o})=>{const c=y(e),s=[];for(let u=0;u<c.length;u++)if(h(c[u],o)){const d={...c[u]};Object.assign(c[u],n),s.push(c[u]),b(e,"UPDATE",c[u],d)}return p(e),{data:s,error:null}},delete:async({from:e,params:n})=>{const o=y(e),c=[];for(let s=o.length-1;s>=0;s--)h(o[s],n)&&(c.push(o[s]),o.splice(s,1),b(e,"DELETE",null,c[c.length-1]));return p(e),{data:c,error:null}},subscribe:({from:e,on:n},o)=>{r[e]||(r[e]=[]);const c=(s,u,d)=>{n&&n!=="*"&&n!==d.event||o(s,u,d)};return r[e].push(c),()=>{const s=r[e].indexOf(c);s>-1&&r[e].splice(s,1)}}}};export{S as localAdapter,x as setup};
1
+ const x=(i,f)=>{if(!f)return!0;for(const l in f){const c=f[l];if(c===null){if(i[l]!==null&&i[l]!==void 0)return!1}else if(Array.isArray(c)){if(!c.includes(i[l]))return!1}else if(typeof c=="object"){for(const u in c)if(u==="gt"&&!(i[l]>c[u])||u==="gte"&&!(i[l]>=c[u])||u==="lt"&&!(i[l]<c[u])||u==="lte"&&!(i[l]<=c[u])||u==="neq"&&i[l]===c[u]||u==="like"&&!String(i[l]).includes(c[u]))return!1}else if(i[l]!==c)return!1}return!0},E=(i,{order:f,limit:l,offset:c,single:u})=>{let d=[...i];if(f){const y=typeof f=="string"?f:f.by,b=f.asc!==!1;d.sort((p,h)=>p[y]<h[y]?b?-1:1:p[y]>h[y]?b?1:-1:0)}return c&&(d=d.slice(c)),l&&(d=d.slice(0,l)),u?{data:d[0]||null,error:null}:{data:d,error:null}},v=async({data:i,...f})=>T(i,f),T=(i={},f={})=>{const l={},c={},u=f.persist!==!1&&typeof localStorage<"u",d=f.prefix||"smbls_db_",y=e=>{if(!l[e])if(u)try{const s=localStorage.getItem(d+e);l[e]=s?JSON.parse(s):i[e]||[]}catch{l[e]=i[e]||[]}else l[e]=i[e]||[];return l[e]},b=e=>{if(u)try{localStorage.setItem(d+e,JSON.stringify(l[e]))}catch{}},p=(e,s,o,t)=>{const r=c[e];if(r)for(const n of r)n(o,t,{event:s,table:e})};let h=Date.now();return{name:"local",store:l,select:async({from:e,params:s,...o})=>{const r=y(e).filter(n=>x(n,s));return E(r,o)},insert:async({from:e,data:s})=>{const o=y(e),r=(Array.isArray(s)?s:[s]).map(n=>{const a={id:n.id||++h,...n};return o.push(a),a});b(e);for(const n of r)p(e,"INSERT",n,null);return{data:Array.isArray(s)?r:r[0],error:null}},update:async({from:e,data:s,params:o})=>{const t=y(e),r=[];for(let n=0;n<t.length;n++)if(x(t[n],o)){const a={...t[n]};Object.assign(t[n],s),r.push(t[n]),p(e,"UPDATE",t[n],a)}return b(e),{data:r,error:null}},upsert:async({from:e,data:s,params:o})=>{const t=y(e),r=Array.isArray(s)?s:[s],n=[];for(const a of r){const S=o||(a.id!==void 0?{id:a.id}:null),A=S?t.findIndex(g=>x(g,S)):-1;if(A>-1){const g={...t[A]};Object.assign(t[A],a),n.push(t[A]),p(e,"UPDATE",t[A],g)}else{const g={id:a.id||++h,...a};t.push(g),n.push(g),p(e,"INSERT",g,null)}}return b(e),{data:Array.isArray(s)?n:n[0],error:null}},delete:async({from:e,params:s})=>{const o=y(e),t=[];for(let r=o.length-1;r>=0;r--)x(o[r],s)&&(t.push(o[r]),o.splice(r,1),p(e,"DELETE",null,t[t.length-1]));return b(e),{data:t,error:null}},subscribe:({from:e,on:s},o)=>{c[e]||(c[e]=[]);const t=(r,n,a)=>{s&&s!=="*"&&s!==a.event||o(r,n,a)};return c[e].push(t),()=>{const r=c[e].indexOf(t);r>-1&&c[e].splice(r,1)}}}};export{T as localAdapter,v as setup};
@@ -1 +1 @@
1
- const w=(o,c,e)=>{const d=c&&!c.startsWith("/")?"/":"",r=new URL(`${o}${d}${c}`);if(e)for(const u in e){const l=e[u];l!=null&&r.searchParams.set(u,typeof l=="object"?JSON.stringify(l):l)}return r.toString()},p=async(o,c)=>{const e=await globalThis.fetch(o,c),d=e.headers.get("content-type"),r=d&&d.includes("json")?await e.json():await e.text();return{data:r,error:e.ok?null:r?.message||r?.error||e.statusText,status:e.status}},k=async({url:o,headers:c,auth:e,fetchOptions:d})=>{if(!o)throw new Error("@symbo.ls/fetch rest: url is required");return S(o,c,e,d)},S=(o,c={},e,d={})=>{let r=e?.token||null;const u=t=>{const s={...c,...t};return r&&(s.Authorization=`Bearer ${r}`),s},l=t=>({...u(t),"Content-Type":"application/json"}),h=t=>({...d,...t}),b=(t,s)=>{const n=s||o;if(t&&(t.startsWith("http://")||t.startsWith("https://")))return t;const a=t&&!t.startsWith("/")?"/":"";return`${n}${a}${t||""}`};return{name:"rest",setToken:t=>{r=t},getSession:async()=>{if(!e?.sessionUrl)return r?{token:r}:null;const t=await p(b(e.sessionUrl,e.baseUrl),h({headers:u()}));return t.error?null:t.data},signIn:async t=>{if(!e?.signInUrl)throw new Error("rest: auth.signInUrl not configured");const s=await p(b(e.signInUrl,e.baseUrl),h({method:"POST",headers:l(),body:JSON.stringify(t)}));return s.data?.token&&(r=s.data.token),s},signOut:async()=>(e?.signOutUrl&&await p(b(e.signOutUrl,e.baseUrl),h({method:"POST",headers:u()})),r=null,{error:null}),select:({from:t,params:s,select:n,limit:a,offset:U,order:i,single:$,headers:g,baseUrl:f})=>{const y={...s};return n&&(y.select=n),a&&(y.limit=a),U&&(y.offset=U),$&&(y.single=!0),i&&(typeof i=="string"?y.order=i:Array.isArray(i)?y.order=i.map(O=>`${O.by}:${O.asc===!1?"desc":"asc"}`).join(","):i.by&&(y.order=`${i.by}:${i.asc===!1?"desc":"asc"}`)),p(w(f||o,t||"",y),h({headers:u(g)}))},rpc:({from:t,params:s,headers:n,baseUrl:a})=>p(b(`rpc/${t}`,a),h({method:"POST",headers:l(n),body:JSON.stringify(s)})),insert:({from:t,data:s,headers:n,baseUrl:a})=>p(b(t,a),h({method:"POST",headers:l(n),body:JSON.stringify(s)})),update:({from:t,data:s,params:n,method:a,headers:U,baseUrl:i})=>{const $=n?.id||s?.id,g=$?`${t}/${$}`:t;return p(b(g,i),h({method:a||"PATCH",headers:l(U),body:JSON.stringify(s)}))},delete:({from:t,params:s,headers:n,baseUrl:a})=>{const U=s?.id,i=U?`${t}/${U}`:t;return p(b(i,a),h({method:"DELETE",headers:u(n)}))}}};export{S as restAdapter,k as setup};
1
+ const S=(l,c,e)=>{const y=c&&!c.startsWith("/")?"/":"",r=new URL(`${l}${y}${c}`);if(e)for(const p in e){const o=e[p];o!=null&&r.searchParams.set(p,typeof o=="object"?JSON.stringify(o):o)}return r.toString()},h=async(l,c)=>{const e=await globalThis.fetch(l,c),y=e.headers.get("content-type"),r=y&&y.includes("json")?await e.json():await e.text();return{data:r,error:e.ok?null:r?.message||r?.error||e.statusText,status:e.status}},P=async({url:l,headers:c,auth:e,fetchOptions:y})=>{if(!l)throw new Error("@symbo.ls/fetch rest: url is required");return T(l,c,e,y)},T=(l,c={},e,y={})=>{let r=e?.token||null;const p=t=>{const s={...c,...t};return r&&(s.Authorization=`Bearer ${r}`),s},o=t=>({...p(t),"Content-Type":"application/json"}),d=t=>({...y,...t}),b=(t,s)=>{const n=s||l;if(t&&(t.startsWith("http://")||t.startsWith("https://")))return t;const a=t&&!t.startsWith("/")?"/":"";return`${n}${a}${t||""}`};return{name:"rest",setToken:t=>{r=t},getSession:async()=>{if(!e?.sessionUrl)return r?{token:r}:null;const t=await h(b(e.sessionUrl,e.baseUrl),d({headers:p()}));return t.error?null:t.data},signIn:async t=>{if(!e?.signInUrl)throw new Error("rest: auth.signInUrl not configured");const s=await h(b(e.signInUrl,e.baseUrl),d({method:"POST",headers:o(),body:JSON.stringify(t)}));return s.data?.token&&(r=s.data.token),s},signOut:async()=>(e?.signOutUrl&&await h(b(e.signOutUrl,e.baseUrl),d({method:"POST",headers:p()})),r=null,{error:null}),select:({from:t,params:s,select:n,limit:a,offset:u,order:i,single:$,headers:g,baseUrl:f})=>{const U={...s};return n&&(U.select=n),a&&(U.limit=a),u&&(U.offset=u),$&&(U.single=!0),i&&(typeof i=="string"?U.order=i:Array.isArray(i)?U.order=i.map(O=>`${O.by}:${O.asc===!1?"desc":"asc"}`).join(","):i.by&&(U.order=`${i.by}:${i.asc===!1?"desc":"asc"}`)),h(S(f||l,t||"",U),d({headers:p(g)}))},rpc:({from:t,params:s,headers:n,baseUrl:a})=>h(b(`rpc/${t}`,a),d({method:"POST",headers:o(n),body:JSON.stringify(s)})),insert:({from:t,data:s,headers:n,baseUrl:a})=>h(b(t,a),d({method:"POST",headers:o(n),body:JSON.stringify(s)})),update:({from:t,data:s,params:n,method:a,headers:u,baseUrl:i})=>{const $=n?.id||s?.id,g=$?`${t}/${$}`:t;return h(b(g,i),d({method:a||"PATCH",headers:o(u),body:JSON.stringify(s)}))},upsert:({from:t,data:s,params:n,method:a,headers:u,baseUrl:i})=>{const $=n?.id||s?.id,g=$?`${t}/${$}`:t;return h(b(g,i),d({method:a||"PUT",headers:o(u),body:JSON.stringify(s)}))},delete:({from:t,params:s,headers:n,baseUrl:a})=>{const u=s?.id,i=u?`${t}/${u}`:t;return h(b(i,a),d({method:"DELETE",headers:p(n)}))}}};export{T as restAdapter,P as setup};
package/dist/esm/index.js CHANGED
@@ -1 +1 @@
1
- import{isArray as x,isFunction as l,isObject as Y,isString as B,exec as Tt}from"@symbo.ls/utils";const Nt=["select","rpc","insert","update","delete","subscribe"],It={supabase:()=>import("./adapters/supabase.js"),rest:()=>import("./adapters/rest.js"),local:()=>import("./adapters/local.js")},it=Symbol.for("@symbo.ls/fetch:CUSTOM_ADAPTERS"),J=(()=>{if(typeof globalThis<"u"&&globalThis[it])return globalThis[it];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,it,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[it]}catch{}return e})(),nt=Symbol.for("@symbo.ls/fetch:PENDING_ADAPTERS"),_t=(()=>{if(typeof globalThis<"u"&&globalThis[nt])return globalThis[nt];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,nt,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[nt]}catch{}return e})(),Ot=5e3,qt=e=>new Promise(t=>{if(J[e])return t(J[e]);const s=_t[e]||=[];let r=!1;const i=()=>{r||(r=!0,t(J[e]||null))};s.push(i),setTimeout(i,Ot)}),Vt=(e,t)=>{J[e]=t;const s=_t[e];if(s&&s.length){_t[e]=[];for(const r of s)r()}},Xt=e=>{const t={};for(const s of Nt)e[s]&&(t[s]=e[s]);return t.name=e.name||"custom",t},Ut=async e=>{if(!e)return null;if(typeof e.select=="function")return e;const{adapter:t,...s}=e,r=typeof t=="string"?t:typeof e=="string"?e:null;if(!r)return null;let i=J[r]||It[r];if(!i&&(i=await qt(r),!i))throw new Error(`Unknown db adapter: "${r}". Use registerAdapter() for adapters with optional deps.`);const a=await i();if(!a||typeof a.setup!="function")throw new Error(`Adapter "${r}" loader returned a module without a setup() function. Got keys: [${Object.keys(a||{}).join(", ")}]. Likely a bundler chunk-resolution issue.`);return a.setup(s)},H=e=>{if(!e)return 0;if(typeof e=="number")return e;const t=e.match(/^(\d+)(ms|s|m|h)$/);if(!t)return 0;const s=parseInt(t[1]),r=t[2];return r==="ms"?s:r==="s"?s*1e3:r==="m"?s*6e4:r==="h"?s*36e5:0},w=new Map,U=new Map,V=new Map,zt=(e,t,s)=>{if(e.cache?.key)return e.cache.key;const r=Qt(e.params),i=s?`${s}:`:"",a=`${e.from}:${e.method}:${i}${JSON.stringify(r||"")}`;return t?`${a}:${t}`:a},X=e=>w.get(e)||null,Z=(e,t,s)=>{const r=w.get(e),i={data:t,error:s,time:Date.now(),stale:!1};w.set(e,i);const a=U.get(e);if(a)for(const o of a)l(o)&&o(i);return i},tt=e=>{if(e){const t=w.get(e);t&&(t.stale=!0);return}for(const[,t]of w)t.stale=!0},ft=e=>{if(e){w.delete(e);return}w.clear()},Zt=(e,t)=>(U.has(e)||U.set(e,new Set),U.get(e).add(t),()=>{const s=U.get(e);s&&(s.delete(t),s.size===0&&U.delete(e))}),yt=e=>!e&&e!==!1?{staleTime:6e4,gcTime:3e5}:e===!1?null:e===!0?{staleTime:6e4,gcTime:3e5}:typeof e=="number"?{staleTime:e,gcTime:Math.max(e*5,3e5)}:B(e)?{staleTime:H(e),gcTime:3e5}:{staleTime:H(e.stale||e.staleTime)||6e4,gcTime:H(e.gc||e.gcTime||e.expire)||3e5,key:e.key};let ct=null;const jt=()=>{ct||(ct=setInterval(()=>{const e=Date.now();for(const[t,s]of w){const r=U.get(t);!(r&&r.size>0)&&e-s.time>(s.gcTime||3e5)&&w.delete(t)}},3e4),ct.unref&&ct.unref())};jt();const Ft=3,gt=e=>Math.min(1e3*2**e,3e4),Et=e=>{const t=e.retry;return t===!1?{count:0}:t===!0||t===void 0?{count:Ft,delay:gt}:typeof t=="number"?{count:t,delay:gt}:{count:t.count??Ft,delay:l(t.delay)?t.delay:typeof t.delay=="number"?()=>t.delay:gt}},Dt=async(e,t)=>{const{count:s,delay:r}=t;let i;for(let a=0;a<=s;a++)try{const o=await e();if(o?.error){if(i=o.error,a<s){const f=l(r)?r(a,i):r;await new Promise(n=>setTimeout(n,f));continue}return o}return o}catch(o){if(i=o,a<s){const f=l(r)?r(a,o):r;await new Promise(n=>setTimeout(n,f));continue}throw o}return{data:null,error:i}},W={focus:new Set,online:new Set};let Ct=!1;const Gt=()=>{if(Ct||typeof window>"u")return;Ct=!0;const e=()=>{for(const s of W.focus)s()},t=()=>{for(const s of W.online)s()};window.addEventListener("visibilitychange",()=>{document.visibilityState==="visible"&&e()}),window.addEventListener("focus",e),window.addEventListener("online",t)},Pt=(e,t)=>{const s=t.__ref;if(e===!0)return{from:s.__state||t.key,method:"select",query:!0,on:"create"};if(B(e))return{from:e,method:"select",query:!0,on:"create"};if(Y(e)){const r={...e};return r.from=r.from||s.__state||t.key,r.method=r.method||"select",r.on=r.on||"create",r.query===void 0&&(r.query=!0),r}return null},Qt=e=>(!e||l(e),e),ut=(e,t)=>{const s=e?.state?.root||t?.state?.root;if(s?.lang)return s.lang},At=(e,t)=>{const r=(e?.state?.root||t?.state?.root)?.__fetchScope;return r?String(r):void 0},Lt=(e,t,s)=>{let r;if(!e)r=void 0;else if(l(e))r=e(t,t.state);else{r={};for(const i in e){const a=e[i];r[i]=l(a)?a(t,t.state):a}}return r},St=async(e,t)=>{if(!e||e.__authInitialized||(e.__authInitialized=!0,!e.getSession))return;const s=(r,i)=>{const a=t.state?.root;a?.update&&a.update({auth:{user:r,session:i,loading:!1}})};try{const r=await e.getSession();s(r?.user||null,r)}catch(r){console.warn("[fetch] Failed to restore auth session:",r.message)}e.onAuthStateChange&&e.onAuthStateChange((r,i)=>{s(i?.user||null,i)})},bt=async(e,t)=>{if(l(e.select))return e.auth!==!1&&await St(e,t),e;if(e.__resolved)return e.auth!==!1&&await St(e.__resolved,t),e.__resolved;if(e.__resolving)return e.__resolving;e.__resolving=Ut(e);const s=await e.__resolving;return delete e.__resolving,s?(s.auth=e.auth,s.url=e.url,s.key=e.key,e.__resolved=s,t.fetch=s,e.auth!==!1&&await St(s,t),s):null},T=(e,t,...s)=>{const r=l(e[t])?e[t]:null;r&&r.call(e,...s,e,e.state,e.context)},C=(e,t,s,...r)=>{const i=e&&l(e[s])?e[s]:null;i&&i.call(t,...r,t,t.state,t.context)},et=e=>{const t={},s=e.node;if(!s)return t;if(s.tagName==="FORM"){const i=new FormData(s);for(const[a,o]of i.entries())t[a]=o;return t}const r=s.querySelectorAll("input, textarea, select");for(let i=0;i<r.length;i++){const a=r[i],o=a.name||a.getAttribute("name");o&&(a.type==="checkbox"?t[o]=a.checked:a.type==="file"?t[o]=a.files:t[o]=a.value)}return t},z=(e,t,s,r={})=>{const i=s&&s.startsWith("~/"),a=i&&e.state?.root||e.state;if(a?.update)if(i){const o=s.slice(2);t&&typeof t=="object"&&!Array.isArray(t)&&o in t?a.update(t,{preventFetch:!0,...r}):a.update({[o]:t},{preventFetch:!0,...r})}else{const o=s?{[s]:t}:t;a.update(o,{preventFetch:!0,...r})}},k=(e,t)=>{const s=e?.__ref;s&&(s.__fetchStatus=t,s.__fetching=t.isFetching,s.__fetchError=t.error)},I=async(e,t,s,r={})=>{const i=s?.fetch;if(!i||!t||!t.__ref||e.enabled===!1||l(e.enabled)&&!e.enabled(t,t.state)||e.skip===!0||l(e.skip)&&e.skip(t,t.state))return;const a=await bt(i,s);if(!a||!t.__ref)return;const o=t.__ref,{from:f,method:n,query:_,params:m,cache:st,transform:F,single:lt,auth:rt,fields:R,as:O,limit:K,offset:at,order:j,headers:N,baseUrl:y,page:g,cursor:v,getNextPageParam:G,getPreviousPageParam:S,infinite:P,placeholderData:q,initialData:Q,select:A,keepPreviousData:$}=e;let E=B(A)?A:void 0;if(_&&l(t.getQuery)){const d=t.getQuery(a.name||"paths");d&&(E=d.select||d.length&&d.join(",")||E)}const L=Lt(m,t,s),D=yt(st),ot=Et(e),dt=ut(t,s),mt=At(t,s),$t=dt?`:${dt}`:"",Mt=mt?`${mt}:`:"",p=D?st?.key||`${f}:${n}:${Mt}${JSON.stringify(L||"")}${P?":infinite":""}${g?":p"+JSON.stringify(g):""}${$t}`:null;if(!o.__fetchInitialized&&p&&(o.__fetchInitialized=!0,Q!==void 0)){const d=l(Q)?Q():Q;Z(p,d,null)}if((n==="select"||n==="rpc")&&p&&D){const d=X(p);if(d&&!d.error){const u=Date.now()-d.time,h=d.stale||u>(D.staleTime||0);let c=d.data;if(l(A)&&(c=A(c,t,t.state)),F&&(c=F(c,t,t.state)),z(t,c,O),!h){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"});return}}else if(q!==void 0){const u=l(q)?q(t,t.state):q;u!==void 0&&z(t,u,O)}}if(p&&V.has(p)&&!r.force){const d=V.get(p);try{const u=await Promise.race([d,new Promise((h,c)=>setTimeout(()=>c("DEDUP_TIMEOUT"),15e3))]);if(u?.data!==void 0){let h=u.data;l(A)&&(h=A(h,t,t.state)),F&&(h=F(h,t,t.state)),z(t,h,O),T(t,"onFetchComplete",h),C(e,t,"onFetchComplete",h)}}catch{}return}const Rt=!X(p)?.data;k(t,{isFetching:!0,isLoading:Rt,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const M=s?.analyze?.emitNetwork,ht=M?typeof performance<"u"?performance.now():Date.now():0;M&&M({phase:"start",mode:"query",from:f,method:n,cacheKey:p,lang:dt});const vt=Dt(async()=>{if(rt!==!1&&a.auth!==!1&&a.getSession){const c=await a.getSession();if(rt===!0&&!c){const b={message:"Not authenticated"};return k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:b,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",b),C(e,t,"onFetchError",b),{data:null,error:b}}}const d=a[n];if(!l(d))return{data:null,error:{message:`Method "${n}" not found on adapter`}};const u={from:f,select:E,params:L,single:lt,limit:K,offset:at,order:j,headers:N,baseUrl:y},h=ut(t,s);if(h&&(u.headers={...u.headers,"Accept-Language":h}),g!==void 0){if(Y(g))g.offset!==void 0&&(u.offset=g.offset),g.limit!==void 0&&(u.limit=g.limit),g.cursor!==void 0&&(u.params={...u.params,cursor:g.cursor});else if(typeof g=="number"){const c=e.pageSize||K||20;u.offset=(g-1)*c,u.limit=c}}if(v!==void 0&&(u.params={...u.params,cursor:v}),n==="insert"||n==="update"||n==="upsert"){let c;if(R===!0||e.on==="submit")c=et(t);else if(x(R)){const b=et(t);c={};for(const pt of R)c[pt]=b[pt]}else t.state?.parse?c=t.state.parse():Y(t.state)&&(c={...t.state});F&&(c=F(c,t,t.state)),u.data=c}if(n==="rpc"){let c=L;if(e.on==="submit"){const b=et(t);c=e.transformParams?e.transformParams(b,t,t.state):b}u.params=c}if(n==="subscribe"){const c=a.subscribe({from:f,params:L,on:e.subscribeOn},(b,pt,Jt)=>{z(t,b,O),T(t,"onFetchComplete",b),C(e,t,"onFetchComplete",b)});return o.__unsubscribe=c,{data:null,error:null,subscribed:!0}}return d(u)},ot);p&&V.set(p,vt);try{const d=await vt;if(d?.subscribed)return;const{data:u,error:h}=d||{};if(h){p&&Z(p,null,h),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:h,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",h),C(e,t,"onFetchError",h),M&&M({phase:"error",mode:"query",from:f,method:n,cacheKey:p,error:h.message||h,durationMs:(typeof performance<"u"?performance.now():Date.now())-ht});return}if(u!==void 0){if((n==="select"||n==="rpc")&&p){const b=Z(p,u,null);b.gcTime=D?.gcTime||3e5}let c=u;P&&x(u)&&(o.__pages||(o.__pages=[]),r.direction==="previous"?o.__pages.unshift(u):o.__pages.push(u),c=o.__pages.flat(),G&&(o.__nextPageParam=G(u,o.__pages),o.__hasNextPage=o.__nextPageParam!=null),S&&(o.__prevPageParam=S(u,o.__pages),o.__hasPreviousPage=o.__prevPageParam!=null)),l(A)&&(c=A(c,t,t.state)),F&&(c=F(c,t,t.state)),(!$||c!==void 0)&&z(t,c,O),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",c),C(e,t,"onFetchComplete",c),M&&M({phase:"success",mode:"query",ok:!0,from:f,method:n,cacheKey:p,durationMs:(typeof performance<"u"?performance.now():Date.now())-ht})}}catch(d){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:d,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",d),C(e,t,"onFetchError",d),M&&M({phase:"error",mode:"query",from:f,method:n,cacheKey:p,error:d.message||String(d),durationMs:(typeof performance<"u"?performance.now():Date.now())-ht})}finally{p&&V.delete(p)}},xt=(e,t,s)=>{const r=e.on,i=t.__ref;i.__fetchListeners||(i.__fetchListeners=[]);const a=e.__runner||I;if(r==="submit"){const o=n=>{n.preventDefault(),a(e,t,s)},f=t.node;f&&(f.addEventListener("submit",o),i.__fetchListeners.push(()=>f.removeEventListener("submit",o)))}else if(r==="click"){const o=()=>a(e,t,s),f=t.node;f&&(f.addEventListener("click",o),i.__fetchListeners.push(()=>f.removeEventListener("click",o)))}else r==="stateChange"&&(i.__fetchOnStateChange=()=>a(e,t,s))},Wt=(e,t,s)=>{const r=t.__ref;r.__fetchCleanups||(r.__fetchCleanups=[]),Gt();const i=e.refetchInterval;if(i){const a=typeof i=="number"?i:H(i);if(a>0){const o=setInterval(()=>{(e.refetchIntervalInBackground||typeof document>"u"||document.visibilityState==="visible")&&I(e,t,s,{force:!0})},a);r.__fetchCleanups.push(()=>clearInterval(o))}}if(e.refetchOnWindowFocus!==!1){const a=()=>{const o=ut(t,s),f=At(t,s),n=zt(e,o,f),_=X(n),m=yt(e.cache);(!_||_.stale||Date.now()-_.time>(m?.staleTime||0))&&I(e,t,s,{force:!0})};W.focus.add(a),r.__fetchCleanups.push(()=>W.focus.delete(a))}if(e.refetchOnReconnect!==!1){const a=()=>I(e,t,s,{force:!0});W.online.add(a),r.__fetchCleanups.push(()=>W.online.delete(a))}},Kt=(e,t,s)=>{if(!t.optimistic)return null;const r=e.__ref,i=t.as,a=e.state?.parse?e.state.parse():Y(e.state)?{...e.state}:e.state;r.__optimisticSnapshot=a;const o=l(t.optimistic)?t.optimistic(s,a,e):t.optimistic;return o!==void 0&&z(e,o,i),a},wt=(e,t)=>{const s=e.__ref,r=s.__optimisticSnapshot;r!==void 0&&(z(e,r,t.as),delete s.__optimisticSnapshot)},Yt=async(e,t,s)=>{const r=s?.fetch;if(!r)return;const i=await bt(r,s);if(!i)return;const a=t.__ref,{method:o,from:f,fields:n,transform:_,as:m,on:st,auth:F,headers:lt,baseUrl:rt,invalidates:R,optimistic:O,onMutate:K,onSuccess:at,onError:j,onSettled:N}=e;let y;if(n===!0||st==="submit")y=et(t);else if(x(n)){const S=et(t);y={};for(const P of n)y[P]=S[P]}else t.state?.parse?y=t.state.parse():Y(t.state)&&(y={...t.state});_&&(y=_(y,t,t.state)),l(K)&&K(y,t,t.state);const g=O?Kt(t,e,y):null;a.__fetching=!0,k(t,{isFetching:!0,isLoading:!1,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const v=s?.analyze?.emitNetwork,G=v?typeof performance<"u"?performance.now():Date.now():0;v&&v({phase:"start",mode:"mutation",from:f,method:o});try{if(F!==!1&&i.auth!==!1&&i.getSession){const L=await i.getSession();if(F===!0&&!L){g!==void 0&&wt(t,e);const D={message:"Not authenticated"};T(t,"onFetchError",D),C(e,t,"onFetchError",D);return}}const S=i[o];if(!l(S))return;const P={from:f,data:y,headers:lt,baseUrl:rt};e.params&&(P.params=Lt(e.params,t,s));const q=ut(t,s);q&&(P.headers={...P.headers,"Accept-Language":q});const Q=Et(e),A=await Dt(()=>S(P),Q),{data:$,error:E}=A||{};if(E){g!==void 0&&wt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:E,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",E),l(j)&&j(E,y,t),l(N)&&N(null,E,y,t),v&&v({phase:"error",mode:"mutation",from:f,method:o,error:E.message||String(E),durationMs:(typeof performance<"u"?performance.now():Date.now())-G});return}if(delete a.__optimisticSnapshot,$!==void 0){const L=m?{[m]:$}:$;t.state?.update&&t.state.update(L,{preventFetch:!0})}if(k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",$),C(e,t,"onFetchComplete",$),l(at)&&at($,y,t),l(N)&&N($,null,y,t),v&&v({phase:"success",mode:"mutation",ok:!0,from:f,method:o,durationMs:(typeof performance<"u"?performance.now():Date.now())-G}),R){const L=x(R)?R:[R];for(const D of L)if(D===!0||D==="*")for(const[ot]of w)ot.startsWith(f+":")&&tt(ot);else tt(D)}}catch(S){g!==void 0&&wt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:S,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",S),C(e,t,"onFetchError",S),l(j)&&j(S,y,t),l(N)&&N(null,S,y,t),v&&v({phase:"error",mode:"mutation",from:f,method:o,error:S.message||String(S),durationMs:(typeof performance<"u"?performance.now():Date.now())-G})}finally{a.__fetching=!1}},kt=(e,t,s,r)=>{if(!e||!r?.fetch)return;const a=Tt(e,t);if(!a)return;const o=x(a)?a.map(n=>Pt(Tt(n,t),t)).filter(Boolean):[Pt(a,t)].filter(Boolean);for(const n of o){const _=n.method==="insert"||n.method==="update"||n.method==="upsert"||n.method==="delete",m=_?Yt:I;n.on==="create"?m(n,t,r):Promise.resolve().then(()=>{xt({...n,__runner:m},t,r)}),!_&&n.on==="create"&&(n.refetchInterval||n.refetchOnWindowFocus!==!1||n.refetchOnReconnect!==!1)&&Wt(n,t,r)}const f=t.__ref;f.refetch=n=>{for(const _ of o)I(_,t,r,{force:!0,...n})},f.fetchNextPage=()=>{const n=o[0];if(!n||!n.infinite)return;const _=f.__nextPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"next",force:!0})},f.fetchPreviousPage=()=>{const n=o[0];if(!n||!n.infinite)return;const _=f.__prevPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"previous",force:!0})}},te={invalidateQueries:e=>{if(!e){tt();return}if(B(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&tt(t);else if(x(e)){const t=e.join(":");for(const[s]of w)s.includes(t)&&tt(s)}},removeQueries:e=>{if(!e){ft();return}if(B(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&ft(t)},getQueryData:e=>X(e)?.data??void 0,setQueryData:(e,t)=>{const s=X(e),r=l(t)?t(s?.data):t;Z(e,r,null)},prefetchQuery:async(e,t)=>{const s=t?.fetch;if(!s)return;const r=await bt(s,t);if(!r)return;const i=e.cache?.key||`${e.from}:${e.method||"select"}:${JSON.stringify(e.params||"")}`,a=r[e.method||"select"];if(!l(a))return;const o=await a({from:e.from,params:e.params,limit:e.limit,offset:e.offset,order:e.order,single:e.single});if(o?.data!==void 0&&!o?.error){const f=Z(i,o.data,null);f.gcTime=yt(e.cache)?.gcTime||3e5}return o},getCache:()=>w,clear:()=>ft(),dropNonLang:e=>{if(!e)return;const t=`:${e}`;for(const[s]of w)s.match(/:([a-z]{2,3})$/i)&&(s.endsWith(t)||ft(s))}},ee={name:"fetch",create(e){const t=e.fetch;t&&(e.parent||V.clear(),kt(t,e,e.state,e.context))}};var se=kt;export{Xt as createAdapter,se as default,kt as executeFetch,ee as fetchPlugin,St as initAdapterAuth,H as parseDuration,te as queryClient,Vt as registerAdapter,Ut as resolveDb};
1
+ import{isArray as x,isFunction as l,isObject as Y,isString as B,exec as Tt}from"@symbo.ls/utils";const Nt=["select","rpc","insert","update","upsert","delete","subscribe"],It={supabase:()=>import("./adapters/supabase.js"),rest:()=>import("./adapters/rest.js"),local:()=>import("./adapters/local.js")},it=Symbol.for("@symbo.ls/fetch:CUSTOM_ADAPTERS"),J=(()=>{if(typeof globalThis<"u"&&globalThis[it])return globalThis[it];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,it,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[it]}catch{}return e})(),nt=Symbol.for("@symbo.ls/fetch:PENDING_ADAPTERS"),_t=(()=>{if(typeof globalThis<"u"&&globalThis[nt])return globalThis[nt];const e={};if(typeof globalThis<"u")try{return Object.defineProperty(globalThis,nt,{value:e,writable:!1,configurable:!1,enumerable:!1}),globalThis[nt]}catch{}return e})(),Ot=5e3,qt=e=>new Promise(t=>{if(J[e])return t(J[e]);const s=_t[e]||=[];let r=!1;const i=()=>{r||(r=!0,t(J[e]||null))};s.push(i),setTimeout(i,Ot)}),Vt=(e,t)=>{J[e]=t;const s=_t[e];if(s&&s.length){_t[e]=[];for(const r of s)r()}},Xt=e=>{const t={};for(const s of Nt)e[s]&&(t[s]=e[s]);return t.name=e.name||"custom",t},Ut=async e=>{if(!e)return null;if(typeof e.select=="function")return e;const{adapter:t,...s}=e,r=typeof t=="string"?t:typeof e=="string"?e:null;if(!r)return null;let i=J[r]||It[r];if(!i&&(i=await qt(r),!i))throw new Error(`Unknown db adapter: "${r}". Use registerAdapter() for adapters with optional deps.`);const a=await i();if(!a||typeof a.setup!="function")throw new Error(`Adapter "${r}" loader returned a module without a setup() function. Got keys: [${Object.keys(a||{}).join(", ")}]. Likely a bundler chunk-resolution issue.`);return a.setup(s)},H=e=>{if(!e)return 0;if(typeof e=="number")return e;const t=e.match(/^(\d+)(ms|s|m|h)$/);if(!t)return 0;const s=parseInt(t[1]),r=t[2];return r==="ms"?s:r==="s"?s*1e3:r==="m"?s*6e4:r==="h"?s*36e5:0},w=new Map,U=new Map,V=new Map,zt=(e,t,s)=>{if(e.cache?.key)return e.cache.key;const r=Qt(e.params),i=s?`${s}:`:"",a=`${e.from}:${e.method}:${i}${JSON.stringify(r||"")}`;return t?`${a}:${t}`:a},X=e=>w.get(e)||null,Z=(e,t,s)=>{const r=w.get(e),i={data:t,error:s,time:Date.now(),stale:!1};w.set(e,i);const a=U.get(e);if(a)for(const o of a)l(o)&&o(i);return i},tt=e=>{if(e){const t=w.get(e);t&&(t.stale=!0);return}for(const[,t]of w)t.stale=!0},ft=e=>{if(e){w.delete(e);return}w.clear()},Zt=(e,t)=>(U.has(e)||U.set(e,new Set),U.get(e).add(t),()=>{const s=U.get(e);s&&(s.delete(t),s.size===0&&U.delete(e))}),yt=e=>!e&&e!==!1?{staleTime:6e4,gcTime:3e5}:e===!1?null:e===!0?{staleTime:6e4,gcTime:3e5}:typeof e=="number"?{staleTime:e,gcTime:Math.max(e*5,3e5)}:B(e)?{staleTime:H(e),gcTime:3e5}:{staleTime:H(e.stale||e.staleTime)||6e4,gcTime:H(e.gc||e.gcTime||e.expire)||3e5,key:e.key};let ct=null;const jt=()=>{ct||(ct=setInterval(()=>{const e=Date.now();for(const[t,s]of w){const r=U.get(t);!(r&&r.size>0)&&e-s.time>(s.gcTime||3e5)&&w.delete(t)}},3e4),ct.unref&&ct.unref())};jt();const Ft=3,gt=e=>Math.min(1e3*2**e,3e4),Et=e=>{const t=e.retry;return t===!1?{count:0}:t===!0||t===void 0?{count:Ft,delay:gt}:typeof t=="number"?{count:t,delay:gt}:{count:t.count??Ft,delay:l(t.delay)?t.delay:typeof t.delay=="number"?()=>t.delay:gt}},Dt=async(e,t)=>{const{count:s,delay:r}=t;let i;for(let a=0;a<=s;a++)try{const o=await e();if(o?.error){if(i=o.error,a<s){const f=l(r)?r(a,i):r;await new Promise(n=>setTimeout(n,f));continue}return o}return o}catch(o){if(i=o,a<s){const f=l(r)?r(a,o):r;await new Promise(n=>setTimeout(n,f));continue}throw o}return{data:null,error:i}},W={focus:new Set,online:new Set};let Ct=!1;const Gt=()=>{if(Ct||typeof window>"u")return;Ct=!0;const e=()=>{for(const s of W.focus)s()},t=()=>{for(const s of W.online)s()};window.addEventListener("visibilitychange",()=>{document.visibilityState==="visible"&&e()}),window.addEventListener("focus",e),window.addEventListener("online",t)},Pt=(e,t)=>{const s=t.__ref;if(e===!0)return{from:s.__state||t.key,method:"select",query:!0,on:"create"};if(B(e))return{from:e,method:"select",query:!0,on:"create"};if(Y(e)){const r={...e};return r.from=r.from||s.__state||t.key,r.method=r.method||"select",r.on=r.on||"create",r.query===void 0&&(r.query=!0),r}return null},Qt=e=>(!e||l(e),e),ut=(e,t)=>{const s=e?.state?.root||t?.state?.root;if(s?.lang)return s.lang},At=(e,t)=>{const r=(e?.state?.root||t?.state?.root)?.__fetchScope;return r?String(r):void 0},Lt=(e,t,s)=>{let r;if(!e)r=void 0;else if(l(e))r=e(t,t.state);else{r={};for(const i in e){const a=e[i];r[i]=l(a)?a(t,t.state):a}}return r},St=async(e,t)=>{if(!e||e.__authInitialized||(e.__authInitialized=!0,!e.getSession))return;const s=(r,i)=>{const a=t.state?.root;a?.update&&a.update({auth:{user:r,session:i,loading:!1}})};try{const r=await e.getSession();s(r?.user||null,r)}catch(r){console.warn("[fetch] Failed to restore auth session:",r.message)}e.onAuthStateChange&&e.onAuthStateChange((r,i)=>{s(i?.user||null,i)})},bt=async(e,t)=>{if(l(e.select))return e.auth!==!1&&await St(e,t),e;if(e.__resolved)return e.auth!==!1&&await St(e.__resolved,t),e.__resolved;if(e.__resolving)return e.__resolving;e.__resolving=Ut(e);const s=await e.__resolving;return delete e.__resolving,s?(s.auth=e.auth,s.url=e.url,s.key=e.key,e.__resolved=s,t.fetch=s,e.auth!==!1&&await St(s,t),s):null},T=(e,t,...s)=>{const r=l(e[t])?e[t]:null;r&&r.call(e,...s,e,e.state,e.context)},C=(e,t,s,...r)=>{const i=e&&l(e[s])?e[s]:null;i&&i.call(t,...r,t,t.state,t.context)},et=e=>{const t={},s=e.node;if(!s)return t;if(s.tagName==="FORM"){const i=new FormData(s);for(const[a,o]of i.entries())t[a]=o;return t}const r=s.querySelectorAll("input, textarea, select");for(let i=0;i<r.length;i++){const a=r[i],o=a.name||a.getAttribute("name");o&&(a.type==="checkbox"?t[o]=a.checked:a.type==="file"?t[o]=a.files:t[o]=a.value)}return t},z=(e,t,s,r={})=>{const i=s&&s.startsWith("~/"),a=i&&e.state?.root||e.state;if(a?.update)if(i){const o=s.slice(2);t&&typeof t=="object"&&!Array.isArray(t)&&o in t?a.update(t,{preventFetch:!0,...r}):a.update({[o]:t},{preventFetch:!0,...r})}else{const o=s?{[s]:t}:t;a.update(o,{preventFetch:!0,...r})}},k=(e,t)=>{const s=e?.__ref;s&&(s.__fetchStatus=t,s.__fetching=t.isFetching,s.__fetchError=t.error)},I=async(e,t,s,r={})=>{const i=s?.fetch;if(!i||!t||!t.__ref||e.enabled===!1||l(e.enabled)&&!e.enabled(t,t.state)||e.skip===!0||l(e.skip)&&e.skip(t,t.state))return;const a=await bt(i,s);if(!a||!t.__ref)return;const o=t.__ref,{from:f,method:n,query:_,params:m,cache:st,transform:F,single:lt,auth:rt,fields:R,as:O,limit:K,offset:at,order:j,headers:N,baseUrl:y,page:g,cursor:v,getNextPageParam:G,getPreviousPageParam:S,infinite:P,placeholderData:q,initialData:Q,select:A,keepPreviousData:$}=e;let E=B(A)?A:void 0;if(_&&l(t.getQuery)){const d=t.getQuery(a.name||"paths");d&&(E=d.select||d.length&&d.join(",")||E)}const L=Lt(m,t,s),D=yt(st),ot=Et(e),dt=ut(t,s),mt=At(t,s),$t=dt?`:${dt}`:"",Mt=mt?`${mt}:`:"",p=D?st?.key||`${f}:${n}:${Mt}${JSON.stringify(L||"")}${P?":infinite":""}${g?":p"+JSON.stringify(g):""}${$t}`:null;if(!o.__fetchInitialized&&p&&(o.__fetchInitialized=!0,Q!==void 0)){const d=l(Q)?Q():Q;Z(p,d,null)}if((n==="select"||n==="rpc")&&p&&D){const d=X(p);if(d&&!d.error){const u=Date.now()-d.time,h=d.stale||u>(D.staleTime||0);let c=d.data;if(l(A)&&(c=A(c,t,t.state)),F&&(c=F(c,t,t.state)),z(t,c,O),!h){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"});return}}else if(q!==void 0){const u=l(q)?q(t,t.state):q;u!==void 0&&z(t,u,O)}}if(p&&V.has(p)&&!r.force){const d=V.get(p);try{const u=await Promise.race([d,new Promise((h,c)=>setTimeout(()=>c("DEDUP_TIMEOUT"),15e3))]);if(u?.data!==void 0){let h=u.data;l(A)&&(h=A(h,t,t.state)),F&&(h=F(h,t,t.state)),z(t,h,O),T(t,"onFetchComplete",h),C(e,t,"onFetchComplete",h)}}catch{}return}const Rt=!X(p)?.data;k(t,{isFetching:!0,isLoading:Rt,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const M=s?.analyze?.emitNetwork,ht=M?typeof performance<"u"?performance.now():Date.now():0;M&&M({phase:"start",mode:"query",from:f,method:n,cacheKey:p,lang:dt});const vt=Dt(async()=>{if(rt!==!1&&a.auth!==!1&&a.getSession){const c=await a.getSession();if(rt===!0&&!c){const b={message:"Not authenticated"};return k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:b,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",b),C(e,t,"onFetchError",b),{data:null,error:b}}}const d=a[n];if(!l(d))return{data:null,error:{message:`Method "${n}" not found on adapter`}};const u={from:f,select:E,params:L,single:lt,limit:K,offset:at,order:j,headers:N,baseUrl:y},h=ut(t,s);if(h&&(u.headers={...u.headers,"Accept-Language":h}),g!==void 0){if(Y(g))g.offset!==void 0&&(u.offset=g.offset),g.limit!==void 0&&(u.limit=g.limit),g.cursor!==void 0&&(u.params={...u.params,cursor:g.cursor});else if(typeof g=="number"){const c=e.pageSize||K||20;u.offset=(g-1)*c,u.limit=c}}if(v!==void 0&&(u.params={...u.params,cursor:v}),n==="insert"||n==="update"||n==="upsert"){let c;if(R===!0||e.on==="submit")c=et(t);else if(x(R)){const b=et(t);c={};for(const pt of R)c[pt]=b[pt]}else t.state?.parse?c=t.state.parse():Y(t.state)&&(c={...t.state});F&&(c=F(c,t,t.state)),u.data=c}if(n==="rpc"){let c=L;if(e.on==="submit"){const b=et(t);c=e.transformParams?e.transformParams(b,t,t.state):b}u.params=c}if(n==="subscribe"){const c=a.subscribe({from:f,params:L,on:e.subscribeOn},(b,pt,Jt)=>{z(t,b,O),T(t,"onFetchComplete",b),C(e,t,"onFetchComplete",b)});return o.__unsubscribe=c,{data:null,error:null,subscribed:!0}}return d(u)},ot);p&&V.set(p,vt);try{const d=await vt;if(d?.subscribed)return;const{data:u,error:h}=d||{};if(h){p&&Z(p,null,h),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:h,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",h),C(e,t,"onFetchError",h),M&&M({phase:"error",mode:"query",from:f,method:n,cacheKey:p,error:h.message||h,durationMs:(typeof performance<"u"?performance.now():Date.now())-ht});return}if(u!==void 0){if((n==="select"||n==="rpc")&&p){const b=Z(p,u,null);b.gcTime=D?.gcTime||3e5}let c=u;P&&x(u)&&(o.__pages||(o.__pages=[]),r.direction==="previous"?o.__pages.unshift(u):o.__pages.push(u),c=o.__pages.flat(),G&&(o.__nextPageParam=G(u,o.__pages),o.__hasNextPage=o.__nextPageParam!=null),S&&(o.__prevPageParam=S(u,o.__pages),o.__hasPreviousPage=o.__prevPageParam!=null)),l(A)&&(c=A(c,t,t.state)),F&&(c=F(c,t,t.state)),(!$||c!==void 0)&&z(t,c,O),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",c),C(e,t,"onFetchComplete",c),M&&M({phase:"success",mode:"query",ok:!0,from:f,method:n,cacheKey:p,durationMs:(typeof performance<"u"?performance.now():Date.now())-ht})}}catch(d){k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:d,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",d),C(e,t,"onFetchError",d),M&&M({phase:"error",mode:"query",from:f,method:n,cacheKey:p,error:d.message||String(d),durationMs:(typeof performance<"u"?performance.now():Date.now())-ht})}finally{p&&V.delete(p)}},xt=(e,t,s)=>{const r=e.on,i=t.__ref;i.__fetchListeners||(i.__fetchListeners=[]);const a=e.__runner||I;if(r==="submit"){const o=n=>{n.preventDefault(),a(e,t,s)},f=t.node;f&&(f.addEventListener("submit",o),i.__fetchListeners.push(()=>f.removeEventListener("submit",o)))}else if(r==="click"){const o=()=>a(e,t,s),f=t.node;f&&(f.addEventListener("click",o),i.__fetchListeners.push(()=>f.removeEventListener("click",o)))}else r==="stateChange"&&(i.__fetchOnStateChange=()=>a(e,t,s))},Wt=(e,t,s)=>{const r=t.__ref;r.__fetchCleanups||(r.__fetchCleanups=[]),Gt();const i=e.refetchInterval;if(i){const a=typeof i=="number"?i:H(i);if(a>0){const o=setInterval(()=>{(e.refetchIntervalInBackground||typeof document>"u"||document.visibilityState==="visible")&&I(e,t,s,{force:!0})},a);r.__fetchCleanups.push(()=>clearInterval(o))}}if(e.refetchOnWindowFocus!==!1){const a=()=>{const o=ut(t,s),f=At(t,s),n=zt(e,o,f),_=X(n),m=yt(e.cache);(!_||_.stale||Date.now()-_.time>(m?.staleTime||0))&&I(e,t,s,{force:!0})};W.focus.add(a),r.__fetchCleanups.push(()=>W.focus.delete(a))}if(e.refetchOnReconnect!==!1){const a=()=>I(e,t,s,{force:!0});W.online.add(a),r.__fetchCleanups.push(()=>W.online.delete(a))}},Kt=(e,t,s)=>{if(!t.optimistic)return null;const r=e.__ref,i=t.as,a=e.state?.parse?e.state.parse():Y(e.state)?{...e.state}:e.state;r.__optimisticSnapshot=a;const o=l(t.optimistic)?t.optimistic(s,a,e):t.optimistic;return o!==void 0&&z(e,o,i),a},wt=(e,t)=>{const s=e.__ref,r=s.__optimisticSnapshot;r!==void 0&&(z(e,r,t.as),delete s.__optimisticSnapshot)},Yt=async(e,t,s)=>{const r=s?.fetch;if(!r)return;const i=await bt(r,s);if(!i)return;const a=t.__ref,{method:o,from:f,fields:n,transform:_,as:m,on:st,auth:F,headers:lt,baseUrl:rt,invalidates:R,optimistic:O,onMutate:K,onSuccess:at,onError:j,onSettled:N}=e;let y;if(n===!0||st==="submit")y=et(t);else if(x(n)){const S=et(t);y={};for(const P of n)y[P]=S[P]}else t.state?.parse?y=t.state.parse():Y(t.state)&&(y={...t.state});_&&(y=_(y,t,t.state)),l(K)&&K(y,t,t.state);const g=O?Kt(t,e,y):null;a.__fetching=!0,k(t,{isFetching:!0,isLoading:!1,isStale:!1,isSuccess:!1,error:null,status:"pending",fetchStatus:"fetching"}),T(t,"onFetchStart"),C(e,t,"onFetchStart");const v=s?.analyze?.emitNetwork,G=v?typeof performance<"u"?performance.now():Date.now():0;v&&v({phase:"start",mode:"mutation",from:f,method:o});try{if(F!==!1&&i.auth!==!1&&i.getSession){const L=await i.getSession();if(F===!0&&!L){g!==void 0&&wt(t,e);const D={message:"Not authenticated"};T(t,"onFetchError",D),C(e,t,"onFetchError",D);return}}const S=i[o];if(!l(S))return;const P={from:f,data:y,headers:lt,baseUrl:rt};e.params&&(P.params=Lt(e.params,t,s));const q=ut(t,s);q&&(P.headers={...P.headers,"Accept-Language":q});const Q=Et(e),A=await Dt(()=>S(P),Q),{data:$,error:E}=A||{};if(E){g!==void 0&&wt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:E,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",E),l(j)&&j(E,y,t),l(N)&&N(null,E,y,t),v&&v({phase:"error",mode:"mutation",from:f,method:o,error:E.message||String(E),durationMs:(typeof performance<"u"?performance.now():Date.now())-G});return}if(delete a.__optimisticSnapshot,$!==void 0){const L=m?{[m]:$}:$;t.state?.update&&t.state.update(L,{preventFetch:!0})}if(k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!0,error:null,status:"success",fetchStatus:"idle"}),T(t,"onFetchComplete",$),C(e,t,"onFetchComplete",$),l(at)&&at($,y,t),l(N)&&N($,null,y,t),v&&v({phase:"success",mode:"mutation",ok:!0,from:f,method:o,durationMs:(typeof performance<"u"?performance.now():Date.now())-G}),R){const L=x(R)?R:[R];for(const D of L)if(D===!0||D==="*")for(const[ot]of w)ot.startsWith(f+":")&&tt(ot);else tt(D)}}catch(S){g!==void 0&&wt(t,e),k(t,{isFetching:!1,isLoading:!1,isStale:!1,isSuccess:!1,error:S,status:"error",fetchStatus:"idle"}),T(t,"onFetchError",S),C(e,t,"onFetchError",S),l(j)&&j(S,y,t),l(N)&&N(null,S,y,t),v&&v({phase:"error",mode:"mutation",from:f,method:o,error:S.message||String(S),durationMs:(typeof performance<"u"?performance.now():Date.now())-G})}finally{a.__fetching=!1}},kt=(e,t,s,r)=>{if(!e||!r?.fetch)return;const a=Tt(e,t);if(!a)return;const o=x(a)?a.map(n=>Pt(Tt(n,t),t)).filter(Boolean):[Pt(a,t)].filter(Boolean);for(const n of o){const _=n.method==="insert"||n.method==="update"||n.method==="upsert"||n.method==="delete",m=_?Yt:I;n.on==="create"?m(n,t,r):Promise.resolve().then(()=>{xt({...n,__runner:m},t,r)}),!_&&n.on==="create"&&(n.refetchInterval||n.refetchOnWindowFocus!==!1||n.refetchOnReconnect!==!1)&&Wt(n,t,r)}const f=t.__ref;f.refetch=n=>{for(const _ of o)I(_,t,r,{force:!0,...n})},f.fetchNextPage=()=>{const n=o[0];if(!n||!n.infinite)return;const _=f.__nextPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"next",force:!0})},f.fetchPreviousPage=()=>{const n=o[0];if(!n||!n.infinite)return;const _=f.__prevPageParam;if(_==null)return;const m={...n,cursor:_};I(m,t,r,{direction:"previous",force:!0})}},te={invalidateQueries:e=>{if(!e){tt();return}if(B(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&tt(t);else if(x(e)){const t=e.join(":");for(const[s]of w)s.includes(t)&&tt(s)}},removeQueries:e=>{if(!e){ft();return}if(B(e))for(const[t]of w)(t.startsWith(e)||t.includes(e))&&ft(t)},getQueryData:e=>X(e)?.data??void 0,setQueryData:(e,t)=>{const s=X(e),r=l(t)?t(s?.data):t;Z(e,r,null)},prefetchQuery:async(e,t)=>{const s=t?.fetch;if(!s)return;const r=await bt(s,t);if(!r)return;const i=e.cache?.key||`${e.from}:${e.method||"select"}:${JSON.stringify(e.params||"")}`,a=r[e.method||"select"];if(!l(a))return;const o=await a({from:e.from,params:e.params,limit:e.limit,offset:e.offset,order:e.order,single:e.single});if(o?.data!==void 0&&!o?.error){const f=Z(i,o.data,null);f.gcTime=yt(e.cache)?.gcTime||3e5}return o},getCache:()=>w,clear:()=>ft(),dropNonLang:e=>{if(!e)return;const t=`:${e}`;for(const[s]of w)s.match(/:([a-z]{2,3})$/i)&&(s.endsWith(t)||ft(s))}},ee={name:"fetch",create(e){const t=e.fetch;t&&(e.parent||V.clear(),kt(t,e,e.state,e.context))}};var se=kt;export{Xt as createAdapter,se as default,kt as executeFetch,ee as fetchPlugin,St as initAdapterAuth,H as parseDuration,te as queryClient,Vt as registerAdapter,Ut as resolveDb};
package/index.js CHANGED
@@ -4,7 +4,7 @@ import { isArray, isFunction, isObject, isString, exec } from '@symbo.ls/utils'
4
4
 
5
5
  // --- Adapter resolution ---
6
6
 
7
- const ADAPTER_METHODS = ['select', 'rpc', 'insert', 'update', 'delete', 'subscribe']
7
+ const ADAPTER_METHODS = ['select', 'rpc', 'insert', 'update', 'upsert', 'delete', 'subscribe']
8
8
 
9
9
  // Cross-bundle adapter registry. Bundlers will happily ship two separate
10
10
  // module instances of `@symbo.ls/fetch` — one inside `smbls.esm.js` for
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@symbo.ls/fetch",
3
- "version": "3.14.595",
3
+ "version": "3.14.599",
4
4
  "license": "CC-BY-NC-4.0",
5
5
  "type": "module",
6
6
  "module": "./dist/esm/index.js",
@@ -41,10 +41,10 @@
41
41
  "adapters"
42
42
  ],
43
43
  "dependencies": {
44
- "@symbo.ls/utils": "^3.14.105"
44
+ "@symbo.ls/utils": "^3.14.599"
45
45
  },
46
46
  "peerDependencies": {
47
- "@supabase/supabase-js": "^2.108.0"
47
+ "@supabase/supabase-js": "^2.110.8"
48
48
  },
49
49
  "peerDependenciesMeta": {
50
50
  "@supabase/supabase-js": {