Generating documentation with Compodoc and JSDoc
The reality of software projects is that people sometimes come and go. Therefore, the bigger our application is, the higher value we put on the ability to more…
The reality of software projects is that people sometimes come and go. Therefore, the bigger our application is, the higher value we put on the ability to more easily introduce new teammates into the codebase. One of the ways to improve this process is to create documentation. This way, we can accumulate knowledge even if the people that wrote the code are no longer around and can’t explain their thought process in person. The main issue is that writing and maintaining documentation takes valuable time. Since we could spend it on writing the actual code, it is sometimes difficult to justify it to the business side of our project. After all, time is money. Writing documentation can save us time in the long run, though. Fortunately, some tools can make writing documentation more manageable and less time-consuming. An interesting fact about NestJS is that it shares a considerable chunk of the approach to the code structure with Angular. Because of that, a tool called Compodoc made with Angular in mind works with NestJS applications too. Introducing Compodoc Compodoc aims to generate the documentation for our project. It is open-source and can work offline. While it automatically creates the NestJS documentation, there are many ways to expand and customize it. To start working with Compodoc, we first need to install it.npm install @compodoc/compodocA fitting way to run it is to add it to the scripts in our package.json: package.json "scripts": { "start": "nest start", "documentation:serve": "npx @compodoc/compodoc -p tsconfig.json --serve", // ... },Thanks to adding the -serve, Compodoc generates the HTML files with our NestJS documentation and serves it at http://localhost:8080. There are other flags that you might find useful. Type npx @compodoc/compodoc -help for a full list. For example, we can use --theme to choose one of the available themes. The cool thing that we see right away in the project overview is a visual representation of our whole architecture with our modules and services. If our project is big, it can help get a better grasp of the code structure and how various modules work with each other. Documenting individual files An interesting thing about Compodoc is that it analyzes not only modules and their dependencies but also individual files. Fortunately, it can interpret our TypeScript code along with the information about the types. So, for example, we can click on CreateCategoryDto above. This way, we can get more details about the argument of the createCategory method. Providing additional information with JSDoc While Compodoc can analyze our TypeScript code, we can help it by using the JSDoc markup language. It is a well-known way of writing comments in our code that various tools and IDEs can understand and support./** * A method that fetches the categories from the database */ getAllCategories() { return this.categoriesRepository.find({ relations: ['posts'] }); }Compodoc supports the following tags from JSDoc: @returns With the @returns tag, we can provide the return type of a method and describe it. However, since we use TypeScript, we can use it to define the return type instead./** * A method that fetches the categories from the database * @returns A promise with the list of categories */ getAllCategories(): Promise<Category[]> { return this.categoriesRepository.find({ relations: ['posts'] }); } @param By using the @param tag, we provide the type of the argument and additional description. Again, we can use TypeScript to define the type of the argument./** * A method that deletes a category from the database * @param id An id of a category. A category with this id should exist in the database */ async deleteCategory(id: number): Promise