npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

swagger-express-middleware3

v0.1.5

Published

Swagger middleware and mocks for Express

Downloads

42

Readme

Swagger Express Middleware 3

This fork of the original Swagger Express Middleware has been created to implement OpenAPI 3 support

Many Thanks to the original author: James Messinger

Swagger middleware and mocks for Express.js

Build Status Dependencies Coverage Status Tested on APIs.guru Codacy Score Inline docs

npm License

Features

  • Supports Swagger 2.0 specs in JSON or YAML Swagger Express Middleware uses 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,000 real-world APIs from Google, Instagram, Spotify, etc. All tests are run on Mac, Linux, and Windows using all LTS versions of Node. But nothing's perfect, so if you find a bug, please report it.

  • Mock middleware 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 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 Parses incoming requests and converts everything into the correct data types, according to your Swagger API definition.

  • Validate Request middleware 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 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 Serves the Swagger API file(s) in JSON or YAML format so they can be used with front-end tools like Swagger UI, Swagger Editor, and Postman.

Installation and Use

Install using NPM.

npm install swagger-express-middleware

Then use it in your Node.js script like this:

const express = require('express');
const createMiddleware = require('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.

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.

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.

Contributing

I welcome any contributions, enhancements, and bug-fixes. File an issue on GitHub and submit a pull request.

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

OpenAPI 3 support

I need OpenAPI 3 support and will slowly be updating the code accordingly

  1. Parameters no longer have its type directly specified, this is no specified in a schema property.
  2. Update to latest swagger-parser

License

Swagger Express Middleware is 100% free and open-source, under the MIT license. Use it however you want.