Swagger Sails JS

后端 未结 5 792
执念已碎
执念已碎 2020-12-08 11:04

Any idea of how to integrate swagger, swagger-ui with a sails js project?

Where can i found information about it, or about another

相关标签:
5条回答
  • 2020-12-08 11:48

    install: npm i @logisticinfotech/sails-hook-actions2-swagger and open in your browser http://localhost:1337/swagger.

    only that, cool.

    0 讨论(0)
  • 2020-12-08 11:49

    I was searching for a solution for this a little over a week ago, but I found the information scattered a bit. Here's how I integrated swagger and swagger-ui with my sails 0.11.x project.

    1. Install sails-swagger

    npm install --save sails-swagger
    

    This provides a hook that will dynamically generate the swagger document. Unfortunately, it's only compatible with sails 0.12.x (which is an rc at the time of this post). So, to get it working with 0.11.x you have to extend SwaggerController and overwrite the hook's name in config/installedHooks.js (issue reference)

    api/controllers/SwaggerController.js

    'use strict';
    
    /**
     * This is a temp fix while one sails 11.x
     * @see https://github.com/tjwebb/sails-swagger/issues/3
     */
    var _ = require('lodash');
    var _super = require('sails-swagger/dist/api/controllers/SwaggerController');
    
    _.merge(exports, _super);
    _.merge(exports, {
      _config: {
        actions: false,
        shortcuts: false,
        rest: false
      }
    });
    

    config/installedHooks.js

    'use strict';
    
    /**
     * This is a temp fix while one sails 11.x
     * @see https://github.com/tjwebb/sails-swagger/issues/3
     */
    module.exports.installedHooks = {
      'sails-swagger': {
        'name': 'swagger'
      }
    };
    

    Lastly, provide the hook's configuration per the docs

    config/swagger.js

    'use strict';
    
    module.exports.swagger = {
      /**
       * require() the package.json file for your Sails app.
       */
      pkg: require('../package')
    };
    

    2. Install swagger-ui

    npm install --save-dev swagger-ui@2
    

    There are a few ways to handle this. You can use the vanilla swagger-ui module and add everything in its dist to your build process. But I chose to copy it into my project because I intend to customize the interface a bit.

    mkdir assets/docs
    cp -r node_modules/swagger-ui/dist/* assets/docs/
    

    NOTE (3/26/2017): swagger-ui v3.x has been re-built using React (yay!), but easily integrating it with sails is not obvious at the moment.

    3. Configure swagger-ui

    Lastly, you need to point swagger-ui to your dynamically generated swagger doc. The default route for the swagger doc is /swagger/doc, so you can set it explicitly in the swagger-ui configuration.

    assets/docs/index.html

    <script type="text/javascript">
        $(function () {
          var url = '/swagger/doc';
    
          // Pre load translate...
          if(window.SwaggerTranslator) {
            window.SwaggerTranslator.translate();
          }
          window.swaggerUi = new SwaggerUi({
            url: url,
            // ...removed for brevity
    </script>
    

    4. Profit

    Now you can navigate to /docs in your browser and see your beautiful documentation.

    0 讨论(0)
  • 2020-12-08 11:50

    I found a solution using swagger-ui. I create a folder called docs in assets sails folder, put the swagger-ui content in there, and edit the swagger.json file. Then, I did sails lift. The docs is now available in http://ip_address:1337/docs. I'm using sails v- 0.11.0

    0 讨论(0)
  • 2020-12-08 11:58

    In terms of generating the documentation automatically, you can check out this library as I think it better solves the problem and simply generates the swagger.json file automatically from your controllers and routes.

    npm install sails-hook-swagger-generator --save
    

    then simply sails lift and check the swagger folder, which you must have created before running sails lift

    Updates

    It now has support OAS 3.0 and also better documentation for actions and action2s

    for more about the configuration, you can view the repo here

    0 讨论(0)
  • 2020-12-08 12:03

    Install

    $ npm install sails-swagger --save
    

    Configuration

        // config/swagger.js
    module.exports.swagger = {
      /**
       * require() the package.json file for your Sails app.
       */
      pkg: require('../package'),
      ui: {
        url: 'http://swagger.balderdash.io'
      }
    };
    

    After installing and configuring swagger, you can find the docs output on the /swagger/doc route.

    0 讨论(0)
提交回复
热议问题