-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Florian
committed
Nov 12, 2023
1 parent
fe12893
commit 4386e82
Showing
7 changed files
with
3,903 additions
and
5,632 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<h1 align="center">http-wizard</h1> | ||
<p align="center"><a href="https://git.io/typing-svg"><img src="https://readme-typing-svg.demolab.com?font=Fira+Code&size=18&duration=2000&pause=2000¢er=true&width=540&height=80&lines=First+class+api+client+for+node+servers." alt="Typing SVG" /></a></p> | ||
|
||
### 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 exemple of usage | ||
|
||
https://github.com/flodlc/http-wizard/assets/3781663/71c749f0-3493-4865-8a9a-41421a371a05 | ||
|
||
### 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 exemple 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<Router, ZodTypeProvider>(axios.instance()); | ||
const users = await apiClient.ref("[GET]/users").query({}); | ||
// users array is safe: { id:string, name:string }[] | ||
``` | ||
|
||
Easy right ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.mjs", | ||
"packageManager": "[email protected]", | ||
"version": "1.3.12", | ||
"version": "1.3.14", | ||
"scripts": { | ||
"dev": "tsup src/index.ts --watch", | ||
"build": "tsup src/index.ts --dts", | ||
|
@@ -29,10 +29,7 @@ | |
"zod": "^3.22.4" | ||
}, | ||
"peerDependencies": { | ||
"@sinclair/typebox": "^0.29.0", | ||
"@tanstack/react-query": "^5.8.1", | ||
"axios": "^1.4.0", | ||
"zod": "3.x" | ||
"axios": "^1.4.0" | ||
}, | ||
"files": [ | ||
"dist/*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<h1 align="center">http-wizard</h1> | ||
<p align="center"><a href="https://git.io/typing-svg"><img src="https://readme-typing-svg.demolab.com?font=Fira+Code&size=18&duration=2000&pause=2000¢er=true&width=540&height=80&lines=First+class+api+client+for+node+servers." alt="Typing SVG" /></a></p> | ||
|
||
### 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 exemple of usage | ||
|
||
https://github.com/flodlc/http-wizard/assets/3781663/71c749f0-3493-4865-8a9a-41421a371a05 | ||
|
||
### 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 exemple 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<Router, ZodTypeProvider>(axios.instance()); | ||
const users = await apiClient.ref("[GET]/users").query({}); | ||
// users array is safe: { id:string, name:string }[] | ||
``` | ||
|
||
Easy right ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,34 +7,29 @@ | |
"types": "./dist/index.d.ts", | ||
"module": "./dist/index.mjs", | ||
"packageManager": "[email protected]", | ||
"version": "1.3.12", | ||
"version": "1.3.14", | ||
"scripts": { | ||
"dev": "tsup src/index.ts --watch", | ||
"build": "tsup src/index.ts --dts", | ||
"test": "jest" | ||
}, | ||
"dependencies": { | ||
"axios": "^1.4.0" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"@sinclair/typebox": "^0.31.23", | ||
"@tanstack/react-query": "^5.8.1", | ||
"@types/jest": "^29.5.2", | ||
"@types/node": "^18.7.16", | ||
"axios": "^1.4.0", | ||
"esbuild-jest": "^0.5.0", | ||
"http-wizard": "1.3.11", | ||
"@http-wizard/core": "1.3.13", | ||
"jest": "^29.5.0", | ||
"ts-jest": "^29.1.1", | ||
"tsup": "^6.2.3", | ||
"typescript": "^5.0.4", | ||
"zod": "^3.22.4" | ||
}, | ||
"peerDependencies": { | ||
"@sinclair/typebox": "^0.29.0", | ||
"@tanstack/react-query": "5.x", | ||
"http-wizard": "1.3.11", | ||
"zod": "3.x" | ||
"@http-wizard/core": "1.3.13" | ||
}, | ||
"files": [ | ||
"dist/*" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type { Client, RouteDefinition, OkResponse } from "http-wizard"; | ||
export type { Client, RouteDefinition, OkResponse } from "@http-wizard/core"; | ||
export { createQueryClient } from "./ReactQuery"; |
Oops, something went wrong.