Reputation: 459
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
Upvotes: 22
Views: 25234
Reputation: 6895
Dynamically import the correct serviceaccount.json file using path initialization.
import { credential } from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
let env = process.env.NODE_ENV || 'staging'
if (env === 'development') env = 'staging'
export const firebaseApp = initializeApp({
credential: credential.cert(`${__dirname}/${env}/firebase-adminsdk.json`)
})
Upvotes: 0
Reputation: 5827
It seems that types are provided when imported using ES6 Modules:
tsconfig.json
{
"compilerOptions": {
"resolveJsonModule": true, // json imports
"esModuleInterop": true, // import common modules as ES6 Modules
"allowSyntheticDefaultImports": true, // support typesystem compatibility
}
}
index.ts
import firebase from 'firebase-admin';
import serviceAccount from './service-account.json';
//snake_case to camelCase
const params = {
type: serviceAccount.type,
projectId: serviceAccount.project_id,
privateKeyId: serviceAccount.private_key_id,
privateKey: serviceAccount.private_key,
clientEmail: serviceAccount.client_email,
clientId: serviceAccount.client_id,
authUri: serviceAccount.auth_uri,
tokenUri: serviceAccount.token_uri,
authProviderX509CertUrl: serviceAccount.auth_provider_x509_cert_url,
clientC509CertUrl: serviceAccount.client_x509_cert_url
}
firebase.initializeApp({
credential: firebase.credential.cert(params),
})
Upvotes: 11
Reputation: 41
import { initializeApp, cert } from 'firebase-admin/app';
import { getMessaging } from 'firebase-admin/messaging';
const app: App = initializeApp(
{
credential: cert('./firebase.json'),
},
'appname',
);
class PushNotificationService {
options = {
priority: 'high',
timeToLive: 60 * 60 * 24,
};
send = (userToken: any, message: any): void => {
getMessaging(app)
.sendToDevice(userToken, message, this.options)
.then(response => {
console.log('Successfully sent message:', response);
})
.catch(error => {
console.log('Error sending message:', error);
});
};
}
export default PushNotificationService;
Upvotes: 2
Reputation: 51
import * as admin from 'firebase-admin';
const serviceAccount = require('./firebase.json');
if (!admin.apps.length) {
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
}
export const firebaseDB = admin.firestore();
Upvotes: 0
Reputation: 455
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)
});
Upvotes: 20
Reputation: 26333
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';
Upvotes: 30
Reputation: 193
I know i'm late but I found another way to do this, using the answer given by Peter:
{
"compilerOptions": {
"resolveJsonModule": true, // json imports
"esModuleInterop": true, // import common modules as ES6 Modules
"allowSyntheticDefaultImports": true, // support typesystem compatibility
}
}
import firebase from 'firebase-admin';
import serviceAccount from './service-account.json';
firebase.initializeApp({
credential: firebase.credential.cert(serviceAccount as any), //Cast as any instead of clone the JSON.
})
Upvotes: 3
Reputation: 51
For those who are still struggling, reloading VSCode after installing firebase-admin did the work for me.
Upvotes: 4