From 3a3f085696a1bebd9714b699ec44da6cc19e2d65 Mon Sep 17 00:00:00 2001 From: Bailey Tincher Date: Sat, 2 Jan 2021 23:07:53 -0600 Subject: [PATCH] Refactor core type generics naming --- packages/core/index.d.ts | 147 ++++++++++++++++++++++++++++----------- 1 file changed, 105 insertions(+), 42 deletions(-) diff --git a/packages/core/index.d.ts b/packages/core/index.d.ts index aecc651d3..8680f2736 100644 --- a/packages/core/index.d.ts +++ b/packages/core/index.d.ts @@ -1,57 +1,120 @@ import { - Callback, - Context, - Handler -} from 'aws-lambda' - -declare type EventType = - T extends (event: infer EventArgType, context: C, callback: Callback) => void ? EventArgType : - T extends (event: infer EventArgType, context: C) => Promise ? EventArgType : - never; - -declare type HandlerReturnType = - T extends (event: any, context: C) => Promise ? RetType : - T extends (event: any, context: C, callback: Callback) => void ? RetType : - never; - -declare type AsyncHandler = - ((event: any, context: C, callback: Callback) => void) | - ((event: any, context: C) => Promise); - -declare const middy: , C extends Context = Context>(handler: H) => middy.Middy< - EventType, - HandlerReturnType, - C -> + Callback as AWSCallback, + Context as AWSContext, + Handler as AWSHandler, +} from "aws-lambda"; + +declare type EventType< + Handler, + Context extends AWSContext = AWSContext +> = Handler extends ( + event: infer EventArgType, + context: Context, + callback: AWSCallback +) => void + ? EventArgType + : Handler extends ( + event: infer EventArgType, + context: Context + ) => Promise + ? EventArgType + : never; + +declare type HandlerReturnType< + Handler, + Context extends AWSContext = AWSContext +> = Handler extends ( + event: unknown, + context: Context +) => Promise + ? Response + : Handler extends ( + event: unknown, + context: Context, + callback: AWSCallback + ) => void + ? Response + : never; + +declare type AsyncHandler< + Event = unknown, + Response = unknown, + Context extends AWSContext = AWSContext +> = + | ((event: Event, context: Context, callback: AWSCallback) => void) + | ((event: Event, context: Context) => Promise); + +declare const middy: < + Handler extends AsyncHandler, + Context extends AWSContext = AWSContext +>( + handler: Handler +) => middy.Middy< + EventType, + HandlerReturnType, + Context +>; declare namespace middy { - interface Middy extends Handler { - use: >(middlewares: M | M[]) => Middy; - before: (callbackFn: MiddlewareFunction) => Middy; - after: (callbackFn: MiddlewareFunction) => Middy; - onError: (callbackFn: MiddlewareFunction) => Middy; + interface Middy< + Event = unknown, + Response = unknown, + Context extends AWSContext = AWSContext + > extends AWSHandler { + use: >( + middlewares: M | M[] + ) => Middy; + before: ( + callbackFn: MiddlewareFunction + ) => Middy; + after: ( + callbackFn: MiddlewareFunction + ) => Middy; + onError: ( + callbackFn: MiddlewareFunction + ) => Middy; } - type Middleware = (config?: C) => MiddlewareObject; + type Middleware< + Config = unknown, + Event = unknown, + Response = unknown, + Context extends AWSContext = AWSContext + > = (config?: Config) => MiddlewareObject; - interface MiddlewareObject { - before?: MiddlewareFunction; - after?: MiddlewareFunction; - onError?: MiddlewareFunction; + interface MiddlewareObject< + Event = unknown, + Response = unknown, + Context extends AWSContext = AWSContext + > { + before?: MiddlewareFunction; + after?: MiddlewareFunction; + onError?: MiddlewareFunction; } - type MiddlewareFunction = (handler: HandlerLambda, next: NextFunction) => void | Promise; + type MiddlewareFunction< + Event, + Response, + Context extends AWSContext = AWSContext + > = ( + handler: HandlerLambda, + next: NextFunction + ) => void | Promise; - type NextFunction = (error?: any) => void; + type NextFunction = (error?: Error) => void; - interface HandlerLambda { - event: T; - context: C; - response: V; + interface HandlerLambda< + Event = unknown, + Response = unknown, + Context extends AWSContext = AWSContext + > { + event: Event; + context: Context; + response: Response; error: Error; - callback: Callback; + callback: AWSCallback; } } -export default middy +export default middy; export as namespace middy;