-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): Adding AngularFireFunctions with httpCallable (#1532)
- Loading branch information
1 parent
92f77dc
commit 26f3f5f
Showing
18 changed files
with
260 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { AngularFireFunctions } from './functions'; | ||
import '@firebase/functions' | ||
|
||
@NgModule({ | ||
providers: [ AngularFireFunctions ] | ||
}) | ||
export class AngularFireFunctionsModule { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { ReflectiveInjector, Provider } from '@angular/core'; | ||
import { TestBed, inject } from '@angular/core/testing'; | ||
import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from 'angularfire2'; | ||
import { AngularFireFunctions, AngularFireFunctionsModule } from 'angularfire2/functions'; | ||
import { COMMON_CONFIG } from './test-config'; | ||
|
||
describe('AngularFireFunctions', () => { | ||
let app: FirebaseApp; | ||
let afFns: AngularFireFunctions; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
AngularFireModule.initializeApp(COMMON_CONFIG), | ||
AngularFireFunctionsModule | ||
] | ||
}); | ||
inject([FirebaseApp, AngularFireFunctions], (app_: FirebaseApp, _fn: AngularFireFunctions) => { | ||
app = app_; | ||
afFns = _fn; | ||
})(); | ||
}); | ||
|
||
afterEach(done => { | ||
app.delete(); | ||
done(); | ||
}); | ||
|
||
it('should be exist', () => { | ||
expect(afFns instanceof AngularFireFunctions).toBe(true); | ||
}); | ||
|
||
it('should have the Firebase Functions instance', () => { | ||
expect(afFns.functions).toBeDefined(); | ||
}); | ||
|
||
}); | ||
|
||
const FIREBASE_APP_NAME_TOO = (Math.random() + 1).toString(36).substring(7); | ||
|
||
describe('AngularFireFunctions with different app', () => { | ||
let app: FirebaseApp; | ||
let afFns: AngularFireFunctions; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
AngularFireModule.initializeApp(COMMON_CONFIG), | ||
AngularFireFunctionsModule | ||
], | ||
providers: [ | ||
{ provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO }, | ||
{ provide: FirebaseOptionsToken, useValue: COMMON_CONFIG } | ||
] | ||
}); | ||
inject([FirebaseApp, AngularFireFunctions], (app_: FirebaseApp, _fns: AngularFireFunctions) => { | ||
app = app_; | ||
afFns = _fns; | ||
})(); | ||
}); | ||
|
||
afterEach(done => { | ||
app.delete(); | ||
done(); | ||
}); | ||
|
||
describe('<constructor>', () => { | ||
|
||
it('should be an AngularFireAuth type', () => { | ||
expect(afFns instanceof AngularFireFunctions).toEqual(true); | ||
}); | ||
|
||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { FirebaseFunctions } from '@firebase/functions-types'; | ||
import { FirebaseOptions, FirebaseAppConfig } from '@firebase/app-types'; | ||
import { Injectable, Inject, Optional, NgZone, PLATFORM_ID } from '@angular/core'; | ||
import { Observable, from } from 'rxjs'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
import { FirebaseOptionsToken, FirebaseNameOrConfigToken, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2'; | ||
|
||
@Injectable() | ||
export class AngularFireFunctions { | ||
|
||
/** | ||
* Firebase Functions instance | ||
*/ | ||
public readonly functions: FirebaseFunctions; | ||
|
||
public readonly scheduler: FirebaseZoneScheduler; | ||
|
||
constructor( | ||
@Inject(FirebaseOptionsToken) options:FirebaseOptions, | ||
@Optional() @Inject(FirebaseNameOrConfigToken) nameOrConfig:string|FirebaseAppConfig|undefined, | ||
@Inject(PLATFORM_ID) platformId: Object, | ||
zone: NgZone | ||
) { | ||
this.scheduler = new FirebaseZoneScheduler(zone, platformId); | ||
|
||
this.functions = zone.runOutsideAngular(() => { | ||
const app = _firebaseAppFactory(options, nameOrConfig); | ||
return app.functions(); | ||
}); | ||
|
||
} | ||
|
||
public httpsCallable<T=any, R=any>(name: string) { | ||
const callable = this.functions.httpsCallable(name); | ||
return (data: T) => { | ||
const callable$ = from(callable(data)); | ||
return this.scheduler.runOutsideAngular( | ||
callable$.pipe( | ||
map(r => r.data as R) | ||
) | ||
) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import './functions.spec'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './public_api'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "angularfire2/functions", | ||
"version": "ANGULARFIRE2_VERSION", | ||
"description": "The functions module", | ||
"main": "../bundles/functions.umd.js", | ||
"module": "index.js", | ||
"es2015": "./es2015/index.js", | ||
"keywords": [ | ||
"angular", | ||
"firebase", | ||
"rxjs" | ||
], | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/angular/angularfire2.git" | ||
}, | ||
"author": "angular,firebase", | ||
"license": "MIT", | ||
"peerDependencies": { | ||
"angularfire2": "ANGULARFIRE2_VERSION", | ||
"@angular/common": "ANGULAR_VERSION", | ||
"@angular/core": "ANGULAR_VERSION", | ||
"@angular/platform-browser": "ANGULAR_VERSION", | ||
"@angular/platform-browser-dynamic": "ANGULAR_VERSION", | ||
"@firebase/app": "FIREBASE_APP_VERSION", | ||
"@firebase/functions": "FIREBASE_FUNCTIONS_VERSION", | ||
"rxjs": "RXJS_VERSION", | ||
"zone.js": "ZONEJS_VERSION" | ||
}, | ||
"typings": "index.d.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './functions'; | ||
export * from './functions.module'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
export const COMMON_CONFIG = { | ||
apiKey: "AIzaSyBVSy3YpkVGiKXbbxeK0qBnu3-MNZ9UIjA", | ||
authDomain: "angularfire2-test.firebaseapp.com", | ||
databaseURL: "https://angularfire2-test.firebaseio.com", | ||
storageBucket: "angularfire2-test.appspot.com", | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"experimentalDecorators": true, | ||
"emitDecoratorMetadata": true, | ||
"module": "es2015", | ||
"target": "es2015", | ||
"noImplicitAny": false, | ||
"outDir": "../../dist/packages-dist/functions/es2015", | ||
"rootDir": ".", | ||
"sourceMap": true, | ||
"inlineSources": true, | ||
"declaration": false, | ||
"removeComments": true, | ||
"strictNullChecks": true, | ||
"lib": ["es2015", "dom", "es2015.promise", "es2015.collection", "es2015.iterable"], | ||
"skipLibCheck": true, | ||
"moduleResolution": "node", | ||
"paths": { | ||
"angularfire2": ["../../dist/packages-dist"] | ||
} | ||
}, | ||
"files": [ | ||
"index.ts", | ||
"../../node_modules/zone.js/dist/zone.js.d.ts" | ||
], | ||
"angularCompilerOptions": { | ||
"skipTemplateCodegen": true, | ||
"strictMetadataEmit": true, | ||
"enableSummariesForJit": false | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "./tsconfig-build.json", | ||
"compilerOptions": { | ||
"target": "es5", | ||
"outDir": "../../dist/packages-dist/functions", | ||
"declaration": true | ||
}, | ||
"files": [ | ||
"public_api.ts", | ||
"../../node_modules/zone.js/dist/zone.js.d.ts" | ||
], | ||
"angularCompilerOptions": { | ||
"skipTemplateCodegen": true, | ||
"strictMetadataEmit": true, | ||
"enableSummariesForJit": false, | ||
"flatModuleOutFile": "index.js", | ||
"flatModuleId": "angularfire2/functions" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"extends": "./tsconfig-esm.json", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"angularfire2": ["../../dist/packages-dist"], | ||
"angularfire2/functions": ["../../dist/packages-dist/functions"] | ||
} | ||
}, | ||
"files": [ | ||
"index.spec.ts", | ||
"../../node_modules/zone.js/dist/zone.js.d.ts" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.