Skip to content

Commit

Permalink
Merge pull request #1133 from robertbeal/http-event-normalizer-errors…
Browse files Browse the repository at this point in the history
…-fix

fix: don't throw error for non http event
  • Loading branch information
willfarrell authored Nov 15, 2023
2 parents 483bb3f + aaf136d commit e14c932
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 24 deletions.
16 changes: 4 additions & 12 deletions packages/http-event-normalizer/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@ const context = {
getRemainingTimeInMillis: () => 1000
}

test('It should throw error when invalid version', async (t) => {
test('It should not throw error when invalid version', async (t) => {
const event = {
version: '3.0'
}

const handler = middy((event) => event).use(httpEventNormalizer())
try {
await handler(event, context)
} catch (e) {
t.is(e.message, 'Unknown http event format')
}
t.notThrows(async () => await handler(event, context))
})

test('It should do nothing if not HTTP event', async (t) => {
test('It should not error if not an HTTP event', async (t) => {
const event = {
source: 's3'
}

const handler = middy((event) => event).use(httpEventNormalizer())
try {
await handler(event, context)
} catch (e) {
t.is(e.message, 'Unknown http event format')
}
t.notThrows(async () => await handler(event, context))
})

test('It should default queryStringParameters with REST API', async (t) => {
Expand Down
12 changes: 0 additions & 12 deletions packages/http-event-normalizer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,6 @@ const httpEventNormalizerMiddleware = () => {
const { event } = request

const version = pickVersion(event)
const isHttpEvent = isVersionHttpEvent[version]?.(event)
if (!isHttpEvent) {
throw new Error('Unknown http event format', {
cause: { package: '@middy/http-event-normalizer' }
})
}
// VPC Lattice is an http event, however uses a different notation
// - query_string_parameters
// - is_base64_encoded
Expand All @@ -35,10 +29,4 @@ const pickVersion = (event) => {
return event.version ?? (event.method ? 'vpc' : '1.0')
}

const isVersionHttpEvent = {
'1.0': (event) => typeof event.httpMethod !== 'undefined',
'2.0': (event) => typeof event.requestContext.http.method !== 'undefined',
vpc: (event) => typeof event.method !== 'undefined'
}

export default httpEventNormalizerMiddleware

0 comments on commit e14c932

Please sign in to comment.