# swag4k

> A swagger endpoint creater for koa apps

Latest version **1.0.11** (published 2017-02-10) · UNLICENSED license · 0 weekly downloads

## Install

```sh
npm install swag4k
pnpm add swag4k
yarn add swag4k
bun add swag4k
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.11 |
| Published | 2017-02-10 |
| First published | 2017-02-07 |
| Weekly downloads | 0 |
| License | UNLICENSED |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 (+11 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Mike Brennan |
| Maintainers | fifthfloorsoftware, mikiebrenbren |
| Keywords | swagger, koa, swagger-ui, swagger for koa, swagger koa, jsdoc, api, joi, joi to swagger, documentation, swagger docs, docs |

## Links

- npm: https://www.npmjs.com/package/swag4k
- Repository: https://github.com/fifthfloorsoftware/swag4k
- Homepage: https://github.com/fifthfloorsoftware/swag4k#readme
- Issues: https://github.com/fifthfloorsoftware/swag4k/issues
- npm.io page: https://npm.io/package/swag4k

## Dependencies (5)

- [joi](https://npm.io/package/joi.md) ^10.2.1
- [lodash](https://npm.io/package/lodash.md) 4.17.4
- [doctrine](https://npm.io/package/doctrine.md) 2.0.0
- [koa-router](https://npm.io/package/koa-router.md) 5.2.3
- [joi-to-swagger](https://npm.io/package/joi-to-swagger.md) ^1.1.0

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 1.0.11 (latest) — 2017-02-10
- 1.0.10 — 2017-02-10
- 1.0.9 — 2017-02-09
- 1.0.8 — 2017-02-08
- 1.0.7 — 2017-02-07
- 1.0.6 — 2017-02-07
- 1.0.5 — 2017-02-07
- 1.0.4 — 2017-02-07
- 1.0.3 — 2017-02-07
- 1.0.1 — 2017-02-07

## README

#swag4k 

swagger-for-koa creates a [swagger](http://swagger.io/) endpoint for your api based on the description provided by your jsdocs.

In contrast to other swagger-koa implementations, swag4k does not force the user to have a ui bundled into your service, parses javadocs formatted 
in json(no .yml!), is compatible with [joi](https://github.com/hapijs/joi) schemas, creates a swagger [v2 spec](http://petstore.swagger.io/v2/swagger.json).  

##Usage
###koa configuration 

```javascript
//example invocation of init function with example opts; creates endpoint with path /api/public/v1/swagger
const swag4k = require('swag4k')
  opts.info = {
    description: 'This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/).  For this sample, you can use the api key `special-key` to test the authorization filters.',
    version: '1.0.0',
    title: 'Swagger Petstore',
    contact: {email: 'johndoe@gmail.com'},
    basePath: '/v2'
  }
  opts.swagger = '2.0'
  opts.basePath = '/api/public/v1'
  opts.swaggerPath = '/swagger'
  opts.schemes = ['http', 'https']
  opts.securityDefinitions = {} 
  opts.files = ['./api'] //files that contain swagger jsdocs
  opts.addDefaults = true //adds default definitions contained in defaults file 
  opts.joiDefinitions = [
      Joi.object().keys({
        id:      Joi.number().integer().positive().required(),
        name:    Joi.string(),
        email:   Joi.string().email().required(),
        created: Joi.date().allow(null),
        active:  Joi.boolean().default(true),
      })
  ]
  const app = koa()
  swag4k.init(opts, app)  
```   

###Router configuration

```javascript
// example of what a jsdoc should look like in an api file
const router = require('koa-router')


//jsdoc with @swagger title should be at top of any file you want swagger4k to find
/**
 * @swagger
 * @name pet
 * @description Everything about your pet
 * @externalDocs {"description" : "find out more", "url": "path/to/README.md"}
 */


//endpoint jsdocs must have path and method titles
/**
 * @resourcePath /pet
 * @method POST
 * @summary Add a new pet to the pet store
 * @operationId addPet
 * @consumes ["application/json", "application/xml"]
 * @produces ["application/json", "application/xml"]
 * @tags ["pets"]
 * @parameters [
    {
      "description": "Pet object that needs to be added to the store",
      "in": "body",
      "name": "body",
      "required": true,
      "schema": {
        "$ref": "#/definitions/Pet"
      }
    }
  ] 
 * @responses { "405" : {"description": "Invalid input"} }
 * @security [
   {
     "petstore_auth": [
       "write:pets",
       "read:pets"
     ]
   }
 ]
 * @deprecated false
 */
router.post('/pet', postPet)
function * postPet(next) {
  //creating new pet
}

/**
 * @resourcePath /pet
 * @method PUT
 * ...
 * @tags ["pets"]
 */
router.put('/put', putPet)
function * putPet(next) {
  //updating new pet
}

/**
 * @resourcePath /pet/findByStatus
 * @method GET
 * ...
 * @tags ["pets"]
 */
router.get('/pet/findByStatus', findByStatus)
function * findByStatus(next) {
  //fetching new pet by status
}

/**
 * * @resourcePath /pet/findByStatus
 * @method DELETE
 
 * ...
 * @responses  {
 * 	"200": {
 * 		"description": "successful operation",
 * 		"schema": {
 * 			"type": "string"
 * 		},
 * 		"headers": {
 * 			"X-Rate-Limit": {
 * 				"type": "integer",
 * 				"format": "int32",
 * 				"description": "calls per hour allowed by the user"
 * 			},
 * 			"X-Expires-After": {
 * 				"type": "string",
 * 				"format": "date-time",
 * 				"description": "date in UTC when token expires"
 * 			}
 * 		}
 * 	},
 * 	"400": {
 * 		"description": "Invalid username/password supplied"
 * 	}
 * }
 */
router.delete('/pet/findByStatus', deleteByStatus)
function * deleteByStatus(next) {
  //deleting pet by status
}


//this is what a pet looks like 
/**
 * @definition
 * @Pet {
 * 	"type": "object",
 * 	"required": [
 * 		"name",
 * 		"photoUrls"
 * 	],
 * 	"properties": {
 * 		"id": {
 * 			"type": "integer",
 * 			"format": "int64"
 * 		},
 * 		"category": {
 * 			"$ref": "#/definitions/Category"
 * 		},
 * 		"name": {
 * 			"type": "string",
 * 			"example": "doggie"
 * 		},
 * 		"photoUrls": {
 * 			"type": "array",
 * 			"xml": {
 * 				"name": "photoUrl",
 * 				"wrapped": true
 * 			},
 * 			"items": {
 * 				"type": "string"
 * 			}
 * 		},
 * 		"tags": {
 * 			"type": "array",
 * 			"xml": {
 * 				"name": "tag",
 * 				"wrapped": true
 * 			},
 * 			"items": {
 * 				"$ref": "#/definitions/Tag"
 * 			}
 * 		},
 * 		"status": {
 * 			"type": "string",
 * 			"description": "pet status in the store",
 * 			"enum": [
 * 				"available",
 * 				"pending",
 * 				"sold"
 * 			]
 * 		}
 * 	},
 * 	"xml": {
 * 		"name": "Pet"
 * 	}
 * }
 */
```

---
_Source: https://npm.io/package/swag4k · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
