diff --git a/docs/functions/functions.md b/docs/functions/functions.md index 347fbb0cc..646dd028f 100644 --- a/docs/functions/functions.md +++ b/docs/functions/functions.md @@ -26,29 +26,6 @@ import { environment } from '../environments/environment'; export class AppModule {} ``` -### Configure the Function region with the FunctionsRegionToken Injection Token - -Allow configuration of Function region with the `FunctionsRegionToken` Injection Token by adding it to the `providers` section of your `NgModule`. The default is `us-central1`. - -```ts -import { NgModule } from '@angular/core'; -import { AngularFireFunctionsModule, FunctionsRegionToken } from '@angular/fire/functions'; - -@NgModule({ - imports: [ - ... - AngularFireFunctionsModule, - ... - ], - ... - providers: [ - { provide: FunctionsRegionToken, useValue: 'asia-northeast1' } - ] -}) -export class AppModule {} - -``` - ### Injecting the AngularFireFunctions service Once the `AngularFireFunctionsModule` is registered you can inject the `AngularFireFunctions` service. @@ -91,3 +68,51 @@ export class AppComponent { ``` Notice that calling `httpsCallable()` does not initiate the request. It creates a function, which when called creates an Observable, subscribe or convert it to a Promise to initiate the request. + +## Configuration via Dependency Injection + +### Functions Region + +Allow configuration of the Function's region by adding `FUNCTIONS_REGION` to the `providers` section of your `NgModule`. The default is `us-central1`. + +```ts +import { NgModule } from '@angular/core'; +import { AngularFireFunctionsModule, FUNCTIONS_REGION } from '@angular/fire/functions'; + +@NgModule({ + imports: [ + ... + AngularFireFunctionsModule, + ... + ], + ... + providers: [ + { provide: FUNCTIONS_REGION, useValue: 'asia-northeast1' } + ] +}) +export class AppModule {} + +``` + +### Cloud Functions emulator + +Point callable Functions to the Cloud Function emulator by adding `FUNCTIONS_ORIGIN` to the `providers` section of your `NgModule`. + +```ts +import { NgModule } from '@angular/core'; +import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/functions'; + +@NgModule({ + imports: [ + ... + AngularFireFunctionsModule, + ... + ], + ... + providers: [ + { provide: FUNCTIONS_ORIGIN, useValue: 'http://localhost:5005' } + ] +}) +export class AppModule {} + +``` \ No newline at end of file diff --git a/package.json b/package.json index 9bed920e5..e2be3426c 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@angular/core": ">=6.0.0 <8", "@angular/platform-browser": ">=6.0.0 <8", "@angular/platform-browser-dynamic": ">=6.0.0 <8", - "firebase": ">= 5.5.0 <7", + "firebase": ">= 5.5.7 <7", "rxjs": "^6.0.0", "ws": "^3.3.2", "xhr2": "^0.1.4", diff --git a/src/functions/functions.spec.ts b/src/functions/functions.spec.ts index 1708b1cb9..40b3b3a58 100644 --- a/src/functions/functions.spec.ts +++ b/src/functions/functions.spec.ts @@ -1,7 +1,7 @@ import { ReflectiveInjector, Provider } from '@angular/core'; import { TestBed, inject } from '@angular/core/testing'; import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from '@angular/fire'; -import { AngularFireFunctions, AngularFireFunctionsModule, FunctionsRegionToken } from '@angular/fire/functions'; +import { AngularFireFunctions, AngularFireFunctionsModule, FUNCTIONS_REGION, FUNCTIONS_ORIGIN } from '@angular/fire/functions'; import { COMMON_CONFIG } from './test-config'; describe('AngularFireFunctions', () => { @@ -51,7 +51,8 @@ describe('AngularFireFunctions with different app', () => { providers: [ { provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO }, { provide: FirebaseOptionsToken, useValue: COMMON_CONFIG }, - { provide: FunctionsRegionToken, useValue: 'asia-northeast1' }, + { provide: FUNCTIONS_ORIGIN, useValue: 'http://0.0.0.0:9999' }, + { provide: FUNCTIONS_REGION, useValue: 'asia-northeast1' } ] }); inject([FirebaseApp, AngularFireFunctions], (app_: FirebaseApp, _fns: AngularFireFunctions) => { diff --git a/src/functions/functions.ts b/src/functions/functions.ts index 55a18c6a1..99c7c3813 100644 --- a/src/functions/functions.ts +++ b/src/functions/functions.ts @@ -4,7 +4,10 @@ import { map } from 'rxjs/operators'; import { FirebaseOptions, FirebaseAppConfig } from '@angular/fire'; import { FirebaseFunctions, FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from '@angular/fire'; +// SEMVER: @ v6 remove FunctionsRegionToken in favor of FUNCTIONS_REGION export const FunctionsRegionToken = new InjectionToken('angularfire2.functions.region'); +export const FUNCTIONS_ORIGIN = new InjectionToken('angularfire2.functions.origin'); +export const FUNCTIONS_REGION = FunctionsRegionToken; @Injectable() export class AngularFireFunctions { @@ -21,7 +24,8 @@ export class AngularFireFunctions { @Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|null|undefined, @Inject(PLATFORM_ID) platformId: Object, zone: NgZone, - @Optional() @Inject(FunctionsRegionToken) region:string|null + @Optional() @Inject(FUNCTIONS_REGION) region:string|null, + @Optional() @Inject(FUNCTIONS_ORIGIN) origin:string|null ) { this.scheduler = new FirebaseZoneScheduler(zone, platformId); @@ -30,6 +34,10 @@ export class AngularFireFunctions { return app.functions(region || undefined); }); + if (origin) { + this.functions.useFunctionsEmulator(origin); + } + } public httpsCallable(name: string) { diff --git a/yarn.lock b/yarn.lock index 5b494b826..bb3f1788f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1746,7 +1746,7 @@ dateformat@^3.0.0: resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae" integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q== -debug@2, debug@2.6.9, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9, debug@~2.6.4, debug@~2.6.6, debug@~2.6.9: +debug@2, debug@2.6.9, debug@^2.1.2, debug@^2.2.0, debug@^2.3.3, debug@^2.6.8, debug@^2.6.9, debug@~2.6.4, debug@~2.6.6, debug@~2.6.9: version "2.6.9" resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f" integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA== @@ -1760,7 +1760,7 @@ debug@3.1.0, debug@~3.1.0: dependencies: ms "2.0.0" -debug@4, debug@^4.1.0: +debug@4: version "4.1.1" resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791" integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw== @@ -2411,7 +2411,7 @@ fined@^1.0.1: object.pick "^1.2.0" parse-filepath "^1.0.1" -"firebase@>= 5.5.0 <7": +"firebase@>= 5.5.7 <7": version "6.0.2" resolved "https://registry.yarnpkg.com/firebase/-/firebase-6.0.2.tgz#a2d1daa9a3f329aac2974349d521fbd923ee0f35" integrity sha512-KA4VviZQJCzCIkCEvt3sJEsNe/HpqQdNE+ajy3wELHCxNV8PK8eBa10qxWDQhNgEtorHHltgYw3VwS0rbSvWrQ== @@ -4523,11 +4523,16 @@ multipipe@^0.1.2: dependencies: duplexer2 "0.0.2" -nan@^2.0.5, nan@^2.12.1, nan@^2.13.2: +nan@^2.0.5, nan@^2.12.1: version "2.13.2" resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7" integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw== +nan@^2.13.2: + version "2.14.0" + resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c" + integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg== + nan@~2.10.0: version "2.10.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f" @@ -4566,11 +4571,11 @@ ncp@^2.0.0: integrity sha1-GVoh1sRuNh0vsSgbo4uR6d9727M= needle@^2.2.1: - version "2.3.1" - resolved "https://registry.yarnpkg.com/needle/-/needle-2.3.1.tgz#d272f2f4034afb9c4c9ab1379aabc17fc85c9388" - integrity sha512-CaLXV3W8Vnbps8ZANqDGz7j4x7Yj1LW4TWF/TQuDfj7Cfx4nAPTvw98qgTevtto1oHDrh3pQkaODbqupXlsWTg== + version "2.2.4" + resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e" + integrity sha512-HyoqEb4wr/rsoaIDfTH2aVL9nWtQqba2/HvMv+++m8u0dz808MaagKILxtfeSN7QU7nvbQ79zk3vYOJp9zsNEA== dependencies: - debug "^4.1.0" + debug "^2.1.2" iconv-lite "^0.4.4" sax "^1.2.4"