Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(functions): Adding AngularFireFunctions with httpCallable #1532

Merged
merged 10 commits into from
May 16, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@
"@firebase/database-types": "^0.1.1",
"@firebase/firestore": "^0.2.3",
"@firebase/firestore-types": "^0.1.1",
"@firebase/functions": "^0.1.0",
"@firebase/functions-types": "^0.1.0",
"@firebase/messaging-types": "^0.1.1",
"@firebase/storage": "^0.1.6",
"@firebase/storage-types": "^0.1.1",
"bufferutil": "^3.0.3",
"firebase": "^4.8.2",
"firebase": "^4.11.0",
"rxjs": "^5.5.4",
"utf-8-validate": "^4.0.0",
"ws": "^3.3.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 @@ -19,6 +20,7 @@ export class FirebaseApp implements _FirebaseApp {
storage: (storageBucket?: string) => FirebaseStorage;
delete: () => Promise<void>;
firestore: () => FirebaseFirestore;
functions: () => FirebaseFunctions;
}

export function _firebaseAppFactory(config: FirebaseOptions, name?: string): FirebaseApp {
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 { }
79 changes: 79 additions & 0 deletions src/functions/functions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { ReflectiveInjector, Provider } from '@angular/core';
import { Observable } from 'rxjs/Observable'
import { Subject } from 'rxjs/Subject'
import { Observer } from 'rxjs/Observer';
import { TestBed, inject } from '@angular/core/testing';
import { _do } from 'rxjs/operator/do';
import { take } from 'rxjs/operator/take';
import { skip } from 'rxjs/operator/skip';
import { FirebaseApp, FirebaseAppConfig, AngularFireModule, FirebaseAppName } 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().then(done, done.fail);
});

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: FirebaseAppName, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FirebaseAppConfig, useValue: COMMON_CONFIG }
]
});
inject([FirebaseApp, AngularFireFunctions], (app_: FirebaseApp, _fns: AngularFireFunctions) => {
app = app_;
afFns = _fns;
})();
});

afterEach(done => {
app.delete().then(done, done.fail);
});

describe('<constructor>', () => {

it('should be an AngularFireAuth type', () => {
expect(afFns instanceof AngularFireFunctions).toEqual(true);
});

});

});
45 changes: 45 additions & 0 deletions src/functions/functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { FirebaseFunctions, HttpsCallableResult } from '@firebase/functions-types';
import { FirebaseOptions } from '@firebase/app-types';
import { Injectable, Inject, Optional, NgZone } from '@angular/core';
import { Observable } from 'rxjs/Observable';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can now just do

import { Observable } from 'rxjs';


import { FirebaseAppConfig, FirebaseAppName, _firebaseAppFactory, FirebaseZoneScheduler } from 'angularfire2';

import 'rxjs/add/observable/fromPromise';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to use pipeable operators.

import 'rxjs/add/operator/map'

@Injectable()
export class AngularFireFunctions {

/**
* Firebase Functions instance
*/
public readonly functions: FirebaseFunctions;

public readonly scheduler: FirebaseZoneScheduler;

public httpsCallable<T, R>(name: string) {
const callable = this.functions.httpsCallable(name);
return (data: T) => {
return this.scheduler.runOutsideAngular(
Observable.fromPromise(callable(data))
Copy link
Member

@davideast davideast Mar 30, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use import { from } from 'rxjs/observable/from';. It also works with promises. Then use the rxjs/operators specifier to import the pipeable operators.

import { from } from 'rxjs/observable/from';
import { map } from 'rxjs/operators';

from(promise).pipe(
  map(() => {})
);

.map(r => r.data as R)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davideast what do you think about mapping .data here? This is the only thing I'm on the fence about. It's nice ergo buuut what if the CF3 team decides to pass more than .data back at some point? That would force an API break.

)
}
}

constructor(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. Bring the constructor above the method above.

@Inject(FirebaseAppConfig) config:FirebaseOptions,
@Optional() @Inject(FirebaseAppName) name:string,
private zone: NgZone
) {
this.scheduler = new FirebaseZoneScheduler(zone);

this.functions = zone.runOutsideAngular(() => {
const app = _firebaseAppFactory(config, name);
return app.functions();
});

}

}
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