Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

gerjs-core

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gerjs-core

Create Swagger documentation from Joi models

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

[BETA] - gerJs

Create Swagger documentation from Joi models

Example for Koa : https://github.com/dobobaie/example-koa-gerjs-server
Example for Express : https://github.com/dobobaie/example-express-gerjs-server

Help us to improve the project by contributing 👥

☁️ Installation

$ npm install gerjs-core

📝 Usage

Initialization

Create a new instance :

const modelsAPI = require("./models/models");
const gerJs = require("gerjs-core")({
  swagger: {
    title: "Swagger title",
    description: "Swagger description",
    servers_url: [ "http://apiUrl/" ],
    vesion: "1.0.0"
  },
  destinationPath: "/path"
})(modelsAPI);

📋 Instance options

{
  swagger: {
    title: "Swagger title", // string ; default: Swagger documentation
    description: "Swagger description", // string ; default: Retrieve all information from the API inside this documents.
    servers_url: [ "http://apiUrl/" ], // Array<string> ; default: ['http://locahost:8000/']
    vesion: "1.0.0" // string ; default: "1.0.0"
  },
  destinationPath: "/path" // string ; required
}

⚙️ Model examples

Joi is required to create the models

const Joi = require('@hapi/joi');

const refs = {
  token:
    Joi.object({
      token: Joi.string().required().min(12).max(30),
    }).label("token"),
  classic:
    Joi.object({
      message: Joi.string(),
      result: Joi.boolean()
    }).label("classic")
};

const models =
{
  'POST/register': {
    description: 'Register route',
    tags: [ // tags are generate automaticly but you can customize them
      "register"
    ],
    body:
      Joi.object({
        username: Joi.string().default("John").description(`Username of the user`),
        firstname: Joi.string().min(12).max(30).required().description(`Lastname of the user`),
        lastname: Joi.string().description(`Lastname of the user`),
        extra: refs.classic,
        test: Joi.array().items(
          Joi.object({
            number: Joi.number()
          })
        ),
        created: Joi.date().default(Date.now),
        status: Joi.any().default('registered')
      }).description("Body description").required(),
    response: refs.classic.description("Extra register description for the response with a reference")
  },

  'PUT/users/:id': {
    queries: refs.token,
    body: 
      Joi.object({
        username: Joi.string().required(),
        password: Joi.string().required()
      }),
    response: refs.classic.description('Extra description for the response with a reference').default({ toto: Joi.string() })
  },
  
  'GET/': {
      queries: refs.token,
      response: Joi.string().default("Default response").description('...')
      // OR
      response: Joi.any().meta({ "Content-Type": "text/plain" }).description('...')
  }
};

module.exports = models;

Usage Model

{
  // METHOD + ROUTE
  '{POST|GET|PUT|DELETE|...}{route: / | /hello | /...}': {
    description: String, // default: empty
    tags: Array<String>, // default: automatic
    queries: Joi, // optional
    body: Joi, // optional
    response: Joi, // required
  }
}

Usage Refs

This is classic Joi declaration. But label(String) is required to make a link between each same refs.

For example :

const Joi = require('@hapi/joi');

const refs = {
  token:
    Joi.object({
      token: Joi.string().required().min(12).max(30),
    }).label("token"),
};

As you can see refs.token own token label from .label("token") declaration.
So everytime refs.token will be called, the token decaration will be referenced in the same reference in the Swagger file.

If you don't specified the label declaration then another reference will be created with another specific name.

Specify Content-Type

The Content-type is automaticly determined by the Joi declaration.
Ex: Joi.object(...) is application/json.

About the response part, you can determined the Content-Type directly in the meta declaration.
Ex: response: Joi.any().meta({ "Content-Type": "text/plain" }) the route return a Content-Type type text/plain.

👥 Contributing

Please help us to improve the project by contributing :)

❓️ Testing

Clone the repo and run from the project root:

$ npm install
$ npm test

Keywords

FAQs

Package last updated on 02 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc