1 | "use strict";
|
2 | var __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 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | const aws_sdk_1 = require("aws-sdk");
|
12 | const lambda = new aws_sdk_1.Lambda({ region: 'us-east-1' });
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function 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 | }
|
30 | exports.invoke = invoke;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 | function 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 | }
|
49 | exports.invokeEvent = invokeEvent;
|
50 |
|
51 | function 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 | }
|
61 | function 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 |
|
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 |
|
\ | No newline at end of file |