import { Inject , Injectable , InjectionToken , NgZone , Optional } from '@angular/core' ; import { from , Observable , of } from 'rxjs' ; import { map , observeOn , shareReplay , switchMap } from 'rxjs/operators' ; import { ɵAngularFireSchedulers } from '@angular/fire' ; import { ɵlazySDKProxy , ɵPromiseProxy , ɵapplyMixins } from '@angular/fire/compat' ; import { FirebaseOptions } from 'firebase/app' ; import { ɵfirebaseAppFactory , FIREBASE_APP_NAME , FIREBASE_OPTIONS , ɵcacheInstance } from '@angular/fire/compat' ; import firebase from 'firebase/compat/app' ; import { proxyPolyfillCompat } from './base' ; import { HttpsCallableOptions } from '@firebase/functions-types' ; import { AppCheckInstances } from '@angular/fire/app-check' ; export const ORIGIN = new InjectionToken < string > ( 'angularfire2.functions.origin' ) ; export const REGION = new InjectionToken < string > ( 'angularfire2.functions.region' ) ; type UseEmulatorArguments = Parameters < firebase . functions . Functions [ 'useEmulator' ] > ; export const USE_EMULATOR = new InjectionToken < UseEmulatorArguments > ( 'angularfire2.functions.use-emulator' ) ; export interface AngularFireFunctions extends Omit < ɵPromiseProxy < firebase . functions . Functions > , 'httpsCallable' > { } @ Injectable ( { providedIn : 'any' } ) export class AngularFireFunctions { public readonly httpsCallable : < T = any , R = any > ( name : string , options ? : HttpsCallableOptions ) => ( data : T ) => Observable < R > ; constructor ( @ Inject ( FIREBASE_OPTIONS ) options , @ Optional ( ) @ Inject ( FIREBASE_APP_NAME ) name , zone , schedulers , @ Optional ( ) @ Inject ( REGION ) region , @ Optional ( ) @ Inject ( ORIGIN ) origin , @ Optional ( ) @ Inject ( USE_EMULATOR ) _useEmulator , @ Optional ( ) _appCheckInstances ) { const useEmulator = _useEmulator ; const functions = of ( undefined ) . pipe ( observeOn ( schedulers . outsideAngular ) , switchMap ( ( ) => import ( 'firebase/compat/functions' ) ) , map ( ( ) => ɵfirebaseAppFactory ( options , zone , name ) ) , map ( app => ɵcacheInstance ( `${ app . name }.functions.${ region || origin }` , 'AngularFireFunctions' , app . name , ( ) => { let functions ; if ( region && origin ) { throw new Error ( 'REGION and ORIGIN can\'t be used at the same time.' ) ; } functions = app . functions ( region || origin || undefined ) ; if ( useEmulator ) { functions . useEmulator ( ... useEmulator ) ; } return functions ; } , [ region , origin , useEmulator ] ) ) , shareReplay ( { bufferSize : 1 , refCount : false } ) ) ; this . httpsCallable = < T = any , R = any > ( name , options ? ) => ( data ) => from ( functions ) . pipe ( observeOn ( schedulers . insideAngular ) , switchMap ( functions => functions . httpsCallable ( name , options ) ( data ) ) , map ( r => r . data as R ) ) ; return ɵlazySDKProxy ( this , functions , zone ) ; } } ɵapplyMixins ( AngularFireFunctions , [ proxyPolyfillCompat ] ) ;