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

Commit 1150640

Browse files
Support for Firebase 'Callable' functions #806 (docs)
1 parent 07a0ad6 commit 1150640

File tree

5 files changed

+221
-15
lines changed

5 files changed

+221
-15
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,18 @@
1515
<img src="docs/images/firebase-logo.png" width="116px" height="32px" alt="Firebase"/><br/>
1616

1717
## Features
18-
* [Realtime Database](docs/DATABASE.md)
19-
* [Firestore](docs/FIRESTORE.md)
18+
* [AdMob](docs/ADMOB.md)
19+
* [Analytics](docs/ANALYTICS.md)
2020
* [Authentication](docs/AUTHENTICATION.md)
21-
* [Remote Config](docs/REMOTECONFIG.md)
22-
* [Firebase Cloud Messaging](docs/MESSAGING.md)
23-
* [Non-Firebase Push Messaging](docs/NON_FIREBASE_MESSAGING.md) 🆕
24-
* [Storage](docs/STORAGE.md)
21+
* [Cloud Firestore](docs/FIRESTORE.md)
22+
* [Cloud Functions](docs/FUNCTIONS.md)
23+
* [Cloud Messaging](docs/MESSAGING.md) / [Non-Firebase Push Messaging](docs/NON_FIREBASE_MESSAGING.md)
2524
* [Crash Reporting / Crashlytics](docs/CRASHREPORTING.md)
26-
* [Analytics](docs/ANALYTICS.md)
2725
* [Invites and Dynamic Links](docs/INVITES_DYNAMICLINKS.md)
28-
* [AdMob](docs/ADMOB.md)
2926
* [ML Kit](docs/ML_KIT.md)
27+
* [Realtime Database](docs/DATABASE.md)
28+
* [Remote Config](docs/REMOTECONFIG.md)
29+
* [Storage](docs/STORAGE.md)
3030

3131
## Prerequisites
3232
Head on over to [https://console.firebase.google.com/](https://console.firebase.google.com/) and sign up for a free account.

demo/app/main-view-model.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,17 +198,17 @@ export class HelloWorldModel extends Observable {
198198

199199
public doWebCallableFunction(): void {
200200
// see the implementation of this function @ https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/ff95c77c7b09acf66654f53c52e8ae0c8d7b1c78/demo/firebasefunctions/functions/src/index.ts#L15-L19
201-
const fn = firebaseWebApi.functions().httpsCallable("helloName");
201+
const fn = firebase.functions.httpsCallable("helloName");
202202

203203
fn("Nativescript-Plugin-Firebase!")
204-
.then(dataCue => {
204+
.then((dataCue: any) => {
205205
alert({
206206
title: "Callable Function Result",
207207
message: dataCue.message,
208208
okButtonText: "Nice!"
209209
});
210210
})
211-
.catch(errorMessage => {
211+
.catch((errorMessage: string) => {
212212
alert({
213213
title: "An Error Occurred",
214214
message: errorMessage,

docs/FUNCTIONS.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
<img src="https://raw.githubusercontent.com/EddyVerbruggen/nativescript-plugin-firebase/master/docs/images/features/functions.png" height="85px" alt="Cloud Functions"/>
2+
3+
> Added in plugin version 7.1.0, by [breningham](https://github.com/breningham)
4+
5+
## Cloud Functions?
6+
Cloud Functions for Firebase lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests.
7+
Your code is stored in Google's cloud and runs in a managed environment. There's no need to manage and scale your own servers.
8+
9+
[Learn more here..](https://firebase.google.com/docs/functions/)
10+
11+
> Note that you don't need to enable this feature in the plugin unless you want to [call those Cloud Functions from your app](https://firebase.google.com/docs/functions/callable).
12+
13+
## Enabling Cloud Functions
14+
To add this feature to your project, either:
15+
16+
* Remove `firebase.nativescript.json` from the root of the project and run `npm i`, or
17+
* Edit that file and add `"functions": true`.
18+
19+
In both cases, remove the `/platforms` folder afterwards so the required native library will be added upon the next build.
20+
21+
22+
## Functions
23+
You can use either the Web API syntax (easy for interoperability with a web version of your app), or our custom native syntax.
24+
Use whichever syntax you like most - the underlying implementation is the same.
25+
26+
### httpsCallable
27+
This example uses the Cloud Function as [implemented here](https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/ff95c77c7b09acf66654f53c52e8ae0c8d7b1c78/demo/firebasefunctions/functions/src/index.ts#L15-L19).
28+
29+
<details>
30+
<summary>Native API</summary>
31+
32+
```typescript
33+
import * as firebase from "nativescript-plugin-firebase";
34+
35+
const fn = firebase.functions.httpsCallable("helloName");
36+
37+
fn("Nativescript-Plugin-Firebase!")
38+
.then((dataCue: any) => console.log("Callable Function Result: " + dataCue.message))
39+
.catch((errorMessage: string) => console.log("Callable Function Error: " + errorMessage));
40+
```
41+
</details>
42+
43+
<details>
44+
<summary>Web API</summary>
45+
46+
```typescript
47+
const firebaseWebApi = require("nativescript-plugin-firebase/app"); // mind the /app!
48+
49+
const fn = firebaseWebApi.functions().httpsCallable("helloName");
50+
51+
fn("Nativescript-Plugin-Firebase!")
52+
.then((dataCue: any) => console.log("Callable Function Result: " + dataCue.message))
53+
.catch((errorMessage: string) => console.log("Callable Function Error: " + errorMessage));
54+
```
55+
</details>
56+
57+
### downloadFile
58+
As with `uploadFile` you can either pass in a full local path to a file, or (as a convenience) use the `file-system` module that comes shipped with {N} as standard.
59+
60+
In this example we'll download the previously uploaded file to a certain path on the local filesystem.
61+
62+
<details>
63+
<summary>Native API</summary>
64+
65+
```js
66+
// init the file-system module
67+
var fs = require("tns-core-modules/file-system");
68+
69+
// let's first determine where we'll create the file using the 'file-system' module
70+
var documents = fs.knownFolders.documents();
71+
var logoPath = documents.path + "/telerik-logo-downloaded.png";
72+
73+
// this will create or overwrite a local file in the app's documents folder
74+
var localLogoFile = documents.getFile("telerik-logo-downloaded.png");
75+
76+
// now download the file with either of the options below:
77+
firebase.storage.downloadFile({
78+
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
79+
bucket: 'gs://n-plugin-test.appspot.com',
80+
// the full path of an existing file in your Firebase storage
81+
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png',
82+
// option 1: a file-system module File object
83+
localFile: fs.File.fromPath(logoPath),
84+
// option 2: a full file path (ignored if 'localFile' is set)
85+
localFullPath: logoPath
86+
}).then(
87+
function (uploadedFile) {
88+
console.log("File downloaded to the requested location");
89+
},
90+
function (error) {
91+
console.log("File download error: " + error);
92+
}
93+
);
94+
```
95+
</details>
96+
97+
<details>
98+
<summary>Web API</summary>
99+
100+
#### TypeScript
101+
102+
```typescript
103+
import * as fs from "tns-core-modules/file-system";
104+
105+
const storageRef = firebaseWebApi.storage().ref();
106+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
107+
108+
// let's first determine where we'll create the file using the 'file-system' module
109+
const documents = fs.knownFolders.documents();
110+
const logoPath = documents.path + "/telerik-logo-downloaded.png";
111+
112+
childRef.download(logoPath)
113+
.then(() => console.log("The file has been downloaded"))
114+
.catch(error => console.log("Download error: " + error));
115+
```
116+
</details>
117+
118+
### getDownloadUrl
119+
If you just want to know the remote URL of a file in remote storage so you can either share it or download the file by any other means than `downloadFile` then use this method.
120+
121+
In this example we'll determine the remote URL of the previously uploaded file.
122+
123+
<details>
124+
<summary>Native API</summary>
125+
126+
```js
127+
firebase.storage.getDownloadUrl({
128+
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
129+
bucket: 'gs://n-plugin-test.appspot.com',
130+
// the full path of an existing file in your Firebase storage
131+
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
132+
}).then(
133+
function (url) {
134+
console.log("Remote URL: " + url);
135+
},
136+
function (error) {
137+
console.log("Error: " + error);
138+
}
139+
);
140+
```
141+
</details>
142+
143+
<details>
144+
<summary>Web API</summary>
145+
146+
#### TypeScript
147+
148+
```typescript
149+
const storageRef = firebaseWebApi.storage().ref();
150+
const childRef = storageRef.child("uploads/images/telerik-logo-uploaded.png");
151+
152+
childRef.getDownloadURL()
153+
.then(theUrl => console.log("Download url: " + theUrl))
154+
.catch(error => console.log("Download error: " + error));
155+
```
156+
</details>
157+
158+
### deleteFile
159+
You can pass in remote file path to delete it.
160+
161+
<details>
162+
<summary>Native API</summary>
163+
164+
```js
165+
firebase.storage.deleteFile({
166+
// optional, can also be passed during init() as 'storageBucket' param so we can cache it
167+
bucket: 'gs://n-plugin-test.appspot.com',
168+
// the full path of an existing file in your Firebase storage
169+
remoteFullPath: 'uploads/images/telerik-logo-uploaded.png'
170+
}).then(
171+
function () {
172+
console.log("File deleted.");
173+
},
174+
function (error) {
175+
console.log("File deletion Error: " + error);
176+
}
177+
);
178+
```
179+
</details>
180+
181+
<details>
182+
<summary>Web API</summary>
183+
184+
#### TypeScript
185+
186+
```typescript
187+
firebaseWebApi.storage().ref()
188+
.child("uploads/images/telerik-logo-uploaded.png")
189+
.delete()
190+
.then(() => console.log("Deleted file"))
191+
.catch(error => console.log("Error deleting file: " + error));
192+
```
193+
</details>

docs/images/features/functions.png

44.9 KB
Loading

src/firebase.d.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -733,10 +733,13 @@ export namespace firestore {
733733
}
734734

735735
export interface DocumentSnapshot {
736-
ios?: any; /* FIRDocumentSnapshot */
737-
android?: any; /* com.google.firebase.firestore.DocumentSnapshot */
736+
ios?: any;
737+
/* FIRDocumentSnapshot */
738+
android?: any;
739+
/* com.google.firebase.firestore.DocumentSnapshot */
738740
id: string;
739741
exists: boolean;
742+
740743
data(): DocumentData;
741744
}
742745

@@ -803,17 +806,25 @@ export namespace firestore {
803806

804807
export interface Transaction {
805808
get(documentRef: DocumentReference): DocumentSnapshot;
809+
806810
set(documentRef: DocumentReference, data: DocumentData, options?: SetOptions): Transaction;
811+
807812
update(documentRef: DocumentReference, data: UpdateData): Transaction;
813+
808814
update(documentRef: DocumentReference, field: string | FieldPath, value: any, ...moreFieldsAndValues: any[]): Transaction;
815+
809816
delete(documentRef: DocumentReference): Transaction;
810817
}
811818

812819
export interface WriteBatch {
813820
set(documentRef: DocumentReference, data: DocumentData, options?: SetOptions): WriteBatch;
821+
814822
update(documentRef: DocumentReference, data: UpdateData): WriteBatch;
823+
815824
update(documentRef: DocumentReference, field: string | FieldPath, value: any, ...moreFieldsAndValues: any[]): WriteBatch;
825+
816826
delete(documentRef: DocumentReference): WriteBatch;
827+
817828
commit(): Promise<void>;
818829
}
819830

@@ -847,9 +858,11 @@ export namespace firestore {
847858
}
848859

849860
export namespace functions {
850-
export type HttpsCallable<I, O> = ( callableData: I ) => Promise<O>;
851-
export function httpsCallable<I, O>( callableFunctionName: string ): HttpsCallable<I, O>;
861+
export type HttpsCallable<I, O> = (callableData: I) => Promise<O>;
862+
863+
export function httpsCallable<I, O>(callableFunctionName: string): HttpsCallable<I, O>;
852864
}
865+
853866
// Auth
854867
export function login(options: LoginOptions): Promise<User>;
855868

0 commit comments

Comments
 (0)