Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: options.apis allows globs in addition to full file paths #19

Closed
jonboiser opened this issue Mar 31, 2016 · 6 comments
Closed

Comments

@jonboiser
Copy link
Contributor

This would be useful for projects that have lots routes/controllers broken up into many files. Also, when a new route module is introduced to the project, we will not need to add its path to the array.

Example:

var swaggerJSDoc = require('swagger-jsdoc');

var options = {
  swaggerDefinition: {
    info: {
      title: 'Hello World', // Title (required)
      version: '1.0.0', // Version (required)
    },
  },
  apis: ['./routes/*.js'], // Path to the API docs
};

// Initialize swagger-jsdoc -> returns validated swagger spec in json format
var swaggerSpec = swaggerJSDoc(options);

If this would be a useful addition, I would be happy to submit a PR.

@jonboiser jonboiser changed the title Feature request: options.apis allows globs instead of full file paths Feature request: options.apis allows globs in addition to full file paths Mar 31, 2016
@chdanielmueller
Copy link
Member

Hi @jonboiser

I like this idea.
A implementation in swagger-jsdoc would be cool.

I'm using a glob function myself for generating this.
May this gives you some ideas.

var _ = require('lodash');
var glob = require('glob');

function(globPatterns, removeRoot) {
  // For context switching
  var _this = this;

  // URL paths regex
  var urlRegex = new RegExp('^(?:[a-z]+:)?\/\/', 'i');

  // The output array
  var output = [];

  // If glob pattern is array so we use each pattern in a recursive way, otherwise we use glob
  if (_.isArray(globPatterns)) {
    globPatterns.forEach(function(globPattern) {
      output = _.union(output, _this.getGlobbedFiles(globPattern, removeRoot));
    });
  } else if (_.isString(globPatterns)) {
    if (urlRegex.test(globPatterns)) {
      output.push(globPatterns);
    } else {
      var files = glob.sync(globPatterns);
      if (removeRoot) {
        files = files.map(function(file) {
          return file.replace(removeRoot, '');
        });
      }
      output = _.union(output, files);
    }
  }

  return output;
};

@jonboiser
Copy link
Contributor Author

Sounds good! I'll start working on it.

@ghost
Copy link

ghost commented Apr 4, 2016

Hi @jonboiser when will this feature be released as its something that's quite useful for the current project am working on.

@jonboiser
Copy link
Contributor Author

@monk8800 I've set up a PR implementing the feature. It's actually pretty simple; I just need to expand the test to cover it.

I don't know when it would be released, but it's actually simple enough for you to bring in glob into your project and do something similar.

@chdanielmueller
Copy link
Member

@monk8800: I plan on taking a look at it tomorrow.
I think the changes will be implemented until the end of this week.

@ghost
Copy link

ghost commented Apr 4, 2016

Sounds good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants