lemon-core
Version:
Lemon Serverless Micro-Service Platform
79 lines (78 loc) • 2.64 kB
TypeScript
import { NextContext } from 'lemon-model';
import { ProtocolParam, CoreConfigService } from '../core-services';
type MyHandler<TEvent = any, TResult = any> = (ctx: any, req: any) => Promise<TResult>;
type HandlerType = 'web' | 'sns' | 'sqs' | 'wss' | 'dds' | 'cron' | 'cognito' | 'notification' | 'dynamo-stream';
/**
* class: `LambdaHandlerService`
* - must override if need to customize packing contexxt.
*/
interface LambdaHandlerService<T extends MyHandler = MyHandler> {
/**
* MUST override this hanle()
*/
handle: T;
/**
* (optional) pack the origin context to application context.
* - override this function if required!
*
* @param ctx event context
* @param req http request
*/
packContext?(ctx: any, req: any): Promise<NextContext>;
/**
* (optional) handle Protocol Request.
* - override this function if required!
*
* @param param protocol param.
*/
handleProtocol?<TResult = any>(param: ProtocolParam): Promise<TResult>;
}
interface HandlerMap {
[key: string]: LambdaHandlerService | any;
}
export declare abstract class FunctionSubHandler<T extends MyHandler> implements LambdaHandlerService<T> {
protected functions: FunctionHandler;
protected type: string;
constructor(functions: FunctionHandler, type?: HandlerType);
abstract handle: T;
}
/**
* class: `LambdaHandler`
* - general lambda handler so that routes to proper target handlers.
*/
export declare class FunctionHandler {
static REPORT_ERROR: boolean;
protected _map: HandlerMap;
config: CoreConfigService;
constructor(config?: CoreConfigService);
/**
* set service lambda handler.
* @param type name of type
* @param handler handler of service
*/
setHandler(type: HandlerType, handler: LambdaHandlerService | any): void;
findService: (ctx: any) => HandlerType;
/**
* decode event to proper handler.
* - NOTE! - returns promised results with `async`
*
* @returns boolean
*/
handle(ctx: any, req: any): Promise<any>;
/**
* handle param via protocol-service.
* - sub-service could call this method() to bypass request.
*
* @param param protocol parameters
*/
handleProtocol<TResult = any>(param: ProtocolParam): Promise<TResult>;
/**
* (default) pack the origin context to application context.
* - override this function if required!
*
* @param event origin event
* @param $ctx origin context of lambda
*/
packContext($ctx: any, req: any): Promise<NextContext>;
}
export {};