forked from spring-media/aws-lambda-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (63 loc) · 2.57 KB
/
index.ts
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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'
import { EventProcessor } from './lib/EventProcessor'
export interface RouteConfig {
proxyIntegration?: ProxyIntegrationConfig
sns?: SnsConfig
sqs?: SqsConfig
s3?: S3Config
debug?: boolean
}
export type RouterEvent = ProxyIntegrationEvent | SnsEvent | SqsEvent | S3Event
export const handler = (routeConfig: RouteConfig) => {
const eventProcessorMapping = extractEventProcessorMapping(routeConfig)
return async <TContext extends Context> (event: RouterEvent, context: TContext) => {
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 as any)[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)
}
throw error.toString()
}
}
throw 'No event processor found to handle this kind of event!'
}
}
const extractEventProcessorMapping = (routeConfig: RouteConfig) => {
const processorMap = new Map<string, EventProcessor>()
for (const key of Object.keys(routeConfig)) {
if (key === 'debug') {
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
}