1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.lambda = exports.Lambda = void 0;
|
19 | const common = require("../common");
|
20 |
|
21 | class Lambda {
|
22 | handle(standard) {
|
23 | return async (event, context, callback) => {
|
24 | const metadata = {
|
25 | context,
|
26 | event,
|
27 | };
|
28 | const entireBodyFormat = typeof event.headers !== 'object' || Array.isArray(event.headers);
|
29 |
|
30 | const headers = entireBodyFormat
|
31 | ? {}
|
32 | : Object.keys(event.headers).reduce((o, k) => {
|
33 | o[k.toLowerCase()] = event.headers[k];
|
34 | return o;
|
35 | }, {});
|
36 | const body = entireBodyFormat
|
37 | ? event
|
38 | : typeof event.body === 'string'
|
39 | ? JSON.parse(event.body)
|
40 | : event.body;
|
41 | const result = await standard(body, headers, { lambda: metadata }).catch((e) => {
|
42 | common.error(e.stack || e);
|
43 | callback(e);
|
44 | });
|
45 | if (!result) {
|
46 | return;
|
47 | }
|
48 | const { status } = result;
|
49 | callback(null, {
|
50 | statusCode: status,
|
51 | body: JSON.stringify(result.body),
|
52 | headers: result.headers,
|
53 | });
|
54 | };
|
55 | }
|
56 | isContext(second) {
|
57 | return typeof second.succeed === 'function';
|
58 | }
|
59 | isCallback(third) {
|
60 | return typeof third === 'function';
|
61 | }
|
62 | check(first, second, third) {
|
63 | return this.isContext(second) && this.isCallback(third);
|
64 | }
|
65 | }
|
66 | exports.Lambda = Lambda;
|
67 |
|
68 | exports.lambda = new Lambda();
|
69 |
|
\ | No newline at end of file |