2.0.0 • Published 5 years ago

swagger-json v2.0.0

Weekly downloads
72
License
ISC
Repository
github
Last release
5 years ago

swagger-json

Generate swagger json schema based on Joi schemas.

Installation

npm install swagger-json

Usage

const { swaggerDoc } = require('swagger-json');
const Joi = require('joi');

const host = 'localhost:3000';
const basePath = '/';
const info = {
  "version": "1.0.0",
  "title": "Title Example",
  "description": "Description API example",
  "termsOfService": "http://swagger.io/terms/",
  "contact": {
    "name": "Example team"
  },
  "license": {
    "name": "MIT"
  }
};

// This will generate initial doc
swaggerDoc.createJsonDoc(info, host, basePath);

const joiSchema = {
  body: Joi.object({
    email: Joi.string().email().required().description('Email address for new user').example('vladimir@gmail.com'),
    password: Joi.string().min(7).max(24).required().description('Password for new user').example("Somestrongpasswrod#"),
    salt: Joi.string().required().description('Salt for new user').example('asdsdgdsafs324eqwedagsdfafsdf')
  }).meta({ modelName: 'Register' }),
  headers: Joi.object({
    authorization: Validation.tokenValidation.string().token().required().description('Auth token').example('asdasfadfasdsasdas')
  }),
  group: 'Register routes',
  description: 'Route to register user to the system'
}

// create a new route
swaggerDoc.addNewRoute(joiSchema, '/v1/user/register', 'post');

// your swagger json file will have been written to "swagger.json"

Usage without writing the swagger.json file to the file system

// use swaggerDocJson instead of swaggerDoc from the swagger-json module
const { swaggerDocJson } = require('swagger-json');

// using the same joiSchema, info, host, and basePath as the above example.
swaggerDocJson.addNewRoute(joiSchema, '/v1/user/register', 'post');

const swaggerSpec = swaggerDocJson.createJsonDoc(info, host, basePath);
2.0.0

5 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago