ts-ez-api
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

ts-ez-api

API Schema Generator with Zod Validation

This package provides an easy way to define API schemas with automatic request payload and response validation using Zod. It generates type-safe endpoints and handles API requests with Axios, ensuring your payloads and responses conform to the expected schema at runtime. Ideal for developers looking to build robust, validated REST APIs with minimal effort.

Key features:

Define API routes with payload and return schemas using Zod. Automatic validation of request payloads and responses. Supports GET, POST, PUT, and DELETE methods. Built-in integration with Axios for HTTP requests.

Create schema

import { z } from "zod";
import { Config, createApiSchema } from ".";

const config: Config = {
  url: "https://jsonplaceholder.typicode.com",
};

// Example usage
export const api = createApiSchema(config, {
  posts: {
    update: {
      endpoint: "/posts/{id}",
      method: "PUT",
      payload: {
        content: z.string(),
        title: z.string(),
      },
      return: {
        id: z.number(),
      },
    },
  },
});

Call your endpoints

const result = await api.posts.updatePost({
  payload: {
    content: "This is the updated content of my post!",
    title: "My new post title",
  },
  params: {
    id: "1",
  },
});

Dependents (0)

Package Sidebar

Install

npm i ts-ez-api

Weekly Downloads

4

Version

1.0.4

License

ISC

Unpacked Size

21.7 kB

Total Files

6

Last publish

Collaborators

  • theobourgeois