LoginSignup
1
0

More than 3 years have passed since last update.

Cloud Functions for Firebaseで関数をシンプルにファイルごとに分割する

Posted at

Cloud Functions for Firebase にて、関数を定義する際にデフォルトのfunctions/index.js以下に関数を書いていくとすぐにファイルが肥大化してしまいます。

funcions/index.js
const functions = require("firebase-functions");

// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions

exports.helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});

そこで、ある程度の規模になりそうな場合は以下のようにファイルを分割した上で、関数をエクスポートして利用すると見通しが良くなるかと思います。

以下はその例です(TypeScript です)。

functions/src/index.ts
import * as admin from "firebase-admin";
admin.initializeApp();

export * from "./hello";
functions/src/hello.ts
import * as functions from "firebase-functions";

export const helloWorld = functions.https.onRequest((request, response) => {
  response.send("Hello from Firebase!");
});

export * from "./hello";とすることで、hello.ts内でexportした関数はすべて利用できるようになります。

ぜひ参考にしてみてください😄

〜宣伝〜

こんな感じで Firebase を使って、AnyMake というサービスを個人で開発しています。
スクリーンショット 2020-07-17 13.53.27.png
気になる方はぜひチェックしてみてください👍

:arrow_forward: AnyMake - 最高のプロダクトと共に。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0