|
| 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> |
0 commit comments