Upgrade 1.x -> 2.x
aka "The async/await Update"
Version 2.x of Middy no longer supports Node.js versions 10.x. You are highly encouraged to move to Node.js 14.x,
which support ES6 modules by default (export), optional chaining (?.) and nullish coalescing operator (??) natively.
Core
- In handler
callback(err, response)have been removed forasync/awaitsupportreturn responseto triggeraftermiddleware stackthrow new Error(...)to triggeronErrormiddleware stack
- In middleware
next(err)has been removed forasync/awaitsupportthrow new Error(...)to triggeronErrormiddleware stackreturn responseto short circuit any middleware stack and respond. v1.x currently throws an error when something is returned
Middleware
cache
Deprecated. Too generic and had low usage.
However, you can use the following if needed:
const { createHash } = require('crypto')
module.exports = (opts) => {
const storage = {}
const defaults = {
calculateCacheId: async (event) =>
createHash('md5').update(JSON.stringify(event)).digest('hex'),
getValue: async (key) => storage[key],
setValue: async (key, value) => {
storage[key] = value
}
}
const options = { ...defaults, ...opts }
let currentCacheKey
const cacheMiddlewareBefore = async (request) => {
const cacheKey = await options.calculateCacheId(request.event)
const response = await options.getValue(cacheKey)
if (response) {
return response
}
request.internal.cacheKey = cacheKey
}
const cacheMiddlewareAfter = async (request) => {
await options.setValue(request.internal.cacheKey, request.response)
}
return {
before: cacheMiddlewareBefore,
after: cacheMiddlewareAfter
}
}
db-manager
Deprecated. Too generic and had low usage. You can check out middy-rds as a possible alternative or example on building your own replacement.
do-not-wait-for-empty-event-loop
No change
function-shield
Deprecated. Only supported up to Node v10.
http-content-negotiation
No change
http-cors
Added new options to support more headers
- methods
- exposeHeaders
- requestHeaders
- requestMethods
http-error-handler
Added in support to honour httpError.expose. Errors with statusCode >= 500 are no longer applied to response by default. Added new option to catch any non-http and statusCode >= 500 errors
- fallbackMessage
http-event-normalizer
No change
http-header-normalizer
No change
http-json-body-parser
No change
http-multipart-body-parser
No change
http-partial-response
No change
http-response-serializer
No change
http-security-headers
No longer adds statusCode:500 when there is no response.
http-urlencode-body-parser
Remove extended option. Only uses qs as the parser, formally enabled by options {extended: true}.
http-urlencode-path-parser
No change
input-output-logger
- Now additionally logs response from the
onErrormiddleware stack - Support for omiting within nested arrays
- Add in support for
replacerto be passed intoJSON.stringify()
rds-signer
New middleware to fetch RDS credential used when connecting with IAM roles. This was built into db-manager.
s3-key-normalizer
No change
s3-object-response
New middleware to fetch and respond to S3 Object Get request event.
secrets-manager
Refactored, see documentation
sqs-json-body-parser
No change
sqs-partial-batch-failure
Replaced option sqs with AwsClient and added in more options for control.
ssm
Refactored, see documentation
sts
New middleware to fetch assume role credentials.
validator
Upgraded ajv and it's plugins to support JSON Schema Draft 2020-12 specification. Defaults were change because of this.
- Plugin
ajv-keywordsremoved from being included by default because it's quite a large package and usually only one keyword is used. - Plugin
ajv-errorsremoved from included by default because it conflicts withajv-i18nwhen dealing with custom messages for multiple languages
warmup
Deprecated. This was a work round for a missing feature in AWS Lambda. AWS added in the ability to use provisioned concurrency on 2019-12-03, removing the need for this work around.
However, you can use the following if needed:
middy(lambdaHandler).before((request) => {
if (request.event.source === 'serverless-plugin-warmup') {
console.log('Exiting early via warmup Middleware')
return 'warmup'
}
})