UNPKG

simple-lambda-client

Version:

A simple, convenient way to invoke aws lambda functions with best practices.

59 lines 2.16 kB
"use strict"; 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(); /** * a method to execute a lambda invocation via the aws api with best practices */ const executeLambdaInvocation = async ({ serviceName, functionName, stage, event, logDebug, }) => { const lambdaName = [serviceName, stage, functionName].join('-'); // log the request, if enabled if (logDebug) logDebug(`${lambdaName}.invoke.input`, { event, }); // invoke the lambda const response = await lambda .invoke({ FunctionName: lambdaName, Payload: JSON.stringify(event), }) .promise(); if (response.StatusCode !== 200) throw new errors_1.UnsuccessfulStatusCodeError({ code: response.StatusCode, payload: response.Payload, }); // 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; } // 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, }); // log the response, if enabled if (logDebug) logDebug(`${lambdaName}.invoke.output`, { result: payload, }); // return the payload return payload; }; exports.executeLambdaInvocation = executeLambdaInvocation; //# sourceMappingURL=executeLambdaInvocation.js.map