Feed it a JSON Spec, it will spit out a lightweight HTTP client!
Why
While there are plenty of "code generators" for OpenAPI Specification, they create a lot of "garbage" code that you may not need,
and there while there are others that follow a similar path of this library, they still attempt to do too much! (like request validation before sending)
This library does not concern itself with anything other than constructing an HTTP request and sending it!
FAQ
Why no validation?
You should rely on validation & sanitation at the source of truth: The API server itself!
YAML Support?
This package does not natively support OpenAPI Specification YAML format, but you can easily convert to JSON before calling oas-rqeuest
const spec = require('./petstore.json')const OASRequest = require('oas-request')(spec)// define root server urlconst request = new OASRequest({ server: 'http://petstore.swagger.io/v1'})// or use one from the OpenAPI Specificationconst request = new OASRequest({ server: { url: spec.servers[0].url // populate values for server (see OpenAPI Specification #4.7.5) variables: { version: 'v2' } }})// auto generated methods match OpenAPI Specification "operationId"await request.listPets
Advanced Usage
const spec = require('./petstore.json')const OASRequest = require('oas-request')(spec)// always use JSON headersconst request = new OASRequest({ server: 'http://petstore.swagger.io/v1' headers: { 'accept': 'application/json', 'content-type': 'application/json' }})// POST with JSONconst body = JSON.stringify(body)const response = await request.createPets({ body })const data = await
new OASRequest(APIOptions)
Construct a new instance of the API request, returns an Object with auto generated method names matching each of the unique OpenAPI Specification operationId
a Function with the signature: Function(url, requestOptions) to execute the HTTP request, the default built-in function uses cross-fetch, you can customize the client to use whatever HTTP library you prefer.