1.0.3 • Published 5 years ago

express-auto-swagger-generator v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

To do documentation of API, These are the modules used to generate API documentation: 1) swagger-spec-express - to generate swagger.json 2) joi-to-swagger - to create models with joi validation support.

Things to be done:

Define this in index.js

const options = { title: packageJson.title, version: packageJson.version, termsOfService: 'You may only use this api for reasons!', host: 'localhost:3000', basePath: '/', schemes: 'http', 'https', securityDefinitions: { basicAuth: { type: 'basic', description: "HTTP Basic Authentication. Works over HTTPS", scheme: 'http' } },

security: [{basicAuth: []}],

defaultSecurity: "basicAuth",

}; swagger.initialise(app, options);

In routes: swagger.swaggerize(router);

define schema: const postSchema = { body: { name: Joi.string().required(), age: Joi.number().required(), nationality: Joi.string().required(), email: Joi.string().email().required() }, model: "postPeople", // mandatory to pass - used for creating models in swagger. group: 'People', // mandatory to pass description: 'Route to people to the system' }

router.post('/', validator.body(postSchema.body), function (req, res) { const PersonInfo = req.body; const newPerson = new Person({ name: PersonInfo.name, age: PersonInfo.age, nationality: PersonInfo.nationality });

newPerson.save(function (err, Person) {
    if (err) res.json(err);
    res.json(Person);
});

}).describe({ ...create(postSchema)});

validation is express-joi-validation object can validate - body, path, query, header.

create(postSchema) will generate body, header, queryparam, pathparam for the particular API.

create first will work in this manner: 1) create model for responses. 2) create responses for the particular schema 3) create body parameters, if exists 4) create query and path parameters, if exists 5) create header, if exists 6) return json will all the above mentioned details.

// for response create a response.json file and give the name: { "getPeopleResponseModel": { "name": "string", "age": "string", "nationality": "string" }, "postPeopleResponseModel": { "name": "string", "age": "string", "nationality": "string" }, "patchPeopleResponseModel": { "name": "string", "age": "string", "nationality": "string" } }

The name of each response payload should be like: getPeople - model name in API schema and model name should sufix with ResponseModel.

Contributors

Vikas Patidar Nidhi Tripathi