diff --git a/src/lib/firebase.ts b/src/lib/firebase.ts index 15b324f..a47fec5 100644 --- a/src/lib/firebase.ts +++ b/src/lib/firebase.ts @@ -8,6 +8,8 @@ import { initializeFirestore, } from 'firebase/firestore' import { Platform } from 'react-native' +import { connectFunctionsEmulator, getFunctions } from 'firebase/functions' +import skeetCloudConfig from '@root/skeet-cloud.config.json' export const firebaseApp = !getApps().length ? initializeApp(firebaseConfig) @@ -61,3 +63,13 @@ export const analytics = firebaseApp ? getAnalytics(firebaseApp) : undefined + +const getFirebaseFunction = () => { + const firebaseFunction = getFunctions(firebaseApp) + firebaseFunction.region = skeetCloudConfig.app.region + if (process.env.NODE_ENV !== 'production') { + connectFunctionsEmulator(firebaseFunction, platformDevIP, 5001) + } + return firebaseFunction +} +export const functions = firebaseApp ? getFirebaseFunction() : undefined diff --git a/src/lib/skeet/functions.ts b/src/lib/skeet/functions.ts index 3670558..62ae1d6 100644 --- a/src/lib/skeet/functions.ts +++ b/src/lib/skeet/functions.ts @@ -1,7 +1,8 @@ import skeetCloudConfig from '@root/skeet-cloud.config.json' import { toKebabCase } from '@/utils/character' -import { auth, platformDevIP } from '@/lib/firebase' +import { auth, platformDevIP, functions } from '@/lib/firebase' import { signOut } from 'firebase/auth' +import { httpsCallable, httpsCallableFromURL } from 'firebase/functions' export const fetchSkeetFunctions = async ( functionName: string, @@ -40,3 +41,32 @@ export const fetchSkeetFunctions = async ( } } } + +export const callSkeetFunctions = async ( + functionName: string, + methodName: string, + params: T +) => { + try { + const callableFunction = + process.env.NODE_ENV === 'production' && + skeetCloudConfig.app.hasLoadBalancer + ? functions + ? httpsCallableFromURL( + functions, + `https://${ + skeetCloudConfig.app.lbDomain + }/${functionName}/${toKebabCase(methodName)}` + ) + : undefined + : functions + ? httpsCallable(functions, methodName) + : undefined + + const res = await callableFunction?.(params) + return res + } catch (err: any) { + console.error(err) + throw new Error(err.message) + } +} diff --git a/webapp/src/lib/firebase.ts b/webapp/src/lib/firebase.ts index ecc5958..c965e1f 100644 --- a/webapp/src/lib/firebase.ts +++ b/webapp/src/lib/firebase.ts @@ -4,12 +4,11 @@ import { initializeApp, getApp, getApps } from 'firebase/app' import { connectAuthEmulator, getAuth } from 'firebase/auth' import { getStorage, connectStorageEmulator } from 'firebase/storage' import { - DocumentData, - FirestoreDataConverter, - QueryDocumentSnapshot, connectFirestoreEmulator, initializeFirestore, } from 'firebase/firestore' +import { connectFunctionsEmulator, getFunctions } from 'firebase/functions' +import skeetCloudConfig from '@root/skeet-cloud.config.json' export const firebaseApp = !getApps().length ? initializeApp(firebaseConfig) @@ -58,3 +57,13 @@ export const analytics = firebaseApp ? getAnalytics(firebaseApp) : undefined + +const getFirebaseFunction = () => { + const firebaseFunction = getFunctions(firebaseApp) + firebaseFunction.region = skeetCloudConfig.app.region + if (process.env.NODE_ENV !== 'production') { + connectFunctionsEmulator(firebaseFunction, platformDevIP, 5001) + } + return firebaseFunction +} +export const functions = firebaseApp ? getFirebaseFunction() : undefined diff --git a/webapp/src/lib/skeet/functions.ts b/webapp/src/lib/skeet/functions.ts index 9c3cafd..8935fb1 100644 --- a/webapp/src/lib/skeet/functions.ts +++ b/webapp/src/lib/skeet/functions.ts @@ -1,8 +1,9 @@ import skeetCloudConfig from '@root/skeet-cloud.config.json' import { toKebabCase } from '@/utils/character' -import { auth } from '@/lib/firebase' +import { auth, functions } from '@/lib/firebase' import { signOut } from 'firebase/auth' import { platformDevIP } from '@/lib/firebase' +import { httpsCallable, httpsCallableFromURL } from 'firebase/functions' export const fetchSkeetFunctions = async ( functionName: string, @@ -41,3 +42,32 @@ export const fetchSkeetFunctions = async ( } } } + +export const callSkeetFunctions = async ( + functionName: string, + methodName: string, + params: T, +) => { + try { + const callableFunction = + process.env.NODE_ENV === 'production' && + skeetCloudConfig.app.hasLoadBalancer + ? functions + ? httpsCallableFromURL( + functions, + `https://${ + skeetCloudConfig.app.lbDomain + }/${functionName}/${toKebabCase(methodName)}`, + ) + : undefined + : functions + ? httpsCallable(functions, methodName) + : undefined + + const res = await callableFunction?.(params) + return res + } catch (err: any) { + console.error(err) + throw new Error(err.message) + } +}