From 34ebfe4fd881a5cbc2c79a4bf5b3ebf8dfc1012d Mon Sep 17 00:00:00 2001 From: Anshuman Pandey <54475686+pandeymangg@users.noreply.github.com> Date: Fri, 19 Apr 2024 19:02:27 +0530 Subject: [PATCH] fix: fixes js package proxy (#2490) --- packages/js/src/index.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/js/src/index.ts b/packages/js/src/index.ts index 2d8709f96c..ebf737d5e5 100644 --- a/packages/js/src/index.ts +++ b/packages/js/src/index.ts @@ -35,6 +35,19 @@ const formbricksProxyHandler: ProxyHandler = { get(_target, prop, _receiver) { return async (...args: any[]) => { if (!window.formbricks && !sdkLoadingPromise && !isErrorLoadingSdk) { + // This happens most likely when the user calls a method before `formbricks.init` + + if (prop !== "init") { + console.error("🧱 Formbricks - You need to call formbricks.init before calling any other method"); + return; + } + + // still need to check if the apiHost is passed + if (!args[0]) { + console.error("🧱 Formbricks - You need to pass the apiHost as the first argument"); + return; + } + const { apiHost } = args[0]; sdkLoadingPromise = loadSDK(apiHost).catch((error) => { console.error(`🧱 Formbricks - Error loading SDK: ${error}`);