UNPKG

@omnizapi/connect

Version:

Omnizapi SDK for browser applications

61 lines (54 loc) • 2.31 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Omnizapi SDK - Vanilla HTML Example</title> </head> <body> <h1>Connect your channel</h1> <button id="connect-btn" type="button">Connect channel</button> <p id="status" role="status"></p> <!-- Version pinned to 1.1.1; test your integration and serve this page over HTTP(S). --> <script src="https://unpkg.com/@omnizapi/connect@1.1.1/dist/omnizapi.min.js"></script> <script> const button = document.getElementById("connect-btn"); const statusElement = document.getElementById("status"); let client; button.addEventListener("click", async () => { if (button.disabled) return; button.disabled = true; button.textContent = "Connecting..."; statusElement.textContent = "Connecting..."; try { client ??= Omnizapi.newClient({ publicKey: "your-public-key" }); const result = await client.connect({ channelId: "your-channel-id" }); if (!result.success) { statusElement.textContent = `Connection not completed (${result.reason}).`; return; } // Your authenticated backend validates credentials and result.channelId. statusElement.textContent = "Finalizing on the backend..."; const response = await fetch("/api/connect-channel", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(result), }); if (!response.ok) { statusElement.textContent = "The backend could not finalize the connection."; return; } statusElement.textContent = "Channel connected!"; } catch { // The browser global exposes the factory, not the named error export. // Never log raw errors or credentials, including URLs in error messages. statusElement.textContent = "Could not confirm the connection. Check the integration and backend status before retrying."; } finally { button.disabled = false; button.textContent = "Connect channel"; } }); </script> </body> </html>