You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
hapi-swagger no longer bundles joi to fix #648. Install hapi-swagger with peer dependencies using:
npx install-peerdeps hapi-swagger
If you want to view the documentation from your API you will also need to install the inert and vision plugs-ins which support templates and static
content serving.
In your Hapi apps please check the main JavaScript file and add the following code to already created a Hapi server object.
You will also add the routes for you API as describe on hapi website.
As a project may be a mixture of web pages and API endpoints you need to tag the routes you wish Swagger to
document. Simply add the tags: ['api'] property to the route object for any endpoint you want documenting.
You can even specify more tags and then later generate tag-specific documentation. If you specify
tags: ['api', 'foo'], you can later use /documentation?tags=foo to load the documentation on the
HTML page (see next section).
{method: 'GET',path: '/todo/{id}/',options: {handler: handlers.getToDo,description: 'Get todo',notes: 'Returns a todo item by the id passed in the path',tags: ['api'],// ADD THIS TAGvalidate: {params: Joi.object({id : Joi.number().required().description('the id for the todo item'),})}},}
Once you have tagged your routes start the application. The plugin adds a page into your site with the route /documentation,
so the the full URL for the above options would be https://localhost:3000/documentation.
Typescript
hapi-swagger exports its own typescript definition file that can be used when registering the plugin with Hapi. See example below:
Install Typescript Definition Files
npm i @types/hapi__hapi @types/hapi__inert @types/hapi__joi @types/hapi__vision @types/node hapi-swagger --save-dev
Register Plugin with Typescript
import*asHapifrom'@hapi/hapi';import*asHapiSwaggerfrom'hapi-swagger';// code omitted for brevityconstswaggerOptions: HapiSwagger.RegisterOptions={info: {title: 'Test API Documentation'}};constplugins: Array<Hapi.ServerRegisterPluginObject<any>>=[{plugin: Inert},{plugin: Vision},{plugin: HapiSwagger,options: swaggerOptions}];awaitserver.register(plugins);
I would like to thank all that have contributed to the project over the last couple of years. This is a hard project to maintain, getting Hapi to work with Swagger is like putting a round plug in a square hole. Without the help of others it would not be possible.