Saving the swagger document
We have covered all of the available decorators in the Nest.js swagger module and the decorators already available in Nest.js to produce a swagger document and expose the swagger UI. This works great when your APIs are primarily used by developers in their own projects or when testing the APIs on a local development server or in a staging environment. For APIs that are primarily used for a specific front-end application, you may not wish to expose the swagger UI for the general public to be able to use. In such a case, you can still produce a swagger document for storage and use it on your own or your teams other projects.
To accomplish this, we will write a new Typescript file that can be executed as part of a build chain. We will use the fs-extras
NodeJS module to make writing our file to disk much simpler.
import
*
as
fs
from
'fs-extra'
;
async
function
writeDoc() {
const
app
=
await
NestFactory
.
create
(
AppModule
);
const
document
=
SwaggerModule...