问题描述
我有类似的功能(在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/express
与npm 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请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!