oas2client
v1.0.2
Published
Convert OpenAPI specifications to clients, with extendable configuration
Downloads
3
Readme
OAS2Client
Convert OpenAPI Specification to a typed client implementing Axios
Usage
- Add the package to your project
yarn add oas2client
- 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
.
- You can then execute this script to create the services in the specified directory
npx esno create-services.ts
- 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()