https://github.com/seamapi/typed-axios
A simple way to create an Axios instance that is fully typed with the routes from an API
https://github.com/seamapi/typed-axios
Last synced: 2 months ago
JSON representation
A simple way to create an Axios instance that is fully typed with the routes from an API
- Host: GitHub
- URL: https://github.com/seamapi/typed-axios
- Owner: seamapi
- Created: 2023-01-14T02:09:24.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2025-03-08T11:31:14.000Z (9 months ago)
- Last Synced: 2025-10-01T15:56:19.884Z (2 months ago)
- Language: TypeScript
- Homepage:
- Size: 882 KB
- Stars: 6
- Watchers: 5
- Forks: 1
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Typed Axios `typed-axios-instance`

Typed Axios Instance is a simple way to create an Axios instance that is fully typed with the routes from an API.
The benefit of using TypedAxiosInstance is you don't need to create or import a client
for a third party API, you can just apply types (generated from [OpenAPI](#) or
[Nextlove](https://github.com/seamapi/nextlove)) to an existing Axios instance.
```ts
import type { TypedAxios } from "typed-axios-instance"
import axios from "axios"
// Need help generating these routes? You can generate them from...
// nextlove: https://github.com/seamapi/nextlove
// openapi: TODO
type Routes = [
{
route: "/things/create"
method: "POST"
jsonBody: {
name?: string | undefined
}
jsonResponse: {
thing: {
thing_id: string
name: string
created_at: string | Date
}
}
}
]
const myAxiosInstance: TypedAxios = axios.create({
baseURL: "http://example-api.com",
})
// myAxiosInstance now has intelligent autocomplete!
```


## Installation
```
npm add --dev typed-axios-instance
# yarn add --dev typed-axios-instance
```
## Route Definition
There are two ways of specifying routes for `TypedAxios`
- `type Routes = RouteDef[]`
- `type Routes = { [route:string]: RouteDef }`
> Using `RouteDef[]` allows you to do [HTTP Method Discrimination](#http-method-discrimination)
> and is the recommended method.
This is the type for `RouteDef`:
```ts
export type RouteDef = {
route: string
method: HTTPMethod // you can supply multiple e.g. `"PATCH" | "POST"`
// INPUTS
queryParams?: Record
jsonBody?: Record
commonParams?: Record
formData?: Record
// RESPONSES
jsonResponse?: Record
}
```
## HTTP Method Discrimination
There are two ways of specifying route definitions, if you specify the route
definitions as an array (default for OpenAPI schemas), you'll get more specific
autocomplete results, e.g. the response or request type will be based on what
method is being used.