@clayster404/zod-message-protocol
v1.0.0
Published
Gives you type-safe message passing (using Zod!) between two different environments.
Downloads
3
Readme
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