express-typed
Advanced tools
Comparing version 0.1.2 to 0.1.3
import express, { Request, Response, NextFunction } from "express"; | ||
export type IHandlerResponse<Res extends any[] = []> = { | ||
status<const T>(arg: T): IHandlerResponse<[...Res, { | ||
export type TypedResponse<Res extends any[] = []> = { | ||
status<const T>(arg: T): TypedResponse<[...Res, { | ||
status: T; | ||
}]>; | ||
links<const T>(arg: T): IHandlerResponse<[...Res, { | ||
links<const T>(arg: T): TypedResponse<[...Res, { | ||
links: T; | ||
}]>; | ||
sendStatus<const T>(arg: T): IHandlerResponse<[...Res, { | ||
sendStatus<const T>(arg: T): TypedResponse<[...Res, { | ||
sendStatus: T; | ||
}]>; | ||
contentType<const T>(arg: T): IHandlerResponse<[...Res, { | ||
contentType<const T>(arg: T): TypedResponse<[...Res, { | ||
contentType: T; | ||
}]>; | ||
type<const T>(arg: T): IHandlerResponse<[...Res, { | ||
type<const T>(arg: T): TypedResponse<[...Res, { | ||
type: T; | ||
}]>; | ||
format<const T>(arg: T): IHandlerResponse<[...Res, { | ||
format<const T>(arg: T): TypedResponse<[...Res, { | ||
format: T; | ||
}]>; | ||
attachment<const T>(arg: T): IHandlerResponse<[...Res, { | ||
attachment<const T>(arg: T): TypedResponse<[...Res, { | ||
attachment: T; | ||
}]>; | ||
json<const T>(arg: T): IHandlerResponse<[...Res, { | ||
json<const T>(arg: T): TypedResponse<[...Res, { | ||
json: T; | ||
}]>; | ||
jsonp<const T>(arg: T): IHandlerResponse<[...Res, { | ||
jsonp<const T>(arg: T): TypedResponse<[...Res, { | ||
jsonp: T; | ||
}]>; | ||
send<const T>(arg: T): IHandlerResponse<[...Res, { | ||
send<const T>(arg: T): TypedResponse<[...Res, { | ||
send: T; | ||
@@ -35,3 +35,3 @@ }]>; | ||
export type SendMethod = "send" | "json" | "jsonp"; | ||
export type IHandlerRequest<Req extends any[] = []> = {} & Request; | ||
export type TypedRequest<Req extends any[] = []> = {} & Request; | ||
export type HandlerMethods = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head"; | ||
@@ -43,3 +43,3 @@ /** | ||
[K in string]: R[K] extends TypedRouter<infer N> ? TypedRouter<N> : { | ||
[key in HandlerMethods]?: (req: IHandlerRequest, res: IHandlerResponse, next: NextFunction) => void; | ||
[key in HandlerMethods]?: (req: TypedRequest, res: TypedResponse, next: NextFunction) => void; | ||
} | TypedRouter<any>; | ||
@@ -51,2 +51,3 @@ }> { | ||
} | ||
export type ParseRoutes<T extends TypedRouter<any>> = FlatNestedRouters<T["routes"]>; | ||
export type FlatNestedRouters<T> = { | ||
@@ -62,7 +63,33 @@ [K in keyof T]: K extends string ? (x: T[K] extends TypedRouter<infer N> ? FlatNestedRouters<{ | ||
export type UnionToIntersection<U> = (U extends any ? (x: U) => void : never) extends (x: infer I) => void ? I : never; | ||
export type GetRouteResponseInfoHelper<Router extends TypedRouter<any>["routes"], Path extends keyof FlatNestedRouters<Router>, Method extends keyof FlatNestedRouters<Router>[Path]> = UnionToIntersection<(ReturnType<FlatNestedRouters<Router>[Path][Method] extends (...args: any) => any ? FlatNestedRouters<Router>[Path][Method] : never> extends IHandlerResponse<infer Res> ? Res : never) extends (infer U)[] ? U : never>; | ||
export type GetRouteResponseInfo<Router extends TypedRouter<any>["routes"], Path extends keyof FlatNestedRouters<Router>, Method extends keyof FlatNestedRouters<Router>[Path], Info extends keyof GetRouteResponseInfoHelper<Router, Path, Method> | "body" = "body"> = Info extends "body" ? GetRouteResponseInfoHelper<Router, Path, Method>[Extract<keyof GetRouteResponseInfoHelper<Router, Path, Method>, SendMethod>] : Info extends keyof GetRouteResponseInfoHelper<Router, Path, Method> ? GetRouteResponseInfoHelper<Router, Path, Method>[Info] : GetRouteResponseInfoHelper<Router, Path, Method>; | ||
export type ParseRoutes<T extends TypedRouter<any>> = FlatNestedRouters<T["routes"]>; | ||
export type KeysWithMethod<T, Method extends string> = { | ||
[K in keyof T]: Method extends keyof T[K] ? K : never; | ||
}[keyof T]; | ||
/** | ||
* Get the response info for a given route and method | ||
* for example | ||
* GetRouteResponseInfoHelper<typeof typedRouter, "/", "get"> might return { status: 200, send: "Hello world" } | ||
*/ | ||
export type GetRouteResponseInfoHelper<Router extends TypedRouter<any>["routes"], Path extends keyof Router, Method extends keyof Router[Path]> = UnionToIntersection<(ReturnType<Router[Path][Method] extends (...args: any) => any ? Router[Path][Method] : never> extends TypedResponse<infer Res> ? Res : never) extends (infer U)[] ? U : never>; | ||
/** | ||
* Get the response info for a given route, method, and info type | ||
* for example | ||
* - GetRouteResponseInfo<typeof typedRouter, "/", "get"> might return "Hello world" | ||
* - GetRouteResponseInfo<typeof typedRouter, "/", "get", "status"> might return 200 | ||
*/ | ||
export type GetRouteResponseInfo<Router extends TypedRouter<any>["routes"], Path extends keyof Router, Method extends keyof Router[Path], Info extends keyof GetRouteResponseInfoHelper<Router, Path, Method> | "body" = "body"> = Info extends "body" ? GetRouteResponseInfoHelper<Router, Path, Method>[Extract<keyof GetRouteResponseInfoHelper<Router, Path, Method>, SendMethod>] : Info extends keyof GetRouteResponseInfoHelper<Router, Path, Method> ? GetRouteResponseInfoHelper<Router, Path, Method>[Info] : GetRouteResponseInfoHelper<Router, Path, Method>; | ||
/** | ||
* 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>> = { | ||
[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]>; | ||
/** | ||
* 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; | ||
}; |
{ | ||
"name": "express-typed", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
6875
116