JSPM

express-openapi

0.2.18
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 37881
  • Score
    100M100P100Q141705F
  • License MIT

Effortlessly add routes and middleware to express apps with openapi documents.

Package Exports

  • express-openapi

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (express-openapi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

express-openapi NPM version Downloads Build Status Coveralls Status

Effortlessly add routes and middleware to express apps with openapi documents.

Highlights

Example

This uses the sample project located at ./test/sample-projects/basic-usage/. Check out the other sample projects for complete usage examples. You'll learn all you need to know by studying it and the related tests.

var app = require('express')();
var openapi = require('express-openapi');

openapi.initialize({
  apiDoc: require('./test/sample-projects/basic-usage/api-doc.js'),
  app: app,
  docsPath: '/api-docs',
  routes: './test/sample-projects/basic-usage/api-routes/'
});

API

.initialize(args)

Initializes routes and middleware on an express app.

args.apiDoc {Object}

This is an openapi (swagger 2.0) compliant document. See the OpenAPI-Specification for more details.

args.apiDoc.paths should be an empty object. express-openapi will populate this for you. This prevents you from defining your paths in 2 places.

args.apiDoc.basePath will add a prefix to all routes added by express-openapi.

args.apiDoc.definitions will be used for de-referencing $ref properties in parameters.

args.app {Object}

The express app you wish to initialize.

args.routes {String}

A path to the directory that contains your route files.

Route files are logically structured according to their URL path.

For example, if you have the following routes that you wish to add to your express app:

GET /v1/users/:id
POST /v1/users

You would define basePath: '/v1' in your apiDoc, and layout your routes directory as follows:

<project>
        `routes/
               `users/
                     `:id.js
                users.js

The contents of <project>/routes/users/:id.js would look like this:

module.exports = {
  get: [
    /* business middleware not expressible by openapi documentation goes here */
    function(req, res) {
      res.status(200).json(/* return the user or an error */);
    }
  ]
};

module.exports.get.apiDoc = {
  description: 'A description for retrieving a user.',
  tags: ['users'],
  operationId: 'getUser',
  parameters: [
    {
      in: 'path',
      name: 'id',
      required: true,
      type: 'integer'
    }
  ],
  responses: {
    default: {
      $ref: '#/definitions/Error'
    }
  }
};

Modules under args.routes expose methods. Methods may either be a method handler function, or an array of business specific middleware + a method handler function.

express-openapi will prepend middleware to this stack based on the parameters defined in the method's apiDoc property. If no apidoc property exists on the module method, then express-openapi will add no additional middleware.

args.docsPath {String} [optional]

Changes the default location (/api-docs) to serve up args.apiDoc with populated paths. This is imoprtant if you wish to support multiple versions of your app and for Swagger UI support.

args.errorTransformer {Function} [optional]

Transforms errors to a standard format as defined by the application. See express-openapi-validation#args.errorTransformer for more info.

LICENSE

The MIT License (MIT)

Copyright (c) 2016 Kogo Software LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.