0.2.16 • Published 4 years ago

openapi-server v0.2.16

Weekly downloads
98
License
MIT
Repository
github
Last release
4 years ago

openapi-server

Framework agnostic OpenAPI server.

How is this different from existing OpenAPI server frameworks?

Existing frameworks assume a setting, like being integrated into Express.

This framework is completely agnostic and only assumes Node request and response objects to be used.

Example

Controller

In this example request variables are still used, but the parameters defined in the OpenAPI spec are passed in one object. Regardless whether they came from the path, headers or query string.

const { response } = require("openapi-server");

function helloWorld({ name, ["User-Agent"]: userAgent, x }, req) {
  return response({
    message: `Hello ${name}! You're on ${req.path} (Your IP is ${req.connection.remoteAddress} and your User-Agent is ${userAgent}, x = ${x})`
  });
}

module.exports = {
  helloWorld
};

Full example including authentication

const bodyParser = require("body-parser");
const express = require("express");

const createMiddleware = require("openapi-server/lib/middleware");
const response = require("openapi-server/lib/response");

const controllers = {
  auth: {
    login({ username, password }) {
      if (username != "test@localhost" || password != "test") {
        return response(null, 401);
      }

      return response({
        token: "1234"
      });
    }
  }
};

const spec = {
  paths: {
    "/login": {
      post: {
        tags: ["auth"],
        operationId: "login",
        requestBody: {
          content: {
            "application/json": {
              schema: {
                type: "object",
                properties: {
                  emailAddress: {
                    type: "string",
                    example: "test@localhost"
                  },
                  password: {
                    type: "string",
                    example: "test"
                  }
                }
              }
            }
          }
        },
        responses: {
          200: {
            description: "The session was created.",
            content: {
              "application/json": {
                schema: {
                  type: "object",
                  properties: {
                    token: { type: "string" }
                  }
                }
              }
            }
          },
          401: {
            description: "The credentials were invalid.",
            content: {
              "text/plain": {
                schema: {
                  type: "string",
                  example: ""
                }
              }
            }
          }
        }
      }
    }
  }
};

const app = express();

app.use(bodyParser.json());

app.get("/health", (req, res) => {
  res.send("ok");
});

app.use("/api", createMiddleware(spec, controllers));

app.listen(8080);

We can use curl to do a test request:

curl -H "Content-Type: application/json" -d '{"emailAddress": "test@localhost","password": "test"}' http://localhost:8080/api/login

Above request will produce this result:

{
  "token": "1234"
}
0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago