Skip to content

Commit

Permalink
feat: add support for Step Functions (#668)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominhhai authored Dec 21, 2023
1 parent 6eaeb0a commit 1151a3a
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,8 @@ serverlessExpress({
'AWS_SQS': '/sqs'
'AWS_EVENTBRIDGE': '/eventbridge',
'AWS_KINESIS_DATA_STREAM': '/kinesis',
'AWS_S3': '/s3',
'AWS_STEP_FUNCTIONS': '/step-functions',
}
})
```
Expand Down
2 changes: 1 addition & 1 deletion src/configure.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Handler } from 'aws-lambda';
import { Logger } from './logger';
import Framework from './frameworks';

type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3';
type EventSources = 'AWS_SNS' | 'AWS_DYNAMODB' | 'AWS_EVENTBRIDGE' | 'AWS_SQS' | 'AWS_KINESIS_DATA_STREAM' | 'AWS_S3' | 'AWS_STEP_FUNCTIONS';

interface EventSource {
getRequest?: any; // TODO:
Expand Down
27 changes: 27 additions & 0 deletions src/event-sources/aws/step-functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const getRequestValuesFromStepFunctions = ({ event }) => {
const method = 'POST'
const headers = { host: 'stepfunctions.amazonaws.com' }
const body = event

return {
method,
headers,
body
}
}

const getResponseToStepFunctions = ({
body,
isBase64Encoded = false
}) => {
if (isBase64Encoded) {
throw new Error('base64 encoding is not supported')
}

return JSON.parse(body)
}

module.exports = {
getRequest: getRequestValuesFromStepFunctions,
getResponse: getResponseToStepFunctions
}
4 changes: 2 additions & 2 deletions src/event-sources/azure/http-function-runtime-v4.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getRequest, getResponse } = require('./http-function-runtime-v3')

module.exports = {
getRequest: getRequest,
getResponse: getResponse
getRequest,
getResponse
}
3 changes: 3 additions & 0 deletions src/event-sources/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const azureHttpFunctionV4EventSource = require('./azure/http-function-runtime-v4
const awsEventBridgeEventSource = require('./aws/eventbridge')
const awsKinesisEventSource = require('./aws/kinesis')
const awsS3 = require('./aws/s3')
const awsStepFunctionsEventSource = require('./aws/step-functions')

function getEventSource ({ eventSourceName }) {
switch (eventSourceName) {
Expand Down Expand Up @@ -37,6 +38,8 @@ function getEventSource ({ eventSourceName }) {
return awsKinesisEventSource
case 'AWS_S3':
return awsS3
case 'AWS_STEP_FUNCTIONS':
return awsStepFunctionsEventSource
default:
throw new Error('Couldn\'t detect valid event source.')
}
Expand Down
4 changes: 4 additions & 0 deletions src/event-sources/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ function getEventSourceNameBasedOnEvent ({
return 'AWS_EVENTBRIDGE'
}

if (event.context && event.context.Execution && event.context.State && event.context.StateMachine) {
return 'AWS_STEP_FUNCTIONS'
}

throw new Error('Unable to determine event source based on event.')
}

Expand Down

0 comments on commit 1151a3a

Please sign in to comment.