Skip to content

Commit

Permalink
feat(functions): Adding AngularFireFunctions with httpCallable (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdaniels authored May 16, 2018
1 parent 92f77dc commit 26f3f5f
Show file tree
Hide file tree
Showing 18 changed files with 260 additions and 3 deletions.
1 change: 1 addition & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ module.exports = function(config) {
'dist/packages-dist/bundles/auth.umd.{js,map}',
'dist/packages-dist/bundles/database.umd.{js,map}',
'dist/packages-dist/bundles/firestore.umd.{js,map}',
'dist/packages-dist/bundles/functions.umd.{js,map}',
'dist/packages-dist/bundles/storage.umd.{js,map}',
'dist/packages-dist/bundles/database-deprecated.umd.{js,map}',
'dist/packages-dist/bundles/test.umd.{js,map}',
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
"@firebase/database-types": "^0.3.1",
"@firebase/firestore": "^0.5.2",
"@firebase/firestore-types": "^0.4.1",
"@firebase/functions": "^0.2.2",
"@firebase/functions-types": "^0.1.2",
"@firebase/messaging-types": "^0.2.2",
"@firebase/storage": "^0.2.2",
"@firebase/storage-types": "^0.2.2",
Expand Down
2 changes: 2 additions & 0 deletions src/core/firebase.app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { FirebaseDatabase } from '@firebase/database-types';
import { FirebaseMessaging } from '@firebase/messaging-types';
import { FirebaseStorage } from '@firebase/storage-types';
import { FirebaseFirestore } from '@firebase/firestore-types';
import { FirebaseFunctions } from '@firebase/functions-types';

export class FirebaseApp implements _FirebaseApp {
name: string;
Expand All @@ -20,6 +21,7 @@ export class FirebaseApp implements _FirebaseApp {
storage: (storageBucket?: string) => FirebaseStorage;
delete: () => Promise<void>;
firestore: () => FirebaseFirestore;
functions: () => FirebaseFunctions;
}

export function _firebaseAppFactory(options: FirebaseOptions, nameOrConfig?: string | FirebaseAppConfig) {
Expand Down
8 changes: 8 additions & 0 deletions src/functions/functions.module.ts
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 { }
75 changes: 75 additions & 0 deletions src/functions/functions.spec.ts
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);
});

});

});
46 changes: 46 additions & 0 deletions src/functions/functions.ts
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)
)
)
}
}

}
1 change: 1 addition & 0 deletions src/functions/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './functions.spec';
1 change: 1 addition & 0 deletions src/functions/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './public_api';
31 changes: 31 additions & 0 deletions src/functions/package.json
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"
}
2 changes: 2 additions & 0 deletions src/functions/public_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './functions';
export * from './functions.module';
7 changes: 7 additions & 0 deletions src/functions/test-config.ts
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",
};
33 changes: 33 additions & 0 deletions src/functions/tsconfig-build.json
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
}
}

19 changes: 19 additions & 0 deletions src/functions/tsconfig-esm.json
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"
}
}
14 changes: 14 additions & 0 deletions src/functions/tsconfig-test.json
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"
]
}
1 change: 1 addition & 0 deletions src/root.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './packages-dist/auth/auth.spec';
export * from './packages-dist/firestore/firestore.spec';
export * from './packages-dist/firestore/document/document.spec';
export * from './packages-dist/firestore/collection/collection.spec';
export * from './packages-dist/functions/functions.spec';
export * from './packages-dist/database/database.spec';
export * from './packages-dist/database/utils.spec';
export * from './packages-dist/database/observable/fromRef.spec';
Expand Down
1 change: 1 addition & 0 deletions src/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"angularfire2/auth": ["./auth"],
"angularfire2/database": ["./database"],
"angularfire2/firestore": ["./firestore"],
"angularfire2/functions": ["./functions"],
"angularfire2/storage": ["./storage"],
"angularfire2/database-deprecated": ["./database-deprecated"]
},
Expand Down
Loading

0 comments on commit 26f3f5f

Please sign in to comment.