Skip to content

Commit

Permalink
feat(functions): Allow configuration of Functions Emulator Origin (#2017
Browse files Browse the repository at this point in the history
)

* Allow use of Cloud Functions emulator via FUNCTIONS_ORIGIN DI token
* Rearranged DI docs and added emulator origin
* Export FUNCTIONS_REGION as an alternative to FunctionsRegionToken
  • Loading branch information
jamesdaniels authored May 20, 2019
1 parent 1c9ee1d commit d12b4c5
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 35 deletions.
71 changes: 48 additions & 23 deletions docs/functions/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {}

```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/functions/functions.spec.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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) => {
Expand Down
10 changes: 9 additions & 1 deletion src/functions/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>('angularfire2.functions.region');
export const FUNCTIONS_ORIGIN = new InjectionToken<string>('angularfire2.functions.origin');
export const FUNCTIONS_REGION = FunctionsRegionToken;

@Injectable()
export class AngularFireFunctions {
Expand All @@ -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);

Expand All @@ -30,6 +34,10 @@ export class AngularFireFunctions {
return app.functions(region || undefined);
});

if (origin) {
this.functions.useFunctionsEmulator(origin);
}

}

public httpsCallable<T=any, R=any>(name: string) {
Expand Down
21 changes: 13 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand All @@ -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==
Expand Down Expand Up @@ -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==
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit d12b4c5

Please sign in to comment.