emittableevent
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -7,2 +7,8 @@ "use strict"; | ||
constructor(eventInput, awsRequestContext) { | ||
if (!eventInput.eventName || | ||
!eventInput.eventBusName || | ||
!eventInput.data || | ||
!eventInput.metadataConfig || | ||
!awsRequestContext) | ||
throw new MissingRequiredInputsError(); | ||
const { eventName, eventBusName, data, metadataConfig } = eventInput; | ||
@@ -20,3 +26,3 @@ this.eventBusName = eventBusName; | ||
const detailType = eventName; | ||
const metadata = this.produceMetadata(); | ||
const metadata = this.produceMetadata(eventName); | ||
const source = `${metadata.domain.toLowerCase()}.${metadata.system.toLowerCase()}.${detailType.toLowerCase()}`; | ||
@@ -34,3 +40,3 @@ const detail = { | ||
} | ||
produceMetadata() { | ||
produceMetadata(eventName) { | ||
const metadata = this.metadataConfig; | ||
@@ -43,2 +49,3 @@ const requestContext = this.awsRequestContext; | ||
...this.metadataConfig, | ||
eventName, | ||
timestamp: `${timeNow}`, | ||
@@ -90,2 +97,11 @@ timestampHuman: new Date(timeNow).toISOString(), | ||
exports.EmittableEvent = EmittableEvent; | ||
class MissingRequiredInputsError extends Error { | ||
constructor() { | ||
super(); | ||
this.name = 'MissingRequiredInputsError'; | ||
const message = `Missing required fields to create the event!`; | ||
this.message = message; | ||
console.error(message); | ||
} | ||
} | ||
class MissingMetadataFieldsError extends Error { | ||
@@ -92,0 +108,0 @@ constructor() { |
@@ -33,3 +33,5 @@ export declare type MetadataConfigInput = StaticMetadata & { | ||
}; | ||
export declare type Metadata = MetadataConfigInput & DynamicMetadata; | ||
export declare type Metadata = MetadataConfigInput & DynamicMetadata & { | ||
eventName: string; | ||
}; | ||
declare type LifecycleStage = 'production' | 'qa' | 'test' | 'development' | 'staging' | 'demo'; | ||
@@ -36,0 +38,0 @@ declare type DataSensitivity = 'public' | 'sensitive' | 'proprietary' | 'secret'; |
{ | ||
"name": "emittableevent", | ||
"description": "EmittableEvent is an opinionated abstraction class for generating rich EventBridge events.", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"author": "Mikael Vesavuori", | ||
@@ -28,3 +28,4 @@ "license": "MIT", | ||
"!/lib/**/*.map", | ||
"!/tests" | ||
"!/tests", | ||
"!/testdata" | ||
], | ||
@@ -71,4 +72,4 @@ "scripts": { | ||
"dependencies": { | ||
"emittableevent": "^0.0.3" | ||
"emittableevent": "^0.0.4" | ||
} | ||
} |
# EmittableEvent | ||
`EmittableEvent` is an opinionated abstraction class for generating rich EventBridge events. | ||
**`EmittableEvent` is an opinionated abstraction class for generating rich EventBridge events**. | ||
![Build Status](https://github.com/mikaelvesavuori/emittableevent/workflows/main/badge.svg) | ||
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=mikaelvesavuori_emittableevent&metric=alert_status)](https://sonarcloud.io/dashboard?id=mikaelvesavuori_emittableevent) | ||
[![codecov](https://codecov.io/gh/mikaelvesavuori/emittableevent/branch/main/graph/badge.svg?token=S7D3RM9TO7)](https://codecov.io/gh/mikaelvesavuori/emittableevent) | ||
[![Maintainability](https://api.codeclimate.com/v1/badges/15a30d2b3f679507fdd0/maintainability)](https://codeclimate.com/github/mikaelvesavuori/emittableevent/maintainability) | ||
--- | ||
_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). | ||
## Usage | ||
### Basic importing and usage | ||
```typescript | ||
// ES5 format | ||
const { EmittableEvent } = require('emittableevent'); | ||
// ES6 format | ||
import { EmittableEvent } from 'emittableevent'; | ||
class MyEvent extends EmittableEvent { | ||
// Do something here if you want, else just leave it as is! | ||
} | ||
const event = new MyEvent(eventInput, awsRequestContext); | ||
``` | ||
## Demo | ||
```typescript | ||
// Your own event class | ||
@@ -12,3 +45,3 @@ class MyEvent extends EmittableEvent { | ||
// Convenience utility to produce required static metadata | ||
const getMetadataConfig = (version = 1): MetadataConfigInput => { | ||
const getMetadataConfig = (version = 1) => { | ||
return { | ||
@@ -29,3 +62,3 @@ version, | ||
const eventInput = { | ||
eventName: 'Created', | ||
eventName: 'MyEvent', | ||
eventBusName: 'MyEventBus', | ||
@@ -39,3 +72,3 @@ data: { | ||
// Create the event | ||
const event = new CreatedEvent(eventInput, requestContext); | ||
const event = new MyEvent(eventInput, requestContext); | ||
@@ -45,1 +78,12 @@ // ...and here's the actual full body of the event | ||
``` | ||
The event will look similar to: | ||
```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\",\"correlationId\":\"26dd1faf-a901-4413-92db-9e09b7915a3c\",\"resource\":\"/\",\"accountId\":\"123412341234\",\"runtime\":\"\",\"functionName\":\"\",\"functionMemorySize\":\"\",\"functionVersion\":\"\",\"lifecycleStage\":\"dev\",\"region\":\"\"},\"data\":{\"something\":\"some value here\"}}" | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14418
238
86
+ Addedemittableevent@0.0.4(transitive)
Updatedemittableevent@^0.0.4