Skip to content

Commit

Permalink
feat: Updated node and module to latest version, exported function to…
Browse files Browse the repository at this point in the history
… validate request params, bug fixes and improvement
  • Loading branch information
vpatidar-systango committed Mar 6, 2020
1 parent 6fe67ec commit 4ad93d3
Show file tree
Hide file tree
Showing 11 changed files with 3,252 additions and 1,034 deletions.
113 changes: 90 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NPM module to generate swagger documentation for KOA APIs with minimum additiona
>[![Downloads](https://badgen.net/npm/dt/swagger-generator-koa)](https://www.npmjs.com/package/swagger-generator-koa) [![npm dependents](https://badgen.net/npm/dependents/swagger-generator-koa)](https://www.npmjs.com/package/swagger-generator-koa?activeTab=dependents)
## Description
This NPM module let's you generate swagger (OpenAPI) documentation for your KOA APIs without putting in much extra efforts. You just need to follow the convention for your request and response objects, and the module will take care of the rest. This module will cover your controllers, API specs along with request and response object structures.
This NPM module let's you validate and generate swagger (OpenAPI) documentation for your KOA APIs without putting in much extra efforts. You just need to follow the convention for your request and response objects, and the module will take care of the rest. This module will cover your controllers, API specs along with request and response object structures.


## Usage ##
Expand Down Expand Up @@ -64,7 +64,7 @@ swagger.serveSwagger(app, "/swagger", options, {routePath : './src/routes/', req
const Router = require('koa-router');
const router = new Router();
const userController = require('../controller/user');
const validation = require('koa2-validation');
const {validation} = require('swagger-generator-koa');
var requestModel = require('../requestModel/users');
const BASE_URL = `/users`;

Expand Down Expand Up @@ -155,53 +155,120 @@ module.exports = {
module.exports = {
createUser: { // This name should be model name defined in request model.
201: {
message: "User created successfully"
message: {
type: 'string'
}
},
500: {
internal: "Server Error"
internal: {
type: 'string'
}
}
},
getUsers: {
200: [{
id: 'number',
firstName: 'string',
lastName: 'string',
address: 'string',
contact: 'number',
createdAt: 'date',
updatedAt: 'date'
id: {
type: 'number'
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
address: {
type: 'string'
},
contact: {
type: 'number'
},
createdAt: {
type: 'number',
format: 'date-time'
},
updatedAt: {
type: 'number',
format: 'date-time'
}
}],
500: {
internal: "Server Error"
internal: {
type: 'string'
}
}
},
updateUser:{
updateUser: {
201: {
message: "User Updated successfully"
message: {
type: 'string'
}
},
500: {
internal: "Server Error"
internal: {
type: 'string'
}
}
},
getUserDetails: {
200: {
id: 'number',
firstName: 'string',
lastName: 'string',
address: 'string',
contact: 'number',
createdAt: 'date',
updatedAt: 'date'
id: {
type: 'number'
},
firstName: {
type: 'string'
},
lastName: {
type: 'string'
},
address: {
type: 'string'
},
contact: {
type: 'number'
},
createdAt: {
type: 'number',
format: 'date-time'
},
updatedAt: {
type: 'number',
format: 'date-time'
}
},
500: {
internal: "Server Error"
internal: {
type: 'string'
}
}
},
};
```

Open `http://`<app_host>`:`<app_port>`/swagger` in your browser to view the documentation.

# Version changes

## v2.0.0

#### Added Request Parameter Validation Function

- Use `validation` function exported from this module to validate request params.

```javascript
'use strict';
const Router = require('koa-router');
const router = new Router();
const userController = require('../controller/user');
const {validation} = require('swagger-generator-koa');
var requestModel = require('../requestModel/users');
const BASE_URL = `/users`;

router.post(`${BASE_URL}/`, validation(requestModel[0]), userController.createUser);

module.exports = router;

```

## Requirements

- Node v10 or above
Expand Down
5 changes: 3 additions & 2 deletions example/koa-swagger-example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Koa = require('koa');
const bodyParser = require('koa-bodyparser');
const indexRoutes = require('./src/routes/index');
const userRoutes = require('./src/routes/users');
const swagger = require('../swagger-generator-koa');
const swagger = require('swagger-generator-koa');

const app = new Koa();
const PORT = process.env.PORT || 5000;
Expand All @@ -19,6 +19,7 @@ app.use(async (ctx, next) => {
ctx.body = {
success: false,
message: err.message,
errors: err.errors
};
}
});
Expand Down Expand Up @@ -52,6 +53,6 @@ const options = {
};


swagger.serveSwagger(app, "/swagger", options, {routePath : '../../koa-swagger-example/src/routes', requestModelPath: '../../koa-swagger-example/src/requestModel', responseModelPath: '../../koa-swagger-example/src/responseModel'});
swagger.serveSwagger(app, "/swagger", options, {routePath : './src/routes', requestModelPath: './src/requestModel', responseModelPath: './src/responseModel'});

module.exports = server;
Loading

0 comments on commit 4ad93d3

Please sign in to comment.