|
| 1 | +# express-typed |
| 2 | + |
| 3 | +_Not stable yet_ |
| 4 | + |
| 5 | +[](https://www.npmjs.com/package/express-typed) |
| 6 | +[](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