[英]How do you add nested params to tsdoc documentation
The code below shows functionality that can potentially be used to register a user.下面的代码显示了可能用于注册用户的功能。 The request body takes in multiple nested parameters such as
firstName
, lastName
, and more.请求正文接受多个嵌套参数,例如
firstName
、 lastName
等。 How can I adequately document these nested parameters with tsdoc?如何使用 tsdoc 充分记录这些嵌套参数?
/**
* @remarks
* This method creates a user.
*
* @param req - The request object
* @param res - The response object
*
* @returns Created user object showing the created user
* ```
*{
"message": {
"_id": "5ef3249f6a01c1006e091f92",
"id": "1u_RGoYwV",
"firstName": "test",
"lastName": "User",
"password": "$2a$10$ZQf23Qx910iJLzHO65BOO.RShufiU.YAT/IXnGQwreQ0rdoElrSQG",
"email": "test@gmail.com",
"avatarUrl": "https://www.image.com/cat.jpg",
* }
* }
* ```
*
*/
async create(req: Request, res: Response) {
let user = new UserModel(this.db);
const {
firstName,
lastName,
email,
password,
currentOrganization,
avatarUrl
} = req.body;
const hashedPassword = await PasswordHelper.hashPassword(password);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
user.setPassword(hashedPassword);
user.setAvatarUrl(avatarUrl);
user.setCurrentOrganization(currentOrganization);
await user.save();
const users = user.get();
res.status(200).send({ message: users });
}
Declare an interface, document the properties there and reference the interface in the function.声明一个接口,在那里记录属性并引用 function 中的接口。
Note that cross referencing (eg using @see
and @link
) does not appear to be well supported in general in editors.请注意,交叉引用(例如,使用
@see
和@link
)在编辑器中似乎没有得到很好的支持。 Though just having the name there may help people find the interface.虽然只是有名称可能会帮助人们找到界面。
Judging by the usage, I assume your Request
and Response
represent not the native Request
and Response
types, but the ones provided by Express.从用法来看,我假设您的
Request
和Response
不是原生的Request
和Response
类型,而是 Express 提供的类型。
The express.Request<P>
type accepts a type argument P
. express.Request<P>
类型接受类型参数P
。 This type represents the parameters provided in the request.该类型表示请求中提供的参数。
The easiest way to provide (and document) them is by creating an interface:提供(和记录)它们的最简单方法是创建一个接口:
interface Payload {
/**
* First name of the user.
*/
firstName: string;
lastName: string;
email: string;
password: string;
currentOrganization: string;
avatarUrl: string;
[index: string]: string;
}
Feed that interface to your method:将该接口提供给您的方法:
async create(req: Request<Payload>, res: Response) {
The provided params will be accessible under req.params
— they will also have inline docs!提供的参数可以在
req.params
下访问——它们也有内联文档!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.