-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: eventBridgeMessage handler to pass details object (#52)
* 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
1 parent
bef4f5d
commit 47efd0d
Showing
3 changed files
with
12 additions
and
26 deletions.
There are no files selected for viewing
10 changes: 1 addition & 9 deletions
10
packages/sls-aws/src/eventbridge/__tests__/json-eventbridge-message.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 1 addition & 9 deletions
10
packages/sls-aws/src/eventbridge/json-eventbridge-message.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |