@whop/iframe
Version:
Powers communication between Whop and your embedded app
1 lines • 8.27 kB
Source Map (JSON)
{"version":3,"sources":["../src/sdk/mobile-app-postmessage.ts","../src/sdk/sync-href.ts","../src/sdk/index.ts"],"sourcesContent":["export function getReactNativePostMessage() {\n\tconst reactNativePostMessage =\n\t\ttypeof window !== \"undefined\" &&\n\t\t\"ReactNativeWebView\" in window &&\n\t\ttypeof window.ReactNativeWebView === \"object\" &&\n\t\twindow.ReactNativeWebView &&\n\t\t\"postMessage\" in window.ReactNativeWebView &&\n\t\ttypeof window.ReactNativeWebView.postMessage === \"function\"\n\t\t\t? (data: string) => {\n\t\t\t\t\tif (\n\t\t\t\t\t\ttypeof window !== \"undefined\" &&\n\t\t\t\t\t\t\"ReactNativeWebView\" in window &&\n\t\t\t\t\t\ttypeof window.ReactNativeWebView === \"object\" &&\n\t\t\t\t\t\twindow.ReactNativeWebView &&\n\t\t\t\t\t\t\"postMessage\" in window.ReactNativeWebView &&\n\t\t\t\t\t\ttypeof window.ReactNativeWebView.postMessage === \"function\"\n\t\t\t\t\t)\n\t\t\t\t\t\twindow?.ReactNativeWebView?.postMessage(data);\n\t\t\t\t}\n\t\t\t: undefined;\n\treturn reactNativePostMessage;\n}\n\nexport function getSwiftPostMessage() {\n\tconst swiftMessageHandler =\n\t\ttypeof window !== \"undefined\" &&\n\t\t\"webkit\" in window &&\n\t\ttypeof window.webkit === \"object\" &&\n\t\twindow.webkit !== null &&\n\t\t\"messageHandlers\" in window.webkit &&\n\t\ttypeof window.webkit.messageHandlers === \"object\" &&\n\t\twindow.webkit.messageHandlers !== null &&\n\t\t\"SwiftWebView\" in window.webkit.messageHandlers &&\n\t\ttypeof window.webkit.messageHandlers.SwiftWebView === \"object\" &&\n\t\twindow.webkit.messageHandlers.SwiftWebView !== null &&\n\t\t\"postMessage\" in window.webkit.messageHandlers.SwiftWebView\n\t\t\t? window.webkit.messageHandlers.SwiftWebView\n\t\t\t: null;\n\n\tconst swiftPostMessage = swiftMessageHandler\n\t\t? (data: string) => {\n\t\t\t\tif (typeof swiftMessageHandler.postMessage === \"function\") {\n\t\t\t\t\tswiftMessageHandler.postMessage(data);\n\t\t\t\t}\n\t\t\t}\n\t\t: undefined;\n\n\treturn swiftPostMessage;\n}\n","declare global {\n\tinterface Window {\n\t\t_whop_sync_href_interval?: ReturnType<typeof setInterval>;\n\t}\n}\n\nexport function syncHref({\n\tonChange,\n}: { onChange: (req: { href: string }) => Promise<\"ok\"> }) {\n\tif (typeof window === \"undefined\") return;\n\n\tconst initialHref = window.location.href;\n\tonChange({ href: initialHref }).catch(() => null);\n\tlet lastKnown = initialHref;\n\n\twindow.addEventListener(\"popstate\", () => {\n\t\tconst { href } = window.location;\n\t\tonChange({ href }).catch(() => null);\n\t\tlastKnown = href;\n\t});\n\n\tif (window._whop_sync_href_interval) {\n\t\tclearInterval(window._whop_sync_href_interval);\n\t}\n\twindow._whop_sync_href_interval = setInterval(() => {\n\t\tconst { href } = window.location;\n\t\tif (href === lastKnown) return;\n\t\tonChange({ href }).catch(() => null);\n\t\tlastKnown = href;\n\t}, 250);\n}\n","import { appsServerSchema } from \"@/sdk/apps-server\";\nimport {\n\tgetReactNativePostMessage,\n\tgetSwiftPostMessage,\n} from \"@/sdk/mobile-app-postmessage\";\nimport { syncHref } from \"@/sdk/sync-href\";\nimport {\n\ttype ServerImplementation,\n\tcreateSDK as createPostmessageSdk,\n\tpostmessageTransport,\n} from \"@/sdk/transport\";\nimport { reactNativeClientTransport } from \"@/sdk/transport/postmessage\";\nimport type { frostedV2Theme } from \"@/sdk/utils\";\nimport { whopServerSchema } from \"@/sdk/whop-server\";\nimport type { z } from \"zod\";\n\nexport { appsServerSchema, whopServerSchema };\n\nfunction setColorTheme(theme: z.infer<typeof frostedV2Theme>) {\n\tdocument.documentElement.dispatchEvent(\n\t\tnew CustomEvent(\"frosted-ui:set-theme\", {\n\t\t\tdetail: theme,\n\t\t}),\n\t);\n}\n\n/**\n * Create an iframe SDK for a client app. This will communicate with the parent\n * window which is required to be whop.com.\n */\nexport function createSdk({\n\tonMessage = {},\n\tappId = process.env.NEXT_PUBLIC_WHOP_APP_ID,\n\toverrideParentOrigins,\n}: {\n\tonMessage?: ServerImplementation<typeof appsServerSchema, false>;\n\tappId?: string;\n\toverrideParentOrigins?: string[];\n}) {\n\tconst mobileWebView = getSwiftPostMessage() ?? getReactNativePostMessage();\n\n\tconst remoteWindow =\n\t\ttypeof window === \"undefined\" ? undefined : window.parent;\n\n\tif (!appId) {\n\t\tthrow new Error(\n\t\t\t\"[createSdk]: appId is required. Please provide an appId or set the NEXT_PUBLIC_WHOP_APP_ID environment variable.\",\n\t\t);\n\t}\n\n\tconst sdk = createPostmessageSdk({\n\t\tclientSchema: whopServerSchema,\n\t\tserverSchema: appsServerSchema,\n\t\tforceCompleteness: false,\n\t\tserverImplementation: onMessage,\n\t\tlocalAppId: appId,\n\t\tremoteAppId: \"app_whop\",\n\t\ttransport: mobileWebView\n\t\t\t? reactNativeClientTransport({\n\t\t\t\t\tpostMessage: mobileWebView,\n\t\t\t\t\ttargetOrigin: \"com.whop.whopapp\",\n\t\t\t\t})\n\t\t\t: postmessageTransport({\n\t\t\t\t\tremoteWindow,\n\t\t\t\t\ttargetOrigins: overrideParentOrigins ?? [\n\t\t\t\t\t\t\"https://whop.com\",\n\t\t\t\t\t\t\"https://dash.whop.com\",\n\t\t\t\t\t\t\"http://localhost:8003\",\n\t\t\t\t\t],\n\t\t\t\t}),\n\t\tserverComplete: true,\n\t\tserverMiddleware: [\n\t\t\t{\n\t\t\t\tonColorThemeChange: setColorTheme,\n\t\t\t},\n\t\t],\n\t\ttimeout: 15000,\n\t\ttimeouts: {\n\t\t\tinAppPurchase: 1000 * 60 * 60 * 24, // 24 hours, we never want this to timeout.\n\t\t\tonHrefChange: 500, // we don't really care about a response here.\n\t\t},\n\t});\n\n\tif (typeof window !== \"undefined\") {\n\t\tsdk\n\t\t\t.getColorTheme()\n\t\t\t.then(setColorTheme)\n\t\t\t.catch(() => null);\n\t\tdocument.documentElement.addEventListener(\"frosted-ui:mounted\", () => {\n\t\t\tsdk\n\t\t\t\t.getColorTheme()\n\t\t\t\t.then(setColorTheme)\n\t\t\t\t.catch(() => null);\n\t\t});\n\t}\n\n\tsyncHref({ onChange: sdk.onHrefChange });\n\n\treturn sdk;\n}\n\nexport type WhopIframeSdk = ReturnType<typeof createSdk>;\n"],"mappings":";;;;;;;;;;AAAO,SAAS,4BAA4B;AAC3C,QAAM,yBACL,OAAO,WAAW,eAClB,wBAAwB,UACxB,OAAO,OAAO,uBAAuB,YACrC,OAAO,sBACP,iBAAiB,OAAO,sBACxB,OAAO,OAAO,mBAAmB,gBAAgB,aAC9C,CAAC,SAAiB;AAClB,QACC,OAAO,WAAW,eAClB,wBAAwB,UACxB,OAAO,OAAO,uBAAuB,YACrC,OAAO,sBACP,iBAAiB,OAAO,sBACxB,OAAO,OAAO,mBAAmB,gBAAgB;AAEjD,cAAQ,oBAAoB,YAAY,IAAI;AAAA,EAC9C,IACC;AACJ,SAAO;AACR;AAEO,SAAS,sBAAsB;AACrC,QAAM,sBACL,OAAO,WAAW,eAClB,YAAY,UACZ,OAAO,OAAO,WAAW,YACzB,OAAO,WAAW,QAClB,qBAAqB,OAAO,UAC5B,OAAO,OAAO,OAAO,oBAAoB,YACzC,OAAO,OAAO,oBAAoB,QAClC,kBAAkB,OAAO,OAAO,mBAChC,OAAO,OAAO,OAAO,gBAAgB,iBAAiB,YACtD,OAAO,OAAO,gBAAgB,iBAAiB,QAC/C,iBAAiB,OAAO,OAAO,gBAAgB,eAC5C,OAAO,OAAO,gBAAgB,eAC9B;AAEJ,QAAM,mBAAmB,sBACtB,CAAC,SAAiB;AAClB,QAAI,OAAO,oBAAoB,gBAAgB,YAAY;AAC1D,0BAAoB,YAAY,IAAI;AAAA,IACrC;AAAA,EACD,IACC;AAEH,SAAO;AACR;;;AC1CO,SAAS,SAAS;AAAA,EACxB;AACD,GAA2D;AAC1D,MAAI,OAAO,WAAW,YAAa;AA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