npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

emittableevent

v1.0.1

Published

EmittableEvent is an opinionated abstraction class for generating rich EventBridge events.

Downloads

1,077

Readme

EmittableEvent

EmittableEvent is an opinionated abstraction class for generating rich EventBridge events.

Build Status

Quality Gate Status

codecov

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.

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.

Usage

Basic importing and usage

Using EmittableEvent is mostly a concern of creating your own classes extending it and providing the initial, required static metadata.

// ES5 format
const { EmittableEvent } = require('emittableevent');
// ES6 format
import { EmittableEvent } from 'emittableevent';

// Request context from API Gateway or whatever
const awsRequestContext = event.requestContext;

// Your own event class
class MyEvent extends EmittableEvent {
  // Do something here if you want, else just leave it as is!
}

// Convenience utility to produce required static metadata
// Can of course be a static metadata JSON as well!
const getMetadataConfig = (
  version = 1,
  eventType: any = 'DomainEvent',
  jurisdiction: any = 'eu'
): MetadataConfigInput => {
  return {
    version,
    eventType,
    domain: 'MyDomain',
    system: 'MySystem',
    service: 'MyService',
    team: 'MyTeam',
    hostPlatform: 'aws',
    owner: 'Sam Person',
    jurisdiction
  };
};

// Input for actually creating the event
const eventInput = {
  eventName: 'MyEvent',
  eventBusName: 'MyEventBus',
  data: {
    something: 'some value here if you want'
  },
  metadataConfig: getMetadataConfig()
};

// Create the event
const myEvent = new MyEvent(eventInput, awsRequestContext);

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:

// ...and here's the actual full body of the event
const eventPayload = MyEvent.get();

The final event will look similar to:

{
  "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\"}}"
}

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.

{
  "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.