diff --git a/.gitignore b/.gitignore index 2cfe542..8d823bc 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,3 @@ node_modules coverage .idea *.iml -lib/*.d.ts -lib/*.js -index.js -index.d.ts diff --git a/dist/index.d.ts b/dist/index.d.ts new file mode 100644 index 0000000..5956598 --- /dev/null +++ b/dist/index.d.ts @@ -0,0 +1,18 @@ +import { LegacyProxyIntegrationConfig, LegacyProxyIntegrationEvent } from './lib/legacyProxyIntegration'; +import { ProxyIntegrationConfig, ProxyIntegrationEvent } from './lib/proxyIntegration'; +import { SnsConfig, SnsEvent } from './lib/sns'; +import { SqsConfig, SqsEvent } from './lib/sqs'; +import { S3Config, S3Event } from './lib/s3'; +import { Context } from 'aws-lambda'; +export interface RouteConfig { + legacyProxyIntegration?: LegacyProxyIntegrationConfig; + proxyIntegration?: ProxyIntegrationConfig; + sns?: SnsConfig; + sqs?: SqsConfig; + s3?: S3Config; + debug?: boolean; + onError?: ErrorHandler; +} +export declare type ErrorHandler = (error?: Error, event?: RouterEvent, context?: TContext) => Promise | any | void; +export declare type RouterEvent = LegacyProxyIntegrationEvent | ProxyIntegrationEvent | SnsEvent | SqsEvent | S3Event; +export declare const handler: (routeConfig: RouteConfig) => (event: RouterEvent, context: TContext) => Promise; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..50cc514 --- /dev/null +++ b/dist/index.js @@ -0,0 +1,62 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.handler = void 0; +const handler = (routeConfig) => { + const eventProcessorMapping = extractEventProcessorMapping(routeConfig); + return async (event, context) => { + if (routeConfig.debug) { + console.log('Lambda invoked with request:', event); + console.log('Lambda invoked with context:', context); + } + for (const [eventProcessorName, eventProcessor] of eventProcessorMapping.entries()) { + try { + // the contract of 'processors' is as follows: + // - their method 'process' is called with (config, event) + // - the method... + // - returns null: the processor does not feel responsible for the event + // - throws Error: the 'error.toString()' is taken as the error message of processing the event + // - returns object: this is taken as the result of processing the event + // - returns promise: when the promise is resolved, this is taken as the result of processing the event + const result = eventProcessor.process(routeConfig[eventProcessorName], event, context); + if (result) { + // be resilient against a processor returning a value instead of a promise: + return await result; + } + else { + if (routeConfig.debug) { + console.log("Event processor couldn't handle request."); + } + } + } + catch (error) { + if (error.stack) { + console.log(error.stack); + } + if (routeConfig.onError) { + const result = await routeConfig.onError(error, event, context); + if (result) { + return result; + } + } + throw error.toString(); + } + } + throw 'No event processor found to handle this kind of event!'; + }; +}; +exports.handler = handler; +const extractEventProcessorMapping = (routeConfig) => { + const processorMap = new Map(); + for (const key of Object.keys(routeConfig)) { + if (key === 'debug' || key === 'onError') { + continue; + } + try { + processorMap.set(key, require(`./lib/${key}`)); + } + catch (error) { + throw new Error(`The event processor '${key}', that is mentioned in the routerConfig, cannot be instantiated (${error.toString()})`); + } + } + return processorMap; +}; diff --git a/package.json b/package.json index 5864864..f13931a 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "AWS lambda router", "main": "dist/index.js", "types": "dist/index.d.ts", - "files": ["dist/index.*", "dist/**/*"], + "files": ["dist/**"], "scripts": { "test": "yarn clean && jest --coverage", "build": "rimraf .dist/ && tsc"