0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

firebase cloud functionsのtypescriptソースをファイル分割する

Last updated at Posted at 2023-01-29

備忘録。
moduleAとmoduleBをindex.tsで読み込みre:exportする。

moduleA.ts
import * as functions from "firebase-functions";

export const helloWorldA = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs! I'm from module A");
  response.send("Hello from module A");
});

export const waitFunction = functions.https.onRequest(async (request, response)=>{
  functions.logger.info("waiting...");
  await new Promise((resolve)=>setTimeout(resolve, 3000));
  functions.logger.info("done!");
  response.send("ok");
});
moduleB.ts
import * as functions from "firebase-functions";

export const helloWorldB = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs! I'm from module B");
  response.send("Hello from module B");
});
index.ts
import * as functions from "firebase-functions";
export * as moduleA from "./moduleA";
export * as moduleB from "./moduleB";

exportでas句を用いなければunpackしてexportできる。
動作テストにはfunctions:shellを用いると良い。デフォルトでnpm run shellでビルドと同時にできる。

ちなみにrequire句を用いたりすると型情報が失われるので注意。
typescriptのexportについては下記を参考。
TypeScript: 38の具体例から学ぶnamed export

0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?