Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit ff95c77

Browse files
Merge pull request #917 from breningham/implement-callable-functions
Ready For Testing - Implement callable functions
2 parents 5a7414a + 9e513b8 commit ff95c77

17 files changed

+241
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ src/platforms/android/include.gradle
2323
!src/platforms/web/typings/firebase-webapi.d.ts
2424
!src/platforms/ios/typings/*.d.ts
2525
!src/platforms/android/typings/**/*.d.ts
26+
!src/functions/functions.d.ts
2627
!src/scripts/*.js
2728
!demo/karma.conf.js
2829
demo/*.d.ts

demo/app/main-page.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<TabViewItem.view>
1919
<ScrollView>
2020
<GridLayout columns="*, *"
21-
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
21+
rows="auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto, auto"
2222
horizontalAlignment="stretch"
2323
class="tab-content">
2424
<Button row="0" colSpan="2" text="init firebase - do this first" tap="{{ doWebInit }}" class="button button-positive"/>
@@ -72,6 +72,10 @@
7272

7373
<Label row="19" colSpan="2" text="{{ storageFeedback }}" class="message" textWrap="true"/>
7474

75+
<Label row="20" colSpan="2" text="Callable Functions" class="subtitle" />
76+
77+
<Button row="21" col="0" text="callable 1" tap="{{ doCallableFunction }}" class="button button-functions" />
78+
7579
</GridLayout>
7680
</ScrollView>
7781
</TabViewItem.view>

demo/app/main-view-model.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { MessagingViewModel } from './messaging-view-model';
88

99
const firebaseWebApi = require("nativescript-plugin-firebase/app");
1010

11+
1112
declare const Crashlytics: any;
1213

1314
export class HelloWorldModel extends Observable {
@@ -195,6 +196,25 @@ export class HelloWorldModel extends Observable {
195196
}
196197
}
197198

199+
public doCallableFunction(): void {
200+
const fn = firebaseWebApi.functions().httpsCallable('helloName');
201+
202+
fn( 'Nativescript-Plugin-Firebase!' ).then((DataCue) => {
203+
alert({
204+
title: "Callable Function Result",
205+
message: DataCue.message,
206+
okButtonText: "Nice!"
207+
});
208+
})
209+
.catch((errorMessage) => {
210+
alert({
211+
title: "An Error Occurred",
212+
message: errorMessage,
213+
okButtonText: "OK, thanks"
214+
});
215+
});
216+
}
217+
198218
public doWebAddValueEventListenerForCompanies(): void {
199219
const path = "/companies";
200220
const onValueEvent = result => {

demo/firebase.nativescript.json

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
{
2-
"using_ios": true,
3-
"using_android": true,
4-
"firestore": false,
5-
"realtimedb": true,
6-
"authentication": true,
7-
"remote_config": true,
8-
"messaging": true,
9-
"crashlytics": true,
10-
"crash_reporting": false,
11-
"storage": true,
12-
"facebook_auth": true,
13-
"google_auth": true,
14-
"admob": true,
15-
"invites": true,
16-
"dynamic_links": true,
17-
"ml_kit": false
18-
}
2+
"external_push_client_only": false,
3+
"using_ios": true,
4+
"using_android": true,
5+
"firestore": true,
6+
"realtimedb": true,
7+
"authentication": true,
8+
"remote_config": true,
9+
"messaging": true,
10+
"crashlytics": true,
11+
"crash_reporting": true,
12+
"storage": true,
13+
"functions": true,
14+
"facebook_auth": true,
15+
"google_auth": true,
16+
"admob": true,
17+
"invites": true,
18+
"dynamic_links": true,
19+
"ml_kit": false
20+
}

demo/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"nativescript": {
33
"id": "org.nativescript.firebasedemo",
4-
"tns-ios": {
5-
"version": "4.2.0"
6-
},
74
"tns-android": {
85
"version": "4.2.0"
6+
},
7+
"tns-ios": {
8+
"version": "4.3.0-2018-09-19-01"
99
}
1010
},
1111
"dependencies": {
1212
"nativescript-plugin-firebase": "file:../publish/package/nativescript-plugin-firebase-7.0.1.tgz",
1313
"nativescript-theme-core": "^1.0.4",
1414
"nativescript-unit-test-runner": "^0.3.4",
15-
"tns-core-modules": "~4.2.0"
15+
"tns-core-modules": "^4.2.1"
1616
},
1717
"devDependencies": {
1818
"@types/jasmine": "~2.8.0",

src/app/functions/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as firebase from "../../firebase";
2+
3+
export namespace functions {
4+
// tslint:disable-next-line:class-name
5+
export class Functions {
6+
httpsCallable<I, O>( functionName: string ) {
7+
return firebase.functions.httpsCallable<I, O>(functionName);
8+
}
9+
}
10+
}

src/app/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { auth as firebaseAuthModule } from "./auth";
88
import { database as firebaseDatabaseModule } from "./database";
99
import { firestore as firebaseFirestoreModule } from "./firestore";
1010
import { storage as firebaseStorageModule } from "./storage";
11+
import { functions as firebaseFunctionsModule } from './functions';
1112

1213
export function initializeApp(options?: firebase.InitOptions, name? /* ignored */: string): Promise<any> {
1314
return firebase.init(options);
@@ -46,6 +47,17 @@ export function firestore(app?: any): firebaseFirestoreModule.Firestore {
4647
return firestoreCache;
4748
}
4849

50+
let functionsCache;
51+
export function functions(app?: any): firebaseFunctionsModule.Functions {
52+
if (app) {
53+
console.log("The 'app' param is ignored at the moment.");
54+
}
55+
if ( !functionsCache ) {
56+
functionsCache = new firebaseFunctionsModule.Functions();
57+
}
58+
return functionsCache;
59+
}
60+
4961
let storageCache;
5062
export function storage(app?: any): firebaseStorageModule.Storage {
5163
if (app) {

src/firebase.android.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
isDocumentReference
77
} from "./firebase-common";
88
import * as firebaseMessaging from "./messaging/messaging";
9+
import * as firebaseFunctions from "./functions/functions";
910
import * as appModule from "tns-core-modules/application";
1011
import { AndroidActivityResultEventData } from "tns-core-modules/application";
1112
import { ad as AndroidUtils, layout } from "tns-core-modules/utils/utils";
@@ -190,7 +191,7 @@ firebase.toValue = val => {
190191
}
191192
}
192193
return returnVal;
193-
}
194+
};
194195

195196
firebase.toJsObject = javaObj => {
196197
if (javaObj === null || typeof javaObj !== "object") {
@@ -448,6 +449,8 @@ firebase.subscribeToTopic = firebaseMessaging.subscribeToTopic;
448449
firebase.unsubscribeFromTopic = firebaseMessaging.unsubscribeFromTopic;
449450
firebase.areNotificationsEnabled = firebaseMessaging.areNotificationsEnabled;
450451

452+
firebase.functions = firebaseFunctions;
453+
451454
firebase.addOnDynamicLinkReceivedCallback = callback => {
452455
return new Promise((resolve, reject) => {
453456
try {

src/firebase.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,10 @@ export namespace firestore {
846846
function batch(): firestore.WriteBatch;
847847
}
848848

849+
export namespace functions {
850+
export type HttpsCallable<I, O> = ( callableData: I ) => Promise<O>;
851+
export function httpsCallable<I, O>( callableFunctionName: string ): HttpsCallable<I, O>;
852+
}
849853
// Auth
850854
export function login(options: LoginOptions): Promise<User>;
851855

src/firebase.ios.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as application from "tns-core-modules/application/application";
1010
import { ios as iOSUtils } from "tns-core-modules/utils/utils";
1111
import { device } from "tns-core-modules/platform/platform";
1212
import { DeviceType } from "tns-core-modules/ui/enums/enums";
13+
import * as firebaseFunctions from './functions/functions';
1314
import { firestore, User } from "./firebase";
1415
import { firebaseUtils } from "./utils";
1516

@@ -42,6 +43,8 @@ firebase.subscribeToTopic = firebaseMessaging.subscribeToTopic;
4243
firebase.unsubscribeFromTopic = firebaseMessaging.unsubscribeFromTopic;
4344
firebase.areNotificationsEnabled = firebaseMessaging.areNotificationsEnabled;
4445

46+
firebase.functions = firebaseFunctions;
47+
4548
firebase.addAppDelegateMethods = appDelegate => {
4649
// we need the launchOptions for this one so it's a bit hard to use the UIApplicationDidFinishLaunchingNotification pattern we're using for other things
4750
appDelegate.prototype.applicationDidFinishLaunchingWithOptions = (application, launchOptions) => {

0 commit comments

Comments
 (0)