emittableevent
Advanced tools
Comparing version 0.0.5 to 0.0.6
@@ -23,3 +23,3 @@ export declare type MetadataConfigInput = StaticMetadata & { | ||
requestTimeEpoch: number; | ||
lifecycleStage: LifecycleStage; | ||
lifecycleStage: string; | ||
resource: string; | ||
@@ -37,5 +37,4 @@ accountId: string; | ||
}; | ||
declare type LifecycleStage = 'production' | 'qa' | 'test' | 'development' | 'staging' | 'demo'; | ||
declare type DataSensitivity = 'public' | 'sensitive' | 'proprietary' | 'secret'; | ||
declare type Jurisdiction = 'eu' | 'us' | 'cn' | 'apj'; | ||
export {}; |
{ | ||
"name": "emittableevent", | ||
"description": "EmittableEvent is an opinionated abstraction class for generating rich EventBridge events.", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"author": "Mikael Vesavuori", | ||
@@ -14,8 +14,6 @@ "license": "MIT", | ||
"aws", | ||
"serverless", | ||
"metrics", | ||
"aws-emf", | ||
"embedded-metric-format", | ||
"metric-logger", | ||
"minimal" | ||
"domain-events", | ||
"eventbridge", | ||
"event", | ||
"event-generation" | ||
], | ||
@@ -70,6 +68,3 @@ "bugs": { | ||
] | ||
}, | ||
"dependencies": { | ||
"emittableevent": "^0.0.4" | ||
} | ||
} |
@@ -11,10 +11,14 @@ # EmittableEvent | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/15a30d2b3f679507fdd0/maintainability)](https://codeclimate.com/github/mikaelvesavuori/emittableevent/maintainability) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/b65ba911cad077660f9c/maintainability)](https://codeclimate.com/github/mikaelvesavuori/emittableevent/maintainability) | ||
--- | ||
The problem that `EmittableEvent` solves is that it provides a simple interface to create richly metadata-detailed events. The risk of _not_ using a utility to do this is, of course, sprawl and unsynced solutions. Not using a conformant way will mean that teams across your organization will all have to deal with building possibly (unsynced) solutions to produce the events. Creating rich events is a non-trivial matter so it's just more boilerplate off your hands. | ||
_Note that `EmittableEvent` is primarily meant to function in an AWS Lambda context, however it will function just fine also outside of one but will be missing certain metadata._ | ||
For a great complementary solution for a Domain Event Publisher and an Event Emitter abstraction using `EmittableEvent`, [see my related Gist](https://gist.github.com/mikaelvesavuori/80c22979546693545256b71b9d3d1cce). | ||
## Consider using a Domain Event Publisher | ||
For a great complementary solution using a Domain Event Publisher and an Event Emitter abstraction together with `EmittableEvent`, [see my related Gist](https://gist.github.com/mikaelvesavuori/80c22979546693545256b71b9d3d1cce). | ||
## Usage | ||
@@ -24,2 +28,4 @@ | ||
Using `EmittableEvent` is mostly a concern of creating your own classes extending it and providing the initial, required static metadata. | ||
```typescript | ||
@@ -31,12 +37,5 @@ // ES5 format | ||
class MyEvent extends EmittableEvent { | ||
// Do something here if you want, else just leave it as is! | ||
} | ||
// Request context from API Gateway or whatever | ||
const awsRequestContext = event.requestContext; | ||
const event = new MyEvent(eventInput, awsRequestContext); | ||
``` | ||
## Demo | ||
```typescript | ||
// Your own event class | ||
@@ -48,6 +47,7 @@ class MyEvent extends EmittableEvent { | ||
// Convenience utility to produce required static metadata | ||
// Can of course be a static metadata JSON as well! | ||
const getMetadataConfig = (version = 1) => { | ||
return { | ||
version, | ||
eventType: 'DomainEvent', | ||
eventType: 'DomainEvent' as any, // These have hard types internally | ||
domain: 'MyDomain', | ||
@@ -59,3 +59,3 @@ system: 'MySystem', | ||
owner: 'Sam Person', | ||
jurisdiction: 'eu' | ||
jurisdiction: 'eu' as any // These have hard types internally | ||
}; | ||
@@ -75,9 +75,17 @@ }; | ||
// Create the event | ||
const event = new MyEvent(eventInput, requestContext); | ||
const myEvent = new MyEvent(eventInput, requestContext); | ||
``` | ||
Another benefit of this approach is that you can now "type" your events rather than pass around dumb data blobs. | ||
### Retrieving the event payload | ||
This is simple. Just do: | ||
```typescript | ||
// ...and here's the actual full body of the event | ||
console.log(event.get()); | ||
const eventPayload = MyEvent.get(); | ||
``` | ||
The event will look similar to: | ||
The final event will look similar to: | ||
@@ -89,4 +97,48 @@ ```json | ||
"DetailType": "MyEvent", | ||
"Detail": "{\"metadata\":{\"version\":1,\"eventType\":\"DomainEvent\",\"domain\":\"MyDomain\",\"system\":\"MySystem\",\"service\":\"MyService\",\"team\":\"MyTeam\",\"hostPlatform\":\"aws\",\"owner\":\"Sam Person\",\"jurisdiction\":\"eu\",\"eventName\":\"MyEvent\",\"correlationId\":\"26dd1faf-a901-4413-92db-9e09b7915a3c\",\"resource\":\"/\",\"accountId\":\"123412341234\",\"runtime\":\"\",\"functionName\":\"\",\"functionMemorySize\":\"\",\"functionVersion\":\"\",\"lifecycleStage\":\"dev\",\"region\":\"\"},\"data\":{\"something\":\"some value here\"}}" | ||
"Detail": "{\"metadata\":{\"version\":1,\"eventType\":\"DomainEvent\",\"domain\":\"MyDomain\",\"system\":\"MySystem\",\"service\":\"MyService\",\"team\":\"MyTeam\",\"hostPlatform\":\"aws\",\"owner\":\"Sam Person\",\"jurisdiction\":\"eu\",\"eventName\":\"MyEvent\",\"timestamp\":\"1666808901725\",\"timestampHuman\":\"2022-10-26T18:28:21.725Z\",\"requestTimeEpoch\":1666808901376,\"id\":\"f9cd2b03-c0ce-4678-8307-a51dd69d4284\",\"correlationId\":\"39594a3d-26d5-4d06-85e0-6d77afbe4ea9\",\"resource\":\"/demo\",\"accountId\":\"123412341234\",\"runtime\":\"AWS_Lambda_nodejs16.x\",\"functionName\":\"my-service-dev-Demo\",\"functionMemorySize\":\"1024\",\"functionVersion\":\"$LATEST\",\"lifecycleStage\":\"dev\",\"region\":\"eu-north-1\"},\"data\":{\"something\":\"some value here if you want\"}}" | ||
} | ||
``` | ||
## The beautified event shape | ||
The below is an example of how a generated EventBridge event might look like. The `detail` section is a string, but for readability I've made it into an object here. | ||
```json | ||
{ | ||
"EventBusName": "MyEventBus", | ||
"Source": "mydomain.mysystem.myevent", | ||
"DetailType": "MyEvent", | ||
"Detail": { | ||
"metadata": { | ||
"version": 1, | ||
"eventType": "DomainEvent", | ||
"domain": "MyDomain", | ||
"system": "MySystem", | ||
"service": "MyService", | ||
"team": "MyTeam", | ||
"hostPlatform": "aws", | ||
"owner": "Sam Person", | ||
"jurisdiction": "eu", | ||
"eventName": "MyEvent", | ||
"timestamp": "1666808901725", | ||
"timestampHuman": "2022-10-26T18:28:21.725Z", | ||
"requestTimeEpoch": 1666808901376, | ||
"id": "f9cd2b03-c0ce-4678-8307-a51dd69d4284", | ||
"correlationId": "39594a3d-26d5-4d06-85e0-6d77afbe4ea9", | ||
"resource": "/demo", | ||
"accountId": "123412341234", | ||
"runtime": "AWS_Lambda_nodejs16.x", | ||
"functionName": "my-service-dev-Demo", | ||
"functionMemorySize": "1024", | ||
"functionVersion": "$LATEST", | ||
"lifecycleStage": "dev", | ||
"region": "eu-north-1" | ||
}, | ||
"data": { "something": "some value here if you want" } | ||
} | ||
} | ||
``` | ||
## License | ||
MIT. See `LICENSE` for more details. |
16648
0
138
237
- Removedemittableevent@^0.0.4
- Removedemittableevent@0.0.10.0.30.0.4(transitive)