Skip to content

Commit

Permalink
Merge pull request #52 from elsoul/fb/callable
Browse files Browse the repository at this point in the history
@euledge/add-https-callable-function-client
  • Loading branch information
KishiTheMechanic authored Jan 6, 2024
2 parents 4e4030e + fd341c5 commit b2a63e1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 5 deletions.
12 changes: 12 additions & 0 deletions src/lib/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
32 changes: 31 additions & 1 deletion src/lib/skeet/functions.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(
functionName: string,
Expand Down Expand Up @@ -40,3 +41,32 @@ export const fetchSkeetFunctions = async <T>(
}
}
}

export const callSkeetFunctions = async <T>(
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)
}
}
15 changes: 12 additions & 3 deletions webapp/src/lib/firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
32 changes: 31 additions & 1 deletion webapp/src/lib/skeet/functions.ts
Original file line number Diff line number Diff line change
@@ -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 <T>(
functionName: string,
Expand Down Expand Up @@ -41,3 +42,32 @@ export const fetchSkeetFunctions = async <T>(
}
}
}

export const callSkeetFunctions = async <T>(
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)
}
}

0 comments on commit b2a63e1

Please sign in to comment.