UNPKG

2.44 kBJavaScriptView Raw
1"use strict";
2/**
3 * Copyright 2018 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.lambda = exports.Lambda = void 0;
19const common = require("../common");
20/** @hidden */
21class 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 // convert header keys to lowercase for case insensitive header retrieval
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}
66exports.Lambda = Lambda;
67/** @hidden */
68exports.lambda = new Lambda();
69//# sourceMappingURL=lambda.js.map
\No newline at end of file