All files index.ts

100% Statements 18/18
100% Branches 16/16
100% Functions 1/1
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52    1x         9x 9x 9x 2x 3x         9x 6x 6x 5x 5x       9x 3x 3x   9x 1x   9x                                        
import { APIGatewayProxyEvent, Context, APIGatewayProxyResult, APIGatewayProxyStructuredResultV2 } from 'aws-lambda';
 
export const websocketHandler = async (
    event: APIGatewayProxyEvent,
    context: Context,
    config: Configuration
): Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2> => {
    const { action } = JSON.parse(<string>event.body);
    let runData = {};
    if (config.middlewares) {
        for (const configMW of config.middlewares) {
            runData = await configMW(event, context, runData);
        }
    }
 
    let handler;
    if (config.actions) {
        for (const configAction of config.actions) {
            if (configAction.name === action) {
                !config.enableLogging || console.log(`Executing action ${action}`);
                handler = configAction.handler(event, context, runData);
            }
        }
    }
    if (!handler && config.fallback) {
        !config.enableLogging || console.log(`Executing fallback as there were no ${action}`);
        handler = config.fallback(event, context, runData);
    }
    if (!handler) {
        handler = Promise.reject({ statusCode: 400, body: 'No action specified' });
    }
    return handler;
};
 
export interface Configuration {
    actions?: ActionsHandlers[];
    middlewares?: Array<(event?: APIGatewayProxyEvent, context?: Context, runData?: any) => Promise<any>>;
    fallback?: handlerFn;
    enableLogging?: boolean;
}
 
export interface ActionsHandlers {
    name: string;
    handler: handlerFn;
}
 
type handlerFn = (
    event?: APIGatewayProxyEvent,
    context?: Context,
    runData?: any
) => Promise<APIGatewayProxyResult | APIGatewayProxyStructuredResultV2>;