简体   繁体   English

是否可以为 httpsCallable 函数指定输入和返回类型?

[英]Is it possible to specify input and return types for httpsCallable functions?

I am using a callable function in app to update user claims.我在应用程序中使用可调用的 function 来更新用户声明。 The firebase functions are in Typescript and there is an interface to describe the shape of data that the function requires. firebase 函数在 Typescript 中,并且有一个接口来描述 function 所需的数据形状。

I would like to do the same thing client side, so that any developer on the team can quickly find out what the requirements for the cloud function are, without looking at the code in the functions directory.我想在客户端做同样的事情,这样团队中的任何开发人员都可以快速找出对云 function 的要求,而无需查看函数目录中的代码。

The Firebase Cloud Function in functions/src/index.ts : 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}`,
      };
    });
  }
);

Client side usage:客户端使用:

// 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);
  };

It seems the solution is what you are trying to do.看来解决方案就是您正在尝试做的事情。 You can specify types for for request data and response like this:您可以为请求数据和响应指定类型,如下所示:

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.

相关问题 `httpsCallable` 与 `httpsCallableFromURL` 可调用 Firebase 函数 - `httpsCallable` vs. `httpsCallableFromURL` callable Firebase Functions 快速解码 httpsCallable 云函数响应 - Swift decode httpsCallable cloud functions response 我们如何更改 firebase.functions().httpsCallable 中的 responseType - how can we change responseType in firebase.functions().httpsCallable 如何将 Firebase Functions v8 添加到 Swift iOS 项目并在 httpsCallable 中使用 Codable? - How to add Firebase Functions v8 to a Swift iOS project and use Codable in httpsCallable? Flutter Firebase 使用 httpsCallable 删除 function - Flutter Firebase delete function with httpsCallable 是否可以使用 cli 工具在 .zip 压缩标准输入到标准 output 中指定一个文件名? - Is it possible to specify a file name inside .zip archive compressing standard input to standard output using cli tools? Python - PIP:是否可以指定架构? - Python - PIP : Is it possible to specify architecture? Firebase + Nuxt SSR - httpsCallable 错误“未定义标头” - Firebase + Nuxt SSR - httpsCallable error "Headers is not defined" AWS Step 函数:输入参数 - AWS Step Functions: Input Parameter 是否可以在一个初始化容器中指定多个图像? - is it possible to specify multiple images in an init container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM