Skip to content

confuser/exegesis-koa

Repository files navigation

exegesis-koa

Build Status Coverage Status Known Vulnerabilities

exegesis

n. An explanation or critical interpretation of a text, especially an API definition document.

-- No dictionary ever

This library implements a Koa middleware for OpenAPI 3.x.

npm install exegesis-koa

Tutorial

Check out the tutorial here.

Usage

const Koa = require('koa')
const path = require('path')
const exegesisKoa = require('exegesis-koa')

async function createServer() {
    // See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
    const options = {
        controllers: path.resolve(__dirname, './controllers')
    }
    const exegesisMiddleware = exegesisKoa(path.resolve(__dirname, './openapi.yaml'), options)

    const app = new Koa()

    // If you have any body parsers, this should go before them.
    app.use(async (ctx, next) => {
      try {
        await next()
      } catch (err) {
        ctx.status = 500
        ctx.body = `Internal error: ${err.message}`
      }
    })
    app.use(exegesisMiddleware)
    app.use(async (ctx) => {
      if (ctx.status === 404) {
        ctx.status = 404
      }
    })

    app.listen()
}

Calling exegesiskoa(openApiFile, options) will return a Promise which resolves to a koa middleware.

openApiFile is either a path to your openapi.yaml or openapi.json file, or it can be a JSON object with the contents of your OpenAPI document. This should have the x-exegesis-controller extension defined on any paths you want to be able to access.

options can be anything you can pass to exegesis. At a minimum, you'll probably want to provide options.controllers, a path to where your controller modules can be found. If you have any security requirements defined, you'll also want to pass in some authenticators. To enable response validation, you'll want to provide a validation callback function via onResponseValidationError(). Exegesis's functionality can also be extended using plugins, which run on every request. Plugins let you add functionality like role base authorization, or CORS.

About

Koa middleware to handle OpenAPI 3.x.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published