{"id":17657972,"url":"https://github.com/3rd/zodbus","last_synced_at":"2025-05-07T10:06:02.255Z","repository":{"id":175075834,"uuid":"653295327","full_name":"3rd/zodbus","owner":"3rd","description":"ZodBus is a fully-typed event bus powered by Zod, with nested namespace \u0026 wildcard support and payload validation.","archived":false,"fork":false,"pushed_at":"2024-05-17T20:41:36.000Z","size":172,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-02T08:42:06.577Z","etag":null,"topics":["events","typescript","zod"],"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/3rd.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-13T19:25:15.000Z","updated_at":"2024-05-18T10:41:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"829747a3-eafb-42fc-9e63-c0c4a2da7899","html_url":"https://github.com/3rd/zodbus","commit_stats":null,"previous_names":["3rd/zodbus"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3rd%2Fzodbus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3rd%2Fzodbus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3rd%2Fzodbus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/3rd%2Fzodbus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/3rd","download_url":"https://codeload.github.com/3rd/zodbus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223806487,"owners_count":17205982,"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":["events","typescript","zod"],"created_at":"2024-10-23T14:43:49.418Z","updated_at":"2024-11-09T09:05:30.369Z","avatar_url":"https://github.com/3rd.png","language":"TypeScript","readme":"# ZodBus\n\nZodBus is a fully-typed event bus powered by [Zod](https://zod.dev), with nested namespace \u0026 wildcard support and payload validation.\n\nhttps://github.com/3rd/zodbus/assets/59587503/08d2c13c-cccd-407f-b14c-957d7dd5115d\n\n## Installation\n\nZodBus is published on the NPM Registry: [https://npmjs.com/package/zodbus](https://npmjs.com/package/zodbus)\n\n```sh\nnpm install -D zod zodbus\npnpm install -D zod zodbus\nyarn add -D zod zodbus\n```\n\n## Usage\n\n```ts\nimport { z } from \"zod\";\nimport { create } from \"zodbus\";\n\n// This is how you define your event schema.\n//  By traversing the schema, event names are built by concatenating the parent keys with dots,\n//  and when a ZodType is encountered it means we have reached the end of the key definition,\n//  and the ZodType describes the payload signature for the current event.\nconst schema = {\n  foo: {\n    bar: {\n      baz: z.object({    // this creates the event \"foo.bar.baz\",\n        id: z.string(),  // with the { id: string; val: number } payload type\n        val: z.number()\n      })\n    }\n  },\n  zip: {\n    za: z.string(),      // this creates the event \"zip.za\" with a string payload\n    zb: z.number(),      // this creates the event \"zip.zb\" with a number payload\n  }\n};\n\nconst bus = create({ schema, validate: true });\n\n// Non-wildcard subscription signatures are fully-typed.\nbus.subscribe(\"foo.bar.baz\", (data: { id: string; val: number; }, event: \"foo.bar.baz\") =\u003e {});\nbus.subscribe(\"zip.za\", (data: string, event: \"zip.za\") =\u003e {});\n\n// Wildcard event signatures are typed as (data: unknown; event: string), it's up to you to handle the type.\n//  The \"*\" wildcard is special, as it refers to all events, regardless of the nesting depth.\n//  All other wildcard patterns refer to a specific event shape.\nbus.subscribe(\"*\", (data: unknown, event: string) =\u003e {}); // will be called for all events\nbus.subscribe(\"foo.*.baz\", ...); // will be called when publishing a \"foo.bar.baz\" event\nbus.subscribe(\"foo.bar.*\", ...); // same\nbus.subscribe(\"*.bar.*\", ...); // same\nbus.subscribe(\"*.*.*\", ...); // same\nbus.subscribe(\"*.*\", ...); // will be called for \"zip.za\" and \"zip.zb\"\nbus.subscribeOnce(\"*.*\", ...); // you can also subscribe to an event only once\n\n// You can unsubscribe one or all handlers from one or multiple events.\nbus.unsubscribe(\"zip.za\", handler); // unsubscribe handler from \"zip.za\"\nbus.unsubscribe(\"zip.*\", handler); // unsubscribe handler from \"zip.za\" and \"zip.bzb\"\nbus.unsubscribe(\"zip.*\"); // unsubscribe all handlers from \"zip.za\" and \"zip.bzb\"\nbus.unsubscribe(\"*\", handler); // unsubscribe handler from all events\nbus.unsubscribe(\"*\"); // unsubscribe all handlers from all events\n\n// Publishing is only allowed on fully-qualified event names.\nbus.publish(\"foo.bar.baz\", { id: \"uwu\", val: 4815162342 });\n\n// Utilities\nbus.getEventNames(); // [\"foo.bar.baz\", \"zip.za\", \"zip.zb\"]\nbus.getListeners(\"zip.*\"); // gets the listeners for \"zip.*\"\nbus.getListeners(); // gets all listeners\nconst data = await bus.waitFor(\"zip.zap\"); // typed waitFor with timeout and filter support\n```\n\n### Payload validation\n\nPayload validation is performed by Zod based on the schema, and **enabled by default**.\n\\\nIt is however optional if you don't have to validate external data.\n\\\nFor most use-cases the type checking is enough as it won't allow you to make mistakes.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3rd%2Fzodbus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F3rd%2Fzodbus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F3rd%2Fzodbus/lists"}