|  | 
|  | 1 | +# fastify-openapi-docs | 
|  | 2 | + | 
|  | 3 | +[](https://npm.im/fastify-openapi-docs) | 
|  | 4 | +[](https://david-dm.org/ShogunPanda/fastify-openapi-docs) | 
|  | 5 | +[](https://github.com/ShogunPanda/fastify-openapi-docs/actions?query=workflow%3ACI) | 
|  | 6 | +[](https://codecov.io/gh/ShogunPanda/fastify-openapi-docs) | 
|  | 7 | + | 
|  | 8 | +A simple plugin for Fastify that generates OpenAPI spec automatically. | 
|  | 9 | + | 
|  | 10 | +http://sw.cowtech.it/fastify-openapi-docs | 
|  | 11 | + | 
|  | 12 | +## Installation | 
|  | 13 | + | 
|  | 14 | +Just run: | 
|  | 15 | + | 
|  | 16 | +```bash | 
|  | 17 | +npm install fastify-openapi-docs --save | 
|  | 18 | +``` | 
|  | 19 | + | 
|  | 20 | +## Usage | 
|  | 21 | + | 
|  | 22 | +Register as a plugin as early as possible (in order to track all routes), optional providing any of the following options: | 
|  | 23 | + | 
|  | 24 | +- `openapi`: The OpenAPI document header. | 
|  | 25 | +- `prefix`: Where to serve the OpenAPI files. The default value is `/docs/`. | 
|  | 26 | +- `skipUI`: If set `true`, the the OpenAPI / Swagger UI will not be served. | 
|  | 27 | + | 
|  | 28 | +Routes should contain a `openapi` object inside the `config` with all desired OpenAPI annotations. | 
|  | 29 | + | 
|  | 30 | +Non JSON responses can be generated by setting the `$raw` key in the response schema to the appropriate MIME type. | 
|  | 31 | + | 
|  | 32 | +Empty responses can be generated by setting the `$empty` key in the response schema to `true`. | 
|  | 33 | + | 
|  | 34 | +Routes can be omitted from spec by the list by setting `hide` option to `true` inside their `config`. | 
|  | 35 | + | 
|  | 36 | +Once the server is started, it will serve the OpenAPI specification on both `/{prefix}/openapi.json` and `/{prefix}/openapi.yaml`. | 
|  | 37 | + | 
|  | 38 | +If the UI is enabled, it will be reachable at `/${prefix}`. | 
|  | 39 | + | 
|  | 40 | +## Example | 
|  | 41 | + | 
|  | 42 | +```js | 
|  | 43 | +const server = require('fastify')() | 
|  | 44 | + | 
|  | 45 | +server.register(require('fastify-openapi-docs'), { | 
|  | 46 | +  openapi: { | 
|  | 47 | +    // All these fields are optional, but they should be provided to satisfy OpenAPI specification. | 
|  | 48 | +    openapi: '3.0.3', | 
|  | 49 | +    info: { | 
|  | 50 | +      title: 'Title', | 
|  | 51 | +      description: 'Description', | 
|  | 52 | +      contact: { | 
|  | 53 | +        name: 'Shogun', | 
|  | 54 | +        url: 'https://cowtech.it', | 
|  | 55 | + | 
|  | 56 | +      }, | 
|  | 57 | +      license: { | 
|  | 58 | +        name: 'ISC', | 
|  | 59 | +        url: `https://choosealicense.com/licenses/isc/` | 
|  | 60 | +      }, | 
|  | 61 | +      version: '1.0.0' | 
|  | 62 | +    }, | 
|  | 63 | +    servers: [ | 
|  | 64 | +      { url: 'https://example.com', description: 'Production Server' }, | 
|  | 65 | +      { url: 'https://dev.example.com', description: 'Development Server' } | 
|  | 66 | +    ], | 
|  | 67 | +    tags: [{ name: 'service', description: 'Service' }], | 
|  | 68 | +    components: { | 
|  | 69 | +      securitySchemes: { | 
|  | 70 | +        jwtBearer: { | 
|  | 71 | +          type: 'http', | 
|  | 72 | +          scheme: 'bearer', | 
|  | 73 | +          bearerFormat: 'JWT' | 
|  | 74 | +        } | 
|  | 75 | +      } | 
|  | 76 | +    } | 
|  | 77 | +  } | 
|  | 78 | +}) | 
|  | 79 | + | 
|  | 80 | +server.addSchema({ | 
|  | 81 | +  type: 'object', | 
|  | 82 | +  $id: '#request', | 
|  | 83 | +  description: 'The request payload', | 
|  | 84 | +  properties: { | 
|  | 85 | +    id: { | 
|  | 86 | +      type: 'string', | 
|  | 87 | +      description: 'The operation id', | 
|  | 88 | +      pattern: '^.+$', | 
|  | 89 | +      example: true | 
|  | 90 | +    } | 
|  | 91 | +  }, | 
|  | 92 | +  required: ['id'], | 
|  | 93 | +  additionalProperties: false | 
|  | 94 | +}) | 
|  | 95 | + | 
|  | 96 | +server.addSchema({ | 
|  | 97 | +  type: 'object', | 
|  | 98 | +  $id: '#response', | 
|  | 99 | +  description: 'The response payload', | 
|  | 100 | +  properties: { | 
|  | 101 | +    ok: { | 
|  | 102 | +      type: 'boolean', | 
|  | 103 | +      description: 'The operation response', | 
|  | 104 | +      example: true | 
|  | 105 | +    } | 
|  | 106 | +  }, | 
|  | 107 | +  required: ['ok'], | 
|  | 108 | +  additionalProperties: false | 
|  | 109 | +}) | 
|  | 110 | + | 
|  | 111 | +server.route({ | 
|  | 112 | +  method: 'POST', | 
|  | 113 | +  url: '/path', | 
|  | 114 | +  schema: { | 
|  | 115 | +    body: { $ref: '#request' }, | 
|  | 116 | +    response: { | 
|  | 117 | +      200: { $ref: '#response' } | 
|  | 118 | +    } | 
|  | 119 | +  }, | 
|  | 120 | +  config: { | 
|  | 121 | +    openapi: { | 
|  | 122 | +      description: 'Makes a request', | 
|  | 123 | +      summary: 'Main route', | 
|  | 124 | +      tags: ['service'], | 
|  | 125 | +      security: [{ jwtBearer: [] }] | 
|  | 126 | +    } | 
|  | 127 | +  }, | 
|  | 128 | +  handler(_, reply) { | 
|  | 129 | +    reply.send({ ok: true }) | 
|  | 130 | +  } | 
|  | 131 | +}) | 
|  | 132 | + | 
|  | 133 | +server.listen(3000) | 
|  | 134 | +``` | 
|  | 135 | + | 
|  | 136 | +Once started, the following routes will be available: | 
|  | 137 | + | 
|  | 138 | +- `http://localhost:3000/docs/openapi.yaml` | 
|  | 139 | +- `http://localhost:3000/docs/openapi.json` | 
|  | 140 | +- `http://localhost:3000/docs` (OpenAPI UI) | 
|  | 141 | + | 
|  | 142 | +Note that `$ref` in the schemas will be resolved and replaced accordingly to OpenAPI spec. | 
|  | 143 | + | 
|  | 144 | +## Contributing to fastify-openapi-docs | 
|  | 145 | + | 
|  | 146 | +- Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. | 
|  | 147 | +- Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. | 
|  | 148 | +- Fork the project. | 
|  | 149 | +- Start a feature/bugfix branch. | 
|  | 150 | +- Commit and push until you are happy with your contribution. | 
|  | 151 | +- Make sure to add tests for it. This is important so I don't break it in a future version unintentionally. | 
|  | 152 | + | 
|  | 153 | +## Copyright | 
|  | 154 | + | 
|  | 155 | +Copyright (C) 2021 and above Shogun ([email protected] ). | 
|  | 156 | + | 
|  | 157 | +Licensed under the ISC license, which can be found at https://choosealicense.com/licenses/isc. | 
0 commit comments