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

commons-swagger-generation

v1.1.15

Published

swagger routes to file generation: controller, router, service, model, repository, testcases.

Downloads

192

Readme

SWAGGER FILE GENERATION

You must meet the requirements of creating the swagger.json file in order to output the error prone code.

  1. Create Swagger file with proper route name

  2. Ensure proper HTTP method is set in the route property

  3. Code generator will look for the last fragment in the route for a controller function name to put in the generated code Eg 1: consider the below path /books/v1/deleteBook from this path the code generator will take deleteBook as a function name

    Eg 2: what is your route has a param fragment? like the one below /books/v1/getBook/{bookId} So in this case the code generator will check for the 2nd last fragment which is getBook

  4. Ensure that if you are defining your own function without the use of code generator it must follow this export syntax module.exports.getBooks = () => {} As this is the export syntax other functions are considered helper function like the one below const sortSomething = () => {} So the code generator will append the new methods after your code

  5. If you run code generator again it will look inside the contoller for the derived function name and if it finds one it will skip so you don't have to worry about it overwriting your code.

Use the below syntax for generating static files from swagger definition at swagger.json

Step 1. Add Script for fast and easy use in package.json file

Add "generate-swag" :"node generate.js" the npm script to your package.json.

Assuming you have generate.js file at the root of your project.

"scripts": {
    "start": "node index.js",
    "test": "mocha --timeout 60000",
    "coverage": "npx nyc mocha --timeout 60000",
    "generate-swag": "node generate.js"
}

Step 2. Calling the method

Also Create another file called generate.js at the root as mentioned above

Note: You can call the method anywhere. This is just a showcase of how you can invoke this method Refer the sample file here generate.js for details.

const fileGeneration = require('commons-swagger-generation')

const constants = {
    logLevel: 1,
    methodMapper: {},
    fileTypes: {},
    maxRotations: 3
}

fileGeneration.generateControllers(constants)
/**
 * @@ logLevel:         supported options: 1 or 2. 1 will give basic and 2 will give detailed logs
 * @@ methodMapper:     It is used to map schema files whether to validate query or validate body against UI payload
 * @@ fileTypes:        The filename, template name, folder name that code generator writes comes from this variable
 * @@ maxRotations:     It is used to specify the max rounds for getting the unique possibility of function name inside controller
 */

Step 2. Run the script

The below script will generate/add/update the file based on the swagger routes that is defined in swagger.json

Make sure you have given proper config and folder names as it will oerwrite/append based on the scenario

To avoid unnecessary code loss what you can do is check VS Code git log and confirm the changes

npm run generate-swag

Supported files are:

If you are running the command and your let's say your path is as mentioned below: /books/v1/getBooks

The code generator in this case will write the below mentioned file and basic setup code to get you started

  • src/controller/getBooks.js
  • src/model/getBooks/index.js
  • src/model/getBooks/schema/index.json
  • src/repositories/getBooks.js
  • src/routers/index.js
  • src/routers/getBooks.js
  • src/services/getBooks.js
  • test/definitions/books.js
  • test_cases/books.json

Currently supported methods:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

Use LogLevel to debug the situation

You can set the logLevel in the config

  1. use logLevel = 1 where there is no issue and for most cases. This will take lesser time compared to when the log is more detailed
  2. use logLevel = 2 where you need to debug the errors or find out any bug/issue present in this generator

Support And Issues

If you face any kind of issue contact me or can raise an issue for this module