1.0.2 • Published 2 years ago

oas2client v1.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

OAS2Client

Convert OpenAPI Specification to a typed client implementing Axios

Usage

  1. Add the package to your project
yarn add oas2client
  1. Choose a directory for persisting the services, and point the OAS file (either remote or local)
// create-services.ts
import { createClientFromOpenAPI } from 'oas2client'

await createClientFromOpenAPI(
    'api/my-api.yml',   // Path to OAS file
    'services',         // Path to services directory
    {
        // Optional function to transform operations' name
        transformOperationName: operationName => toCamelCase(operationName) 
    }
)

The name of the file will be used as the name of the service, in this case, my-api.

  1. You can then execute this script to create the services in the specified directory
npx esno create-services.ts
  1. With the services created, you can just import then in your code
// index.ts
import MyAPI from './services/my-api'

const client = await MyAPI({ /* axios configuration */})

Options

transformOperationName

A function to transform the operation name.

Example

// if the operation name was '__list_users' providing the following options
const options = {
    transformOperationName: operation => operation
        .match(/^__(.*)$/)[1]
        .replace(/_[a-z]/g, match => match[1].toUpperCase())
}
// the resulting function name will be listUsers()

Examples