{"id":21952008,"url":"https://github.com/systangotechnologies/swagger-generator-koa","last_synced_at":"2025-04-23T04:03:46.467Z","repository":{"id":35082560,"uuid":"201899371","full_name":"SystangoTechnologies/swagger-generator-koa","owner":"SystangoTechnologies","description":"Allows you to programatically annotate your koa models with swagger info and then generate and validate your json spec file","archived":false,"fork":false,"pushed_at":"2023-01-11T02:02:34.000Z","size":702,"stargazers_count":10,"open_issues_count":18,"forks_count":4,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-23T04:03:41.963Z","etag":null,"topics":["generator","openapi3","swagger","swagger-documentation","swagger-generator-koa","swagger2"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SystangoTechnologies.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-12T09:28:50.000Z","updated_at":"2024-01-30T16:14:31.000Z","dependencies_parsed_at":"2023-01-15T13:27:46.495Z","dependency_job_id":null,"html_url":"https://github.com/SystangoTechnologies/swagger-generator-koa","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystangoTechnologies%2Fswagger-generator-koa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystangoTechnologies%2Fswagger-generator-koa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystangoTechnologies%2Fswagger-generator-koa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SystangoTechnologies%2Fswagger-generator-koa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SystangoTechnologies","download_url":"https://codeload.github.com/SystangoTechnologies/swagger-generator-koa/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250366710,"owners_count":21418770,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["generator","openapi3","swagger","swagger-documentation","swagger-generator-koa","swagger2"],"created_at":"2024-11-29T06:19:04.675Z","updated_at":"2025-04-23T04:03:46.432Z","avatar_url":"https://github.com/SystangoTechnologies.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# swagger-generator-koa\n\nNPM module to generate swagger documentation for KOA APIs with minimum additional effort.\n\n\u003e[![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)\n\n## Description\nThis 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.\n\n\n## Usage ##\n\nInstall using npm:\n\n```bash\n$ npm install --save swagger-generator-koa\n```\n\n### Koa setup `index.js` ###\n\n```javascript\nconst Koa = require('koa');\nconst app = new Koa();\nconst swagger = require(\"swagger-generator-koa\");\n\n// Define your router here\n\nconst options = {\n\ttitle: \"swagger-generator-koa\",\n\tversion: \"1.0.0\",\n\thost: \"localhost:5000\",\n\tbasePath: \"/\",\n\tschemes: [\"http\", \"https\"],\n\tsecurityDefinitions: {\n\t\tBearer: {\n\t\t\tdescription: 'Example value:- Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjU5MmQwMGJhNTJjYjJjM',\n\t\t\ttype: 'apiKey',\n\t\t\tname: 'Authorization',\n\t\t\tin: 'header'\n\t\t}\n\t},\n\tsecurity: [{Bearer: []}],\n\tdefaultSecurity: 'Bearer'\n};\n\n\n/**\n * serveSwagger must be called after defining your router.\n * @param app Koa object\n * @param endPoint Swagger path on which swagger UI display\n * @param options Swagget Options.\n * @param path.routePath path to folder in which routes files defined.\n * @param path.requestModelPath Optional parameter which is path to folder in which requestModel defined, if not given request params will not display on swagger documentation.\n * @param path.responseModelPath Optional parameter which is path to folder in which responseModel defined, if not given response objects will not display on swagger documentation.\n */\nswagger.serveSwagger(app, \"/swagger\", options, {routePath : './src/routes/', requestModelPath: './src/requestModel', responseModelPath: './src/responseModel'});\n\n```\n\n### Koa router `user.js` ###\n\n```javascript\n\nconst Router = require('koa-router');\nconst router = new Router();\nconst userController = require('../controller/user');\nconst {validation} = require('swagger-generator-koa');\nvar requestModel = require('../requestModel/users');\nconst BASE_URL = `/users`;\n\nrouter.post(`${BASE_URL}/`, validation(requestModel[0]), userController.createUser);\n\nrouter.get(`${BASE_URL}/`, userController.getUsers);\n\nrouter.put(`${BASE_URL}/:userId`, userController.updateUser);\n\nrouter.get(`${BASE_URL}/:userId`, userController.getUserDetails);\n\nrouter.delete(`${BASE_URL}/:userId`, userController.deleteUser);\n\nmodule.exports = router;\n\n```\n\n## Request Model `/requestModel/user.js`\n  - File name for request model should be same as router file.\n  - Define request model with their order of apis in router js file. For example first api in user router is create user so you need to define createUser schema with key 0.\n  - Add boolean flag \"excludeFromSwagger\" inside requestmodel if you want to exclude any particular api from swagger documentation.\n  - This Request model follows Joi module conventions, so it can also be used for request parameters validation.\n\n```javascript\nconst Joi = require(\"@hapi/joi\");\n/**\n * File name for request and response model should be same as router file.\n * Define request model with their order in router js file.\n * For example first api in user router is create user so we define createUser schema with key 0.\n */\nmodule.exports = {\n    // Here 0 is the order of api in route file.\n    0: {\n        body: {\n            firstName: Joi.string().required(),\n            lastName: Joi.string().required(),\n            address: Joi.string().required(),\n            contact: Joi.number().required()\n        },\n        model: \"createUser\", // Name of the model\n        group: \"User\", // Swagger tag for apis.\n        description: \"Create user and save details in database\"\n    },\n    1: {\n        query: {},\n        path: {}, // Define for api path param here.\n        header: {}, // Define if header required.\n        group: \"User\",\n        description: \"Get All User\"\n    },\n    2: {\n        body: {\n            firstName: Joi.string().required(),\n            lastName: Joi.string().required(),\n            address: Joi.string().required(),\n            contact: Joi.number().required()\n        },\n        model: \"updateUser\",\n        group: \"User\",\n        description: \"Update User\"\n    },\n    3: {\n        query: {},\n        path: {\n            userId: Joi.number().required()\n        }, // Define for api path param here.\n        header: {}, // Define if header required.\n        model: 'getUserDetails',\n        group: \"User\",\n        description: \"Get user details\"\n    },\n    4: {\n        excludeFromSwagger: false // Make it true if need to exclude apis from swagger.\n    }\n};\n```\n\n## Response Model `/responseModel/user.js`\n\n - File name for response model should be same as router file.\n - Response name should be same as model name from requestmodel. For example model name of create user api is \"createUser\" so key for response object will be \"createUser\".\n - Inside response model define responses with respect to their status code returned from apis.\n\n```javascript\n\n// The name of each response payload should be model name defined in Request model schema.\n\nmodule.exports = {\n    createUser: { // This name should be model name defined in request model.\n        201: {\n            message: {\n                type: 'string'\n            }\n        },\n        500: {\n            internal: {\n                type: 'string'\n            }\n        }\n    },\n    getUsers: {\n        200: [{\n            id: {\n                type: 'number'\n            },\n            firstName: {\n                type: 'string'\n            },\n            lastName: {\n                type: 'string'\n            },\n            address: {\n                type: 'string'\n            },\n            contact: {\n                type: 'number'\n            },\n            createdAt: {\n                type: 'number',\n                format: 'date-time'\n            },\n            updatedAt: {\n                type: 'number',\n                format: 'date-time'\n            }\n        }],\n        500: {\n            internal: {\n                type: 'string'\n            }\n        }\n    },\n    updateUser: {\n        201: {\n            message: {\n                type: 'string'\n            }\n        },\n        500: {\n            internal: {\n                type: 'string'\n            }\n        }\n    },\n    getUserDetails: {\n        200: {\n            id: {\n                type: 'number'\n            },\n            firstName: {\n                type: 'string'\n            },\n            lastName: {\n                type: 'string'\n            },\n            address: {\n                type: 'string'\n            },\n            contact: {\n                type: 'number'\n            },\n            createdAt: {\n                type: 'number',\n                format: 'date-time'\n            },\n            updatedAt: {\n                type: 'number',\n                format: 'date-time'\n            }\n        },\n        500: {\n            internal: {\n                type: 'string'\n            }\n        }\n    },\n};\n```\n\nOpen `http://`\u003capp_host\u003e`:`\u003capp_port\u003e`/swagger` in your browser to view the documentation.\n\n# Version changes\n\n## v2.0.0\n\n#### Added Request Parameter Validation Function\n\n- Use `validation` function exported from this module to validate request params.\n\n```javascript\n'use strict';\nconst Router = require('koa-router');\nconst router = new Router();\nconst userController = require('../controller/user');\nconst {validation} = require('swagger-generator-koa');\nvar requestModel = require('../requestModel/users');\nconst BASE_URL = `/users`;\n\nrouter.post(`${BASE_URL}/`, validation(requestModel[0]), userController.createUser);\n\nmodule.exports = router;\n\n```\n\n## Requirements\n\n- Node v10 or above\n- KOA 2 or above\n\n## Contributors\n\n[Vikas Patidar](https://www.linkedin.com/in/vikas-patidar-0106/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsystangotechnologies%2Fswagger-generator-koa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsystangotechnologies%2Fswagger-generator-koa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsystangotechnologies%2Fswagger-generator-koa/lists"}