Skip to content

Commit

Permalink
support async handlers inference
Browse files Browse the repository at this point in the history
  • Loading branch information
Eliav2 committed Apr 28, 2024
1 parent 26cda9c commit 864d0f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
7 changes: 7 additions & 0 deletions examples/express-typed-prisma/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./dev.db"
17 changes: 7 additions & 10 deletions packages/express-typed/src/express-typed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ export class TypedRouter<
}
}


// extract any relevant information from TypedRouter, and flatten any nested routers
export type ParseRoutes<T extends TypedRouter<any>> = FlatNestedRouters<T["routes"]>;

Expand Down Expand Up @@ -88,9 +87,9 @@ export type GetRouteResponseInfoHelper<
Method extends keyof Router[Path]
> = UnionToIntersection<
(
ReturnType<
Router[Path][Method] extends (...args: any) => any ? Router[Path][Method] : never
> extends TypedResponse<infer Res>
ReturnType<Router[Path][Method] extends (...args: any) => any ? Router[Path][Method] : never> extends
| TypedResponse<infer Res>
| Promise<TypedResponse<infer Res>>
? Res
: never
) extends (infer U)[]
Expand Down Expand Up @@ -120,22 +119,20 @@ export type GetRouteResponseInfo<
* Get all the keys in the router that have a specific method
* for example, KeysWithMethod<typeof typedRouter, "get"> might return "/" | "/nested"
*/
export type KeysWithMethod<Router extends TypedRouter<any>['routes'], Method extends GetRouterMethods<Router>> = {
export type KeysWithMethod<Router extends TypedRouter<any>["routes"], Method extends GetRouterMethods<Router>> = {
[K in keyof Router]: Method extends keyof Router[K] ? K : never;
}[keyof Router];

/**
* Get all the existing methods for any endpoint in the router
* for example, GetRouterMethods<typeof typedRouter> might return "get" | "post" | "all" or something similar, if those are the methods are defined on some of the endpoints in the router
*/
export type GetRouterMethods<Router extends TypedRouter<any>['routes']> = keyof UnionToIntersection<Router[keyof Router]>;
export type GetRouterMethods<Router extends TypedRouter<any>["routes"]> = keyof UnionToIntersection<Router[keyof Router]>;

/**
* Get all the routes in the router that have a specific method
* for example, GetRoutesWithMethod<typeof typedRouter, "get"> might return { "/": "Hello world", "/nested": "get /nested/" }
*/
export type GetRoutesWithMethod<Router extends TypedRouter<any>['routes'], Method extends GetRouterMethods<Router>> = {
[Path in KeysWithMethod<Router, Method>]: Method extends keyof Router[Path]
? GetRouteResponseInfo<Router, Path, Method>
: never;
export type GetRoutesWithMethod<Router extends TypedRouter<any>["routes"], Method extends GetRouterMethods<Router>> = {
[Path in KeysWithMethod<Router, Method>]: Method extends keyof Router[Path] ? GetRouteResponseInfo<Router, Path, Method> : never;
};

0 comments on commit 864d0f3

Please sign in to comment.