Skip to content

mihai-vlc/zod-message-bus

 
 

Repository files navigation

zod-message-protocol

Gives you type-safe message passing (using Zod!) between two different environments.

// protocol.ts

const protocol = createMessageProtocol({
  events: {
    LOG_IN: {
      username: z.string(),
      password: z.string(),
    },
    LOG_OUT: {},
  },
});

// iframe.ts

// Type safe sender!
const sendToParent = protocol.createHandler(window.parent.postMessage);

// Type safe receiver!
const handleParentEvent = protocol.createHandler((event) => {
  console.log(event);
});

window.addEventListener("message", (event) => {
  handleParentEvent(event.data);
});

Installation

pnpm add @total-typescript/zod-message-protocol

npm i @total-typescript/zod-message-protocol

yarn add @total-typescript/zod-message-protocol

Usage

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 100.0%