Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
added records to the SNS action (#44)
Browse files Browse the repository at this point in the history
added records to the SNS action for further usage
  • Loading branch information
chgohlke authored May 4, 2020
1 parent 6e3590e commit b27d3a7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,15 @@ export const handler = router.handler({
// a regex to match the content of the SNS-Subject:
subject: /.*/,
// Attention: the message is JSON-stringified
action: (sns, context) => service.doSomething(JSON.parse(sns.Message))
action: (sns, context, records) => service.doSomething(JSON.parse(sns.Message))
}
]
}
})
```
The *records* parameter contains `SNSEventRecord[]`. An exampe event structure can be found [here](lib/event-examples/sns.json). For example you can parse now the [message attributes of the SNS](https://docs.aws.amazon.com/sns/latest/dg/sns-message-attributes.html) or reads the topic arn of SNS.
## SQS to Lambda Integrations
For handling calls in Lambdas initiated from AWS-SQS you can use the following code snippet:
Expand Down Expand Up @@ -378,15 +380,14 @@ Increase version in **package.json** (using [semantic version syntax](https://se
Thats all.
## Release History
* 0.8.3
* added records to the SQS action for further processing
* unreleased
* added records to the SQS (#43) and SNS (#44) action for further processing
* 0.8.2
* added support for Open API parameter definitions e.g.: /section/{id}
* 0.8.1
* fix: changed ProxyIntegrationEvent body type to be generic but defaults to unknown
* fix: changed @types/aws-lambda from devDependency to dependency
* **breaking**: error response objects (thrown or rejected) now need to set `statusCode` instead of `status` (consistent with response)
* 0.7.1
* code style cleanup
* fix: hosted package on npmjs should now worked
Expand Down
6 changes: 3 additions & 3 deletions lib/sns.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Context, SNSEvent, SNSMessage } from 'aws-lambda'
import { Context, SNSEvent, SNSMessage, SNSEventRecord } from 'aws-lambda'
import { ProcessMethod } from './EventProcessor'

export type SnsEvent = SNSEvent

export interface SnsRoute {
subject: RegExp
action: (sns: SNSMessage, context: Context) => Promise<any> | any
action: (sns: SNSMessage, context: Context, records: SNSEventRecord[]) => Promise<any> | any
}

export interface SnsConfig {
Expand All @@ -29,7 +29,7 @@ export const process: ProcessMethod<SnsConfig, SnsEvent, Context, any> = (snsCon
for (const routeConfig of snsConfig.routes) {
if (routeConfig.subject instanceof RegExp) {
if (routeConfig.subject.test(sns.Subject)) {
const result = routeConfig.action(sns, context)
const result = routeConfig.action(sns, context, event.Records)
return result || {}
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions test/sns.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { process as sns, SnsConfig, SnsEvent } from '../lib/sns'
describe('sns.processor', () => {
const context = {} as any

it('context should be pass through', () => {
it('context and records should be pass through', () => {
const actionSpy = jasmine.createSpy('action')

const context = { bla: 'blup' } as any
Expand All @@ -12,7 +12,7 @@ describe('sns.processor', () => {

sns(snsCfg, event, context)

expect(actionSpy).toHaveBeenCalledWith(event.Records[0].Sns, context)
expect(actionSpy).toHaveBeenCalledWith(event.Records[0].Sns, context, event.Records)
})

it('should ignore event if it is no SNS event', () => {
Expand Down

0 comments on commit b27d3a7

Please sign in to comment.