如何将jsdoc参数发送到Web请求

如何将jsdoc参数发送到Web请求

本文介绍了如何将jsdoc参数发送到Web请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类似的功能(在Node.js/Firebase中),想知道如何以JSDoc格式添加文档:

I have functions like this (in Node.js/Firebase) and wonder how to add documentation i JSDoc format:

exports.getUserRes = functions.https.onRequest(async (request, response) => {...}

如何将GET/POST/etc参数记录到request?

How do I document the GET/POST/etc parameters to the request?

推荐答案

我在并修改一些代码,
它可能包含express.Request和事件自定义请求主体上定义的所有方法/属性.
它不仅可以在request.body中使用,而且还可以在req.query中使用.那是因为express.Request支持泛型,所以我们可以在JSDOC中使用它.

I combine other answer in How to annotate Express middlewares with JSDoc? and modify some code,
it could include all of the methods/properties defined on express.Request and event custom request body.
It could not only use in request.body, but also support in req.query.
That because express.Request support generics, so we could use this in JSDOC.

首先,请记住将@types/expressnpm install --save-dev @types/express一起安装.

First, remember to install @types/express with npm install --save-dev @types/express.

第二,按照下面的代码进行设置.

Second, setup like following code.

// @ts-check
/**
 * @typedef {object} showRequestBody
 * @property {string} name this is name in request body
 * @property {number} age this is age in request body
 *
 * @typedef {object} showRequestQuery
 * @property {string} name this is name in query
 * @property {number} age this is age in query
 *
 * @param {import('express').Request<{}, {}, showRequestBody, showRequestQuery>} req
 * @param {import('express').Response} res
 * @param {import('express').NextFunction} next
 */
exports.show = function(req, res, next) {
};

注意:我在vscode中使用它.

Note: I use it in vscode.

express.Request上定义的其他方法/属性,例如req.headers

other methods/properties defined on express.Request, for example req.headers

req.body提示

req.query提示

这篇关于如何将jsdoc参数发送到Web请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:44