npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@exxeta/openapi-cop

v1.3.2

Published

OpenAPI compliance proxy

Downloads

21

Readme

Requirements

We run all tests with Node.js versions 10 and 12. Higher versions could possibly work but are not currently supported.

Installation

To install the CLI globally:

npm install -g openapi-cop

To install the package locally (inside an existing NPM package) and run the proxy programatically:

npm install openapi-cop

Usage

There are three ways to run openapi-cop:

  1. Start it with the CLI (1).
  2. Run it programatically inside Node.js (2).
  3. Start a container based on the Docker image (3).

CLI Usage

The openapi-cop node package installs itself as an executable linked as openapi-cop. Run the command with the --help flag to get information about the CLI:

Usage: openapi-cop [options]

Options:
  -s, --file <file>                       path to the OpenAPI definition file
  -h, --host <host>                       the host of the proxy server (default: "localhost")
  -p, --port <port>                       port number on which to run the proxy (default: 8888)
  -t, --target <target>                   full base path of the target API (format: http(s)://host:port/basePath)
  --default-forbid-additional-properties  disallow additional properties when not explicitly specified
  --silent                                do not send responses with validation errors, just set validation headers
  -w, --watch [watchLocation]             watch for changes in a file or directory (falls back to the OpenAPI file)
                                             and restart server accordingly
  -v, --verbose                           show verbose output
  -V, --version                           output the version number
  -h, --help                              output usage information

The proxy validates the requests and responses in the communication with a target server. By default, the proxy will respond with a 500 status code when the validation fails.

{
  "error": {
    "message": "openapi-cop Proxy validation failed",
    "request": {
      "method": "POST",
      "path": "/pets",
      "headers": {
        "host": "localhost:8888",
        "user-agent": "curl/7.59.0",
        "accept": "*/*",
        "content-type": "application/json",
        "content-length": "16"
      },
      "query": {},
      "body": {
        "data": "sent"
      }
    },
    "response": {
      "statusCode": 201,
      "body": "{}",
      "headers": {
        "x-powered-by": "Express",
        "openapi-cop-openapi-file": "7-petstore.yaml",
        "content-type": "application/json; charset=utf-8",
        "content-length": "2",
        "etag": "W/\"2-vyGp6PvFo4RvsFtPoIWeCReyIC8\"",
        "date": "Thu, 25 Jul 2019 13:39:58 GMT",
        "connection": "close"
      },
      "request": {
        "uri": {
          "protocol": "http:",
          "slashes": true,
          "auth": null,
          "host": "localhost:8889",
          "port": "8889",
          "hostname": "localhost",
          "hash": null,
          "search": null,
          "query": null,
          "pathname": "/pets",
          "path": "/pets",
          "href": "http://localhost:8889/pets"
        },
        "method": "POST",
        "headers": {
          "host": "localhost:8888",
          "user-agent": "curl/7.59.0",
          "accept": "*/*",
          "content-type": "application/json",
          "content-length": "16",
          "accept-encoding": "gzip, deflate"
        }
      }
    },
    "validationResults": {
      "request": {
        "valid": true,
        "errors": null
      },
      "response": {
        "valid": false,
        "errors": [
          {
            "keyword": "required",
            "dataPath": "",
            "schemaPath": "#/required",
            "params": {
              "missingProperty": "code"
            },
            "message": "should have required property 'code'"
          }
        ]
      },
      "responseHeaders": {
        "valid": true,
        "errors": null
      }
    }
  }
}

Two headers are added to the response:

  • openapi-cop-validation-result: contains the validation results as JSON.

    {
        request: {
          valid: boolean;
          errors?: Ajv.ErrorObject[] | null;
        },
        response: {
          valid: boolean;
          errors?: Ajv.ErrorObject[] | null;
        },
        responseHeaders: {
          valid: boolean;
          errors?: Ajv.ErrorObject[] | null;
        }
    }
  • openapi-cop-source-request: contains a simplified version of the original request sent by the client as JSON.

    {
      method: string;
      path: string;
      headers: {
        [key: string]: string | string[];
      };
      query?: {
        [key: string]: string | string[];
      } | string;
      body?: any;
    }

See the references of OpenAPI Backend and Ajv for more information.

When the --silent is provided, the proxy will forward the server's response body without modification. In this case, the validation headers are still added.

Module Usage

To run the proxy programatically use runProxy, which returns a Promise<http.Server>:

import {runProxy} from 'openapi-cop';

const server = await runProxy({
  port: 8888,
  host: 'proxyhost',
  targetUrl: 'http://targethost:8989',
  apiDocPath: '/path/to/openapi-file.yaml',
  defaultForbidAdditionalProperties: false,
  silent: false
});

Docker Image Usage

We publish a Docker image lxlu/openapi-cop that you can use for your convenience. This means you can also run openapi-cop with something like

docker run --rm -p 8888:8888 --env TARGET=https://some-host-name:1234 --env FILE=some-openapi-document.json lxlu/openapi-cop

Read more information about the usage here.

FAQ

Contributing

If you want to contribute to openapi-cop, be sure to check the Contributing guidelines and the Contributing wiki page.