ibm-openapi-support
This module is a utility to make the process of loading and parsing documents in the OpenAPI (swagger) format a simple task. The primary use case for this is code generation where a swagger document is loaded, parsed and then relavent data structures for building api endpoints are made available.
Quick start
Swaggerize contains two modules, index.js and utils.js. They are described below:
- utils.loadAsync: This utility method is used for loading a swagger document from a file path or url. The method takes two arguments and returns a dictionary with two keys, loaded and parsed. The loaded key contains a javascript object containing the original document. The parsed key contains the a dictionary with parsed swagger elements. The arguments to loadAsync:
- path or url to the openApi document.
- in-memory filesystem object; only required if loading from a file path.
var swaggerize = var loadedApivar parsedSwagger var memFs = var editor = var store = memFsvar fs = editor return utils
- index.parse: This method is used to parse the swagger document and build a dictionary of structures that contain the routes, resources and basepath required for generating API code. The parse method will use the supplied formatters to modify the path and the resource. This method takes two parameters:
- stringified OpenApi (swagger) document.
- formatters dictionary: This contains formatters for the path pathFormatter and for the resource resourceFormatter The formatters take a path parameter and return a string.
The data returned takes the following format:
{ basepath: string,
resources: { "resourceName": [ { method: string, // method is one of: "post",
// "put",
// "delete",
// "get",
// "patch"
route: string, // path to register
params: [ { model: string, // model name
array: boolean, // is this an array? }
],
responses: [ { model: string, // model name
array: boolean, // is this an array? }
]
}
]
},
models: [ models declarations ]
}
var swaggerize = var swaggerizeUtils = var swaggerDocument = 'your OpenApi (swagger) document goes here' { // take a swagger path and convert the parameters to express format. // i.e. convert "/path/to/{param1}/{param2}" to "/path/to/:param1/:param2" var newPath = thepath return newPath} { // grab the first valid element of a path (or partial path) and return it. return thePath1} thisparsedSwagger = undefined;var formatters = 'pathFormatter': helpersreformatPathToNodeExpress 'resourceFormatter': helpersresourceNameFromPath if swaggerDocument !== undefined return swaggerize if thisparsedSwagger Object