Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
aws-lambda-res
Advanced tools
Readme
is a tiny helper to create a response object for AWS Lambda with Proxy integration
Usage | Annotated source | License
Suppose you have an API Gateway resource with a method configured with Lambda Proxy integration.
Suppose you have a dummy endpoint which returns JSON { "ok": true }
,
then the following code will be a working implementation.
const response = require('aws-lambda-res')
function handler (event, context, callback) {
callback(null, response(200)({ ok: true }))
}
exports.handler = handler
You can pass headers as second argument, body can be null: for example to logout and redirect to homepage you can use something like
function handler (event, context, callback) {
const Expires = 'Sat, 01 Jan 2000 00:00:00 GMT' // Some day in the past.
callback(null, response(302)(null, {
'Location': 'https://example.org',
'set-cookie': `user_authenticated=; Domain=example.com; Expires=${Expires}`
}))
}
exports.handler = handler
Let me write few tips I want to remember. When a method on API Gateway is
configured with Lambda Proxy integration no additional mapping is needed.
Everything you need will be available in event
argument.
To get a JSON payload, just parse it from body.
function handler (event, context, callback) {
const { id, name } = JSON.parse(event.body)
// Follows your code...
}
To extract form parameters, or from any POST
or PUT
method, use querystring
package.
const querystring = require('querystring')
function handler (event, context, callback) {
const { email, password } = querystring.parse(event.body)
// Follows your code...
}
To access query string parameters in a GET
do
function handler (event, context, callback) {
const { param1, param2 } = event.queryStringParameters
// Follows your code...
}
To get cookies, parse event.cookie
.
function handler (event, context, callback) {
const cookies = event.cookie.split(';')
let session
cookies.forEach(cookie => {
if (cookie.indexOf('session=') === 0) {
session = cookie.split('=')[1]
}
})
// Follows your code...
}
To get headers, look into event.headers
.
For example, you can get a JWT header with the following snippet.
function handler (event, context, callback) {
const auth = event.headers.Authorization
const token = (auth && auth.startsWith('BEARER ')) ? auth.substring(7) : null
// Follows your code...
}
For route placeholders, for example suppose you want to handle an enpoint
like GET /user/{id}
, go to API Gateway and create first a resource with
path /user
. Then create a child resource with path /{id}
and add a GET
method. The code of associated Lambda function will be something like the
following.
function handler (event, context, callback) {
const { id } = event.pathParameters
// Follows your code...
}
// This code is generated by command: npm run markdown2code
Lambda Proxy integration needs a response with the following properties:
/**
* Create a response helper.
*
* @params {Number} statusCode
* @returns {Function} awsLambdaResponse helper
*/
function response (statusCode) {
The exported response
function, requires statusCode
argument and returns
an helper function which accepts parameters:
body
: can be any data, even null.headers
, defaults to { 'Content-type': 'application/json' }
. /**
* AWS Lambda response helper.
*
* @params {Object|null} body
* @params {Object} [headers]
* @returns {Object} responseObj required by AWS Lambda Proxy integration
*/
function awsLambdaResponse (
body,
headers = { 'Content-Type': 'application/json' }
) {
const responseObj = {
isBase64Encoded: false,
headers,
statusCode,
body: body === null ? null : JSON.stringify(body)
}
return responseObj
}
Return helper and export function
return awsLambdaResponse
}
module.exports = response
FAQs
is a tiny helper to create a response object for AWS Lambda with Proxy Integration
The npm package aws-lambda-res receives a total of 13 weekly downloads. As such, aws-lambda-res popularity was classified as not popular.
We found that aws-lambda-res demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.