内容简介:OpenAPIThe idea is to place the proxy between a client (e.g. a frontend app) and a web server to catch invalid requests or responses during development. Use this proxy locally or set it up in your development server. In production environments, use the sil
openapi-cop
OpenAPI Co mpliance P roxy that validates requests and responses against an OpenAPI document
The idea is to place the proxy between a client (e.g. a frontend app) and a web server to catch invalid requests or responses during development. Use this proxy locally or set it up in your development server. In production environments, use the silent flag to forward unmodified response bodies. In any case, validation headers are set that allow to trace down violations of your OpenAPI definition.
FAQ
Installation
Do npm install -g openapi-cop , or npm install openapi-cop to install locally.
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.Interface{ 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.Interface{ 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',
apiDocFile: '/path/to/openapi-file.yaml',
defaultForbidAdditionalProperties: false,
silent: false
});
Made By
at
以上所述就是小编给大家介绍的《An OpenAPI proxy that only allows data that matches your swagger.json》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
VC++.NET入门
Davis Chapman / 中国电力出版社 / 2003-5 / 55.0
《VC++.NET入门》提供了学习Visual C++工具的循序渐进的指导和创建应用程序的向导。你将学习设计应用程序窗口、使用控件、显示图形、创建SDI和MDI应用程序、操作数据库以及创建多任务程序,也将学习在微软的新.E平台下使用Visual C++的一些基本技巧,以及学习它影响设计和编写应用程序的方式。一起来看看 《VC++.NET入门》 这本书的介绍吧!