forked from spring-media/aws-lambda-router
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed index files from ignore and add them to repo
- Loading branch information
1 parent
c67607a
commit e5aba76
Showing
4 changed files
with
81 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,3 @@ node_modules | |
coverage | ||
.idea | ||
*.iml | ||
lib/*.d.ts | ||
lib/*.js | ||
index.js | ||
index.d.ts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TContext extends Context = Context> = (error?: Error, event?: RouterEvent, context?: TContext) => Promise<any> | any | void; | ||
export declare type RouterEvent = LegacyProxyIntegrationEvent | ProxyIntegrationEvent | SnsEvent | SqsEvent | S3Event; | ||
export declare const handler: (routeConfig: RouteConfig) => <TContext extends Context>(event: RouterEvent, context: TContext) => Promise<any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters