An intelligent, type-safe Axios
wrapper powered by OpenAPI schemas.
-
✅ Supports OpenAPI 3.0 and 3.1
-
✅ Zero codegen, 100% type-driven
-
✅ Fully compatible with
Axios
-
✅ Intelligent type inference
-
✅ 100% test coverage
$ npm install --save openapi-ts-axios axios
1. Generate typescript definition with openapi-typescript
.
npx openapi-typescript https://petstore3.swagger.io/api/v3/openapi.yaml -o petstore.d.ts
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 } });
openapi-ts-axios
provides two API styles:
-
OpenApiAxios
– Provides same APIs as native Axios with types. -
OpenApiStyleAxios
– Followsopenapi-typescript style
for fully typed API calls.
See below differences:
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'} });
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'} });
MIT License