JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 46
  • Score
    100M100P100Q87841F
  • License MIT

Feed it a JSON Spec, it will spit out a lightweight HTTP client!

Package Exports

  • oas-request

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (oas-request) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

OpenAPI Spec HTTP Client

Feed it a JSON Spec, it will spit out a lightweight HTTP client!

license release super linter test semantic

Why

While there are plenty of "code generators" for OpenAPI, 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 OpenAPI server itself!

What

Some feature highlights:

  • Zero dependencies!
  • Lightweight
  • Node.js and Browser ready (browser support coming soon)
  • Automatic methods creation
  • Path Templating

Usage

const spec = require('./petstore.json')
const API = require('oas-request')(spec)

// define root server url
const client = new API('https://httpbin.org')

// auto generated methods matching your OAS operationIds
await client.listPets(options)
await client.createPets(options)
await client.showPetById(options)

Yaml Support?

This package does not support OAS Yaml format, but you can easily convert to JSON before calling `oas-rqeuest`
using js-yaml
const yaml = require('js-yaml')
const fs   = require('fs')

const spec = yaml.safeLoad(fs.readFileSync('openapi.yml', 'utf8'))


const API = require('oas-request')(spec)
using apidevtools/swagger-cli
npx apidevtools/swagger-cli bundle spec/openapi.yml --outfile spec.json

Options

Each generated method accepts an options object with the following properties:

name type required description
body Object HTTP request body
headers Object HTTP request headers
params Object Path parameters
query Object Query String

Examples

OAS 3.x
{
  "/pets/{petId}": {
    "get": {
      "operationId": "getPetById",
      ...
    },
    "put": {
      "operationId": "updatePetById",
      ...
    }
  }
}
Your App
const spec = require('./petstore.json')
const API = require('oas-request')(spec)

// define root server url
const client = new API('https://httpbin.org')

// auto generated methods matching your OAS operationIds
await client.getPetById({
  params: { petId: 'my-pet' }
})

await client.updatePetById({
  params: { petId: 'my-pet' },
  body: {
    name: "ruby",
    isGoodDog: true
  }
})
HTTP Requests
GET /pets/my-pet HTTP/1.1
Host: httpbin.org
Accept: application/json
PUT /pets/my-pet HTTP/1.1
Host: httpbin.org
Accept: application/json
Content-Type: application/json

{ "name": "ruby", "isGoodDog": true }

Author: Ahmad Nassri • Twitter: @AhmadNassri