Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to annotate Express middlewares with JSDoc?

I'm trying to document an Express middleware, but the build-in validation tool in WebStorm tells me that types are incorrectly assigned in the following JSDoc block:

/**  * My middleware.  *  * @param {Object} req  * @param {Object} res  * @param {Function} next  * @return {Object}  */ exports.show = function(req, res, next) {     ... }; 

In Express sources, I didn't find any @typedefs to help me. Also, I want to avoid things like @param {*}.

What is the correct way to document Express middleware using JSDoc? Thanks for any help.

like image 629
Jun Thong Avatar asked Dec 03 '14 08:12

Jun Thong


Video Answer


1 Answers

You can document your middleware with

const express = require("express");  /**  * @param {express.Request} req  * @param {express.Response} res  * @param {express.NextFunction} next  */ function (req, res, next) {} 

when you have middleware that add attribute to req, you can also add them with

const express = require("express");  /**  * @param {express.Request & {specialParam1 : string, specialParam2 : any}} req  * @param {express.Response} res  * @param {express.NextFunction} next  */ function (req, res, next) {} 

or event better, create a typedef for each source of new elem added on "req" and use "&" to create a type combining them all.

like image 98
Félix Brunet Avatar answered Sep 28 '22 02:09

Félix Brunet