Zodios
Zodios is a typescript api client with auto-completion features backed by axios and zod
What is it ?
It's an axios compatible API client, with the following features:
- really simple centralized API declaration
- typescript autocompletion in your favorite IDE for URL and parameters
- typescript response types
- parameters and responses schema thanks to zod
- response schema validation
- bearer token injection and token renewal with simple token provider interface
- all axios features available
Install
> npm install zodios
or
> yarn add zodios
Usage
Declare your API with zodios
import { Zodios } from "zodios";
import { z } from "zod";
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[
{
method: "get",
path: "/users/:id",
description: "Get a user",
response: z.object({
id: z.number(),
name: z.string(),
}),
},
] as const,
{
tokenProvider: {
getToken: () => Promise.resolve("token"),
}
}
);
const user = await apiClient.get("/users/:id", { params: { id: 7 } });
console.log(user);
Get underlying axios instance
you can get back the underlying axios instance to customize it.
const axiosInstance = apiClient.axios;
Give your own axios intance to zodios
you can instanciate zodios with your own axios intance.
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
{
axiosIntance: customAxiosInstance
}
);
Disable zodios response validation
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
{
validateResponse: false
}
);