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 handledParentEvent = protocol.createHandler((event) => {
console.log(event)
})
window.addEventListener("message", (event) => {
handledParentEvent(event.data);
});
npm i @clxrity/sample-pkg
pnpm add @clxrity/sample-pkg
yarn add @clxrity/sample-pkg