silasbw / swagger-fluent

A fluent client for OpenAPI and Swagger
MIT License
8 stars 8 forks source link

swagger-fluent

Build Status Greenkeeper badge

A fluent OpenAPI and Swagger client for JavaScript and Node.js.

fluent-client represents Path Item Object with chains of objects:

/api/v1/namespaces -> api.v1.namespaces

associates operations on a Path Item Object with functions:

/api/v1/namespaces -> api.v1.namespaces.get()

and represents Path Templating with function calls:

/api/v1/namespaces/{namespace}/pods -> api.v1.namespaces(namespace).pods

Configurable "backends" handle executing API calls by, for example, using fetch or Swagger Client. A backend can also perform error checking. The Swagger Client backend, for example, will perform the usual parameter and resolution checking that swagger-js performs and will throw those errors to the caller.

Using

const spec = require('./swagger.json')
const url = 'https://petstore.swagger.io/v2/'
const FetchBackend = require('swagger-fluent/backends/fetch')
const backend = new FetchBackend({ fetch, url })

const { Client } = require('swagger-fluent')
const client = new Client({ spec, backend })

const response = await client.pet.findByStatus.get({ parameters: { status: 'available' } })

API

Client(options)

Create a fluent client for an OpenAPI or Swagger specification.

FetchBackend(options)

Create a Fetch API-based backend.

const FetchBackend = require('swagger-fluent/backends/fetch')

RequestBackend(options)

const RequestBackend = require('swagger-fluent/backends/request')

SwaggerClientBackend(options)

Create a swagger-js-based backend.

const SwaggetClientBackend = require('swagger-fluent/backends/swagger-client')

Custom backend

The backend must implement an .http method. swagger-fluent passes the following options to the .http method, and returns the result directly to the API caller.