Created
October 4, 2021 12:55
-
-
Save leegilmorecode/78eed413919a3dd08bf7f6ee314db77b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import { APIGatewayProxyHandler, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda'; | |
import * as AWS from 'aws-sdk'; | |
import { validate } from '../shared/validator'; | |
import { OrderCreated } from '../../schema/order_create/ordercreated/OrderCreated'; | |
import { OrderCreatedItem } from '../../schema/order_create/ordercreated/OrderCreatedItem'; | |
import * as schema from '../../schema/order_create/ordercreated/[email protected]'; | |
const eventBridge = new AWS.EventBridge({ region: 'eu-west-1' }); | |
export const handler: APIGatewayProxyHandler = async ({ body }: APIGatewayEvent): Promise<APIGatewayProxyResult> => { | |
try { | |
// you would typically validate this with JSON schema but this is just a basic example | |
if (!body) throw new Error('missing body'); | |
const orderCreatedItem = new OrderCreatedItem(); | |
orderCreatedItem.id = 444; | |
orderCreatedItem.name = 'xxx-111-sss'; | |
const orderCreated = new OrderCreated(); | |
orderCreated.customerId = 222; | |
orderCreated.items = [orderCreatedItem]; | |
orderCreated.orderId = 111; | |
const event: AWS.EventBridge.PutEventsRequest = { | |
Entries: [ | |
{ | |
EventBusName: 'serverless-event-bridge', | |
Source: 'order.create', | |
DetailType: 'OrderCreated', | |
Detail: JSON.stringify(orderCreated), | |
}, | |
], | |
}; | |
console.log(event); | |
validate(event, schema); | |
const createEvent = await eventBridge.putEvents().promise(); | |
console.log(`Event: ${createEvent}`); | |
return { | |
statusCode: 200, | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify( | |
{ | |
message: 'order created', | |
}, | |
null, | |
2, | |
), | |
}; | |
} catch (error: any) { | |
console.error(error); | |
return { | |
statusCode: 500, | |
headers: { | |
'Content-Type': 'application/json', | |
}, | |
body: JSON.stringify('An error has been generated', null, 2), | |
}; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment