Skip to content

Commit d12b4c5

Browse files
authored
feat(functions): Allow configuration of Functions Emulator Origin (#2017)
* 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
1 parent 1c9ee1d commit d12b4c5

File tree

5 files changed

+74
-35
lines changed

5 files changed

+74
-35
lines changed

docs/functions/functions.md

+48-23
Original file line numberDiff line numberDiff line change
@@ -26,29 +26,6 @@ import { environment } from '../environments/environment';
2626
export class AppModule {}
2727
```
2828

29-
### Configure the Function region with the FunctionsRegionToken Injection Token
30-
31-
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`.
32-
33-
```ts
34-
import { NgModule } from '@angular/core';
35-
import { AngularFireFunctionsModule, FunctionsRegionToken } from '@angular/fire/functions';
36-
37-
@NgModule({
38-
imports: [
39-
...
40-
AngularFireFunctionsModule,
41-
...
42-
],
43-
...
44-
providers: [
45-
{ provide: FunctionsRegionToken, useValue: 'asia-northeast1' }
46-
]
47-
})
48-
export class AppModule {}
49-
50-
```
51-
5229
### Injecting the AngularFireFunctions service
5330

5431
Once the `AngularFireFunctionsModule` is registered you can inject the `AngularFireFunctions` service.
@@ -91,3 +68,51 @@ export class AppComponent {
9168
```
9269

9370
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.
71+
72+
## Configuration via Dependency Injection
73+
74+
### Functions Region
75+
76+
Allow configuration of the Function's region by adding `FUNCTIONS_REGION` to the `providers` section of your `NgModule`. The default is `us-central1`.
77+
78+
```ts
79+
import { NgModule } from '@angular/core';
80+
import { AngularFireFunctionsModule, FUNCTIONS_REGION } from '@angular/fire/functions';
81+
82+
@NgModule({
83+
imports: [
84+
...
85+
AngularFireFunctionsModule,
86+
...
87+
],
88+
...
89+
providers: [
90+
{ provide: FUNCTIONS_REGION, useValue: 'asia-northeast1' }
91+
]
92+
})
93+
export class AppModule {}
94+
95+
```
96+
97+
### Cloud Functions emulator
98+
99+
Point callable Functions to the Cloud Function emulator by adding `FUNCTIONS_ORIGIN` to the `providers` section of your `NgModule`.
100+
101+
```ts
102+
import { NgModule } from '@angular/core';
103+
import { AngularFireFunctionsModule, FUNCTIONS_ORIGIN } from '@angular/fire/functions';
104+
105+
@NgModule({
106+
imports: [
107+
...
108+
AngularFireFunctionsModule,
109+
...
110+
],
111+
...
112+
providers: [
113+
{ provide: FUNCTIONS_ORIGIN, useValue: 'http://localhost:5005' }
114+
]
115+
})
116+
export class AppModule {}
117+
118+
```

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"@angular/core": ">=6.0.0 <8",
3636
"@angular/platform-browser": ">=6.0.0 <8",
3737
"@angular/platform-browser-dynamic": ">=6.0.0 <8",
38-
"firebase": ">= 5.5.0 <7",
38+
"firebase": ">= 5.5.7 <7",
3939
"rxjs": "^6.0.0",
4040
"ws": "^3.3.2",
4141
"xhr2": "^0.1.4",

src/functions/functions.spec.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ReflectiveInjector, Provider } from '@angular/core';
22
import { TestBed, inject } from '@angular/core/testing';
33
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from '@angular/fire';
4-
import { AngularFireFunctions, AngularFireFunctionsModule, FunctionsRegionToken } from '@angular/fire/functions';
4+
import { AngularFireFunctions, AngularFireFunctionsModule, FUNCTIONS_REGION, FUNCTIONS_ORIGIN } from '@angular/fire/functions';
55
import { COMMON_CONFIG } from './test-config';
66

77
describe('AngularFireFunctions', () => {
@@ -51,7 +51,8 @@ describe('AngularFireFunctions with different app', () => {
5151
providers: [
5252
{ provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO },
5353
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG },
54-
{ provide: FunctionsRegionToken, useValue: 'asia-northeast1' },
54+
{ provide: FUNCTIONS_ORIGIN, useValue: 'http://0.0.0.0:9999' },
55+
{ provide: FUNCTIONS_REGION, useValue: 'asia-northeast1' }
5556
]
5657
});
5758
inject([FirebaseApp, AngularFireFunctions], (app_: FirebaseApp, _fns: AngularFireFunctions) => {

src/functions/functions.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { map } from 'rxjs/operators';
44
import { FirebaseOptions, FirebaseAppConfig } from '@angular/fire';
55
import { FirebaseFunctions, FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from '@angular/fire';
66

7+
// SEMVER: @ v6 remove FunctionsRegionToken in favor of FUNCTIONS_REGION
78
export const FunctionsRegionToken = new InjectionToken<string>('angularfire2.functions.region');
9+
export const FUNCTIONS_ORIGIN = new InjectionToken<string>('angularfire2.functions.origin');
10+
export const FUNCTIONS_REGION = FunctionsRegionToken;
811

912
@Injectable()
1013
export class AngularFireFunctions {
@@ -21,7 +24,8 @@ export class AngularFireFunctions {
2124
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|null|undefined,
2225
@Inject(PLATFORM_ID) platformId: Object,
2326
zone: NgZone,
24-
@Optional() @Inject(FunctionsRegionToken) region:string|null
27+
@Optional() @Inject(FUNCTIONS_REGION) region:string|null,
28+
@Optional() @Inject(FUNCTIONS_ORIGIN) origin:string|null
2529
) {
2630
this.scheduler = new FirebaseZoneScheduler(zone, platformId);
2731

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

37+
if (origin) {
38+
this.functions.useFunctionsEmulator(origin);
39+
}
40+
3341
}
3442

3543
public httpsCallable<T=any, R=any>(name: string) {

yarn.lock

+13-8
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ dateformat@^3.0.0:
17461746
resolved "https://registry.yarnpkg.com/dateformat/-/dateformat-3.0.3.tgz#a6e37499a4d9a9cf85ef5872044d62901c9889ae"
17471747
integrity sha512-jyCETtSl3VMZMWeRo7iY1FL19ges1t55hMo5yaam4Jrsm5EPL89UQkoQRyiI+Yf4k8r2ZpdngkV8hr1lIdjb3Q==
17481748

1749-
debug@2, [email protected], 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:
1749+
debug@2, [email protected], 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:
17501750
version "2.6.9"
17511751
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
17521752
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
@@ -1760,7 +1760,7 @@ [email protected], debug@~3.1.0:
17601760
dependencies:
17611761
ms "2.0.0"
17621762

1763-
debug@4, debug@^4.1.0:
1763+
debug@4:
17641764
version "4.1.1"
17651765
resolved "https://registry.yarnpkg.com/debug/-/debug-4.1.1.tgz#3b72260255109c6b589cee050f1d516139664791"
17661766
integrity sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==
@@ -2411,7 +2411,7 @@ fined@^1.0.1:
24112411
object.pick "^1.2.0"
24122412
parse-filepath "^1.0.1"
24132413

2414-
"firebase@>= 5.5.0 <7":
2414+
"firebase@>= 5.5.7 <7":
24152415
version "6.0.2"
24162416
resolved "https://registry.yarnpkg.com/firebase/-/firebase-6.0.2.tgz#a2d1daa9a3f329aac2974349d521fbd923ee0f35"
24172417
integrity sha512-KA4VviZQJCzCIkCEvt3sJEsNe/HpqQdNE+ajy3wELHCxNV8PK8eBa10qxWDQhNgEtorHHltgYw3VwS0rbSvWrQ==
@@ -4523,11 +4523,16 @@ multipipe@^0.1.2:
45234523
dependencies:
45244524
duplexer2 "0.0.2"
45254525

4526-
nan@^2.0.5, nan@^2.12.1, nan@^2.13.2:
4526+
nan@^2.0.5, nan@^2.12.1:
45274527
version "2.13.2"
45284528
resolved "https://registry.yarnpkg.com/nan/-/nan-2.13.2.tgz#f51dc7ae66ba7d5d55e1e6d4d8092e802c9aefe7"
45294529
integrity sha512-TghvYc72wlMGMVMluVo9WRJc0mB8KxxF/gZ4YYFy7V2ZQX9l7rgbPg7vjS9mt6U5HXODVFVI2bOduCzwOMv/lw==
45304530

4531+
nan@^2.13.2:
4532+
version "2.14.0"
4533+
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
4534+
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==
4535+
45314536
nan@~2.10.0:
45324537
version "2.10.0"
45334538
resolved "https://registry.yarnpkg.com/nan/-/nan-2.10.0.tgz#96d0cd610ebd58d4b4de9cc0c6828cda99c7548f"
@@ -4566,11 +4571,11 @@ ncp@^2.0.0:
45664571
integrity sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=
45674572

45684573
needle@^2.2.1:
4569-
version "2.3.1"
4570-
resolved "https://registry.yarnpkg.com/needle/-/needle-2.3.1.tgz#d272f2f4034afb9c4c9ab1379aabc17fc85c9388"
4571-
integrity sha512-CaLXV3W8Vnbps8ZANqDGz7j4x7Yj1LW4TWF/TQuDfj7Cfx4nAPTvw98qgTevtto1oHDrh3pQkaODbqupXlsWTg==
4574+
version "2.2.4"
4575+
resolved "https://registry.yarnpkg.com/needle/-/needle-2.2.4.tgz#51931bff82533b1928b7d1d69e01f1b00ffd2a4e"
4576+
integrity sha512-HyoqEb4wr/rsoaIDfTH2aVL9nWtQqba2/HvMv+++m8u0dz808MaagKILxtfeSN7QU7nvbQ79zk3vYOJp9zsNEA==
45724577
dependencies:
4573-
debug "^4.1.0"
4578+
debug "^2.1.2"
45744579
iconv-lite "^0.4.4"
45754580
sax "^1.2.4"
45764581

0 commit comments

Comments
 (0)