Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/APIDevTools/swagger-express-middleware

Swagger 2.0 middlware and mocks for Express.js
https://github.com/APIDevTools/swagger-express-middleware

expressjs javascript middleware nodejs openapi rest-api server swagger

Last synced: 3 months ago
JSON representation

Swagger 2.0 middlware and mocks for Express.js

Awesome Lists containing this project

README

        

Swagger Express Middleware
============================
### Swagger 2.0 middleware and mocks for Express.js

> :warning: **No longer maintained**
> This package has been abandoned for some time, and there are plenty of modern alternatives around which support OpenAPI v3.x listed on [OpenAPI.Tools](https://openapi.tools/) which you can use instead.

[![npm](https://img.shields.io/npm/v/@apidevtools/swagger-express-middleware.svg)](https://www.npmjs.com/package/@apidevtools/swagger-express-middleware)
[![License](https://img.shields.io/npm/l/@apidevtools/swagger-express-middleware.svg)](LICENSE)
[![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen)](https://plant.treeware.earth/APIDevTools/swagger-express-middleware)

Features
--------------------------
- **Supports Swagger 2.0 specs in JSON or YAML**

Swagger Express Middleware uses [Swagger-Parser](https://github.com/APIDevTools/swagger-parser) to parse, validate, and dereference Swagger files. You can even split your spec into multiple different files using `$ref` pointers.

- **Thoroughly tested**

Over 1,000 unit tests and integration tests with 100% code coverage. Tested on [**over 1,500 real-world APIs**](https://apis.guru/browse-apis/) from Google, Microsoft, Facebook, Spotify, etc. All tests are run on Mac, Linux, and Windows using all LTS versions of Node.

- [**Mock middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/mock.html)

**Fully-functional mock** implementations for every operation in your API, including data persistence, all with **zero code!** This is a great way to test-drive your API as you write it, or for quick demos and POCs. You can even extend the mock middleware with your own logic and data to fill in any gaps.

- [**Metadata middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/metadata.html)

Annotates each request with all the relevant information from the Swagger definition. The path, the operation, the parameters, the security requirements - they're all easily accessible at `req.swagger`.

- [**Parse Request middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/parseRequest.html)

Parses incoming requests and converts everything into the correct data types, according to your Swagger API definition.

- [**Validate Request middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/validateRequest.html)

Ensures that every request complies with your Swagger API definition, or returns the appropriate HTTP error codes if needed. Of course, you can catch any validation errors and handle them however you want.

- [**CORS middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/CORS.html)

Adds the appropriate CORS headers to each request and automatically responds to CORS preflight requests, all in compliance with your Swagger API definition.

- [**Files middleware**](https://apitools.dev/swagger-express-middleware/docs/middleware/files.html)

Serves the Swagger API file(s) in JSON or YAML format so they can be used with front-end tools like [Swagger UI](http://www.swagger.io), [Swagger Editor](http://editor.swagger.io), and [Postman](http://getpostman.com).

Installation and Use
--------------------------
Install using [npm](https://docs.npmjs.com/about-npm/).

```bash
npm install @apidevtools/swagger-express-middleware
```
Then use it in your [Node.js](http://nodejs.org/) script like this:

```javascript
const express = require('express');
const createMiddleware = require('@apidevtools/swagger-express-middleware');

let app = express();

createMiddleware('PetStore.yaml', app, function(err, middleware) {
// Add all the Swagger Express Middleware, or just the ones you need.
// NOTE: Some of these accept optional options (omitted here for brevity)
app.use(
middleware.metadata(),
middleware.CORS(),
middleware.files(),
middleware.parseRequest(),
middleware.validateRequest(),
middleware.mock()
);

app.listen(8000, function() {
console.log('The PetStore sample is now running at http://localhost:8000');
});
});
```

Samples & Walkthroughs
--------------------------
Swagger Express Middleware comes two samples that use the [Swagger Pet Store API](https://github.com/APIDevTools/swagger-express-middleware/blob/master/samples/PetStore.yaml).

#### Sample 1
This sample demonstrates the most simplistic usage of Swagger Express Middleware. It simply creates a new Express Application and adds all of the Swagger middleware without changing any options, and without adding any custom middleware.

* [Source Code](https://github.com/APIDevTools/swagger-express-middleware/blob/master/samples/sample1.js)
* [Walkthrough](https://apitools.dev/swagger-express-middleware/docs/walkthroughs/running.html)

#### Sample 2
This sample demonstrates a few more advanced features of Swagger Express Middleware, such as setting a few options, initializing the mock data store, and adding custom middleware logic.

* [Source Code](https://github.com/APIDevTools/swagger-express-middleware/blob/master/samples/sample2.js)
* [Walkthrough](https://apitools.dev/swagger-express-middleware/docs/walkthroughs/walkthrough2.html)

Contributing
--------------------------
I welcome any contributions, enhancements, and bug-fixes. [Open an issue](https://github.com/APIDevTools/swagger-express-middleware/issues) on GitHub and [submit a pull request](https://github.com/APIDevTools/swagger-express-middleware/pulls).

#### Building/Testing
To build/test the project locally on your computer:

1. **Clone this repo**

`git clone https://github.com/APIDevTools/swagger-express-middleware.git`

2. **Install dependencies**

`npm install`

3. **Run the tests**

`npm test`

4. **Run the sample app**

`npm start`

License
--------------------------
Swagger Express Middleware is 100% free and open-source, under the [MIT license](LICENSE). Use it however you want.

This package is [Treeware](http://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/APIDevTools/swagger-express-middleware) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.

Big Thanks To
--------------------------
Thanks to these awesome companies for their support of Open Source developers ❤

[![Travis CI](https://jstools.dev/img/badges/travis-ci.svg)](https://travis-ci.com)
[![SauceLabs](https://jstools.dev/img/badges/sauce-labs.svg)](https://saucelabs.com)
[![Coveralls](https://jstools.dev/img/badges/coveralls.svg)](https://coveralls.io)