Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

firebase-admin with typescript in firebase cloud functions

I am using firebase cloud functions with javascript on cloud functions. And, I want to switch from javascript to typescript.

However I cannot use firebase-admin on typescript as following command failed.

command: npm install @types/firebase-admin --save-dev
error:  '@types/firebase-admin' is not in the npm registry.

According to this release note, it looks that firebase admin support typescript. Can somebody tell us how to use typescript with firebase-admin on cloud functions?

https://firebase.google.com/support/release-notes/admin/node#4.1.3

like image 577
Yuuta Moriyama Avatar asked May 27 '17 18:05

Yuuta Moriyama


People also ask

Can I use Firebase with TypeScript?

Using an existing TypeScript project With this configuration, a firebase deploy --only functions command builds your TypeScript code and deploys it as functions.

What language would you like to use to write Cloud Functions?

Since Cloud Functions run on the Node. js runtime, you need to write Cloud Functions in JavaScript (or a language that can be compiled to JavaScript). And if you are writing Cloud Functions for Firebase, then the only languages that are officially supported are JavaScript and TypeScript. Note: Node.

What is the difference between onCall and onRequest?

onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.

Should I use Firebase functions?

You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.


2 Answers

Another option could be this way.

import * as admin from 'firebase-admin';
import * as serviceAccount from './service-account.json';

const firebaseAdmin = admin.initializeApp({
   credential: admin.credential.cert(serviceAccount as admin.ServiceAccount)
});
like image 126
frfernandezdev Avatar answered Sep 24 '22 02:09

frfernandezdev


You don't need to install an @types module, because firebase-admin ships with TypeScript support in the box. You should be able to use it with TypeScript just by installing firebase-admin.

import * as admin from 'firebase-admin';
like image 23
Michael Bleigh Avatar answered Sep 24 '22 02:09

Michael Bleigh