What is swagger-ui-react?
The swagger-ui-react npm package is a React component for integrating the Swagger UI into a React application. Swagger UI allows developers to visualize and interact with the API's resources without having any of the implementation logic in place. It is particularly useful for API documentation and testing.
What are swagger-ui-react's main functionalities?
Basic Integration
This code demonstrates how to integrate Swagger UI into a React application using the swagger-ui-react package. It imports the necessary components and styles, and then renders the Swagger UI with a given URL to the Swagger JSON.
import React from 'react';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';
const App = () => (
<SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
);
export default App;
Custom Configuration
This code sample shows how to customize the Swagger UI component by passing additional props. In this example, the `docExpansion` prop is set to 'none' to collapse all sections by default, and `defaultModelsExpandDepth` is set to -1 to hide the models section.
import React from 'react';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';
const App = () => (
<SwaggerUI
url="https://petstore.swagger.io/v2/swagger.json"
docExpansion="none"
defaultModelsExpandDepth={-1}
/>
);
export default App;
Using Local Swagger JSON
This example demonstrates how to use a local Swagger JSON file with the Swagger UI component. The local Swagger JSON file is imported and passed to the `spec` prop of the SwaggerUI component.
import React from 'react';
import SwaggerUI from 'swagger-ui-react';
import 'swagger-ui-react/swagger-ui.css';
import swaggerDocument from './swagger.json';
const App = () => (
<SwaggerUI spec={swaggerDocument} />
);
export default App;
Other packages similar to swagger-ui-react
redoc
Redoc is another popular tool for API documentation. It provides a more modern and responsive UI compared to Swagger UI. Redoc is known for its clean and customizable interface, and it supports OpenAPI 3.0. It is often preferred for its aesthetics and ease of use.
swagger-jsdoc
Swagger-jsdoc is a tool that allows you to integrate JSDoc comments in your codebase to generate Swagger documentation. It is useful for those who prefer to keep their API documentation close to their code. Unlike swagger-ui-react, it focuses on generating the Swagger JSON rather than rendering it.
api-docs
API Docs is a lightweight alternative for generating API documentation. It is less feature-rich compared to Swagger UI but can be a good choice for smaller projects or those who need a simple and quick way to document their APIs.
swagger-ui-react
swagger-ui-react
is a flavor of Swagger UI suitable for use in React applications.
It has a few distinctions from the mainstream version of Swagger UI:
- Exports a component instead of a constructor function
- Declares
react
and react-dom
as peerDependencies
Versions of this module mirror the version of Swagger UI included in the distribution.
Quick start
Install swagger-ui-react
:
$ npm i --save swagger-ui-react
Use it in your React application:
import SwaggerUI from "swagger-ui-react"
import "swagger-ui-react/swagger-ui.css"
export default AppComponent = () => <SwaggerUI url="https://petstore.swagger.io/v2/swagger.json" />
Configuration
TODO
Limitations
- Not all configuration bindings are available.
- Custom plugins are not officially supported.
- OAuth redirection handling is not supported.
- Topbar/Standalone mode is not supported.
For anything else, check the Swagger-UI repository.