如何将 firebase 的 Cloud Vision 函数示例翻译成 javascript?

How to translate Cloud Vision function sample for firebase to javascript?

我想将云视觉功能集成到我的 firebase 应用程序中,而不必此时将我的项目迁移到打字稿。我所有的云函数都是用javascript写的。如何将 here(下面供参考)提供的打字稿示例翻译成普通的旧 javascript?

import vision from "@google-cloud/vision";

const client = new vision.ImageAnnotatorClient();

export const annotateImage = functions.https.onCall(async (data, context) => {
  if (!context.auth) {
    throw new functions.https.HttpsError(
      "unauthenticated",
      "annotateImage must be called while authenticated."
    );
  }
  try {
    return await client.annotateImage(JSON.parse(data));
  } catch (e) {
    throw new functions.https.HttpsError("internal", e.message, e.details);
  }
});

我在您共享的代码中没有看到任何类型注释,所以它看起来已经是那种普通的 JavaScript 了。您应该能够将其粘贴到您的 index.js 文件中并按原样使用它。

如果您发现包含类型注释的文件并且想在纯 JavaScript 设置中使用它,您可以使用像 TypeScript playground 这样的工具来 convert TypeScript to JavaScript.