@api-buddy/firebase
Version:
Firebase integration for API Buddy
3 lines • 3.82 kB
JavaScript
import { createRequire } from 'node:module'; const require = createRequire(import.meta.url);
import{initializeApp as _,getApps as D,getApp as x}from"firebase/app";import{getAuth as O}from"firebase/auth";import{getFirestore as U}from"firebase/firestore";import{getStorage as v}from"firebase/storage";var Q={apiKey:process.env.NEXT_PUBLIC_FIREBASE_API_KEY,authDomain:process.env.NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN,projectId:process.env.NEXT_PUBLIC_FIREBASE_PROJECT_ID,storageBucket:process.env.NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET,messagingSenderId:process.env.NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID,appId:process.env.NEXT_PUBLIC_FIREBASE_APP_ID,measurementId:process.env.NEXT_PUBLIC_FIREBASE_MEASUREMENT_ID},y;D().length?y=x():y=_(Q);var Z=O(y),h=U(y),$=v(y);var ee=()=>D().length>0;import{collection as P,doc as m,getDoc as W,getDocs as w,setDoc as q,updateDoc as N,deleteDoc as L,query as S,where as b,orderBy as C,limit as A,startAfter as M,endBefore as X,limitToLast as j,onSnapshot as k,writeBatch as G,runTransaction as J}from"firebase/firestore";var g=class{constructor(e,t=h){this.firestore=t;this.collectionRef=P(this.firestore,e)}collectionRef;async create(e,t){let o=m(this.collectionRef,e);return await q(o,t),{id:e,...t}}async get(e){let t=m(this.collectionRef,e),o=await W(t);return o.exists()?{id:o.id,...o.data()}:null}async update(e,t){let o=m(this.collectionRef,e);await N(o,t)}async delete(e){let t=m(this.collectionRef,e);await L(t)}async query(e={}){let t=this.collectionRef,o=[];if(e.where&&e.where.length>0)for(let r of e.where)o.push(b(r.field,r.operator,r.value));if(e.orderBy&&e.orderBy.length>0)for(let r of e.orderBy)o.push(C(r.field,r.direction||"asc"));return e.limit&&(e.last?o.push(j(e.limit)):o.push(A(e.limit))),e.startAfter&&o.push(M(e.startAfter)),e.endBefore&&o.push(X(e.endBefore)),o.length>0&&(t=S(this.collectionRef,...o)),(await w(t)).docs.map(r=>({id:r.id,...r.data()}))}subscribe(e,t={}){let o=this.collectionRef,s=[];if(t.where&&t.where.length>0)for(let r of t.where)s.push(b(r.field,r.operator,r.value));if(t.orderBy&&t.orderBy.length>0)for(let r of t.orderBy)s.push(C(r.field,r.direction||"asc"));return t.limit&&s.push(A(t.limit)),s.length>0&&(o=S(this.collectionRef,...s)),k(o,r=>{let l=r.docs.map(u=>({id:u.id,...u.data()}));e(l)})}static createBatch(){return G(h)}static async runTransaction(e){return J(h,e)}};import{useEffect as I,useRef as T,useState as a}from"react";function ae(i,e){let[t,o]=a(null),[s,r]=a(!0),[l,u]=a(null);return I(()=>{e?(async()=>{try{r(!0);let n=await i.get(e);o(n)}catch(n){console.error("Error fetching document:",n),u(n)}finally{r(!1)}})():r(!1)},[i,e]),{data:t,loading:s,error:l}}function ce(i,e={}){let[t,o]=a([]),[s,r]=a(!0),[l,u]=a(null),[f,n]=a(!0),c=T(null),R=async(E=!1)=>{if(!(!f&&!E||s))try{r(!0);let d={...e,...c.current&&!E&&{startAfter:c.current}},p=await i.query(d);o(F=>E?p:[...F,...p]),n(p.length===(e.limit||20)),p.length>0&&(c.current=p[p.length-1])}catch(d){console.error("Error fetching collection:",d),u(d)}finally{r(!1)}},B=()=>(c.current=null,R(!0));return I(()=>{B()},[JSON.stringify(e)]),{data:t,loading:s,error:l,hasMore:f,fetchMore:()=>R(!1),refresh:B}}function ue(i,e={}){let[t,o]=a([]),[s,r]=a(!0),[l,u]=a(null),f=T(),n=T(!0);return I(()=>{n.current=!0,r(!0);try{f.current=i.subscribe(c=>{n.current&&(o(c),r(!1))},e)}catch(c){console.error("Error setting up realtime subscription:",c),n.current&&(u(c),r(!1))}return()=>{n.current=!1,f.current&&f.current()}},[i,JSON.stringify(e)]),{data:t,loading:s,error:l}}function le(i){let e=T();return e.current||(e.current=new g(i)),e.current}export{g as FirestoreService,Z as auth,h as db,y as firebaseApp,ee as isFirebaseInitialized,$ as storage,ce as useCollection,ae as useDocument,le as useFirestoreService,ue as useRealtimeCollection};
//# sourceMappingURL=index.mjs.map