Skip to content

Commit fd78fe1

Browse files
authored
Migrate cloud functions to Typescript (#42)
2 parents 590ece8 + 5dcba47 commit fd78fe1

22 files changed

+926
-793
lines changed

README.md

Lines changed: 58 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ Boilerplate with React ⚛️ and Firebase 🔥designed to quickly spin up a ful
1515
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
1616
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
1717

18-
1918
- [What is this?](#what-is-this)
2019
- [Why should I use it?](#why-should-i-use-it)
2120
- [Features](#features)
@@ -112,7 +111,6 @@ React Firebase Admin is our in-house admin dashboard boilerplate, used in many o
112111
- [Redux-persist](https://github.com/rt2zz/redux-persist) (★ 9.8k) persists store state between sessions.
113112
- [React-redux-toastr](https://github.com/diegoddox/react-redux-toastr) (★ 703) a toastr message implemented with Redux.
114113
- [React-router](https://github.com/ReactTraining/react-router) (★ 39.6k) declarative routing for React.
115-
- [Axios](https://github.com/axios/axios) (★ 70.2k) promise-based HTTP client.
116114
- [Prop-Types](https://reactjs.org/docs/typechecking-with-proptypes.html) (★ 3.4k) typechecking for react component props.
117115
- [Classnames](https://github.com/JedWatson/classnames) (★ 11.9k) a simple javascript utility for conditionally joining classNames together.
118116
- [React-datepicker](https://github.com/Hacker0x01/react-datepicker) (★ 4.5k) a simple and reusable datepicker component for React.
@@ -138,22 +136,23 @@ React Firebase Admin is our in-house admin dashboard boilerplate, used in many o
138136

139137
### Cloud functions
140138

141-
- [Express](https://github.com/expressjs/express) (★ 47.5k) fast, unopinionated, minimalist web framework for node.
142-
- [Cors](https://github.com/expressjs/cors) (★ 4.4k) Node.js CORS middleware.
143139
- [Firebase-admin](https://github.com/firebase/firebase-admin-node) (★ 790) Firebase Admin Node.js SDK.
144140
- [Firebase-functions](https://github.com/firebase/firebase-functions) (★ 658) Firebase SDK for Cloud Functions.
145-
- [@google-cloud/storage](https://github.com/googleapis/nodejs-storage) (★ 421) Node.js client for Google Cloud Storage.
141+
- [Typescript](https://github.com/Microsoft/TypeScript) (★ 61.1k) TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
142+
- [TSlint](https://github.com/palantir/tslint) (★ 5.8k) An extensible linter for the TypeScript language.
146143
- [Firebase-function-tools](https://github.com/TarikHuber/react-most-wanted) (★ 780) a tool for naming and loading our Cloud Functions.
147-
- [Cookie-parser](https://github.com/expressjs/cookie-parser) (★ 1.4k) parse HTTP request cookies.
148-
- [Uuid](https://github.com/uuidjs/uuid) (★ 8.7k) generate RFC-compliant UUIDs in JavaScript.
149144
- [Glob](https://github.com/isaacs/node-glob) (★ 6.2k) glob functionality for Node.js.
150-
- [Fs-extra](https://github.com/jprichardson/node-fs-extra) (★ 6.6k) Node.js: extra methods for the fs object like copy(), remove(), mkdirs().
145+
- [Camelcase](https://github.com/sindresorhus/camelcase) (★ 423) convert a dash/dot/underscore/space separated string to camelCase.
151146
- [Resize Image](https://github.com/firebase/extensions/tree/master/storage-resize-images) (★ 372) Firebase Extension to create resized versions of images uploaded to Cloud Storage.
152147

148+
### Unit Testing
149+
150+
- [Firebase-functions-test](https://github.com/firebase/firebase-functions-test) (★ 117) unit testing library for Cloud Functions for Firebase.
151+
153152
## Prerequisites
154153

155154
- [Firebase](https://firebase.google.com/) account in order to set up the serverless BaaS for the project.
156-
- [Node.js](https://nodejs.org/) v12.15.0 or higher (React frontend) and v8.17.0 (Cloud Functions) (You can install them using [NVM](https://github.com/nvm-sh/nvm)).
155+
- [Node.js](https://nodejs.org/) v12.15.0 or higher (React frontend) and v10.0.0 (Cloud Functions) (You can install them using [NVM](https://github.com/nvm-sh/nvm)).
157156
- [VSCode](https://code.visualstudio.com/) editor (preferred) + [ESLint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint), [Prettier](https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode) and [Babel JavaScript ](https://marketplace.visualstudio.com/items?itemName=mgmcdermott.vscode-language-babel) plug-ins.
158157

159158
You also need to be familiar with [HTML](https://developer.mozilla.org/en-US/docs/Web/HTML), [CSS](https://developer.mozilla.org/en-US/docs/Web/CSS), [SASS](https://sass-lang.com/), [JavaScript](https://developer.mozilla.org/en-US/docs/Web/JavaScript) ([ES2015](http://babeljs.io/learn-es2015/)) and [React](https://reactjs.org/) with [React Hooks](https://reactjs.org/docs/hooks-intro.html).
@@ -166,15 +165,21 @@ You also need to be familiar with [HTML](https://developer.mozilla.org/en-US/doc
166165
- In the **_Storage_** section set up your storage bucket with the default values provided by Firebase.
167166
- Setup your sign-in providers in the **_Authentication_** section. On the **_Sign in method_** tab, enable **_Email/Password_** provider with the **_Email Link_** feature.
168167
- Copy the config values you get from you Firebase dashboard's **_Web Setup_** option and enter them into a `.env` file with the same structure as the `.env.example` in the root directory of the repository.
168+
- Setup the [image resize extension](https://firebase.google.com/products/extensions/storage-resize-images) in your Firebase project. For instructions go to the [File upload section](#file-upload)
169169

170170
### Setting up the Firebase project locally
171171

172172
First we need to install the Firebase cli tool and then initialize our new Firebase project.
173173

174174
Run the following commands in the root of the repository:
175175

176-
- `npm install -g firebase-tools`
177-
- `firebase init`
176+
```
177+
npm install -g firebase-tools
178+
```
179+
180+
```
181+
firebase init
182+
```
178183

179184
Select the firebase project you created in the previous step, when prompted select the services you want to setup and check **_Database_**, **_Functions_**, **_Hosting_** and **_Storage_**.
180185

@@ -183,42 +188,41 @@ Select the firebase project you created in the previous step, when prompted sele
183188
- Leave the default name for our rules `.json` file.
184189
- Select **_No_** when prompted if you want to overwrite the file containing our rules.
185190

186-
**_Functions_**:
187-
188-
- For the language to be used in our functions select **_JavaScript_**.
189-
- If we want to use ESLint as our linting tool select **_No_**.
190-
- If we want to overwrite the `package.json`, `.gitignore` and `index.js` select **_No_**.
191-
- Then select **_Yes_** so that we install our dependencies with npm.
192-
193191
**_Hosting_**:
194192

195193
- Set **build** as our public directory for our hosting files.
196194
- Select **_Yes_** when asked if we want our hosting to be configured as a single page application.
197195

198-
Run the following commands in the `functions/` folder (Using Node v8.17.0):
196+
**_Storage_**
199197

200-
- `npm run setup-firebase`
198+
- For the file we should use for the Storage Rules select **storage.rules**.
201199

202-
You'll get prompted to enter the path to you service account key file. To generate it, go to your **_Firebase Dashboard_**, **_Project settings_** tab and then to **_Service accounts_** option, right there you can generate your private key (more info on this [here](https://firebase.google.com/docs/admin/setup#initialize-sdk)).
200+
**_Functions_**:
203201

204-
You'll need to enter the **_email_** and **_password_** for the admin account of the admin dashboard. With this in place, you've successfully created your admin account for the dashboard.
202+
- For the language to be used in our functions select **_TypeScript_**.
203+
- If we want to use ESLint as our linting tool select **_No_**.
204+
- If we want to overwrite the `package.json`, `.gitignore` and `index.js` select **_No_**.
205+
- Then select **_Yes_** so that we install our dependencies with npm.
205206

206-
**_Storage_**
207-
-For the file we should use for the Storage Rules select storage.rules.
207+
Run the following command on the **functions/** folder (Using Node v10.0.0):
208208

209-
### Setting up the React frontend
209+
```
210+
npm run setup-firebase
211+
```
210212

211-
Run the following commands in the root of the repository (Using Node v12.15.0 or higher ):
213+
You'll get prompted to enter the path to you service account key file. To generate it, go to your **_Firebase Dashboard_**, **_Project settings_** tab and then to **_Service accounts_** option, right there you can generate your private key (more info on this [here](https://firebase.google.com/docs/admin/setup#initialize-sdk)).
212214

213-
- `npm run setup-admin-dashboard`
215+
You'll need to enter the **_email_** and **_password_** for the admin account of the admin dashboard. With this in place, you've successfully created your admin account for the dashboard.
214216

215-
If you go to your dashboard you'll see all the Firebase services are now deployed, go to the **_Functons_** section and copy the Cloud Functions URL and paste it into the `REACT_APP_CLOUD_FUNCTIONS_REST_API` environment variable in your `.env` file.
217+
### Setting up the React frontend
216218

217-
It should look like this:
219+
Run the following commands in the root of the repository (Using Node v12.15.0 or higher):
218220

219-
`REACT_APP_CLOUD_FUNCTIONS_REST_API = '<CLOUD FUNCTION URL>/requestsApp'`
221+
```
222+
npm run setup-admin-dashboard
223+
```
220224

221-
At this point we have a fully functional admin dashboard. You can start a local development server with the command `npm run start`, and for the deployed project you can go to the Firebase Dashboard within the **_Hosting_** tab find out the url of the hosted project.
225+
If you go to your dashboard you'll see all the Firebase services are now deployed.
222226

223227
## React frontend
224228

@@ -304,34 +308,31 @@ Launches the test runner in the interactive watch mode.
304308

305309
### How to deploy
306310

307-
`npm run deploy`
311+
```
312+
npm run deploy
313+
```
308314

309315
## Cloud functions
310316

311317
### Folder structure
312318

313319
```
314320
functions/
315-
├── auth/
316-
│ ├── onCreate.function.js
317-
| └── onDelete.function.js
318-
├── db/
319-
│ ├── users/
320-
│ │ ├── onCreate.function.js
321-
│ │ └── onModify.function.js
322-
│ └── ... other database functions ...
323-
├── storage/
324-
│ ... storage functions ...
325-
├── requests/
326-
│ ├── middlewares/
327-
│ │ └── auth.js
328-
│ ├── routes/
329-
│ │ ├── users.js
330-
│ │ └── establishments.js
331-
| └── app.function.js
332-
├── index.js
333-
├── production-key.json
334-
└── staging-key.json
321+
├── src/
322+
│ ├── auth/
323+
│ │ ├── onCreate.function.ts
324+
| │ └── onDelete.function.ts
325+
│ ├── db/
326+
│ │ ├── users/
327+
│ │ ├── onCreate.function.ts
328+
│ │ ├── onModify.function.ts
329+
│ │ └── ... other database functions ...
330+
│ ├── storage/
331+
│ │ ... storage functions ...
332+
│ ├── https/
333+
│ │ ├── createUser.function.ts
334+
│ │ └── ... other https functions ...
335+
│ └── index.ts
335336
```
336337

337338
### Installing dependencies
@@ -578,9 +579,9 @@ const date = Date.now();
578579
579580
For file upload, we used the [Firebase Client](https://firebase.google.com/docs/storage/web/upload-files?authuser=1#upload_files) together with **Firebase Storage** for our storage needs. We store the users profile image in a subfolder named `/users`.
580581
581-
### Image Resize
582+
### Image resize extension
582583
583-
We use [Resize Image](https://github.com/firebase/extensions/tree/master/storage-resize-images) for resizing every image uploaded to the storage.
584+
We use [Resize Image extension](https://github.com/firebase/extensions/tree/master/storage-resize-images) for resizing every image uploaded to the storage.
584585
Every image uploaded to the storage is resized to our size preference (`200px x 200px`).
585586
586587
**Setting your image resize**
@@ -591,7 +592,9 @@ If you want to install it from the cmd, you can execute:
591592
firebase ext:install storage-resize-images --project=projectId
592593
```
593594
594-
Wih your own `projectId`
595+
With your own `projectId`.
596+
597+
Installing the extesion via de Firebase dashboard:
595598
596599
<ol>
597600
<li>Go to your Firebase Proyect on <a href= "https://console.firebase.google.com/u/1/">Firebase Console</a> </li>

firebase.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,11 @@
1818
},
1919
"storage": {
2020
"rules": "storage.rules"
21+
},
22+
"functions": {
23+
"predeploy": [
24+
"npm --prefix \"$RESOURCE_DIR\" run lint",
25+
"npm --prefix \"$RESOURCE_DIR\" run build"
26+
]
2127
}
2228
}

functions/.gitignore

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
node_modules/
1+
## Compiled JavaScript files
2+
**/*.js
3+
**/*.js.map
4+
5+
# Typescript v1 declaration files
6+
typings/
7+
8+
node_modules/
9+
10+
lib/

functions/db/users/onDelete.function.js

Lines changed: 0 additions & 9 deletions
This file was deleted.

functions/db/users/onUpdate.function.js

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)