我有这样的功能(在 Node.js/Firebase 中),并且想知道如何添加 JSDoc 格式的文档:
exports.getUserRes = functions.https.onRequest(async (request, response) => {...}
如何将 GET/POST/etc 参数记录到request
?
我将其他答案结合起来如何使用 JSDoc 对 Express 中间件进行注释? https://stackoverflow.com/questions/27266857/how-to-annotate-express-middlewares-with-jsdoc/65108929并修改一些代码,
它可以包括定义的所有方法/属性express.Request
和事件自定义请求正文。
它不仅可以用在request.body
,而且还支持req.query
.
那是因为express.Request
支持泛型,所以我们可以在 JSDOC 中使用它。
首先,记得安装@types/express
with npm install --save-dev @types/express
.
其次,像下面的代码一样设置。
// @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中使用它。
定义的其他方法/属性express.Request
, 例如req.headers
req.body
hint
req.query
hint
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)