Skip to content

Commit

Permalink
0.1.3 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliav2 authored Apr 28, 2024
1 parent 1e0f396 commit 4236b3c
Show file tree
Hide file tree
Showing 23 changed files with 4,311 additions and 103 deletions.
20 changes: 20 additions & 0 deletions examples/express-typed-prisma/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "express-typed--prisma-demo",
"version": "1.0.0",
"type": "module",
"scripts": {
"dev": "tsx watch src/app.ts"
},
"dependencies": {
"@types/express": "^4.17.21",
"@types/morgan": "^1.9.9",
"@types/node": "^20.12.7",
"axios": "^1.6.8",
"express": "^4.19.2",
"express-typed": "workspace:*",
"morgan": "^1.10.0"
},
"devDependencies": {
"tsx": "^4.7.2"
}
}
18 changes: 18 additions & 0 deletions examples/express-typed-prisma/src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import express from "express";
import logger from "morgan";
import typedRouter from "./routes/index.routes";

// Create Express server
export const app = express();

app.use(logger("dev"));

// Parse incoming requests data
app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.use("/", typedRouter.router);

const server = app.listen(4000, () => {
console.log(`Listening on port ${4000}`);
});
25 changes: 25 additions & 0 deletions examples/express-typed-prisma/src/routes/index.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { GetRouteResponseInfo, TypedRouter, ParseRoutes, GetRouteResponseInfoHelper, HandlerMethods, KeysWithMethod, GetRouterMethods, GetRoutesWithMethod } from "express-typed";

const typedRouter = new TypedRouter({
// example usage
"/": {
get: (req, res) => {
return res.send("Hello world").status(200);
},
},
});

export default typedRouter;

export type AppRoutes = ParseRoutes<typeof typedRouter>;

export type RouteResolver<
Path extends keyof AppRoutes,
Method extends keyof AppRoutes[Path],
Info extends keyof GetRouteResponseInfoHelper<AppRoutes, Path, Method> | "body" = "body"
> = GetRouteResponseInfo<AppRoutes, Path, Method, Info>;


export type RoutesWithMethod<Method extends GetRouterMethods<AppRoutes>> = GetRoutesWithMethod<AppRoutes, Method>;


11 changes: 11 additions & 0 deletions examples/express-typed-prisma/src/routes/nested.routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { TypedRouter } from "express-typed";

const typedRouter = new TypedRouter({
"/": {
get: (req, res) => {
return res.send("even-more-nested").status(200);
},
},
});

export default typedRouter;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"module": "NodeNext",
"esModuleInterop": true,
"target": "esnext",
"noImplicitAny": true,
Expand All @@ -11,6 +10,6 @@
"skipLibCheck": true,
"strict": true
},
"include": ["backend/src/**/*.ts"],
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetRouteResponseInfo, TypedRouter, ParseRoutes, GetRouteResponseInfoHelper, HandlerMethods, KeysWithMethod } from "express-typed";
import { GetRouteResponseInfo, GetRouteResponseInfoHelper, GetRouterMethods, GetRoutesWithMethod, ParseRoutes, TypedRouter } from "express-typed";

import nestedRouter from "./nested.routes";

Expand Down Expand Up @@ -53,9 +53,7 @@ type HomePageStatus = RouteResolver<"/", "get", "status">;
////

//// RoutesWithMethod
export type RoutesWithMethod<Method extends HandlerMethods> = {
[key in KeysWithMethod<AppRoutes, Method>]: Method extends keyof AppRoutes[key] ? GetRouteResponseInfo<AppRoutes, key, Method> : never;
};
export type RoutesWithMethod<Method extends GetRouterMethods<AppRoutes>> = GetRoutesWithMethod<AppRoutes, Method>;

// usage
// get all routes that have a "get" method, and their response types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "./App.css";
import { useAppQuery } from "./queries";

function App() {
const query = useAppQuery("/", "get");
const query = useAppQuery("/nested/", "get");
const data = query.data;
// ^? const query: UseQueryResult<"Hello world", Error>

Expand Down
File renamed without changes.
Loading

0 comments on commit 4236b3c

Please sign in to comment.