拡張機能の Cloud Functions の関数を作成する

拡張機能を作成するときは、Cloud Functions を使用してそのロジックを記述します。これは、自分のプロジェクトでのみ使用する Cloud Functions の関数を記述する場合とほぼ同じです。関数は extension.yaml ファイルで宣言します。ユーザーが拡張機能をインストールすると、関数はプロジェクトにデプロイされます。

Cloud Functions の使用方法に関する一般的な情報については、Cloud Functions のドキュメントをご覧ください。

第 1 世代と第 2 世代の Cloud Functions

Firebase は、第 1 世代と第 2 世代の両方の Cloud Functions をサポートしています。ただし、Firebase Extensions には現在、特定のトリガータイプで使用できる Cloud Functions の世代に制限があります。このため、多くの拡張機能には第 1 世代と第 2 世代の関数が混在しています。

以下の各トリガータイプの説明でサポートされる関数の世代を示します。

その他の注意事項

  • 一部の関数の定義には、extension.yaml ファイルで指定されている情報も指定する必要があります。たとえば、Cloud Firestore の document() メソッドには監視するドキュメントのパターンを指定しますが、これに対応する宣言が extension.yamlresource フィールドにあり、ここにも同じ情報を指定します。

    このような場合、extension.yaml ファイルで指定された構成が使用され、関数の定義で指定された構成は無視されます。

    文書化の目的から、構成済みの値を関数の定義で指定するのが一般的です。このページの例は、このパターンに従っています。

  • Cloud Functions の第 1 世代の SDK には、第 1 世代の関数でパラメータ化された値を操作するための functions.config() メソッドと functions:config:set CLI コマンドがあります。この手法は Cloud Functions で非推奨となっており、拡張機能で機能しません。代わりに、functions.params モジュール(推奨)または process.env を使用してください。

TypeScript の使用

独自の拡張機能の開発に関係するほとんどのドキュメントでは、Cloud Functions for Firebase の JavaScript を使用したワークフローが説明されています。しかし、TypeScript を使用して関数を作成することもできます。

実際のところ、公式 Firebase Extensions はすべて TypeScript で作成されています。これらの拡張機能は、TypeScript を使用する際のおすすめの方法として確認できます。

拡張機能の関数を TypeScript で作成する場合は、拡張機能をインストールする前に次のことを行う必要があります。

  1. 拡張機能の関数のソースコードを JavaScript にコンパイルします。

    firebase ext:dev:init コマンドを使用すると、関数を作成する TypeScript を選択できます。このコマンドは、インストール可能な拡張機能のほかに、npm run build で実行できるビルド スクリプトも作成します。

  2. package.json ファイルで、生成された JavaScript の main フィールドを参照します。

  3. ローカルソースから拡張機能をインストールまたはアップロードする場合は、まず TypeScript ファイルをコンパイルします。

サポートされている関数トリガー

HTTP トリガー

HTTP トリガー関数は、公開 https エンドポイントにデプロイされ、エンドポイントへのアクセス時に実行されます。

HTTP でトリガーされる関数の作成方法については、Cloud Functions のドキュメントの HTTP リクエストで関数を呼び出すをご覧ください。

関数の定義(第 1 世代のみ)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onRequest(async (req, resp) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

呼び出し可能関数

呼び出し可能関数は HTTP トリガー関数に似ていますが、クライアントサイドのコードから簡単に呼び出せるプロトコルを実装します。

呼び出し可能関数の使用方法については、Cloud Functions のドキュメントのアプリから関数を呼び出すをご覧ください。

関数の定義(第 1 世代のみ)

import { https } from "firebase-functions/v1";

export const yourFunctionName = https.onCall(async (data, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      runtime: nodejs16
      httpsTrigger: {}
  - name: anotherFunction
    type: ...

スケジュール設定された関数トリガー

スケジュール設定された関数は、カスタマイズ可能なスケジュールに基づいて繰り返し実行されます。

スケジュール設定された関数の作成については、Cloud Functions のドキュメントの関数のスケジュールを設定するをご覧ください。

関数の定義(第 1 世代のみ)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.schedule("every 6 hours").onRun((context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: 'every 5 minutes'
  - name: anotherFunction
    type: ...

scheduleTrigger に指定可能なサブフィールドを次に示します。

フィールド 説明
schedule
(必須)

関数を実行する頻度。

このフィールドには、次のいずれかの構文を使用する文字列を指定できます(単一引用符で囲む必要があります)。

timeZone
(省略可)

スケジュールを実行するタイムゾーン。

ユーザーが拡張機能をインストールするときにスケジュールを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      scheduleTrigger:
        schedule: ${SCHEDULE_FREQUENCY}
  - name: anotherFunction
    type: ...

params:
  - param: SCHEDULE_FREQUENCY
    label: Schedule
    description: How often do you want to run yourFunctionName()?
    type: string
    default: 'every 5 minutes'  # Specifying a default is optional.
    required: true

タスクキュー トリガー

タスクキュー関数は、拡張機能のライフサイクル イベントで呼び出されます。また、Admin SDK の TaskQueue.enqueue() メソッドによって拡張機能のタスクキューに手動で追加したときにもトリガーされます。

ライフサイクル イベントを処理する関数の作成方法については、拡張機能のライフサイクル イベントを処理するをご覧ください。

タスクキュー関数の作成については、Cloud Functions のドキュメントの Cloud Tasks で関数をキューに追加するをご覧ください。

関数の定義(第 1 世代のみ)

import { tasks } from "firebase-functions/v1";

export const yourFunctionName = tasks.taskQueue().onDispatch(async (data, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: myTaskFunction
    type: firebaseextensions.v1beta.function
    description: >-
      Perform a task when triggered by a lifecycle event
    properties:
      taskQueueTrigger: {}

taskQueueTrigger プロパティを {} に設定するか、タスクキューのレート制限と再試行の動作を調整するオプション マップを設定します(詳しくは、タスクキューのチューニングを参照)。

拡張機能のライフサイクル イベントで関数をトリガーするには、関数の名前とオプションの処理メッセージを含む lifecycleEvents レコードを追加します。処理が開始すると、このメッセージが Firebase コンソールに表示されます。

lifecycleEvents:
  onInstall:
    function: myTaskFunction
    processingMessage: Resizing your existing images
  onUpdate:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension
  onConfigure:
    function: myOtherTaskFunction
    processingMessage: Setting up your extension

アナリティクス

アナリティクスでトリガーされる関数は、指定したアナリティクス イベントがログに記録されると実行されます。

アナリティクスでトリガーされる関数の詳細については、Cloud Functions のドキュメントの Google アナリティクス トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { analytics } from "firebase-functions/v1";

export const yourFunctionName = analytics.event("event_name").onLog((event, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/ga_event
  - name: anotherFunction
    type: ...

ユーザーが拡張機能をインストールするときにリッスンするアナリティクス イベントを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.analytics/eventTypes/event.log
        resource: projects/${PROJECT_ID}/events/${EVENT_NAME}
  - name: anotherFunction
    type: ...

params:
  - param: EVENT_NAME
    label: Analytics event
    description: What event do you want to respond to?
    type: string
    default: ga_event  # Specifying a default is optional.
    required: true

Authentication

Authentication によってトリガーされる関数は、ユーザーが作成または削除されると実行されます。

認証によってトリガーされる関数の作成については、Cloud Functions のドキュメントの Firebase Authentication トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { auth } from "firebase-functions/v1";

export const yourFunctionName = auth.user().onCreate((user, context) => {
  // ...
});

export const yourFunctionName2 = auth.user().onDelete((user, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/firebase.auth/eventTypes/user.create
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

次の表は、サポートされる Authentication イベントの種類を指定する方法を示しています。

Cloud Functions のイベント トリガー eventType 説明
onCreate() providers/firebase.auth/eventTypes/user.create 新しいユーザーを作成
onDelete() providers/firebase.auth/eventTypes/user.delete ユーザーを削除

Cloud Firestore

Cloud Firestore でトリガーされる関数は、ドキュメントが作成、更新、または削除されると実行されます。

Firestore でトリガーされる関数の作成については、Cloud Functions のドキュメントの Cloud Firestore トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { firestore } from "firebase-functions/v1";

export const yourFunctionName = firestore.document("collection/{doc_id}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = firestore.document("collection/{doc_id}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = firestore.document("collection/{doc_id}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = firestore.document("collection/{doc_id}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/collection/{documentID}
  - name: anotherFunction
    type: ...

次の表は、サポートされている Cloud Firestore イベントの種類を指定する方法を示しています。

Cloud Functions のイベント トリガー eventType 説明
onCreate() providers/cloud.firestore/eventTypes/document.create 新しいドキュメントを作成
onDelete() providers/cloud.firestore/eventTypes/document.delete ドキュメントを削除
onUpdate() providers/cloud.firestore/eventTypes/document.update ドキュメントを更新
onWrite() providers/cloud.firestore/eventTypes/document.write ドキュメントを作成、削除、更新

ユーザーが拡張機能をインストールするときにドキュメント パスを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/cloud.firestore/eventTypes/document.write
        resource: projects/${PROJECT_ID}/databases/(default)/documents/${YOUR_DOCUMENT_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_DOCUMENT_PATH
    label: Cloud Firestore path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{documentID}  # Specifying a default is optional.
    required: true

Pub/Sub

Pub/Sub でトリガーされる関数は、メッセージが特定のトピックにパブリッシュされると実行されます。

Pub/Sub でトリガーされる関数の作成については、Cloud Functions のドキュメントの Pub/Sub トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { pubsub } from "firebase-functions/v1";

export const yourFunctionName = pubsub.topic("topic_name").onPublish((message, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/topic-name
  - name: anotherFunction
    type: ...

ユーザーが拡張機能をインストールするときに Pub/Sub トピックを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.pubsub.topic.publish
        resource: projects/${PROJECT_ID}/topics/${PUBSUB_TOPIC}
  - name: anotherFunction
    type: ...

params:
  - param: PUBSUB_TOPIC
    label: Pub/Sub topic
    description: Which Pub/Sub topic do you want to watch for messages?
    type: string
    default: topic-name  # Specifying a default is optional.
    required: true

Realtime Database

指定されたパターンに一致するパスが作成、更新、削除されたときに、Realtime Database によってトリガーされる関数が実行されます。

RTDB でトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Realtime Database トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { database } from "firebase-functions/v1";

export const yourFunctionName = database.ref("path/to/{item}")
  .onCreate((snapshot, context) => {
    // ...
  });

export const yourFunctionName2 = database.ref("path/to/{item}")
  .onUpdate((change, context) => {
    // ...
  });

export const yourFunctionName3 = database.ref("path/to/{item}")
  .onDelete((snapshot, context) => {
    // ...
  });

export const yourFunctionName4 = database.ref("path/to/{item}")
  .onWrite((change, context) => {
    // onWrite triggers on creation, update, and deletion.
    // ...
  });

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/path/to/{itemId}
  - name: anotherFunction
    type: ...

次の表は、サポートされている Cloud Firestore イベントの種類を指定する方法を示しています。

Cloud Functions のイベント トリガー eventType 説明
onCreate() providers/google.firebase.database/eventTypes/ref.create データを作成
onDelete() providers/google.firebase.database/eventTypes/ref.delete データを削除
onUpdate() providers/google.firebase.database/eventTypes/ref.update データを更新
onWrite() providers/google.firebase.database/eventTypes/ref.write データを作成、削除、更新

ユーザーが拡張機能をインストールするときに監視するパスを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: providers/google.firebase.database/eventTypes/ref.create
        # DATABASE_INSTANCE (project's default instance) is an auto-populated
        # parameter value. You can also specify an instance.
        resource: projects/_/instances/${DATABASE_INSTANCE}/refs/${DB_PATH}
  - name: anotherFunction
    type: ...

params:
  - param: DB_PATH
    label: Realtime Database path
    description: Where do you want to watch for changes?
    type: string
    default: path/to/{itemId}  # Specifying a default is optional.
    required: true

Remote Config

プロジェクトのパラメータ テンプレートが更新されると、Remote Config によってトリガーされる関数が実行されます。

Remote Config でトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Remote Config トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { remoteConfig } from "firebase-functions/v1";

export const yourFunctionName = remoteConfig.onUpdate((version, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.firebase.remoteconfig.update
        resource: projects/${PROJECT_ID}
  - name: anotherFunction
    type: ...

Cloud Storage

Cloud Storage でトリガーされる関数は、オブジェクトの作成、アーカイブ、削除時や、メタデータの変更時に実行されます。

Storage でトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Cloud Storage トリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { storage } from "firebase-functions/v1";

export const yourFunctionName = storage.object().onFinalize((object, context) => {
  // ...
});

export const yourFunctionName2 = storage.object().onMetadataUpdate((object, context) => {
  // ...
});

export const yourFunctionName3 = storage.object().onArchive((object, context) => {
  // ...
});

export const yourFunctionName4 = storage.object().onDelete((object, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        # STORAGE_BUCKET (project's default bucket) is an auto-populated
        # parameter. You can also specify a bucket.
        resource: projects/_/buckets/${STORAGE_BUCKET}
  - name: anotherFunction
    type: ...

次の表は、サポートされている Cloud Storage イベントの種類を指定する方法を示しています。

Cloud Functions のイベント トリガー eventType 説明
onFinalize() google.storage.object.finalize オブジェクトを作成
onMetadataUpdate() google.storage.object.metadataUpdate オブジェクトのメタデータを更新
onArchive() google.storage.object.archive オブジェクトをアーカイブ
onDelete() google.storage.object.delete オブジェクトを削除

ユーザーが拡張機能をインストールするときにストレージ バケットを構成できるようにするには、extension.yaml ファイルに新しいパラメータを追加し、関数の resource 宣言でそのパラメータを参照します。

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.storage.object.finalize
        resource: projects/_/buckets/${YOUR_BUCKET}
  - name: anotherFunction
    type: ...

params:
  - param: YOUR_BUCKET
    label: Cloud Storage bucket
    description: Which bucket do you want to watch for changes?
    type: selectResource
    resourceType: storage.googleapis.com/Bucket
    default: ${STORAGE_BUCKET}  # Specifying a default is optional.
    required: true

Test Lab

Test Lab でトリガーされる関数は、テスト マトリックスがテストを完了すると実行されます。

Test Lab でトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Firebase Test Lab のトリガーをご覧ください。

関数の定義(第 1 世代のみ)

import { testLab } from "firebase-functions/v1";

export const yourFunctionName = testLab.testMatrix().onComplete((matrix, context) => {
  // ...
});

リソース宣言(extension.yaml)

resources:
  - name: yourFunctionName
    type: firebaseextensions.v1beta.function
    properties:
      eventTrigger:
        eventType: google.testing.testMatrix.complete
        resource: projects/${PROJECT_ID}/testMatrices/{matrixId}
  - name: anotherFunction
    type: ...

Crashlytics アラートのトリガー

Crashlytics でトリガーされる関数は、Crashlytics がアラートを公開すると実行されます。

アラートによってトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Firebase アラート トリガーをご覧ください。

関数の定義(第 2 世代のみ)

import {
  onNewFatalIssuePublished,
  onNewNonfatalIssuePublished,
  onNewAnrIssuePublished,
  onRegressionAlertPublished,
  onVelocityAlertPublished,
  onStabilityDigestPublished,
} from "firebase-functions/v2/alerts/crashlytics";

export const yourFunctionName = onNewFatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName2 = onNewNonfatalIssuePublished((event) => {
  // ...
});

export const yourFunctionName3 = onNewAnrIssuePublished((event) => {
  // ...
});

export const yourFunctionName4 = onRegressionAlertPublished((event) => {
  // ...
});

export const yourFunctionName5 = onVelocityAlertPublished((event) => {
  // ...
});

export const yourFunctionName6 = onStabilityDigestPublished((event) => {
  // ...
});

リソース宣言(extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: crashlytics.newFatalIssue
  - name: anotherFunction
    type: ...

alerttype には、次の値を使用できます。

  • crashlytics.newFatalIssue
  • crashlytics.newNonfatalIssue
  • crashlytics.regression
  • crashlytics.stabilityDigest
  • crashlytics.velocity
  • crashlytics.newAnrIssue

Performance Monitoring アラート トリガー

Performance Monitoring によってトリガーされる関数は、Performance Monitoring がアラートを公開すると実行されます。

アラートによってトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Firebase アラート トリガーをご覧ください。

関数の定義(第 2 世代のみ)

import { onThresholdAlertPublished } from "firebase-functions/v2/alerts/performance";

export const yourFunctionName = onThresholdAlertPublished((event) => {
  // ...
});

リソース宣言(extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: performance.threshold
  - name: anotherFunction
    type: ...

App Distribution のアラート トリガー

App Distribution でトリガーされる関数は、App Distribution がアラートを公開すると実行されます。

アラートによってトリガーされる関数の作成方法については、Cloud Functions のドキュメントの Firebase アラート トリガーをご覧ください。

関数の定義(第 2 世代のみ)

import {
  onNewTesterIosDevicePublished,
  onInAppFeedbackPublished
} from "firebase-functions/v2/alerts/appDistribution";

export const yourFunctionName = onNewTesterIosDevicePublished((event) => {
  // ...
});

export const yourFunctionName2 = onInAppFeedbackPublished((event) => {
  // ...
});

リソース宣言(extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
      eventTrigger:
        eventType: google.firebase.firebasealerts.alerts.v1.published
        triggerRegion: global
        eventFilters:
          - attribute: alerttype
            value: appDistribution.inAppFeedback
  - name: anotherFunction
    type: ...

alerttype には、次の値を使用できます。

  • appDistribution.newTesterIosDevice
  • appDistribution.inAppFeedback

カスタム イベント トリガー(Eventarc)

Eventarc でトリガーされる関数は、特定のイベントタイプが特定のチャネルに公開されると実行されます。

Eventarc でトリガーされる関数の作成については、Cloud Functions のドキュメントのカスタム イベント トリガーの作成と処理をご覧ください。

また、拡張機能からイベントを公開し、ユーザーが拡張機能にカスタム ロジックを挿入できるようにすることもできます。拡張機能でデベロッパー提供のカスタム ロジックを使用するをご覧ください。

関数の定義(第 2 世代のみ)

import { onCustomEventPublished } from "firebase-functions/v2/eventarc";

export const yourFunctionName = onCustomEventPublished((event) => {
  // ...
});

リソース宣言(extension.yaml)

apis:
  - apiName: eventarc.googleapis.com
    reason: Powers all events and triggers
  - apiName: run.googleapis.com
    reason: Powers 2nd-gen functions

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      # LOCATION is a user-configured parameter value specified by the user
      # during installation.
      location: ${param:LOCATION}
      buildConfig:
        runtime: nodejs16
      serviceConfig:
        availableMemory: 512M
        timeoutSeconds: 60
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/us-central1/channels/firebase
  - name: anotherFunction
    type: ...

拡張機能のインストール時に、チャネルがすでに存在している必要があります。たとえば、チャネルを作成する別の拡張機能のカスタム イベントに依存している場合は、まずその拡張機能をインストールするようにユーザーに指示します。

上記の例では、us-central1 リージョンのデフォルトの Firebase チャネルのカスタム イベント トリガーを作成します。チャネル名とリージョンは、パラメータを使用してカスタマイズできます。次に例を示します。


params:
  - param: EVENTARC_CHANNEL_NAME
    label: Eventarc channel name
    description: What is the name of the Eventarc channel.
    default: firebase
    type: string
    required: true

resources:
  - name: yourfunctionname
    type: firebaseextensions.v1beta.v2function
    properties:
      location: ${param:LOCATION}
      eventTrigger:
        eventType: firebase.extensions.storage-resize-images.v1.complete
        channel: projects/${param:PROJECT_ID}/locations/${param:LOCATION}/channels/${param:EVENTARC_CHANNEL_NAME}