Package Exports
- @zodios/core
- @zodios/core/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@zodios/core) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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/coreor
> yarn add @zodios/coreUsage
For an almost complete example on how to use zodios and how to split your APIs declarations, take a look at dev.to example.
Declare your API with zodios
Here is an example of API declaration with Zodios.
import { Zodios } from "@zodios/core";
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
alias: "getUser", // optionnal alias to call this endpoint with it
description: "Get a user",
response: z.object({
id: z.number(),
name: z.string(),
}),
},
] as const,
);Calling this API is now easy and has builtin autocomplete features :
// typed auto-complete path auto-complete params
// ▼ ▼ ▼
const user = await apiClient.get("/users/:id", { params: { id: 7 } });
console.log(user);It should output
{ id: 7, name: 'Kurtis Weissnat' }You can also use aliases :
// typed alias auto-complete params
// ▼ ▼ ▼
const user = await apiClient.getUser({ params: { id: 7 } });
console.log(user);Use token provider plugin
Zodios comes with a plugin to inject and renew your tokens :
import { pluginToken } from '@zodios/plugins';
apiClient.use(pluginToken({
getToken: async () => "token"
}));Get underlying axios instance
you can get back the underlying axios instance to customize it.
const axiosInstance = apiClient.axios;Give your own axios instance to zodios
you can instanciate zodios with your own axios intance.
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
// Optional Axios instance
{
axiosIntance: customAxiosInstance
}
);Disable zodios response validation
const apiClient = new Zodios(
"https://jsonplaceholder.typicode.com",
[ ... ] as const,
// Disable validation
{
validateResponse: false
}
);CRUD helper
Zodios has a helper to generate basic CRUD API. It will generate all the api definitions for you :
import { Zodios, asCrudApi } from '@zodios/core';
const apiClient = new Zodios(BASE_URL,
asCrudApi(
'user',
z.object({
id: z.number(),
name: z.string(),
})
));Is the same as :
const apiClient = new Zodios(BASE_URL, [
{
method: "get",
path: "/users",
alias: "getUsers",
description: "Get all users",
response: z.array(userSchema),
},
{
method: "get",
path: "/users/:id",
alias: "getUser",
description: "Get a user",
response: userSchema,
},
{
method: "post",
path: "/users",
alias: "createUser",
description: "Create a user",
parameters: [
{
name: "body",
type: "Body",
description: "The object to create",
schema: userSchema.partial(),
},
],
response: userSchema,
},
{
method: "put",
path: "/users/:id",
alias: "updateUser",
description: "Update a user",
parameters: [
{
name: "body",
type: "Body",
description: "The object to update",
schema: userSchema,
},
],
response: userSchema,
},
{
method: "patch",
path: "/users/:id",
alias: "patchUser",
description: "Patch a user",
parameters: [
{
name: "body",
type: "Body",
description: "The object to patch",
schema: userSchema.partial(),
},
],
response: userSchema,
},
{
method: "delete",
path: "/users/:id",
alias: "deleteUser",
description: "Delete a user",
response: userSchema,
},
] as const);React helpers
Zodios comes with a Query and Mutation hook helper.
It's a thin wrapper around React-Query but with zodios auto completion.
Zodios query hook also returns an invalidation helper to allow you to reset react query cache easily
import React from "react";
import { QueryClient, QueryClientProvider } from "react-query";
import { Zodios } from "@zodios/core";
import { ZodiosHooks } from "@zodios/react";
import { z } from "zod";
// you can define schema before declaring the API to get back the type
const userSchema = z
.object({
id: z.number(),
name: z.string(),
})
.required();
const createUserSchema = z
.object({
name: z.string(),
})
.required();
const usersSchema = z.array(userSchema);
// you can then get back the types
type User = z.infer<typeof userSchema>;
type Users = z.infer<typeof usersSchema>;
const api = [
{
method: "get",
path: "/users",
description: "Get all users",
parameters: [
{
name: "q",
type: "Query",
schema: z.string(),
},
{
name: "page",
type: "Query",
schema: z.string().optional(),
},
],
response: usersSchema,
},
{
method: "get",
path: "/users/:id",
description: "Get a user",
response: userSchema,
},
{
method: "post",
path: "/users",
description: "Create a user",
parameters: [
{
name: "body",
type: "Body",
schema: createUserSchema,
},
],
response: userSchema,
},
] as const;
const baseUrl = "https://jsonplaceholder.typicode.com";
const zodios = new Zodios(baseUrl, api);
const zodiosHooks = new ZodiosHooks("jsonplaceholder", zodios);
const Users = () => {
const {
data: users,
isLoading,
error,
invalidate: invalidateUsers, // zodios also provides invalidation helpers
} = zodiosHooks.useQuery("/users");
const { mutate } = zodiosHooks.useMutation("post", "/users", undefined, {
onSuccess: () => invalidateUsers(),
});
return (
<>
<h1>Users</h1>
<button onClick={() => mutate({ name: "john doe" })}>add user</button>
{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>
)}
</>
);
};
// on another file
const queryClient = new QueryClient();
export const App = () => {
return (
<QueryClientProvider client={queryClient}>
<Users />
</QueryClientProvider>
);
};