@fezi/client
TypeScript icon, indicating that this package has built-in type declarations

0.1.9 • Public • Published

@fezi/client

Type-Safe Fetching Made Easy

Installation

npm install @fezi/client
# or
yarn add @fezi/client
# or
pnpm add @fezi/client

Basic Usage

import { createClientAPI, APIClient } from '@fezi/client';
import { z } from 'zod';

// Create a client
const client = new APIClient({
  baseURL: 'https://api.example.com',
  headers() {
    const token = localStorage.getItem('token');

    return {
      Authorization: `Bearer ${token}`,
      'Content-Type': 'application/json',
    };
  },
});

// Define schemas
const userSchema = z.object({
  id: z.number(),
  name: z.string(),
  email: z.string().email(),
});

// Define routes
const routes = {
  v1: {
    users: {
      get: client
      .route({ path: '/users' })
      .output(z.array(userSchema)),
      getById: client
      .route({ path: '/users/:id' })
      .output(userSchema),
      create: client
      .route({ path: '/users', method: 'POST' })
      .input(userSchema.omit({ id: true })),
    },
  },
};

// Create a router
const api = createClientAPI(client, routes);

// Use the API
const getUsers = await api.v1.users.get.execute();
// getUsers.data
// getUsers.error

const getById = await api.v1.users.getById.execute({ id: 1 });
// getById.data
// getById.error

const createUser = await api.v1.users.create.execute({
  name: 'John Doe',
  email: 'john.doe@example.com',
});
// createUser.data
// createUser.error

License

MIT

Package Sidebar

Install

npm i @fezi/client

Weekly Downloads

1

Version

0.1.9

License

MIT

Unpacked Size

194 kB

Total Files

53

Last publish

Collaborators

  • johngerome