![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Zodios is a typescript api client with auto-completion features backed by axios and zod
It's an axios compatible API client, with the following features:
> npm install zodios
or
> yarn add zodios
For an almost complete example on how to use zodios and how to split your APIs declarations, take a look at dev.to example.
import { Zodios } from "zodios";
import { z } from "zod";
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
// API definition
[
{
method: "get",
path: "/users/:id", // auto detect :id and ask for it in apiClient get params
description: "Get a user",
response: z.object({
id: z.number(),
name: z.string(),
}),
},
] as const,
);
// typed auto-complete path auto-complete params
// ▼ ▼ ▼
const user = await apiClient.get("/users/:id", { params: { id: 7 } });
console.log(user);
// Output: { id: 7, name: 'Kurtis Weissnat' }
Zodios comes with a plugin to inject and renew your tokens :
import { pluginToken } from 'zodios/plugins/token';
apiClient.use(pluginToken({
getToken: async () => "token"
}));
you can get back the underlying axios instance to customize it.
const axiosInstance = apiClient.axios;
you can instanciate zodios with your own axios intance.
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
// Optional Axios instance
{
axiosIntance: customAxiosInstance
}
);
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
// Disable validation
{
validateResponse: false
}
);
Zodios comes with a React Provider to register all your api clients and a hook to use them.
The hook is a thin wrapper around react-query useQuery, so you need to also add a react-query provider.
import { QueryClient, QueryClientProvider } from 'react-query';
import { Paths, Zodios, ZodiosRequestOptions } from "zodios";
import { useZodios, ZodiosProvider } from "zodios/react";
import { z } from "zod";
const userSchema = z
.object({
id: z.number(),
name: z.string(),
});
const usersSchema = z.array(userSchema);
type User = z.infer<typeof userSchema>;
type Users = z.infer<typeof usersSchema>;
const api = [
{
method: "get",
path: "/users",
description: "Get all users",
response: usersSchema,
}
] as const;
const baseUrl = "https://jsonplaceholder.typicode.com";
type Api = typeof api;
function useJsonPlaceholder<Path extends Paths<Api, "get">>(path: Path, config?: ZodiosRequestOptions<Api, "get", Path>) {
return useZodios(baseUrl, path, config);
}
const Users = () => {
const { data: users, isLoading, error } = useJsonPlaceholder("/users");
return (
<div>
<h1>Users</h1>
{isLoading && <div>Loading...</div>}
{error && <div>Error: {(error as Error).message}</div>}
{users && (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
)}
</div>
);
};
const apiClient = new Zodios(baseUrl, api);
const queryClient = new QueryClient();
export const App = () => {
return (
<QueryClientProvider client={queryClient}>
<ZodiosProvider apis={[apiClient]}>
<Users />
</ZodiosProvider>
</QueryClientProvider>
);
};
FAQs
Typescript API client with autocompletion and zod validations
The npm package zodios receives a total of 290 weekly downloads. As such, zodios popularity was classified as not popular.
We found that zodios demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.