Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/flodlc/http-wizard
Http-wizard weaves TypeScript magic, offering a type-safe API client and ensuring a delightful end-to-end developer experience.
https://github.com/flodlc/http-wizard
api client fastify js json-schema react-query tanstack-query typebox typescript zod
Last synced: 25 days ago
JSON representation
Http-wizard weaves TypeScript magic, offering a type-safe API client and ensuring a delightful end-to-end developer experience.
- Host: GitHub
- URL: https://github.com/flodlc/http-wizard
- Owner: flodlc
- License: mit
- Created: 2023-06-03T19:32:59.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-03-03T14:01:45.000Z (8 months ago)
- Last Synced: 2024-09-28T14:22:58.961Z (about 1 month ago)
- Topics: api, client, fastify, js, json-schema, react-query, tanstack-query, typebox, typescript, zod
- Language: TypeScript
- Homepage: https://http-wizard.vercel.app
- Size: 57.8 MB
- Stars: 59
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
http-wizard
### Full documentation website:
[http-wizard.vercel.app](https://http-wizard.vercel.app)
## Introduction
Http-wizard weaves TypeScript magic, offering a type-safe API client and ensuring a delightful end-to-end developer experience. ✨
#### Here is an example of usage
https://github.com/flodlc/http-wizard/assets/3781663/e88fc3f8-4174-4ce0-b0f7-30ab127b4bea
### What it can do:
- 100% type-safe api client with typescript magic (no code generation)
- Fastify first-class support
- React-query first-class support
- Zod and Typebox Type providers
- Delightful end-to-end developer experience (tRPC-like)
- Http standards / REST compatibility: you are owner of your routes
- Type inference utils---
Table of Contents:
- [Installation](#installation)
- [Usage](#usage)---
## Installation
To get started, install http-wizard using npm or yarn:
```sh
npm install @http-wizard/core
# or
yarn add @http-wizard/core
```## Usage
Currently http-wizard uses Zod or Typebox for validation.
Here is an example with Zod.Let's first create a route on the server:
```typescript title="Route creation with Fastify and Zod"
// server.ts
import { createRoute } from '@http-wizard/core';
import { z } from 'zod';const User = z.object({
id: z.string(),
name: z.string(),
});export const getUsers = (fastify: FastifyInstance) => {
return createRoute('/users', {
method: 'GET',
schema: {
response: {
200: z.array(User),
},
},
}).handle((props) => {
fastify.route({
...props,
handler: (request) => {
const users = await db.getUsers();
return users;
},
});
});
};const router = { ...getUsers() };
export type Router = typeof router;
```Now, let's use the Router type on the client:
```typescript title="Client instanciation with axios"
// client.ts
import { createClient, ZodTypeProvider } from '@http-wizard/core';
import axios from 'axios';import type { Router } from './server';
const apiClient = createClient(axios.instance());
const users = await apiClient.ref('[GET]/users').query({});
// users array is safe: { id:string, name:string }[]
```Easy, right?