clxrityy / sample-pkg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sample-pkg

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);
});

installation

npm i @clxrity/sample-pkg
pnpm add @clxrity/sample-pkg
yarn add @clxrity/sample-pkg

About

License:MIT License


Languages

Language:TypeScript 100.0%