Code Monkey home page Code Monkey logo

firebasesamplecloudfunctions's Introduction

Description

  • use VSCode as an editor.
  • Use TypeScript for AltJS. DO NOT WRITE RAW JavaScript at all.
  • Use Yarn instead of npm.
  • Deploy via firebse tools.
  • use latest version package if available. Especially if you use firestore packages for "firebase-admin": "^5.0.0", "firebase-functions": "^0.7.0" or higher is required.

Before hand

Prepare tools and yarn.

npm install -g firebase-tools
npm instakk -g yarn

Style Guide

Step to Start

  1. initialize firebase project

    • firebase init
  2. move to functions directory.

    • functions directory will be created by firebase init.
  3. create function/src directory and move functions/index.js to functions/src/index.ts as for TypeScript.

  4. add functions/package.json file.

  5. install dependencies with yarn or npm. I recommend use yarn.

    • yarn install
  6. create a tsconfig.json.

package.json

{
  "name": "function",
  "version": "0.0.0",
  "description": "cloud functions for twitter authorize",
  "main": "index.js",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase experimental:functions:shell",
    "start": "npm run shell",
    "logs": "firebase functions:log",
    "build": "tsc",
    "watch": "tsc --watch",
    "deploy": "tsc && firebase deploy --only functions"
  },
  "author": "guitarrapc",
  "license": "",
  "dependencies": {
    "@google-cloud/firestore": "^0.10.0",
    "@types/firebase": "^2.4.32",
    "firebase-admin": "~5.5.1",
    "firebase-functions": "^0.7.3"
  },
  "devDependencies": {
    "typescript": "^2.2.2"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "lib": ["es6", "es2015.promise"],
    "module": "commonjs",
    "outDir": "./",
    "noImplicitAny": false,
    "strictNullChecks": true,
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ]
  },
  "include": [
    "src/**/*.ts",
    "spec/**/*.ts"
  ],
  "exclude": [
    "node_modeles"
  ]
}

typings.json

{
  "dependencies": {}
}

Write your Function

you can specify which is entry point .js of function with define in 3 way.

  1. index.js : export 1 or more function.
  2. app.js : export 1 or more functions with package.json file that contains "main" : "app.js"
  3. index.js : import one or more functions from goo.js file and then export one or more functions.

Write in TypeScript

In this scenario, use TypeScript to define functions then transcompile to js.

these .ts files should be under functions/src directory.

  1. index.ts : this handles export functions. no logics at all.
  2. main.ts : this handles initializer and import package.
  3. db.ts : this handles firebase realtimedatabase reference to be used in each functions.
  4. store.ts : this handles firestore reference to be used in each function.
  5. ****.ts : each functions. you will load index.ts and (db.ts | store.ts) for package reference. Make sure return promise.

Login to GCP Project

if needed, run following.

firebase login

Specify GCP Project-Id at .firebaserc

{
  "projects": {
    "default": "fir-sample-12345"
  }
}

Build

this is required to transcompile .ts to .js using tsc.

npm run build

Local Serve

npm run serve

Deploy

deploy all functions

npm run deploy

or

firebase deploy --only functions

deploy specific function

firebase deploy --only functions:functionName

firebasesamplecloudfunctions's People

Contributors

guitarrapc avatar dependabot[bot] avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.