/
AbstractController.ts
40 lines (31 loc) · 1005 Bytes
/
AbstractController.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
export abstract class AbstractController<T, R> {
protected readonly mapping: any;
protected readonly buildFingerprint: string;
protected context: any = {};
constructor(mapping, buildFingerprint) {
this.mapping = mapping;
this.buildFingerprint = buildFingerprint;
}
abstract executeScheduledEvent(): void;
abstract executeInputEvent(input: T): R;
protected async onError(e: Error) {}
protected async compute(event: any) {
if (event['detail-type'] === 'Scheduled Event') {
await this.executeScheduledEvent();
return;
}
return await this.executeInputEvent(event);
}
async runLambda(input: T, context: any) {
this.context = context;
try {
const output = await this.compute(input);
return {
output
}
} catch (e) {
await this.onError(e);
throw e;
}
}
}