This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
index.ts
79 lines (71 loc) · 2.94 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
71
72
73
74
75
76
77
78
79
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
onError?: ErrorHandler
}
export type ErrorHandler<TContext extends Context = Context> = (error?: Error, event?: RouterEvent, context?: TContext) => Promise<any> | any | void
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)
}
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!'
}
}
const extractEventProcessorMapping = (routeConfig: RouteConfig) => {
const processorMap = new Map<string, EventProcessor>()
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
}