[英]`httpsCallable` vs. `httpsCallableFromURL` callable Firebase Functions
[英]Is it possible to specify input and return types for httpsCallable functions?
我在應用程序中使用可調用的 function 來更新用戶聲明。 firebase 函數在 Typescript 中,並且有一個接口來描述 function 所需的數據形狀。
我想在客戶端做同樣的事情,這樣團隊中的任何開發人員都可以快速找出對雲 function 的要求,而無需查看函數目錄中的代碼。
functions/src/index.ts
:
// This is the required data, that I would like to specify client side
interface GivePermissionsParams {
uid: string;
email: string;
newClaim: Partial<Permissions>;
}
/**
* Callable function to give a user new permissions in the form
* of custom claims and firestore properties
*/
exports.givePermission = functions.https.onCall(
async (data: GivePermissionsParams, context) => {
if (!context.auth?.token.admin) {
throw new HttpsError(
'permission-denied',
`Non admin user ${context.auth?.uid} attempted to update permissions`
);
}
return grantPermission(data.uid, data.newClaim).then(() => {
log(`Successfully updated permissions for ${data.email}`);
return {
result: `Successfully updated permissions for ${data.email}`,
};
});
}
);
客戶端使用:
// firebase.ts
// I would like to specify the function param and return types here.
// eg: httpsCallable<myParamsType, myReturnType>
export const givePermission = httpsCallable(functions, 'givePermission');
// in my reactComponent.tsx
const changePermission = async (permission: string, value: boolean) => {
// This payload should match the GivePermissionsParams type as defined in the functions index.ts file.
const functionParams = {
uid: user.uid,
email: user.email,
newClaim: {[permission]: value}
}
const functionRes = await givePermission(functionParams);
};
看來解決方案就是您正在嘗試做的事情。 您可以為請求數據和響應指定類型,如下所示:
interface ReqInterface {
uid: string;
email: string;
newClaim: Partial<Permissions>;
}
interface ResInterface {
result: string;
}
const givePermission = httpsCallable<ReqInterface, ResInterface>(functions, 'givePermission')
const { data } = await givePermission({ url })
// data is ResInterface
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.