Skip to content

Commit

Permalink
fix: eventBridgeMessage handler to pass details object (#52)
Browse files Browse the repository at this point in the history
* fix: eventBridgeMessage handler to pass details object

* fix: json event bridge message tests

* feat: add generic type to event bridge message handler

Co-authored-by: George Gunderson <[email protected]>
  • Loading branch information
gundersong and George Gunderson authored Jan 10, 2022
1 parent bef4f5d commit 47efd0d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import type { Context, EventBridgeEvent } from 'aws-lambda'
import { jsonEventBridgeMessage } from '../json-eventbridge-message'
import { PayloadParseError } from '../../error/parse'

describe('jsonEventBridgeMessage', () => {
it('throws a PayloadParseError if parsing ', () => {
const event = ({} as unknown) as EventBridgeEvent<string, never>
expect(() => jsonEventBridgeMessage(event, {} as Context)).toThrow(
new PayloadParseError('failed to process eventbridge message to json', { originalError: expect.any(Error) })
)
})

it('returns the json parsed detail from the eventbridge event', () => {
const event = ({ detail: '{"data":true}' } as unknown) as EventBridgeEvent<string, never>
const event = ({ detail: { data: true } } as unknown) as EventBridgeEvent<string, never>
expect(jsonEventBridgeMessage(event, {} as Context)).toEqual({ data: true })
})
})
18 changes: 10 additions & 8 deletions packages/sls-aws/src/eventbridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import { response } from '../reponse/response-or-error'
import { jsonEventBridgeEvent } from './json-eventbridge-event'
import { jsonEventBridgeMessage } from './json-eventbridge-message'

export type EventBridgeEventHandler = Handler<EventBridgeEvent<string, never>, Context, Promise<void>>
export type EventBridgeMessageHandler = Handler<EventBridgeEvent<string, string>, Context, Promise<void>>
export type EventBridgeEventHandler<T = never> = Handler<EventBridgeEvent<string, T>, Context, Promise<void>>
export type EventBridgeMessageHandler<T = never> = Handler<EventBridgeEvent<string, T>, Context, Promise<void>>

export const eventBridgeEvent = <D, P extends EventBridgeEvent<string, any>, C = never>(
config?: EnvironmentConfig<EventBridgeEventHandler>
): SlsEnvironment<EventBridgeEventHandler, C, D, P> =>
environment<EventBridgeEventHandler, C, D, P>(config).payload(jsonEventBridgeEvent).successHandler(response)
config?: EnvironmentConfig<EventBridgeEventHandler<never>>
): SlsEnvironment<EventBridgeEventHandler<never>, C, D, P> =>
environment<EventBridgeEventHandler<never>, C, D, P>(config).payload(jsonEventBridgeEvent).successHandler(response)

export const eventBridgeMessage = <D, P, C = never>(
config?: EnvironmentConfig<EventBridgeMessageHandler>
): SlsEnvironment<EventBridgeMessageHandler, C, D, P> =>
environment<EventBridgeMessageHandler, C, D, P>(config).payload(jsonEventBridgeMessage).successHandler(response)
config?: EnvironmentConfig<EventBridgeMessageHandler<never>>
): SlsEnvironment<EventBridgeMessageHandler<never>, C, D, P> =>
environment<EventBridgeMessageHandler<never>, C, D, P>(config)
.payload(jsonEventBridgeMessage)
.successHandler(response)
10 changes: 1 addition & 9 deletions packages/sls-aws/src/eventbridge/json-eventbridge-message.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import type { Context, EventBridgeEvent } from 'aws-lambda'

import { PayloadParseError } from '../error/parse'

const jsonEventBridgeMessage = <T>(event: EventBridgeEvent<string, string>, _context: Context): T => {
try {
return (JSON.parse(event.detail) as unknown) as T
} catch (err) {
throw new PayloadParseError('failed to process eventbridge message to json', { originalError: err })
}
}
const jsonEventBridgeMessage = <T>(event: EventBridgeEvent<string, T>, _context: Context): T => event.detail

export { jsonEventBridgeMessage }

0 comments on commit 47efd0d

Please sign in to comment.