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 Enregistrement.de.l.ecran.2023-11-12.a.13.59.14.movTable of Contents:
To get started, install http-wizard using npm or yarn:
npm install @http-wizard/core # or yarn add @http-wizard/core
Currently http-wizard uses Zod or Typebox for validation. Here is an example with Zod.
Let's first create a route on the server:
// 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:
// client.ts import { createClient, ZodTypeProvider } from '@http-wizard/core'; import axios from 'axios'; import type { Router } from './server'; const apiClient = createClient<Router, ZodTypeProvider>(axios.instance()); const users = await apiClient.ref('[GET]/users').query({}); // users array is safe: { id:string, name:string }[]
Easy, right?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4