我在應用程式中使用可呼叫函式來更新用戶宣告。firebase 函式在 Typescript 中,并且有一個介面來描述函式所需的資料形狀。
我想在客戶端做同樣的事情,這樣團隊中的任何開發人員都可以快速找到云功能的需求,而無需查看功能目錄中的代碼。
Firebase 云功能在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);
};
uj5u.com熱心網友回復:
看來解決方案就是您正在嘗試做的事情。您可以為請求資料和回應指定型別,如下所示:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/507502.html
標籤:Google Cloud Collective 打字稿 火力基地 谷歌云功能
上一篇:react-nativefirestore:使用不同欄位查詢where()和orderBy()
下一篇:使用云功能安排日常會議
