简体   繁体   English

在 Node 和 Typescript 中的 Firebase 云函数中进行前提条件检查的简单方法是什么?

[英]What's a simple way to do precondition checks in Firebase cloud functions in Node and Typescript?

The goal is to enforce that some required parameter has been successfully passed, it's of the right type and otherwise, throw a correct and informative error.目标是强制某些必需的参数已成功传递,它是正确的类型,否则抛出正确且信息丰富的错误。

If-Statements If 语句

This works, but requires too much boilerplate code.这可行,但需要太多样板代码。

export const joinSocialRequest = functions.https.onCall(async (data, context) => {
    const eventId = data.eventId
    if (eventId === undefined) {
       throw new functions.https.HttpsError('unknown','Missing param eventId.')
    }
    ...
}

Using preconditions-ts or preconditions NPM package使用前提条件-ts 或前提条件NPM package

This is better, but all errors are thrown as "Internal", which doesn't let my client engineers know what's going on!这样更好,但是所有错误都作为“内部”抛出,这不会让我的客户工程师知道发生了什么!

import { requiredStringFrom } from 'preconditions-ts';
export const acceptParticipantRequest = functions.https.onCall(async (data, context) => {
    const eventId = requiredStringFrom(data, "eventId")
    ...
}

I can probably wrap the above in a try/catch and throw the correct error, but that means, I will need to write a lot of wrappers!我可能可以将上面的内容包装在 try/catch 中并抛出正确的错误,但这意味着,我将需要编写很多包装器!

Is there a better way?有没有更好的办法?

You could write your own validation assertion helper:您可以编写自己的验证断言助手:

function validate(condition, message, code = 'invalid-argument') {
  if (!condition) {
    throw new functions.https.HttpsError(code, message);
  }
}

export const joinSocialRequest = functions.https.onCall(async (data, context) => {
    const eventId = data.eventId
    validate(eventId !== undefined, 'Missing param eventId.');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 firebase 云功能中具有 typescript 的 firebase-admin - firebase-admin with typescript in firebase cloud functions 我正在尝试将我的简单 typescript 应用程序部署到 firebase 云功能。 但我有这个错误 - I'm trying to deploy my simple typescript app to firebase cloud functions. But I have got this error React + Firebase Cloud Functions in Typescript部署失败 - React + Firebase Cloud Functions in Typescript fails to deploy 有没有办法在没有 Cloud Functions 的情况下在简单的 NodeJS 中使用 Firebase RTDB 触发器? - Is there a way to use Firebase RTDB triggers inside simple NodeJS without Cloud Functions? 为什么 Firebase Cloud Functions 上的 Node 版本这么旧? - Why is the Node version on Firebase Cloud Functions so old? 优化node.js中的Firebase云函数结构 - Optimizing Firebase cloud functions structure in node.js 如何在 firebase 云函数中正确地将 signInWithEmailAndPassword() 转换为 createCustomToken() - Node.js - How to properly convert signInWithEmailAndPassword() to createCustomToken() in firebase cloud functions - Node js Cloud Functions for Firebase 超时 - Cloud Functions for Firebase timeout 如何避免 Firebase 云函数可能出现的竞争条件? - How do you avoid a possible race condition with firebase cloud functions? 如何在 Cloud Functions for Firebase 中获取服务器时间戳? - How do I get the server timestamp in Cloud Functions for Firebase?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM