openapi-ts-axios
v1.0.4
Published
An intelligent, type-safe Axios wrapper powered by OpenAPI schemas.
Maintainers
Readme
openapi-ts-axios
An intelligent, type-safe Axios wrapper powered by OpenAPI schemas.
Features
✅ Supports OpenAPI 3.0 and 3.1
✅ Zero codegen, 100% type-driven
✅ Fully compatible with
Axios✅ Intelligent type inference
✅ 100% test coverage
Install
$ npm install --save openapi-ts-axios axiosBasic Usage
1. Generate typescript definition with openapi-typescript.
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml -o petstore.d.ts
2. Create typed Axios instance.
import axios from 'axios;
import { OpenApiAxios } from 'openapi-ts-axios';
import type { paths } from './petstore.d.ts';
const instance = OpenApiAxios<paths>(axios.create());
// will automatically provides type hints:
// instance.get('/pet/{petId}', { path: { petId: 1 } });APIs
openapi-ts-axios provides two API styles:
OpenApiAxios– Provides same APIs as native Axios with types.OpenApiStyleAxios– Followsopenapi-typescript stylefor fully typed API calls.
See below differences:
OpenApiAxios
import axios from 'axios;
import { OpenApiAxios } from 'openapi-ts-axios';
import type { paths } from './petstore.d.ts';
const instance = OpenApiAxios<paths>(axios.create());
// Axios style API:
// instance.get(path: string, configs?: OpenAPIAxiosRequestConfig)
instance.get('/pet/{petId}', { path: { petId: 1 }, headers: {'Authorization': 'Bearer xxx'} });OpenApiStyleAxios
import axios from 'axios;
import { OpenApiStyleAxios } from 'openapi-ts-axios';
import type { paths } from './petstore.d.ts';
const instance = OpenApiStyleAxios<paths>(axios.create());
// openapi-typescript style API:
// instance.get(path: string, params?: { path?: object, query?: object, body?: object }, configs?: OpenAPIAxiosRequestConfig)
instance.get('/pet/{petId}', { path: { petId: 1 } }, { headers: {'Authorization': 'Bearer xxx'} });License
MIT License
