1.0.2 • Published 4 years ago

swagger-definer v1.0.2

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

SWAGGER-DEFINER

Document your api with ease

enter image description here . . . .enter image description here . . . . enter image description here

SWAGGER-DEFINER is a simple node.js library for defining Swagger 2.0 Spec.
  • Simple API
  • Built-in validation
  • Use TypeScript Annotations or pure Javascript

Installation

$ npm install swagger-definer

Usage examples

// swagger.js
export const swg = new Swagger("petstore.swagger.io", "/v2")
swg.tag("user", "User operations")
swg.security("jwt", {
  type: "apiKey",
  name: "Authorization",
  in: "header",
  description: "JWT Auth"
})

require("UserModel.js")
require("UserRoutes.js")

swg.validate().then(spec => { // Or simply call toJson() for skiping validation
    console.log(spec) // Or write to file
})
// UserModel.js
import { swg } from "swagger.js"

// Using plain JS
swg.definition("User")
  .property("name", "string", "User name", true, "Igor")
class User {
    name: string
}

// Or using TypeScript annotations
const { definition, property } = swg.annotations()

@definition("User")
class User {
  @property("name", "string", "User name", true, "Igor")
  name: string = "Igor"
}
// UserRoutes.js
import { swg } from "swagger.js"

// Using plain JS
swg.path("/user", "post", "createUsers", ["user"], "Create user")
  .parameter("body", "body", "[#/definitions/User]", true, "Array of users")
  .response("200", "Success", "string")
  .response("default", "Success", "string")
  .security("jwt")
function createListOfUsersAction() {
  console.log('User created')
}

// Or using TypeScript annotations
const { path, parameter, response, security } = swg.annotations()

// Annotations not working without class
class UserController {

  @path("/user", "post", "createUser", ["user"], "Create user")
  @parameter("body", "body", "#/definitions/User", true, "User instance")
  @response("200", "Success", "string")
  @response("default", "Error", "string")
  @security("jwt")
  create() {
    console.log('User created')
  }

}

For more examples see: https://github.com/Sujimoshi/swagger-definer/tree/master/examples

About types

Definition.prototype.property(
    name: string, // Name of property
    type: string | Schema, // Type of property. allowed values:
        // {} - Swagger Schema object
        // "#/definitions/Model" - parses to "ModelReference" Schema
        // "[#/definitions/Model]" - parses to array of "ModelReferences" Schema
        // "[type]" - parses to array of "types" Schema
        // "type" - parses to parameter of "type" Schema
    description: string = "", // Description
    required: boolean = true, // Is required
    example: any = "" // Example value for UI
)

Types parsing table:

Common NametypeformatComments
integerintegerint32signed 32 bits
longintegerint64signed 64 bits
floatnumberfloat
doublenumberdouble
stringstring
bytestringbytebase64 encoded characters
binarystringbinaryany sequence of octets
booleanboolean
datestringdateAs defined by full-date - RFC3339
dateTimestringdate-timeAs defined by date-time - RFC3339
passwordstringpasswordUsed to hint UIs the input needs to be obscured.