4.1.2 • Published 6 days ago

exegesis v4.1.2

Weekly downloads
230,333
License
MIT
Repository
github
Last release
6 days ago

Exegesis OpenAPI Engine

NPM version Build Status Coverage Status Greenkeeper badge semantic-release

exegesis

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

-- No dictionary ever

This library implements a framework-agnostic server side implementation of OpenAPI 3.x.

Description

Exegesis is a library for implementing server-side OpenAPI 3.x The library has been written in such a way that hopefully it will also be used to implement future versions of OpenAPI, or possibly even other API description standards altogether.

You probably don't want to be using this library directly. Have a look at:

Features

Tutorial

Check out the tutorial here.

API

compileApi(openApiDoc, options, done)

This function takes an API document and a set of options, and returns a connect-style middleware function which will execute the API.

openApiDoc 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 is described in detail here. 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.

compileRunner(openApiDoc, options, done)

This function is similar to compileApi; it takes an API document and a set of options, and returns a "runner". The runner is a function runner(req, res), which takes in a standard node HTTP request and response. It will not modify the response, however. Instead it returns (either via callback or Promise) and HttpResult object. This is a {headers, status, body} object, where body is a readable stream, read to be piped to the response.

writeHttpResult(httpResult, res, done)

A convenience function for writing an HttpResult from a runner out to the response.

Example

import * as path from 'path';
import * as http from 'http';
import * as exegesis from 'exegesis';

// See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
const options = {
  controllers: path.resolve(__dirname, './src/controllers'),
};

// `compileApi()` can either be used with a callback, or if none is provided,
// will return a Promise.
exegesis.compileApi(
  path.resolve(__dirname, './openapi/openapi.yaml'),
  options,
  (err, middleware) => {
    if (err) {
      console.error('Error creating middleware', err.stack);
      process.exit(1);
    }

    const server = http.createServer((req, res) =>
      middleware(req, res, (err) => {
        if (err) {
          res.writeHead(err.status || 500);
          res.end(`Internal error: ${err.message}`);
        } else {
          res.writeHead(404);
          res.end();
        }
      })
    );

    server.listen(3000);
  }
);

Internal Workings

Internally, when you "compile" an API, Exegesis produces an ApiInterface object. This is an object that, given a method, url, and headers, returns a resolvedOperation - essentially a collection of functions that will parse and validate the body and parameters, has the controller that executes the functionality, etc... The only current implementation for an ApiInterface is the oas3/OpenApi class. Essentially this class's job is to take in an OpenAPI 3.x.x document, and turn it an ApiInterface that Exegesis can use. In theory, however, we could parse some other API document format, produce an ApiInterface, and Exegsis would still be able to run it.

4.1.2

6 days ago

4.1.1

1 year ago

4.0.0

2 years ago

4.1.0

2 years ago

3.0.1

3 years ago

2.5.7

3 years ago

3.0.0

3 years ago

2.5.6

4 years ago

2.5.5

4 years ago

2.5.4

4 years ago

2.5.3

4 years ago

2.5.2

4 years ago

2.5.1

4 years ago

2.5.0

4 years ago

2.4.0

4 years ago

2.3.0

4 years ago

2.2.3

4 years ago

2.2.2

4 years ago

2.2.1

4 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

1.0.0-rc1

6 years ago

1.0.0-rc0

6 years ago

0.4.0

6 years ago

0.3.2

6 years ago

0.3.1

6 years ago

0.3.0

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago