lambda-service-client
Version:
A simple, convenient way to invoke aws lambda functions with best practices.
47 lines • 2.38 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.executeLambdaInvocation = void 0;
const aws_sdk_1 = require("aws-sdk");
const errors_1 = require("./errors");
const lambda = new aws_sdk_1.Lambda();
exports.executeLambdaInvocation = ({ serviceName, functionName, stage, event, }) => __awaiter(void 0, void 0, void 0, function* () {
const lambdaName = [serviceName, stage, functionName].join('-');
// 1. invoke the lambda
const response = yield lambda
.invoke({
FunctionName: lambdaName,
Payload: JSON.stringify(event),
})
.promise();
if (response.StatusCode !== 200)
throw new errors_1.UnsuccessfulStatusCodeError({ code: response.StatusCode, payload: response.Payload });
// 2. attempt to parse the response into object
let payload;
try {
payload = JSON.parse(response.Payload);
}
catch (error) {
// if here, then we couldn't parse the result, it wasn't json. so just return the result unparsed
payload = response.Payload;
}
// 3. evaluate whether response contains an error
const isAnErrorPayload = !!payload && // if the response exists and is truthy, then it may be an error object
(false || // check if any of the following properties exist in the payload (since some responses may exclude one or the other)
payload.errorMessage ||
payload.errorType ||
payload.stackTrace);
if (isAnErrorPayload)
throw new errors_1.LambdaInvocationError({ response: payload, lambda: lambdaName, event });
// 4. return the payload
return payload;
});
//# sourceMappingURL=executeLambdaInvocation.js.map