Skip to content

Commit 4236b3c

Browse files
authored
0.1.3 (#4)
1 parent 1e0f396 commit 4236b3c

File tree

23 files changed

+4311
-103
lines changed

23 files changed

+4311
-103
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "express-typed--prisma-demo",
3+
"version": "1.0.0",
4+
"type": "module",
5+
"scripts": {
6+
"dev": "tsx watch src/app.ts"
7+
},
8+
"dependencies": {
9+
"@types/express": "^4.17.21",
10+
"@types/morgan": "^1.9.9",
11+
"@types/node": "^20.12.7",
12+
"axios": "^1.6.8",
13+
"express": "^4.19.2",
14+
"express-typed": "workspace:*",
15+
"morgan": "^1.10.0"
16+
},
17+
"devDependencies": {
18+
"tsx": "^4.7.2"
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from "express";
2+
import logger from "morgan";
3+
import typedRouter from "./routes/index.routes";
4+
5+
// Create Express server
6+
export const app = express();
7+
8+
app.use(logger("dev"));
9+
10+
// Parse incoming requests data
11+
app.use(express.urlencoded({ extended: true }));
12+
app.use(express.json());
13+
14+
app.use("/", typedRouter.router);
15+
16+
const server = app.listen(4000, () => {
17+
console.log(`Listening on port ${4000}`);
18+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { GetRouteResponseInfo, TypedRouter, ParseRoutes, GetRouteResponseInfoHelper, HandlerMethods, KeysWithMethod, GetRouterMethods, GetRoutesWithMethod } from "express-typed";
2+
3+
const typedRouter = new TypedRouter({
4+
// example usage
5+
"/": {
6+
get: (req, res) => {
7+
return res.send("Hello world").status(200);
8+
},
9+
},
10+
});
11+
12+
export default typedRouter;
13+
14+
export type AppRoutes = ParseRoutes<typeof typedRouter>;
15+
16+
export type RouteResolver<
17+
Path extends keyof AppRoutes,
18+
Method extends keyof AppRoutes[Path],
19+
Info extends keyof GetRouteResponseInfoHelper<AppRoutes, Path, Method> | "body" = "body"
20+
> = GetRouteResponseInfo<AppRoutes, Path, Method, Info>;
21+
22+
23+
export type RoutesWithMethod<Method extends GetRouterMethods<AppRoutes>> = GetRoutesWithMethod<AppRoutes, Method>;
24+
25+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { TypedRouter } from "express-typed";
2+
3+
const typedRouter = new TypedRouter({
4+
"/": {
5+
get: (req, res) => {
6+
return res.send("even-more-nested").status(200);
7+
},
8+
},
9+
});
10+
11+
export default typedRouter;

examples/regular-express/backend/tsconfig.json renamed to examples/express-typed-prisma/tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"compilerOptions": {
3-
"module": "NodeNext",
43
"esModuleInterop": true,
54
"target": "esnext",
65
"noImplicitAny": true,
@@ -11,6 +10,6 @@
1110
"skipLibCheck": true,
1211
"strict": true
1312
},
14-
"include": ["backend/src/**/*.ts"],
13+
"include": ["src/**/*.ts"],
1514
"exclude": ["node_modules"]
1615
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GetRouteResponseInfo, TypedRouter, ParseRoutes, GetRouteResponseInfoHelper, HandlerMethods, KeysWithMethod } from "express-typed";
1+
import { GetRouteResponseInfo, GetRouteResponseInfoHelper, GetRouterMethods, GetRoutesWithMethod, ParseRoutes, TypedRouter } from "express-typed";
22

33
import nestedRouter from "./nested.routes";
44

@@ -53,9 +53,7 @@ type HomePageStatus = RouteResolver<"/", "get", "status">;
5353
////
5454

5555
//// RoutesWithMethod
56-
export type RoutesWithMethod<Method extends HandlerMethods> = {
57-
[key in KeysWithMethod<AppRoutes, Method>]: Method extends keyof AppRoutes[key] ? GetRouteResponseInfo<AppRoutes, key, Method> : never;
58-
};
56+
export type RoutesWithMethod<Method extends GetRouterMethods<AppRoutes>> = GetRoutesWithMethod<AppRoutes, Method>;
5957

6058
// usage
6159
// get all routes that have a "get" method, and their response types

examples/fullstack_react_express-typed/frontend-demo/src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "./App.css";
22
import { useAppQuery } from "./queries";
33

44
function App() {
5-
const query = useAppQuery("/", "get");
5+
const query = useAppQuery("/nested/", "get");
66
const data = query.data;
77
// ^? const query: UseQueryResult<"Hello world", Error>
88

0 commit comments

Comments
 (0)