@formbricks/js
Version:
Formbricks-js allows you to connect your index to Formbricks, display surveys and trigger events.
108 lines (107 loc) • 3.19 kB
JavaScript
//#region src/lib/load-formbricks.ts
var e = null, t = !1, n = [], r = async (e) => {
if (globalThis.formbricks) return {
ok: !0,
data: void 0
};
let t = `${e}/js/formbricks.umd.cjs`, n = document.querySelector(`script[src="${t}"]`);
n && n.remove();
let r = document.createElement("script");
r.type = "text/javascript", r.src = t, r.async = !0;
let i = new Promise((e) => {
let t = setTimeout(() => {
e({
ok: !1,
error: /* @__PURE__ */ Error("Formbricks SDK loading timed out")
});
}, 1e4);
r.onload = () => {
if (clearTimeout(t), globalThis.formbricks) {
e({
ok: !0,
data: void 0
});
return;
}
let n = 0, r = setInterval(() => {
globalThis.formbricks ? (clearInterval(r), e({
ok: !0,
data: void 0
})) : ++n >= 50 && (clearInterval(r), e({
ok: !1,
error: /* @__PURE__ */ Error("Formbricks SDK loaded but not available on globalThis")
}));
}, 10);
}, r.onerror = () => {
clearTimeout(t), e({
ok: !1,
error: /* @__PURE__ */ Error("Failed to load Formbricks SDK")
});
};
});
return document.head.appendChild(r), i;
}, i = (e) => {
let { appUrl: t, environmentId: n, workspaceId: r } = e;
if (!t) return console.error("🧱 Formbricks - Error: appUrl is required"), null;
if (!r && !n) return console.error("🧱 Formbricks - Error: workspaceId or environmentId is required"), null;
n && !r && console.warn("🧱 Formbricks - Warning: environmentId is deprecated and will be removed in a future version. Please use workspaceId instead.");
let i = t.endsWith("/") ? t.slice(0, -1) : t;
return r ? {
appUrl: i,
workspaceId: r,
...n ? { environmentId: n } : {}
} : {
appUrl: i,
environmentId: n
};
}, a = () => {
for (; n.length > 0;) {
let t = n.shift();
if (!t || !e) break;
if (typeof e[t.method] != "function") {
console.error(`🧱 Formbricks - Error: Method ${t.method} does not exist on formbricks`);
continue;
}
e[t.method](...t.args);
}
}, o = async (n) => {
if (t) {
console.warn("🧱 Formbricks - Warning: Formbricks is already initializing.");
return;
}
let o = i(n);
if (o) {
t = !0;
try {
let t = await r(o.appUrl), n = globalThis.formbricks;
if (!t.ok || !n) {
console.error("🧱 Formbricks - Error: Failed to load Formbricks SDK");
return;
}
await n.setup({ ...o }), e = n, a();
} catch (t) {
e = null, console.error("🧱 Formbricks - Error: setup failed", t);
} finally {
t = !1;
}
}
}, s = async (t, ...r) => {
e ? await e[t](...r) : (console.warn("🧱 Formbricks - Warning: Formbricks not initialized. This method will be queued and executed after initialization."), n.push({
method: t,
args: r
}));
}, c = {
setup: (e) => o(e),
setEmail: (e) => s("setEmail", e),
setAttribute: (e, t) => s("setAttribute", e, t),
setAttributes: (e) => s("setAttributes", e),
setLanguage: (e) => s("setLanguage", e),
setUserId: (e) => s("setUserId", e),
setNonce: (e) => s("setNonce", e),
track: (e, t) => s("track", e, t),
logout: () => s("logout"),
registerRouteChange: () => s("registerRouteChange")
};
//#endregion
export { c as default };
//# sourceMappingURL=index.js.map