How to use swagger-tools - 10 common examples

To help you get started, we’ve selected a few swagger-tools examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / swagger-tools / swagger-tools-tests.ts View on Github external
const options = {
    controllers: './controllers',
    useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
};

const swaggerUiOptions = {
    apiDocs: 'apiDocs',
    swaggerUi: 'swaggerUi',
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
// tslint:disable-next-line no-var-requires
const swaggerDoc20 = require('./api/swagger.json');

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc20, middleware => {
    // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
    app.use(middleware.swaggerMetadata());

    // Validate Swagger requests
    app.use(middleware.swaggerValidator());

    // Route validated requests to appropriate controller
    app.use(middleware.swaggerRouter(options));

    // Serve the Swagger documents and Swagger UI
    app.use(middleware.swaggerUi(swaggerUiOptions));
    app.use(middleware.swaggerUi());

    // Start the server
    createServer(app).listen(serverPort, () => {
        console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
github GoogleCloudPlatform / esp-v2 / tests / endpoints / bookstore / http_bookstore.js View on Github external
app.post('/echo/auth', echo);
  app.get('/echo_token/disable_auth', echoToken);
  app.get('/echo_token/default_enable_auth', echoToken);

  // Install logging middleware for all other paths.
  if (options.log === true) {
    app.use(function(req, res, next) {
      console.log(req.method, req.originalUrl);
      next();
    });
  }


  if (options.swagger) {
    // Initialize the Swagger UI middleware.
    swaggerTools.initializeMiddleware(options.swagger, function(middleware) {
      // Serve the Swagger documents and Swagger UI.
      app.use(middleware.swaggerUi());
    });
  }

  // The bookstore example uses a simple, in-memory database
  // for illustrative purposes only.
  var bookstoreDatabase = {
    shelves: {},
    id: 0
  };

  function createShelf(theme) {
    var id = ++bookstoreDatabase.id;
    var shelf = {
      name: 'shelves/' + id,
github cloudendpoints / esp / test / bookstore / bookstore.js View on Github external
app.get('/readiness_check', function(req, res) {
    res.status(200).json({
      msg: 'ready'
    });
  });

  app.get('/liveness_check', function(req, res) {
    res.status(200).json({
      msg: 'alive'
    });
  });


  if (options.swagger) {
    // Initialize the Swagger UI middleware.
    swaggerTools.initializeMiddleware(options.swagger, function(middleware) {
      // Serve the Swagger documents and Swagger UI.
      app.use(middleware.swaggerUi());
    });
  }

  // The bookstore example uses a simple, in-memory database
  // for illustrative purposes only.
  var bookstoreDatabase = {
    shelves: {},
    id: 0
  };

  function createShelf(theme) {
    var id = ++bookstoreDatabase.id;
    var shelf = {
      name: 'shelves/' + id,
github apigee-127 / swagger-tools / examples / 2.0 / index.js View on Github external
var http = require('http');
var swaggerTools = require('swagger-tools');

var serverPort = 3000;

// swaggerRouter configuration
var options = {
  controllers: './controllers',
  useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var swaggerDoc = require('./api/swagger.json');

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  // Start the server
  http.createServer(app).listen(serverPort, function () {
    console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
  });
github kube-HPC / hkube / core / api-server / lib / apiServer.js View on Github external
// swaggerRouter configuration
    const options = {
      swaggerUi: path.join(__dirname, '/../api/swagger.json'),
      controllers: path.join(__dirname, 'controllers'),
      useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
    };

    // The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
    let spec = fs.readFileSync(path.join(__dirname, '../api/swagger.yaml'), 'utf8');
    let swaggerDoc = jsyaml.safeLoad(spec);

    //register answering mechanism


    // Initialize the Swagger middleware
    await swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {

      // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
      app.use(middleware.swaggerMetadata());

      // Validate Swagger requests
      app.use(middleware.swaggerValidator());

      // Route validated requests to appropriate controller
      app.use(middleware.swaggerRouter(options));

      // Serve the Swagger documents and Swagger UI
      app.use(middleware.swaggerUi());

      // Start the server
      http.createServer(app).listen(serverPort, function () {
        console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
github hubrixco / logjack / restapi / index.js View on Github external
const TESTENV = process.env.TEST || false;
if(TESTENV)
  serverHost = 'localhost';

// swaggerRouter configuration
var options = {
  controllers: path.join(__dirname, './controllers'),
  useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {

  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Start the server
  http.createServer(app).listen(serverPort, serverHost, function () {
    console.log('Your server is listening on: ' + serverHost + ":" + serverPort);
  });

});

module.exports = app;
github bradyhouse / house / fiddles / java / fiddle-0006-j2v8RestApi / j2v8RestApi / src / main / nodejs / index.js View on Github external
var jsyaml = require('js-yaml');
var serverPort = 8080;

// swaggerRouter configuration
var options = {
  swaggerUi: path.join(__dirname, '/swagger.json'),
  controllers: path.join(__dirname, './controllers'),
  useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};

// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname,'api/swagger.yaml'), 'utf8');
var swaggerDoc = jsyaml.safeLoad(spec);

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {

  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi());

  // Start the server
  http.createServer(app).listen(serverPort, function () {
    console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
github salesforce / refocus / express.js View on Github external
// Initialize the Swagger middleware
const swaggerFile = fs // eslint-disable-line no-sync
.readFileSync(conf.api.swagger.doc, ENCODING);
const swaggerDoc = yaml.safeLoad(swaggerFile);

if (featureToggles.isFeatureEnabled('hideRoutes')) {
  for (let _path in swaggerDoc.paths) {
    if (swaggerDoc.paths.hasOwnProperty(_path)) {
      if (conf.hiddenRoutes.includes(_path.split('/')[ONE])) {
        delete swaggerDoc.paths[_path];
      }
    }
  }
}

swaggerTools.initializeMiddleware(swaggerDoc, (mw) => {
  /*
   * Custom middleware to add timestamp, request id, and dyno name to the
   * request, for use in logging.
   */
  app.use((req, res, next) => {
    // timestamp
    req.timestamp = Date.now();

    // request id (heroku only)
    if (req.headers && req.headers['x-request-id']) {
      req.request_id = req.headers['x-request-id'];
    }

    // dyno name (heroku only)
    if (process.env.DYNO) {
      req.dyno = process.env.DYNO;
github apigee-127 / swagger-tools / examples / 1.2 / index.js View on Github external
// swaggerRouter configuration
var options = {
  controllers: './controllers',
  useStubs: process.env.NODE_ENV === 'development' ? true : false // Conditionally turn on stubs (mock mode)
};

// The Swagger Resource Listing Document (require it, build it programmatically, fetch it from a URL, ...)
var apiDocJson = require('./api/api-doc.json');
// The Swagger API Declaration Documents (require them, build them programmatically, fetch them from a URL, ...)
var apiDeclarations = [
  require('./api/weather.json')
];

// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(apiDocJson, apiDeclarations, function (middleware) {
  // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
  app.use(middleware.swaggerMetadata());

  // Validate Swagger requests
  app.use(middleware.swaggerValidator());

  // Route validated requests to appropriate controller
  app.use(middleware.swaggerRouter(options));

  // Serve the Swagger documents and Swagger UI
  app.use(middleware.swaggerUi({
    '/weather': apiDeclarations[0]
  }));

  // Start the server
  http.createServer(app).listen(serverPort, function () {
github ph0bos / express-microservice-starter / lib / swagger / index.js View on Github external
module.exports = function initSwagger (app, options) {

  try {
    // Implementor swagger defined routes
    var swaggerDoc = yaml.safeLoad(fs.readFileSync(path.resolve(options.callerPath, options.swaggerConfig.filePath)));

    swagger.initializeMiddleware(swaggerDoc, function (middleware) {

      // Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
      app.use(middleware.swaggerMetadata());

      // Validate Swagger requests
      app.use(middleware.swaggerValidator({
        validateResponse: true
      }));
      
      //  Custom error handler to convert swagger request errors into API friendly json variants
      app.use(requestErrorHandler);

      // Route validated requests to appropriate controller
      app.use(middleware.swaggerRouter({
        controllers: options.swaggerConfig.controllers
      }));

swagger-tools

Various tools for using and integrating with Swagger.

MIT
Latest version published 6 years ago

Package Health Score

47 / 100
Full package analysis

Popular swagger-tools functions