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

@suin/event-data

v1.0.0

Published

A simple event data representation for TypeScript

Downloads

183

Readme

@suin/event-data

A simple event data representation for TypeScript.

This component provides a very simple reusable interface that represents an event.

The interface was designed for the use case that represents event payloads of messaging system like Google Cloud Pub/Sub.

Features

  • Abstract event data type EventData
  • Type guard function
  • Validation function
  • Assertion function

Installation

yarn add @suin/event-data
# or
npm install @suin/event-data

Usages

Declaring the EventData value

The basic way to declare the event data is to annotate value with type EventData:

import { EventData } from '@suin/event-data'

const event: EventData = {
  correlationId: '68a5e0dd-34a0-47fa-94d4-8fa2162301e3',
  data: {
    foo: 1,
    bar: 2,
  },
}

However, this data property becomes type unknown. If you would like to give type information to data property, it would be useful to use newEventData function, since this infers the type:

import { newEventData } from '@suin/event-data'

const event = newEventData({
  correlationId: '68a5e0dd-34a0-47fa-94d4-8fa2162301e3',
  data: {
    foo: 1,
    bar: 2,
  },
})

const foo: number = event.data.foo

Using the type guard function

import { isEventData } from '@suin/event-data'

const event: unknown = {
  /* ... */
}

if (isEventData(event)) {
  console.log(event.correlationId)
  console.log(event.data) // NOTICE: the type is unknown
} else {
  console.log('The value is incompatible with EventData')
}

Using the validation function

import { parseEventData } from '@suin/event-data'

try {
  const event = parseEventData(JSON.parse('{}'))
  console.log('The JSON value is compatible with EventData', event)
} catch (error) {
  console.log(error)
}

Using the assertion function

import { assertEventData } from '@suin/event-data'

try {
  const event: unknown = JSON.parse('{}')
  assertEventData(event)
  console.log('The JSON value is compatible with EventData', event)
} catch (error) {
  console.log(error)
}

API Reference

https://suin.github.io/event-data/