截至 2022 年 8 月 9 日 Cloud Functions 2nd Gen become Generally Available,情况可能发生了变化,因此我将记录在 TypeScript 项目中对我有用的内容。
第一代
客户
“火力基地”:“9.9.3”
import { httpsCallable } from "firebase/functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";
// ...
const addTwoNumbersFuncCallable = httpsCallable<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
firebaseFunctions,
"addTwoNumbersFunc",
);
const result = await addTwoNumbersFuncCallable({
firstNum: 3,
secondNum: 5
});
console.log("Result", result.data);
服务器
“firebase 函数”:“^3.21.0”
import * as functions from "firebase-functions";
import { AddTwoNumbersInputParams, AddTwoNumbersInputResult } from "./util/shared/my-types";
// ...
export const addTwoNumbersFunc = functions.https.runWith({ memory: "1GB" }).onCall((params: AddTwoNumbersInputParams, ctx): AddTwoNumbersInputResult => {
if (!ctx.auth) {
throw new functions.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
}
return { result: params.firstNum + params.secondNum };
}
共享
为了在我的客户端和服务器代码之间共享 TypeScript 接口 AddTwoNumbersInputParams 和 AddTwoNumbersInputResult,我创建了一个指向目录 util/shared 的符号链接,该目录在名为 my-types.ts 的文件中包含以下定义:
export interface AddTwoNumbersInputParams {
firstNum: number
secondNum: number
}
export interface AddTwoNumbersInputResult {
result: number
}
第二代
客户
“火力基地”:“9.9.3”
import { httpsCallableFromURL } from "firebase/functions"; // see note1, note2, note3 below
// ...
const addTwoNumbersFuncCallable = httpsCallableFromURL<AddTwoNumbersInputParams, AddTwoNumbersInputResult>(
firebaseFunctions,
"https://addtwonumbersfunc-fri67ycgta-uc.a.run.app", // see note1, note2, note3 below
);
const result = await addTwoNumbersFuncCallable({
firstNum: 3,
secondNum: 5
});
console.log("Result", result.data);
注意1:callable documentation和http-events documentation都说firebase deploy命令应该输出URL,但我没有看到。我通过访问这里获得了它:
- Firebase Console
- 单击您的项目
- 单击“功能”(在左侧的目录中,如果没有看到,请单击“所有产品”,然后单击“功能”)
- 在触发器列中复制函数的 URL;它应该是
https://<lowercase_func_name>-<random-hash>-<region>.a.run.app的形式
注 2:起初我担心第二代函数会在我的持续集成管道中引入手动步骤,因为它们现在输出一个 URL(或者 it said)。有几个 Firebase 项目代表不同的阶段以促进生产 (Google's recommendation),我想我会遇到新的麻烦来复制和粘贴第二代功能的每个部署的 URL。幸运的是,它并没有我想象的那么糟糕,因为"the URL remains stable after deployment"。因此,我只需部署一次即可获取 URL,将其插入我的客户端代码,此后每次部署都保持不变。也就是说,它仍然是我的每个 Firebase 项目的不同 URL。所以我将不得不做更多的工作来促进生产。但也许他们会解决这个问题,因为他们说了"In a future release, 2nd gen function URLs will be updated to be both stable and deterministic."。
注 3:我发现 URL 的东西太麻烦了,所以我尝试不使用它并在 Firebase Functions 模拟器上取得了成功,但在真正的 Firebase 项目上却失败了。使用模拟器,我几乎可以继续使用接受函数名称的 httpsCallable() 函数,而不是需要 URL 的 httpsCallableFromURL() 函数。它适用于模拟器,但不适用于真正的 Firebase 项目。
服务器
“firebase 函数”:“^3.21.0”
import * as functionsV2 from "firebase-functions/v2";
import {CallableRequest} from "firebase-functions/lib/common/providers/https";
import {HttpsOptions} from "firebase-functions/lib/v2/providers/https";
// ...
const httpsOptions: HttpsOptions = {
memory: "16GiB" // see note4 below
};
// see note5 below
export const addtwonumbersfunc = functionsV2.https.onCall(httpsOptions, (request: CallableRequest<AddTwoNumbersInputParams>): AddTwoNumbersInputResult => {
if (!request.auth) {
throw new functionsV2.https.HttpsError("unauthenticated", "You must be logged in to call server-side functions");
}
return { result: request.data.firstNum + request.data.secondNum };
});
注意4:用于设置内存(等)的runWith() 语法似乎已更改为接受HttpsOptions 的HttpsOptions object that you pass to the onCall(),例如memory。第 2 代令人兴奋的事情之一是它提供了 higher memory allocations than 1st Gen,因此我已经在此处进行了演示,但从“1GB”增加到“16GiB”(另请注意从“GB”到“GiB”的变化)。
注5:"Function names are restricted to lowercase letters, numbers, and dashes."但希望很快就会出现"Support for using capital letters in function names."
共享
无需更改