Zodmon is a typescript api client and an optional api server with auto-completion features including axios and zod
It's an axios compatible API client compatible API server with the following features:
- centralized API declaration and contracts
- typescript!
- parameters and responses schema thanks to zod (w/ validation)
- powerful plugins like
fetchadapter orauthautomatic injection - all axios features available
@tanstack/querywrappers for react
Table of contents:
> npm install @zodmon/coreor
> yarn add @zodmon/coreor
> pnpm add @zodmon/coreHere is an example of API declaration with zodmon.
import { zodmon } from "@zodmon/core";
import { z } from "zod";
const apiClient = new zodmon(
"https://jsonplaceholder.typicode.com",
// API definition
[
{
method: "get",
path: "/users/:id", // auto detect :id and ask for it in apiClient get params
alias: "getUser", // optional alias to call this endpoint with it
description: "Get a user",
response: z.object({
id: z.number(),
name: z.string(),
}),
},
]
);Calling this API is now easy and has builtin autocomplete features :
// typed auto-complete path auto-complete params
// ▼ ▼ ▼
const user = await apiClient.get("/sorcerers/:id", { params: { id: 2 } });
console.log(sorcerer);It should output
{ id: 7, name: 'Gojo Satoru' }You can also use aliases :
// typed alias auto-complete params
// ▼ ▼ ▼
const sorcerer = await apiClient.getSorcerer({ params: { id: 2 } });
console.log(sorcerer);type ZodmonEndpointDescriptions = Array<{
method: "get" | "post" | "put" | "patch" | "delete";
path: string; // example: /posts/:postId/comments/:commentId
alias?: string; // example: getPostComments
immutable?: boolean; // flag a post request as immutable to allow it to be cached with react-query
description?: string;
requestFormat?: "json" | "form-data" | "form-url" | "binary" | "text"; // default to json if not set
parameters?: Array<{
name: string;
description?: string;
type: "Path" | "Query" | "Body" | "Header";
schema: ZodSchema; // you can use zod `transform` to transform the value of the parameter before sending it to the server
}>;
response: ZodSchema; // you can use zod `transform` to transform the value of the response before returning it
status?: number; // default to 200, you can use this to override the success status code of the response (only useful for openApi)
responseDescription?: string; // optional response description of the endpoint
errors?: Array<{
status: number | "default";
description?: string;
schema: ZodSchema; // transformations are not supported on error schemas
}>;
}>;