1.0.0 • Published 2 years ago

@clayster404/zod-message-protocol v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

zod-message-protocol

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

// protocol

const protocol = createMesageProtocol({
  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

npm i @clayster404/zod-message-protocol

1.0.0

2 years ago