Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

aws-core-utils

Package Overview
Dependencies
Maintainers
1
Versions
80
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

aws-core-utils - npm Package Compare versions

Comparing version 7.0.11 to 7.0.12

87

api-lambdas.js

@@ -9,2 +9,3 @@ 'use strict';

const merge = merging.merge;
const isInstanceOf = require('core-functions/objects').isInstanceOf;

@@ -65,2 +66,3 @@ const appErrors = require('core-functions/app-errors');

* @param {Object|undefined} [opts.defaultHeaders] - default custom headers to be included (if any) in a Lambda Proxy response
* @param {undefined|function(error: AppError, auditRef: string|undefined): Object} [opts.toErrorResponse] - an optional function to use to convert the given error into an appropriate error response object to stringify & return or body to include in a Lambda Proxy response to return
*/

@@ -76,14 +78,19 @@ function failCallback(lambdaCallback, error, awsContext, message, code, opts) {

// Resolve the audit reference (if available)
const auditRef = isNotBlank(error.auditRef) ? error.auditRef :
isNotBlank(error.awsRequestId) ? error.awsRequestId : awsContext ? awsContext.awsRequestId : undefined;
// Resolve the AWS request id (if available)
const awsRequestId = isNotBlank(error.awsRequestId) ? error.awsRequestId :
awsContext ? awsContext.awsRequestId : undefined;
if (awsRequestId && isBlank(apiError.awsRequestId)) apiError.awsRequestId = awsRequestId;
// Resolve the audit reference (if available) - falling back to AWS request ID (if available)
const auditRef = isNotBlank(error.auditRef) ? error.auditRef : awsRequestId;
if (opts.useLambdaProxy) {
const statusCode = apiError.httpStatus;
const body = toErrorResponseBody(apiError, auditRef);
const body = resolveErrorResponseBody(apiError, auditRef, opts && opts.toErrorResponse);
const proxyResponse = toLambdaProxyResponse(statusCode, error.headers, body, opts.defaultHeaders);
lambdaCallback(null, proxyResponse);
} else {
if (auditRef && isBlank(apiError.awsRequestId)) apiError.awsRequestId = auditRef;
lambdaCallback(JSON.stringify(apiError), null);
const errorResponse = resolveErrorResponse(apiError, auditRef, opts && opts.toErrorResponse);
lambdaCallback(JSON.stringify(errorResponse), null);
}

@@ -93,2 +100,43 @@ }

/**
* Resolves the body of a Lambda Proxy error response to be returned, using the given `toErrorResponse` function (if any)
* or the default `toErrorResponseBody` function (if none).
* @param {AppError} apiError - the error with which your Lambda was failed
* @param {string|undefined} auditRef - the audit reference to include
* @param {undefined|function(error: AppError, auditRef: string|undefined): Object} [toErrorResponse] - an optional
* function to use to convert the given error into an appropriate error response object to stringify & return or body to
* include in a Lambda Proxy response to return
* @return {Object} the body to include in the Lambda Proxy response
*/
function resolveErrorResponseBody(apiError, auditRef, toErrorResponse) {
try {
return typeof toErrorResponse === 'function' ? toErrorResponse(apiError, auditRef) :
toErrorResponseBody(apiError, auditRef);
} catch (err) {
console.error('ERROR', err);
// fallback to default function if given function fails
return toErrorResponseBody(apiError, auditRef);
}
}
/**
* Resolves the error response to be returned, using the given `toErrorResponse` function (if any) or the given
* `apiError` (if none).
* @param {AppError} apiError - the error with which your Lambda was failed
* @param {string|undefined} auditRef - the audit reference to include
* @param {undefined|function(error: AppError, auditRef: string|undefined): Object} [toErrorResponse] - an optional
* function to use to convert the given error into an appropriate error response object to stringify & return or body to
* include in a Lambda Proxy response to return
* @return {Object} the body to include in the Lambda Proxy response
*/
function resolveErrorResponse(apiError, auditRef, toErrorResponse) {
try {
return typeof toErrorResponse === 'function' ? toErrorResponse(apiError, auditRef) : apiError;
} catch (err) {
console.error('ERROR', err);
// fallback to apiError if given function fails
return apiError;
}
}
/**
* Succeeds the given callback of an AWS Lambda, which is exposed via API Gateway, with the given response or body, by

@@ -117,6 +165,8 @@ * invoking the given lambdaCallback with a JSON stringified version of the converted app error. The given AWS context

function toErrorResponseBody(apiError, auditRef) {
function toErrorResponseBody(apiError, auditReference) {
const body = {code: apiError.code, message: apiError.message};
if (apiError.cause) body.cause = apiError.cause;
const auditRef = auditReference || apiError.auditRef;
if (auditRef) body.auditRef = auditRef;
if (apiError.awsRequestId) body.awsRequestId = apiError.awsRequestId;
return body;

@@ -152,5 +202,5 @@ }

*
* @param {(function(): (Object|StandardContext))|undefined|Object|StandardContext} [generateContext] - an optional function that will be used to generate the initial context to be configured & used (OR or an optional LEGACY module-scope context from which to copy an initial standard context)
* @param {(function(): (Object|StandardSettings))|undefined|Object|StandardSettings} [generateSettings] - an optional function that will be used to generate initial standard settings to use (OR optional LEGACY module-scoped settings from which to copy initial settings to use)
* @param {(function(): (Object|StandardOptions))|undefined|Object|StandardOptions} [generateOptions] - an optional function that will be used to generate initial standard options to use (OR optional LEGACY module-scoped options from which to copy initial options to use)
* @param {(function(): (Object|StandardContext))|undefined|Object|StandardContext} [createContext] - an optional function that will be used to create the initial context to be configured & used (OR or an optional LEGACY module-scope context from which to copy an initial standard context)
* @param {(function(): (Object|StandardSettings))|undefined|Object|StandardSettings} [createSettings] - an optional function that will be used to create the initial standard settings to use (OR optional LEGACY module-scoped settings from which to copy initial settings to use)
* @param {(function(): (Object|StandardOptions))|undefined|Object|StandardOptions} [createOptions] - an optional function that will be used to create the initial standard options to use (OR optional LEGACY module-scoped options from which to copy initial options to use)
* @param {function(event: AWSEvent, context: StandardContext)} fn - your function that must accept the AWS event and a standard context and ideally return a Promise

@@ -165,5 +215,6 @@ * @param {Object|LogLevel|string|undefined} [opts] - optional opts to use (or legacy LogLevel/string `logRequestResponseAtLogLevel` parameter)

* @param {string|undefined} [opts.successMsg] an optional message to log at info level on success
* @param {undefined|function(error: AppError, auditRef: string|undefined): Object} [opts.toErrorResponse] - an optional function to use to convert the given error into an appropriate error response object to stringify & return or body to include in a Lambda Proxy response to return
* @returns {AwsLambdaHandlerFunction} a handler function for your API Gateway exposed Lambda
*/
function generateHandlerFunction(generateContext, generateSettings, generateOptions, fn, opts) {
function generateHandlerFunction(createContext, createSettings, createOptions, fn, opts) {
// Check for Legacy 5th to 9th parameters: logRequestResponseAtLogLevel, allowedHttpStatusCodes, invalidRequestMsg, failureMsg, successMsg

@@ -191,11 +242,11 @@ if (!opts || typeof opts !== 'object') {

// Configure the context as a standard context
context = typeof generateContext === 'function' ? generateContext() :
generateContext && typeof generateContext === 'object' ? copy(generateContext, deep) : {};
context = typeof createContext === 'function' ? createContext() :
createContext && typeof createContext === 'object' ? copy(createContext, deep) : {};
if (!context) context = {};
const settings = typeof generateSettings === 'function' ? copy(generateSettings(), deep) :
generateSettings && typeof generateSettings === 'object' ? copy(generateSettings, deep) : undefined;
const settings = typeof createSettings === 'function' ? copy(createSettings(), deep) :
createSettings && typeof createSettings === 'object' ? copy(createSettings, deep) : undefined;
const options = typeof generateOptions === 'function' ? copy(generateOptions(), deep) :
generateOptions && typeof generateOptions === 'object' ? copy(generateOptions, deep) : undefined;
const options = typeof createOptions === 'function' ? copy(createOptions(), deep) :
createOptions && typeof createOptions === 'object' ? copy(createOptions, deep) : undefined;

@@ -226,3 +277,3 @@ // Configure the context as a standard context

// Fail the Lambda callback
if (err instanceof BadRequest || appErrors.getHttpStatus(err) === 400) {
if (isInstanceOf(err, BadRequest) || appErrors.getHttpStatus(err) === 400) {
// Log the invalid request

@@ -229,0 +280,0 @@ context.warn(isNotBlank(opts.invalidRequestMsg) ? opts.invalidRequestMsg : 'Invalid request', '-', err.message);

{
"name": "aws-core-utils",
"version": "7.0.11",
"version": "7.0.12",
"description": "Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc.",

@@ -14,8 +14,8 @@ "author": "Byron du Preez",

"dependencies": {
"core-functions": "3.0.20",
"core-functions": "3.0.22",
"deep-equal": "1.0.1",
"logging-utils": "4.0.20"
"logging-utils": "4.0.22"
},
"devDependencies": {
"aws-sdk": "2.161.0",
"aws-sdk": "2.190.0",
"aws-core-test-utils": "3.0.7",

@@ -22,0 +22,0 @@ "tape": "^4.8.0",

@@ -1,2 +0,2 @@

# aws-core-utils v7.0.11
# aws-core-utils v7.0.12

@@ -62,2 +62,3 @@ Core utilities for working with Amazon Web Services (AWS), including ARNs, regions, stages, Lambdas, AWS errors, stream events, Kinesis, DynamoDB.DocumentClients, etc.

const apiLambdas = require('aws-core-utils/api-lambdas');
const isInstanceOf = require('core-functions/objects').isInstanceOf;
const appErrors = require('core-functions/app-errors');

@@ -113,3 +114,3 @@ const BadRequest = appErrors.BadRequest;

// i.e. [400, 401, 403, 404, 408, 429, 500, 502, 503, 504]
if (err instanceof BadRequest || appErrors.getHttpStatus(err) === 400) {
if (isInstanceOf(err, BadRequest) || appErrors.getHttpStatus(err) === 400) {
context.warn(opts.invalidRequestMsg || 'Invalid request ...', err.message);

@@ -116,0 +117,0 @@ } else {

## Changes
### 7.0.12
- Added an optional `toErrorResponse` function to the `opts` arguments of `failLambdaCallback` and
`generateHandlerFunction` functions of the `api-lambdas` module to enable callers to customise the error response
object or Lambda Proxy error response body returned
- Updated `core-functions` dependency to version 3.0.22
- Updated `logging-utils` dependency to version 4.0.22
- Updated `aws-sdk` dev dependency to version 2.190.0
### 7.0.11

@@ -4,0 +12,0 @@ - Updated `core-functions` dependency to version 3.0.20

@@ -32,2 +32,4 @@ 'use strict';

const uuid = require('uuid');
function sampleFunction(resolvedResponse, rejectedError, ms) {

@@ -444,2 +446,3 @@ if (!ms) ms = 1;

error.headers = {hdr1: 'h1', hdr3: 'h3'};
error.auditRef = uuid();

@@ -480,3 +483,3 @@ // Create a sample function to be executed within the Lambda handler function

headers: {hdr1: 'h1', hdr3: 'h3', hdr2: 'dh2'}, // merged headers
body: JSON.stringify({code: error.code, message: error.message, auditRef: awsContext.awsRequestId})
body: JSON.stringify({code: error.code, message: error.message, auditRef: error.auditRef, awsRequestId: awsContext.awsRequestId})
};

@@ -483,0 +486,0 @@ t.deepEqual(response, expectedResponse, `response must be ${JSON.stringify(expectedResponse)}`);

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc