Thanks to visit codestin.com
Credit goes to github.com

Skip to content

richiexuetang/zodmon

Repository files navigation

Zodmon

Zodmon logo

Zodmon is a typescript api client and an optional api server with auto-completion features including axios and zod

What is it ?

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 fetch adapter or auth automatic injection
  • all axios features available
  • @tanstack/query wrappers for react

Table of contents:

Install

Client and api definitions :

> npm install @zodmon/core

or

> yarn add @zodmon/core

or

> pnpm add @zodmon/core

Declare your API with zodmon

Here 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);

API definition format

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
  }>;
}>;

About

Generate a typescript http client with zod validation from OpenAPI specs

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published