Skip to content

Commit 14feb4e

Browse files
committed
progress
1 parent 4318b47 commit 14feb4e

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

demo/express-typed-demo/src/routes/index.routes.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const typedRouter = new TypedRouter({
3434
export default typedRouter;
3535

3636
export type AppRoutes = ParseRoutes<typeof typedRouter>;
37-
// ^?
37+
// ^?
3838

3939
//// RouteResolver
4040
export type RouteResolver<

packages/express-typed/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
},
2323
"peerDependencies": {
2424
"express": "*",
25-
"@types/express": "*",
26-
"@types/express-serve-static-core": "*"
25+
"@types/express": "*"
2726
},
2827
"dependencies": {
2928
},

packages/express-typed/src/express-typed.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import express from "express";
2-
import type { Request, Response, NextFunction } from "express-serve-static-core";
1+
import express, { Request, Response, NextFunction } from "express";
32

43
export type IHandlerResponse<Res extends any[] = []> = {
54
status<const T>(arg: T): IHandlerResponse<[...Res, { status: T }]>;

readme.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# express-typed
2+
3+
_Not stable yet_
4+
5+
[![npm version](https://badge.fury.io/js/express-typed.svg)](https://www.npmjs.com/package/express-typed)
6+
[![npm downloads](https://img.shields.io/npm/dm/express-typed.svg)](https://www.npmjs.com/package/express-typed)
7+
8+
Express-Typed is an end-to-end TypeScript wrapper for Express.js, designed to streamline the development process by providing strong typing support throughout your Express application.
9+
10+
Express-Typed **infers types from your backend codebase**, unlike libraries such as [ts-rest](https://ts-rest.com/) and [zodios](https://www.zodios.org/), which often require separate type definitions. This approach offers a developer experience akin to [trpc](https://trpc.io/), just in expressjs, without the need to switch to an entirely different framework.
11+
12+
## Installation
13+
14+
You can install express-typed via npm:
15+
16+
```bash
17+
pnpm add express-typed
18+
19+
# or
20+
21+
yarn add express-typed
22+
23+
# or (why?)
24+
25+
npm install express-typed
26+
```
27+
28+
## Usage
29+
30+
express-typed is focused on your express routers, because that's where you define your API routes.
31+
32+
use `TypedRouter` from `express-typed` instead of `express.Router`, the rest of the code remains the same:
33+
34+
```typescript
35+
import express from "express";
36+
import { TypedRouter, ParseRoutes } from "express-typed";
37+
38+
const app = express();
39+
40+
//// THIS:
41+
const router = express.Router();
42+
43+
router.get("/", async (req: Request, res: Response) => {
44+
res.send("Hello World!").status(200);
45+
});
46+
router.post("/", async (req, res) => {
47+
res.send(req.body).status(200);
48+
});
49+
50+
app.use(router);
51+
//// -->
52+
//// BECOMES THIS:
53+
const typedRouter = new TypedRouter({
54+
get: {
55+
"/": async (req, res) => {
56+
res.send("Hello World!").status(200);
57+
},
58+
},
59+
post: {
60+
"/": async (req, res) => {
61+
res.send(req.body).status(200);
62+
},
63+
},
64+
});
65+
66+
app.use(typedRouter.router);
67+
68+
// this type can be imported and used by your frontend code
69+
export type AppRoutes = ParseRoutes<typeof typedRouter>;
70+
/////
71+
72+
app.listen(3000, () => {
73+
console.log("Server is running on port 3000");
74+
});
75+
```
76+
77+
## Contributing
78+
79+
This library is still in its early stages, and thats exactly the time to suggest significant changes.
80+
81+
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request on GitHub.
82+
83+
The technic that was used in this lib create similar typesafe adapters for other backend frameworks like Fastify, Koa, etc.

0 commit comments

Comments
 (0)