UNPKG

2.71 kBJavaScriptView Raw
1"use strict";
2var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3 return new (P || (P = Promise))(function (resolve, reject) {
4 function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5 function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6 function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7 step((generator = generator.apply(thisArg, _arguments || [])).next());
8 });
9};
10Object.defineProperty(exports, "__esModule", { value: true });
11const aws_sdk_1 = require("aws-sdk");
12const lambda = new aws_sdk_1.Lambda({ region: 'us-east-1' });
13/**
14 * Invokes the function and returns the parsed body
15 * @param {[type]} functionName [description]
16 * @param {[type]} body [description]
17 * @return {[type]} [description]
18 */
19function invoke(functionName, body) {
20 return __awaiter(this, void 0, void 0, function* () {
21 return lambda
22 .invoke({
23 FunctionName: functionName,
24 Payload: createPayload(body),
25 })
26 .promise()
27 .then(data => parseResponse(functionName, data));
28 });
29}
30exports.invoke = invoke;
31/**
32 * Invokes the event
33 * @param {[type]} functionName [description]
34 * @param {[type]} body [description]
35 * @return {[type]} [description]
36 */
37function invokeEvent(functionName, body) {
38 return __awaiter(this, void 0, void 0, function* () {
39 return lambda
40 .invoke({
41 FunctionName: functionName,
42 InvocationType: 'Event',
43 Payload: createPayload(body),
44 })
45 .promise()
46 .then(data => data.StatusCode === 200);
47 });
48}
49exports.invokeEvent = invokeEvent;
50/////////////////////////////
51function createPayload(body) {
52 if (body === undefined)
53 return undefined;
54 else if (typeof body === 'string') {
55 return body;
56 }
57 else if (typeof body === 'object') {
58 return JSON.stringify(body);
59 }
60}
61function parseResponse(functionName, res) {
62 let result;
63 if (res.StatusCode === 200) {
64 result = JSON.parse(res.Payload.toString());
65 }
66 if (!result) {
67 return;
68 }
69 // errors will be serialized with errorMessage, errorType, and stack properties
70 if (result.errorMessage) {
71 let error = new Error(`Invocation of ${functionName} failed.`);
72 console.log(result);
73 error.stack = result.stack;
74 throw error;
75 }
76 else {
77 return result;
78 }
79}
80//# sourceMappingURL=lambda.js.map
\No newline at end of file