lemon-core
Version:
Lemon Serverless Micro-Service Platform
124 lines (123 loc) • 4.48 kB
TypeScript
import { Handler, APIGatewayProxyEvent, APIGatewayProxyResult, CognitoUserPoolTriggerEvent, DynamoDBStreamEvent, SNSEvent as AWSSNSEvent, SQSEvent as AWSSQSEvent } from 'aws-lambda';
import { NextContext } from 'lemon-model';
import { ProtocolParam, CoreConfigService } from './../core-services';
import * as $lambda from 'aws-lambda';
export declare type ConfigService = CoreConfigService;
export declare type Context = $lambda.Context;
/**
* cron event
*
* ```yaml
* # use input field to define cron.
* - schedule:
* name: daily
* input:
* cron:
* name: keepalive
* action: tick
* ```
*/
export interface CronEvent {
cron: {
name: string;
action: string;
};
}
export declare type WEBEvent = APIGatewayProxyEvent;
export declare type WEBResult = APIGatewayProxyResult;
export declare type WSSEvent = APIGatewayProxyEvent;
export declare type DDSEvent = DynamoDBStreamEvent;
export declare type SNSEvent = AWSSNSEvent;
export declare type SQSEvent = AWSSQSEvent;
export declare type WSSResult = any;
export declare type MyHandler<TEvent = any, TResult = any> = (event: TEvent, context: NextContext) => Promise<TResult>;
export declare type WEBHandler = MyHandler<WEBEvent, WEBResult>;
export declare type WSSHandler = MyHandler<WSSEvent, WSSResult>;
export declare type SNSHandler = MyHandler<SNSEvent, void>;
export declare type SQSHandler = MyHandler<SQSEvent, void>;
export declare type CronHandler = MyHandler<CronEvent, void>;
export declare type CognitoHandler = MyHandler<CognitoUserPoolTriggerEvent>;
export declare type DynamoStreamHandler = MyHandler<DynamoDBStreamEvent, void>;
export declare type NotificationHandler = MyHandler<WEBEvent, WEBResult>;
export declare type HandlerType = 'web' | 'sns' | 'sqs' | 'wss' | 'dds' | 'cron' | 'cognito' | 'dynamo-stream' | 'notification';
/**
* class: `LambdaHandlerService`
* - must override if need to customize packing contexxt.
*/
export 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 event origin event
* @param context origin context of lambda
*/
packContext?(event: any, context: Context): 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 | Handler;
}
export declare abstract class LambdaSubHandler<T extends MyHandler> implements LambdaHandlerService<T> {
protected lambda: LambdaHandler;
protected type: string;
constructor(lambda: LambdaHandler, type?: HandlerType);
abstract handle: T;
}
/**
* build reprot-error function in safe.
*
* @param isReport flag to report-error via sns
* @return the last error message
*/
export declare const buildReportError: (isReport?: boolean) => (e: Error, context?: any, event?: any, data?: any) => Promise<string>;
/**
* class: `LambdaHandler`
* - general lambda handler so that routes to proper target handlers.
*/
export declare class LambdaHandler {
static REPORT_ERROR: boolean;
protected _map: HandlerMap;
config: ConfigService;
constructor(config?: ConfigService);
/**
* set service lambda handler.
* @param type name of type
* @param handler handler of service
*/
setHandler(type: HandlerType, handler: LambdaHandlerService | Handler): void;
findService: (event: any) => HandlerType;
/**
* decode event to proper handler.
* - NOTE! - returns promised results with `async`
*
* @returns boolean
*/
handle(event: any, context: Context): 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(event: any, $ctx: Context): Promise<NextContext>;
}
export {};