{"id":21359581,"url":"https://github.com/suin/event-data","last_synced_at":"2025-03-16T06:26:13.303Z","repository":{"id":38174526,"uuid":"282088306","full_name":"suin/event-data","owner":"suin","description":"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.","archived":false,"fork":false,"pushed_at":"2023-01-06T12:18:34.000Z","size":3284,"stargazers_count":0,"open_issues_count":23,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-22T18:51:47.114Z","etag":null,"topics":["event","google-cloud-pubsub","messaging-system"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/suin.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-24T00:53:54.000Z","updated_at":"2020-07-24T00:56:40.000Z","dependencies_parsed_at":"2023-02-06T01:45:28.099Z","dependency_job_id":null,"html_url":"https://github.com/suin/event-data","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suin%2Fevent-data","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suin%2Fevent-data/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suin%2Fevent-data/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suin%2Fevent-data/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suin","download_url":"https://codeload.github.com/suin/event-data/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243835387,"owners_count":20355601,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["event","google-cloud-pubsub","messaging-system"],"created_at":"2024-11-22T05:28:50.910Z","updated_at":"2025-03-16T06:26:13.278Z","avatar_url":"https://github.com/suin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# @suin/event-data\n\nA simple event data representation for TypeScript.\n\nThis component provides a very simple reusable interface that represents an event.\n\nThe interface was designed for the use case that represents event payloads of messaging system like Google Cloud Pub/Sub.\n\n## Features\n\n- Abstract event data type `EventData`\n- Type guard function\n- Validation function\n- Assertion function\n\n## Installation\n\n```bash\nyarn add @suin/event-data\n# or\nnpm install @suin/event-data\n```\n\n## Usages\n\n### Declaring the `EventData` value\n\nThe basic way to declare the event data is to annotate value with type `EventData`:\n\n```typescript\nimport { EventData } from '@suin/event-data'\n\nconst event: EventData = {\n  correlationId: '68a5e0dd-34a0-47fa-94d4-8fa2162301e3',\n  data: {\n    foo: 1,\n    bar: 2,\n  },\n}\n```\n\nHowever, 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:\n\n```typescript\nimport { newEventData } from '@suin/event-data'\n\nconst event = newEventData({\n  correlationId: '68a5e0dd-34a0-47fa-94d4-8fa2162301e3',\n  data: {\n    foo: 1,\n    bar: 2,\n  },\n})\n\nconst foo: number = event.data.foo\n```\n\n### Using the type guard function\n\n```typescript\nimport { isEventData } from '@suin/event-data'\n\nconst event: unknown = {\n  /* ... */\n}\n\nif (isEventData(event)) {\n  console.log(event.correlationId)\n  console.log(event.data) // NOTICE: the type is unknown\n} else {\n  console.log('The value is incompatible with EventData')\n}\n```\n\n### Using the validation function\n\n```typescript\nimport { parseEventData } from '@suin/event-data'\n\ntry {\n  const event = parseEventData(JSON.parse('{}'))\n  console.log('The JSON value is compatible with EventData', event)\n} catch (error) {\n  console.log(error)\n}\n```\n\n### Using the assertion function\n\n```typescript\nimport { assertEventData } from '@suin/event-data'\n\ntry {\n  const event: unknown = JSON.parse('{}')\n  assertEventData(event)\n  console.log('The JSON value is compatible with EventData', event)\n} catch (error) {\n  console.log(error)\n}\n```\n\n## API Reference\n\nhttps://suin.github.io/event-data/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuin%2Fevent-data","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuin%2Fevent-data","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuin%2Fevent-data/lists"}