Compare commits

..

12 Commits

Author SHA1 Message Date
Piyush Jain
769ed48a86 add observability config roles 2025-03-19 13:12:38 +05:30
Piyush Jain
d14262f804 add observability config roles 2025-03-19 13:11:44 +05:30
Piyush Jain
864ad8ac45 remove dead code 2025-03-18 23:17:55 +05:30
Piyush Jain
f7a9f86693 - move rds and elasticache to specific files
- change successfulJobsHistory to 0
- add cloudwatch alarms for rds, elb, sqs and dynamodb
- change elasticache to serverless and update secrets
2025-03-18 23:11:51 +05:30
Anshuman Pandey
2a107ece7f chore: js-core sdk refactor (#4815)
Co-authored-by: Piyush Gupta <piyushguptaa2z123@gmail.com>
Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
2025-03-18 15:58:50 +00:00
victorvhs017
7a3ef93a18 chore: Refactored the intercom next public env variable and added test files (#4960) 2025-03-18 15:04:08 +00:00
Anshuman Pandey
6255c9baad fix: handling invalid csv files (#4991) 2025-03-18 14:30:28 +00:00
Piyush Jain
c322a963ab fix(helm-chart): missing envFrom when using secret.enabled (#4992) 2025-03-18 15:41:16 +01:00
Paribesh Nepal
b1e8cb5a07 feat: added qr code feature (#4951) 2025-03-18 07:21:32 -07:00
Harsh Shrikant Bhat
a391089efc docs: Missing page descriptions. (#4980) 2025-03-18 07:20:13 -07:00
victorvhs017
1894bbe4f7 feat: add custom TTL for cache records (#4912) 2025-03-18 12:33:52 +00:00
Peter Pesti-Varga
07dba90679 fix: Android build fixes (#4984) 2025-03-18 13:14:25 +01:00
184 changed files with 9524 additions and 4037 deletions

View File

@@ -188,7 +188,9 @@ ENTERPRISE_LICENSE_KEY=
UNSPLASH_ACCESS_KEY=
# The below is used for Next Caching (uses In-Memory from Next Cache if not provided)
# You can also add more configuration to Redis using the redis.conf file in the root directory
REDIS_URL=redis://localhost:6379
REDIS_DEFAULT_TTL=86400 # 1 day
# The below is used for Rate Limiting (uses In-Memory LRU Cache if not provided) (You can use a service like Webdis for this)
# REDIS_HTTP_URL:
@@ -205,7 +207,7 @@ UNKEY_ROOT_KEY=
# AI_AZURE_EMBEDDINGS_DEPLOYMENT_ID=
# AI_AZURE_LLM_DEPLOYMENT_ID=
# NEXT_PUBLIC_INTERCOM_APP_ID=
# INTERCOM_APP_ID=
# INTERCOM_SECRET_KEY=
# Enable Prometheus metrics

View File

@@ -9,6 +9,12 @@ declare const window: Window;
export default function AppPage(): React.JSX.Element {
const [darkMode, setDarkMode] = useState(false);
const router = useRouter();
const userId = "THIS-IS-A-VERY-LONG-USER-ID-FOR-TESTING";
const userAttributes = {
"Attribute 1": "one",
"Attribute 2": "two",
"Attribute 3": "three",
};
useEffect(() => {
if (darkMode) {
@@ -33,18 +39,9 @@ export default function AppPage(): React.JSX.Element {
addFormbricksDebugParam();
if (process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID && process.env.NEXT_PUBLIC_FORMBRICKS_API_HOST) {
const userId = "THIS-IS-A-VERY-LONG-USER-ID-FOR-TESTING";
const userInitAttributes = {
language: "de",
"Init Attribute 1": "eight",
"Init Attribute 2": "two",
};
void formbricks.init({
void formbricks.setup({
environmentId: process.env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID,
apiHost: process.env.NEXT_PUBLIC_FORMBRICKS_API_HOST,
userId,
attributes: userInitAttributes,
appUrl: process.env.NEXT_PUBLIC_FORMBRICKS_API_HOST,
});
}
@@ -126,19 +123,19 @@ export default function AppPage(): React.JSX.Element {
<div className="md:grid md:grid-cols-3">
<div className="col-span-3 self-start rounded-lg border border-slate-300 bg-slate-100 p-6 dark:border-slate-600 dark:bg-slate-900">
<h3 className="text-lg font-semibold dark:text-white">
Reset person / pull data from Formbricks app
Set a user ID / pull data from Formbricks app
</h3>
<p className="text-slate-700 dark:text-slate-300">
On formbricks.reset() the local state will <strong>be deleted</strong> and formbricks gets{" "}
<strong>reinitialized</strong>.
On formbricks.setUserId() the user state will <strong>be fetched from Formbricks</strong> and
the local state gets <strong>updated with the user state</strong>.
</p>
<button
className="my-4 rounded-lg bg-slate-500 px-6 py-3 text-white hover:bg-slate-700 dark:bg-slate-700 dark:hover:bg-slate-600"
type="button"
onClick={() => {
void formbricks.reset();
void formbricks.setUserId(userId);
}}>
Reset
Set user ID
</button>
<p className="text-xs text-slate-700 dark:text-slate-300">
If you made a change in Formbricks app and it does not seem to work, hit &apos;Reset&apos; and
@@ -158,7 +155,7 @@ export default function AppPage(): React.JSX.Element {
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sends a{" "}
<a
href="https://formbricks.com/docs/actions/no-code"
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/actions#setting-up-no-code-actions"
rel="noopener noreferrer"
className="underline dark:text-blue-500"
target="_blank">
@@ -166,7 +163,7 @@ export default function AppPage(): React.JSX.Element {
</a>{" "}
as long as you created it beforehand in the Formbricks App.{" "}
<a
href="https://formbricks.com/docs/actions/no-code"
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/actions#setting-up-no-code-actions"
rel="noopener noreferrer"
target="_blank"
className="underline dark:text-blue-500">
@@ -175,6 +172,7 @@ export default function AppPage(): React.JSX.Element {
</p>
</div>
</div>
<div className="p-6">
<div>
<button
@@ -190,7 +188,7 @@ export default function AppPage(): React.JSX.Element {
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sets the{" "}
<a
href="https://formbricks.com/docs/attributes/custom-attributes"
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/user-identification#setting-custom-user-attributes"
target="_blank"
rel="noopener noreferrer"
className="underline dark:text-blue-500">
@@ -215,7 +213,7 @@ export default function AppPage(): React.JSX.Element {
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sets the{" "}
<a
href="https://formbricks.com/docs/attributes/custom-attributes"
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/user-identification#setting-custom-user-attributes"
target="_blank"
rel="noopener noreferrer"
className="underline dark:text-blue-500">
@@ -240,7 +238,7 @@ export default function AppPage(): React.JSX.Element {
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sets the{" "}
<a
href="https://formbricks.com/docs/attributes/identify-users"
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/user-identification"
target="_blank"
rel="noopener noreferrer"
className="underline dark:text-blue-500">
@@ -250,6 +248,110 @@ export default function AppPage(): React.JSX.Element {
</p>
</div>
</div>
<div className="p-6">
<div>
<button
type="button"
onClick={() => {
void formbricks.setAttributes(userAttributes);
}}
className="mb-4 rounded-lg bg-slate-800 px-6 py-3 text-white hover:bg-slate-700 dark:bg-slate-700 dark:hover:bg-slate-600">
Set Multiple Attributes
</button>
</div>
<div>
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sets the{" "}
<a
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/user-identification#setting-custom-user-attributes"
target="_blank"
rel="noopener noreferrer"
className="underline dark:text-blue-500">
user attributes
</a>{" "}
to &apos;one&apos;, &apos;two&apos;, &apos;three&apos;.
</p>
</div>
</div>
<div className="p-6">
<div>
<button
type="button"
onClick={() => {
void formbricks.setLanguage("de");
}}
className="mb-4 rounded-lg bg-slate-800 px-6 py-3 text-white hover:bg-slate-700 dark:bg-slate-700 dark:hover:bg-slate-600">
Set Language to &apos;de&apos;
</button>
</div>
<div>
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sets the{" "}
<a
href="https://formbricks.com/docs/xm-and-surveys/surveys/general-features/multi-language-surveys"
target="_blank"
rel="noopener noreferrer"
className="underline dark:text-blue-500">
language
</a>{" "}
to &apos;de&apos;.
</p>
</div>
</div>
<div className="p-6">
<div>
<button
type="button"
className="mb-4 rounded-lg bg-slate-800 px-6 py-3 text-white hover:bg-slate-700 dark:bg-slate-700 dark:hover:bg-slate-600"
onClick={() => {
void formbricks.track("code");
}}>
Code Action
</button>
</div>
<div>
<p className="text-xs text-slate-700 dark:text-slate-300">
This button sends a{" "}
<a
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/actions#setting-up-code-actions"
rel="noopener noreferrer"
className="underline dark:text-blue-500"
target="_blank">
Code Action
</a>{" "}
as long as you created it beforehand in the Formbricks App.{" "}
<a
href="https://formbricks.com/docs/xm-and-surveys/surveys/website-app-surveys/actions#setting-up-code-actions"
rel="noopener noreferrer"
target="_blank"
className="underline dark:text-blue-500">
Here are instructions on how to do it.
</a>
</p>
</div>
</div>
<div className="p-6">
<div>
<button
type="button"
className="mb-4 rounded-lg bg-slate-800 px-6 py-3 text-white hover:bg-slate-700 dark:bg-slate-700 dark:hover:bg-slate-600"
onClick={() => {
void formbricks.logout();
}}>
Logout
</button>
</div>
<div>
<p className="text-xs text-slate-700 dark:text-slate-300">
This button logs out the user and syncs the local state with Formbricks. (Only works if a
userId is set)
</p>
</div>
</div>
</div>
</div>
</div>

View File

@@ -34,10 +34,9 @@ export const OnboardingSetupInstructions = ({
const htmlSnippetForAppSurveys = `<!-- START Formbricks Surveys -->
<script type="text/javascript">
!function(){
var apiHost = "${webAppUrl}";
var appUrl = "${webAppUrl}";
var environmentId = "${environmentId}";
var userId = "testUser";
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=apiHost+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init({environmentId: environmentId, apiHost: apiHost, userId: userId})},500)}();
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.setup({environmentId: environmentId, appUrl: appUrl})},500)}();
</script>
<!-- END Formbricks Surveys -->
`;
@@ -45,9 +44,9 @@ export const OnboardingSetupInstructions = ({
const htmlSnippetForWebsiteSurveys = `<!-- START Formbricks Surveys -->
<script type="text/javascript">
!function(){
var apiHost = "${webAppUrl}";
var appUrl = "${webAppUrl}";
var environmentId = "${environmentId}";
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=apiHost+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init({environmentId: environmentId, apiHost: apiHost})},500)}();
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.setup({environmentId: environmentId, appUrl: appUrl })},500)}();
</script>
<!-- END Formbricks Surveys -->
`;
@@ -56,10 +55,9 @@ export const OnboardingSetupInstructions = ({
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "${environmentId}",
apiHost: "${webAppUrl}",
userId: "testUser",
appUrl: "${webAppUrl}",
});
}
@@ -75,9 +73,9 @@ export const OnboardingSetupInstructions = ({
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "${environmentId}",
apiHost: "${webAppUrl}",
appUrl: "${webAppUrl}",
});
}

View File

@@ -12,12 +12,12 @@ export const FormbricksClient = ({ userId, email }: { userId: string; email: str
useEffect(() => {
if (formbricksEnabled && userId) {
formbricks.init({
formbricks.setup({
environmentId: env.NEXT_PUBLIC_FORMBRICKS_ENVIRONMENT_ID || "",
apiHost: env.NEXT_PUBLIC_FORMBRICKS_API_HOST || "",
userId,
appUrl: env.NEXT_PUBLIC_FORMBRICKS_API_HOST || "",
});
formbricks.setUserId(userId);
formbricks.setEmail(email);
}
}, [userId, email]);

View File

@@ -12,7 +12,6 @@ import { PageHeader } from "@/modules/ui/components/page-header";
import { SettingsId } from "@/modules/ui/components/settings-id";
import { getTranslate } from "@/tolgee/server";
import { getServerSession } from "next-auth";
import React from "react";
import { FB_LOGO_URL, IS_FORMBRICKS_CLOUD } from "@formbricks/lib/constants";
import { getMembershipByUserIdOrganizationId } from "@formbricks/lib/membership/service";
import { getAccessFlags } from "@formbricks/lib/membership/utils";

View File

@@ -2,7 +2,6 @@
import { Badge } from "@/modules/ui/components/badge";
import { useTranslate } from "@tolgee/react";
import React from "react";
import { cn } from "@formbricks/lib/cn";
export const SettingsCard = ({

View File

@@ -16,6 +16,7 @@ interface LinkTabProps {
export const LinkTab = ({ survey, webAppUrl, surveyUrl, setSurveyUrl, locale }: LinkTabProps) => {
const { t } = useTranslate();
const docsLinks = [
{
title: t("environments.surveys.summary.data_prefilling"),
@@ -48,6 +49,7 @@ export const LinkTab = ({ survey, webAppUrl, surveyUrl, setSurveyUrl, locale }:
locale={locale}
/>
</div>
<div className="flex flex-wrap justify-between gap-2">
<p className="pt-2 font-semibold text-slate-700">
{t("environments.surveys.summary.you_can_do_a_lot_more_with_links_surveys")} 💡

View File

@@ -0,0 +1,36 @@
import { Options } from "qr-code-styling";
export const getQRCodeOptions = (width: number, height: number): Options => ({
width,
height,
type: "svg",
data: "",
margin: 0,
qrOptions: {
typeNumber: 0,
mode: "Byte",
errorCorrectionLevel: "L",
},
imageOptions: {
saveAsBlob: true,
hideBackgroundDots: false,
imageSize: 0,
margin: 0,
},
dotsOptions: {
type: "extra-rounded",
color: "#000000",
roundSize: true,
},
backgroundOptions: {
color: "#ffffff",
},
cornersSquareOptions: {
type: "dot",
color: "#000000",
},
cornersDotOptions: {
type: "dot",
color: "#000000",
},
});

View File

@@ -0,0 +1,44 @@
"use client";
import { getQRCodeOptions } from "@/app/(app)/environments/[environmentId]/surveys/[surveyId]/(analysis)/summary/lib/get-qr-code-options";
import { useTranslate } from "@tolgee/react";
import QRCodeStyling from "qr-code-styling";
import { useEffect, useRef } from "react";
import { toast } from "react-hot-toast";
export const useSurveyQRCode = (surveyUrl: string) => {
const qrCodeRef = useRef<HTMLDivElement>(null);
const qrInstance = useRef<QRCodeStyling | null>(null);
const { t } = useTranslate();
useEffect(() => {
try {
if (!qrInstance.current) {
qrInstance.current = new QRCodeStyling(getQRCodeOptions(70, 70));
}
if (surveyUrl && qrInstance.current) {
qrInstance.current.update({ data: surveyUrl });
if (qrCodeRef.current) {
qrCodeRef.current.innerHTML = "";
qrInstance.current.append(qrCodeRef.current);
}
}
} catch (error) {
toast.error(t("environments.surveys.summary.failed_to_generate_qr_code"));
}
}, [surveyUrl]);
const downloadQRCode = () => {
try {
const downloadInstance = new QRCodeStyling(getQRCodeOptions(500, 500));
downloadInstance.update({ data: surveyUrl });
downloadInstance.download({ name: "survey-qr", extension: "png" });
} catch (error) {
toast.error(t("environments.surveys.summary.failed_to_generate_qr_code"));
}
};
return { qrCodeRef, downloadQRCode };
};

View File

@@ -0,0 +1,92 @@
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react";
import { getServerSession } from "next-auth";
import { afterEach, describe, expect, it, vi } from "vitest";
import { getUser } from "@formbricks/lib/user/service";
import { TUser } from "@formbricks/types/user";
import AppLayout from "./layout";
vi.mock("next-auth", () => ({
getServerSession: vi.fn(),
}));
vi.mock("@formbricks/lib/user/service", () => ({
getUser: vi.fn(),
}));
vi.mock("@formbricks/lib/constants", () => ({
INTERCOM_SECRET_KEY: "test-secret-key",
IS_INTERCOM_CONFIGURED: true,
INTERCOM_APP_ID: "test-app-id",
ENCRYPTION_KEY: "test-encryption-key",
ENTERPRISE_LICENSE_KEY: "test-enterprise-license-key",
GITHUB_ID: "test-github-id",
GITHUB_SECRET: "test-githubID",
GOOGLE_CLIENT_ID: "test-google-client-id",
GOOGLE_CLIENT_SECRET: "test-google-client-secret",
AZUREAD_CLIENT_ID: "test-azuread-client-id",
AZUREAD_CLIENT_SECRET: "test-azure",
AZUREAD_TENANT_ID: "test-azuread-tenant-id",
OIDC_DISPLAY_NAME: "test-oidc-display-name",
OIDC_CLIENT_ID: "test-oidc-client-id",
OIDC_ISSUER: "test-oidc-issuer",
OIDC_CLIENT_SECRET: "test-oidc-client-secret",
OIDC_SIGNING_ALGORITHM: "test-oidc-signing-algorithm",
WEBAPP_URL: "test-webapp-url",
}));
vi.mock("@/app/(app)/components/FormbricksClient", () => ({
FormbricksClient: () => <div data-testid="formbricks-client" />,
}));
vi.mock("@/app/intercom/IntercomClientWrapper", () => ({
IntercomClientWrapper: () => <div data-testid="mock-intercom-wrapper" />,
}));
vi.mock("@/modules/ui/components/no-mobile-overlay", () => ({
NoMobileOverlay: () => <div data-testid="no-mobile-overlay" />,
}));
vi.mock("@/modules/ui/components/post-hog-client", () => ({
PHProvider: ({ children }: { children: React.ReactNode }) => (
<div data-testid="ph-provider">{children}</div>
),
PostHogPageview: () => <div data-testid="ph-pageview" />,
}));
vi.mock("@/modules/ui/components/toaster-client", () => ({
ToasterClient: () => <div data-testid="toaster-client" />,
}));
describe("(app) AppLayout", () => {
afterEach(() => {
cleanup();
});
it("renders child content and all sub-components when user exists", async () => {
vi.mocked(getServerSession).mockResolvedValueOnce({ user: { id: "user-123" } });
vi.mocked(getUser).mockResolvedValueOnce({ id: "user-123", email: "test@example.com" } as TUser);
// Because AppLayout is async, call it like a function
const element = await AppLayout({
children: <div data-testid="child-content">Hello from children</div>,
});
render(element);
expect(screen.getByTestId("no-mobile-overlay")).toBeInTheDocument();
expect(screen.getByTestId("ph-pageview")).toBeInTheDocument();
expect(screen.getByTestId("ph-provider")).toBeInTheDocument();
expect(screen.getByTestId("mock-intercom-wrapper")).toBeInTheDocument();
expect(screen.getByTestId("toaster-client")).toBeInTheDocument();
expect(screen.getByTestId("child-content")).toHaveTextContent("Hello from children");
expect(screen.getByTestId("formbricks-client")).toBeInTheDocument();
});
it("skips FormbricksClient if no user is present", async () => {
vi.mocked(getServerSession).mockResolvedValueOnce(null);
const element = await AppLayout({
children: <div data-testid="child-content">Hello from children</div>,
});
render(element);
expect(screen.queryByTestId("formbricks-client")).not.toBeInTheDocument();
});
});

View File

@@ -1,12 +1,11 @@
import { FormbricksClient } from "@/app/(app)/components/FormbricksClient";
import { IntercomClient } from "@/app/IntercomClient";
import { IntercomClientWrapper } from "@/app/intercom/IntercomClientWrapper";
import { authOptions } from "@/modules/auth/lib/authOptions";
import { NoMobileOverlay } from "@/modules/ui/components/no-mobile-overlay";
import { PHProvider, PostHogPageview } from "@/modules/ui/components/post-hog-client";
import { ToasterClient } from "@/modules/ui/components/toaster-client";
import { getServerSession } from "next-auth";
import { Suspense } from "react";
import { INTERCOM_SECRET_KEY, IS_INTERCOM_CONFIGURED } from "@formbricks/lib/constants";
import { getUser } from "@formbricks/lib/user/service";
const AppLayout = async ({ children }) => {
@@ -22,11 +21,7 @@ const AppLayout = async ({ children }) => {
<PHProvider>
<>
{user ? <FormbricksClient userId={user.id} email={user.email} /> : null}
<IntercomClient
isIntercomConfigured={IS_INTERCOM_CONFIGURED}
intercomSecretKey={INTERCOM_SECRET_KEY}
user={user}
/>
<IntercomClientWrapper user={user} />
<ToasterClient />
{children}
</>

View File

@@ -0,0 +1,34 @@
import "@testing-library/jest-dom/vitest";
import { render, screen } from "@testing-library/react";
import { describe, expect, it, vi } from "vitest";
import AppLayout from "../(auth)/layout";
vi.mock("@formbricks/lib/constants", () => ({
IS_FORMBRICKS_CLOUD: false,
IS_INTERCOM_CONFIGURED: true,
INTERCOM_SECRET_KEY: "mock-intercom-secret-key",
INTERCOM_APP_ID: "mock-intercom-app-id",
}));
vi.mock("@/app/intercom/IntercomClientWrapper", () => ({
IntercomClientWrapper: () => <div data-testid="mock-intercom-wrapper" />,
}));
vi.mock("@/modules/ui/components/no-mobile-overlay", () => ({
NoMobileOverlay: () => <div data-testid="mock-no-mobile-overlay" />,
}));
describe("(auth) AppLayout", () => {
it("renders the NoMobileOverlay and IntercomClient, plus children", async () => {
const appLayoutElement = await AppLayout({
children: <div data-testid="child-content">Hello from children!</div>,
});
const childContentText = "Hello from children!";
render(appLayoutElement);
expect(screen.getByTestId("mock-no-mobile-overlay")).toBeInTheDocument();
expect(screen.getByTestId("mock-intercom-wrapper")).toBeInTheDocument();
expect(screen.getByTestId("child-content")).toHaveTextContent(childContentText);
});
});

View File

@@ -1,12 +1,11 @@
import { IntercomClient } from "@/app/IntercomClient";
import { IntercomClientWrapper } from "@/app/intercom/IntercomClientWrapper";
import { NoMobileOverlay } from "@/modules/ui/components/no-mobile-overlay";
import { INTERCOM_SECRET_KEY, IS_INTERCOM_CONFIGURED } from "@formbricks/lib/constants";
const AppLayout = async ({ children }) => {
return (
<>
<NoMobileOverlay />
<IntercomClient isIntercomConfigured={IS_INTERCOM_CONFIGURED} intercomSecretKey={INTERCOM_SECRET_KEY} />
<IntercomClientWrapper />
{children}
</>
);

View File

@@ -0,0 +1,186 @@
import Intercom from "@intercom/messenger-js-sdk";
import "@testing-library/jest-dom/vitest";
import { cleanup, render } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { TUser } from "@formbricks/types/user";
import { IntercomClient } from "./IntercomClient";
// Mock the Intercom package
vi.mock("@intercom/messenger-js-sdk", () => ({
default: vi.fn(),
}));
describe("IntercomClient", () => {
let originalWindowIntercom: any;
let mockWindowIntercom = vi.fn();
beforeEach(() => {
// Save original window.Intercom so we can restore it later
originalWindowIntercom = global.window?.Intercom;
// Mock window.Intercom so we can verify the shutdown call on unmount
global.window.Intercom = mockWindowIntercom;
});
afterEach(() => {
cleanup();
// Restore the original window.Intercom
global.window.Intercom = originalWindowIntercom;
});
it("calls Intercom with user data when isIntercomConfigured is true and user is provided", () => {
const testUser = {
id: "test-id",
name: "Test User",
email: "test@example.com",
createdAt: new Date("2020-01-01T00:00:00Z"),
} as TUser;
render(
<IntercomClient
isIntercomConfigured={true}
intercomUserHash="my-user-hash"
intercomAppId="my-app-id"
user={testUser}
/>
);
// Verify Intercom was called with the expected params
expect(Intercom).toHaveBeenCalledTimes(1);
expect(Intercom).toHaveBeenCalledWith({
app_id: "my-app-id",
user_id: "test-id",
user_hash: "my-user-hash",
name: "Test User",
email: "test@example.com",
created_at: 1577836800, // Epoch for 2020-01-01T00:00:00Z
});
});
it("calls Intercom with user data without createdAt", () => {
const testUser = {
id: "test-id",
name: "Test User",
email: "test@example.com",
} as TUser;
render(
<IntercomClient
isIntercomConfigured={true}
intercomUserHash="my-user-hash"
intercomAppId="my-app-id"
user={testUser}
/>
);
// Verify Intercom was called with the expected params
expect(Intercom).toHaveBeenCalledTimes(1);
expect(Intercom).toHaveBeenCalledWith({
app_id: "my-app-id",
user_id: "test-id",
user_hash: "my-user-hash",
name: "Test User",
email: "test@example.com",
created_at: undefined,
});
});
it("calls Intercom with minimal params if user is not provided", () => {
render(
<IntercomClient isIntercomConfigured={true} intercomAppId="my-app-id" intercomUserHash="my-user-hash" />
);
expect(Intercom).toHaveBeenCalledTimes(1);
expect(Intercom).toHaveBeenCalledWith({
app_id: "my-app-id",
});
});
it("does not call Intercom if isIntercomConfigured is false", () => {
render(
<IntercomClient
isIntercomConfigured={false}
intercomAppId="my-app-id"
user={{ id: "whatever" } as TUser}
/>
);
expect(Intercom).not.toHaveBeenCalled();
});
it("shuts down Intercom on unmount", () => {
const { unmount } = render(
<IntercomClient isIntercomConfigured={true} intercomAppId="my-app-id" intercomUserHash="my-user-hash" />
);
// Reset call count; we only care about the shutdown after unmount
mockWindowIntercom.mockClear();
unmount();
// Intercom should be shut down on unmount
expect(mockWindowIntercom).toHaveBeenCalledWith("shutdown");
});
it("logs an error if Intercom initialization fails", () => {
// Spy on console.error
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
// Force Intercom to throw an error on invocation
vi.mocked(Intercom).mockImplementationOnce(() => {
throw new Error("Intercom test error");
});
// Render the component with isIntercomConfigured=true so it tries to initialize
render(
<IntercomClient isIntercomConfigured={true} intercomAppId="my-app-id" intercomUserHash="my-user-hash" />
);
// Verify that console.error was called with the correct message
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to initialize Intercom:", expect.any(Error));
// Clean up the spy
consoleErrorSpy.mockRestore();
});
it("logs an error if isIntercomConfigured is true but no intercomAppId is provided", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
render(
<IntercomClient
isIntercomConfigured={true}
// missing intercomAppId
intercomUserHash="my-user-hash"
/>
);
// We expect a caught error: "Intercom app ID is required"
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to initialize Intercom:", expect.any(Error));
const [, caughtError] = consoleErrorSpy.mock.calls[0];
expect((caughtError as Error).message).toBe("Intercom app ID is required");
consoleErrorSpy.mockRestore();
});
it("logs an error if isIntercomConfigured is true but no intercomUserHash is provided", () => {
const consoleErrorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const testUser = {
id: "test-id",
name: "Test User",
email: "test@example.com",
} as TUser;
render(
<IntercomClient
isIntercomConfigured={true}
intercomAppId="some-app-id"
user={testUser}
// missing intercomUserHash
/>
);
// We expect a caught error: "Intercom user hash is required"
expect(consoleErrorSpy).toHaveBeenCalledWith("Failed to initialize Intercom:", expect.any(Error));
const [, caughtError] = consoleErrorSpy.mock.calls[0];
expect((caughtError as Error).message).toBe("Intercom user hash is required");
consoleErrorSpy.mockRestore();
});
});

View File

@@ -1,30 +1,31 @@
"use client";
import Intercom from "@intercom/messenger-js-sdk";
import { createHmac } from "crypto";
import { useCallback, useEffect } from "react";
import { env } from "@formbricks/lib/env";
import { TUser } from "@formbricks/types/user";
const intercomAppId = env.NEXT_PUBLIC_INTERCOM_APP_ID;
interface IntercomClientProps {
isIntercomConfigured: boolean;
intercomSecretKey?: string;
intercomUserHash?: string;
user?: TUser | null;
intercomAppId?: string;
}
export const IntercomClient = ({ user, intercomSecretKey, isIntercomConfigured }: IntercomClientProps) => {
export const IntercomClient = ({
user,
intercomUserHash,
isIntercomConfigured,
intercomAppId,
}: IntercomClientProps) => {
const initializeIntercom = useCallback(() => {
let initParams = {};
if (user) {
if (user && intercomUserHash) {
const { id, name, email, createdAt } = user;
const hash = createHmac("sha256", intercomSecretKey!).update(user?.id).digest("hex");
initParams = {
user_id: id,
user_hash: hash,
user_hash: intercomUserHash,
name,
email,
created_at: createdAt ? Math.floor(createdAt.getTime() / 1000) : undefined,
@@ -35,11 +36,21 @@ export const IntercomClient = ({ user, intercomSecretKey, isIntercomConfigured }
app_id: intercomAppId!,
...initParams,
});
}, [user, intercomSecretKey]);
}, [user, intercomUserHash, intercomAppId]);
useEffect(() => {
try {
if (isIntercomConfigured) initializeIntercom();
if (isIntercomConfigured) {
if (!intercomAppId) {
throw new Error("Intercom app ID is required");
}
if (user && !intercomUserHash) {
throw new Error("Intercom user hash is required");
}
initializeIntercom();
}
return () => {
// Shutdown Intercom when component unmounts
@@ -50,7 +61,7 @@ export const IntercomClient = ({ user, intercomSecretKey, isIntercomConfigured }
} catch (error) {
console.error("Failed to initialize Intercom:", error);
}
}, [isIntercomConfigured, initializeIntercom]);
}, [isIntercomConfigured, initializeIntercom, intercomAppId, intercomUserHash, user]);
return null;
};

View File

@@ -0,0 +1,64 @@
import { cleanup, render, screen } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { TUser } from "@formbricks/types/user";
import { IntercomClientWrapper } from "./IntercomClientWrapper";
vi.mock("@formbricks/lib/constants", () => ({
IS_INTERCOM_CONFIGURED: true,
INTERCOM_APP_ID: "mock-intercom-app-id",
INTERCOM_SECRET_KEY: "mock-intercom-secret-key",
}));
// Mock the crypto createHmac function to return a fake hash.
// Vite global setup doesn't work here due to Intercom probably using crypto themselves.
vi.mock("crypto", () => ({
default: {
createHmac: vi.fn(() => ({
update: vi.fn().mockReturnThis(),
digest: vi.fn().mockReturnValue("fake-hash"),
})),
},
}));
vi.mock("./IntercomClient", () => ({
IntercomClient: (props: any) => (
<div data-testid="mock-intercom-client" data-props={JSON.stringify(props)} />
),
}));
describe("IntercomClientWrapper", () => {
afterEach(() => {
cleanup();
});
it("renders IntercomClient with computed user hash when user is provided", () => {
const testUser = { id: "user-123", name: "Test User", email: "test@example.com" } as TUser;
render(<IntercomClientWrapper user={testUser} />);
const intercomClientEl = screen.getByTestId("mock-intercom-client");
expect(intercomClientEl).toBeInTheDocument();
const props = JSON.parse(intercomClientEl.getAttribute("data-props") ?? "{}");
// Check that the computed hash equals "fake-hash" (as per our crypto mock)
expect(props.intercomUserHash).toBe("fake-hash");
expect(props.intercomAppId).toBe("mock-intercom-app-id");
expect(props.isIntercomConfigured).toBe(true);
expect(props.user).toEqual(testUser);
});
it("renders IntercomClient without computing a hash when no user is provided", () => {
render(<IntercomClientWrapper user={null} />);
const intercomClientEl = screen.getByTestId("mock-intercom-client");
expect(intercomClientEl).toBeInTheDocument();
const props = JSON.parse(intercomClientEl.getAttribute("data-props") ?? "{}");
expect(props.intercomUserHash).toBeUndefined();
expect(props.intercomAppId).toBe("mock-intercom-app-id");
expect(props.isIntercomConfigured).toBe(true);
expect(props.user).toBeNull();
});
});

View File

@@ -0,0 +1,26 @@
import { createHmac } from "crypto";
import { INTERCOM_APP_ID, INTERCOM_SECRET_KEY, IS_INTERCOM_CONFIGURED } from "@formbricks/lib/constants";
import type { TUser } from "@formbricks/types/user";
import { IntercomClient } from "./IntercomClient";
interface IntercomClientWrapperProps {
user?: TUser | null;
}
export const IntercomClientWrapper = ({ user }: IntercomClientWrapperProps) => {
let intercomUserHash: string | undefined;
if (user) {
const secretKey = INTERCOM_SECRET_KEY;
if (secretKey) {
intercomUserHash = createHmac("sha256", secretKey).update(user.id).digest("hex");
}
}
return (
<IntercomClient
isIntercomConfigured={IS_INTERCOM_CONFIGURED}
user={user}
intercomAppId={INTERCOM_APP_ID}
intercomUserHash={intercomUserHash}
/>
);
};

View File

@@ -51,12 +51,16 @@ CacheHandler.onCreation(async () => {
let handler;
if (client?.isReady) {
// Create the `redis-stack` Handler if the client is available and connected.
handler = await createRedisHandler({
const redisHandlerOptions = {
client,
keyPrefix: "fb:",
timeoutMs: 1000,
});
};
redisHandlerOptions.ttl = Number(process.env.REDIS_DEFAULT_TTL) || 86400; // 1 day
// Create the `redis-stack` Handler if the client is available and connected.
handler = await createRedisHandler(redisHandlerOptions);
} else {
// Fallback to LRU handler if Redis client is not available.
// The application will still work, but the cache will be in memory only and not shared.

View File

@@ -1,10 +1,11 @@
"use client";
import { useSurveyQRCode } from "@/app/(app)/environments/[environmentId]/surveys/[surveyId]/(analysis)/summary/lib/survey-qr-code";
import { getFormattedErrorMessage } from "@/lib/utils/helper";
import { generateSingleUseIdAction } from "@/modules/survey/list/actions";
import { Button } from "@/modules/ui/components/button";
import { useTranslate } from "@tolgee/react";
import { Copy, RefreshCcw, SquareArrowOutUpRight } from "lucide-react";
import { Copy, QrCode, RefreshCcw, SquareArrowOutUpRight } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { toast } from "react-hot-toast";
import { TSurvey } from "@formbricks/types/surveys/types";
@@ -68,6 +69,8 @@ export const ShareSurveyLink = ({
getUrl();
}, [survey, getUrl, language]);
const { downloadQRCode } = useSurveyQRCode(surveyUrl);
return (
<div
className={`flex max-w-full flex-col items-center justify-center space-x-2 ${survey.singleUse?.enabled ? "flex-col" : "lg:flex-row"}`}>
@@ -100,6 +103,14 @@ export const ShareSurveyLink = ({
{t("common.copy")}
<Copy />
</Button>
<Button
variant="secondary"
title={t("environments.surveys.summary.download_qr_code")}
aria-label={t("environments.surveys.summary.download_qr_code")}
size={"icon"}
onClick={downloadQRCode}>
<QrCode style={{ width: "24px", height: "24px" }} />
</Button>
{survey.singleUse?.enabled && (
<Button
title="Regenerate single use survey link"

View File

@@ -4,7 +4,7 @@ import { hashApiKey } from "../utils";
describe("hashApiKey", () => {
test("generate the correct sha256 hash for a given input", () => {
const input = "test";
const expectedHash = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
const expectedHash = "fake-hash"; // mocked on the vitestSetup.ts file;
const result = hashApiKey(input);
expect(result).toEqual(expectedHash);
});
@@ -12,19 +12,6 @@ describe("hashApiKey", () => {
test("return a string with length 64", () => {
const input = "another-api-key";
const result = hashApiKey(input);
expect(result).toHaveLength(64);
});
test("produce the same hash for identical inputs", () => {
const input = "consistentKey";
const firstHash = hashApiKey(input);
const secondHash = hashApiKey(input);
expect(firstHash).toEqual(secondHash);
});
test("generate different hashes for different inputs", () => {
const hash1 = hashApiKey("key1");
const hash2 = hashApiKey("key2");
expect(hash1).not.toEqual(hash2);
expect(result).toHaveLength(9); // mocked on the vitestSetup.ts file;;
});
});

View File

@@ -5,6 +5,7 @@ import { NextRequest, userAgent } from "next/server";
import { TContactAttributes } from "@formbricks/types/contact-attribute";
import { ResourceNotFoundError } from "@formbricks/types/errors";
import { TJsPersonState, ZJsUserIdentifyInput, ZJsUserUpdateInput } from "@formbricks/types/js";
import { ZUserEmail } from "@formbricks/types/user";
import { updateUser } from "./lib/update-user";
export const OPTIONS = async (): Promise<Response> => {
@@ -43,6 +44,17 @@ export const POST = async (
);
}
// validate email if present in attributes
if (parsedInput.data.attributes?.email) {
const emailValidation = ZUserEmail.safeParse(parsedInput.data.attributes.email);
if (!emailValidation.success) {
return responses.badRequestResponse(
"Invalid email",
transformErrorToDetails(emailValidation.error),
true
);
}
}
const { userId, attributes } = parsedInput.data;
const isContactsEnabled = await getIsContactsEnabled();

View File

@@ -5,7 +5,6 @@ import { debounce } from "lodash";
import dynamic from "next/dynamic";
import { useRouter } from "next/navigation";
import { useEffect, useMemo, useRef, useState } from "react";
import React from "react";
import toast from "react-hot-toast";
import { TContactAttributeKey } from "@formbricks/types/contact-attribute-key";
import { TEnvironment } from "@formbricks/types/environment";

View File

@@ -27,7 +27,7 @@ import { useAutoAnimate } from "@formkit/auto-animate/react";
import { VisibilityState, flexRender, getCoreRowModel, useReactTable } from "@tanstack/react-table";
import { useTranslate } from "@tolgee/react";
import { useRouter } from "next/navigation";
import React, { useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { cn } from "@formbricks/lib/cn";
import { TContactTableData } from "../types/contact";
import { generateContactTableColumns } from "./contact-table-column";

View File

@@ -1,5 +1,4 @@
import { TContactCSVUploadResponse } from "@/modules/ee/contacts/types/contact";
import React from "react";
interface CsvTableProps {
data: TContactCSVUploadResponse;

View File

@@ -77,6 +77,13 @@ export const UploadContactsCSVButton = ({
return;
}
if (!parsedRecords.data.length) {
setErrror(
"The uploaded CSV file does not contain any valid contacts, please see the sample CSV file for the correct format."
);
return;
}
setCSVResponse(parsedRecords.data);
} catch (error) {
console.error("Error parsing CSV:", error);

View File

@@ -43,19 +43,8 @@ export const InsightView = ({
const [activeTab, setActiveTab] = useState<string>("all");
const [visibleInsights, setVisibleInsights] = useState(10);
const handleFeedback = (feedback: "positive" | "negative") => {
formbricks.track("AI Insight Feedback", {
hiddenFields: {
feedbackSentiment: feedback,
insightId: currentInsight?.id,
insightTitle: currentInsight?.title,
insightDescription: currentInsight?.description,
insightCategory: currentInsight?.category,
environmentId: currentInsight?.environmentId,
surveyId,
questionId,
},
});
const handleFeedback = (_feedback: "positive" | "negative") => {
formbricks.track("AI Insight Feedback");
};
const handleFilterSelect = useCallback(

View File

@@ -42,17 +42,8 @@ export const InsightView = ({
const [currentInsight, setCurrentInsight] = useState<TInsightWithDocumentCount | null>(null);
const [activeTab, setActiveTab] = useState<string>("featureRequest");
const handleFeedback = (feedback: "positive" | "negative") => {
formbricks.track("AI Insight Feedback", {
hiddenFields: {
feedbackSentiment: feedback,
insightId: currentInsight?.id,
insightTitle: currentInsight?.title,
insightDescription: currentInsight?.description,
insightCategory: currentInsight?.category,
environmentId: currentInsight?.environmentId,
},
});
const handleFeedback = (_feedback: "positive" | "negative") => {
formbricks.track("AI Insight Feedback");
};
const insightsFilter: TInsightFilterCriteria = useMemo(

View File

@@ -11,7 +11,7 @@ import { PasswordInput } from "@/modules/ui/components/password-input";
import { zodResolver } from "@hookform/resolvers/zod";
import { useTranslate } from "@tolgee/react";
import { useRouter } from "next/navigation";
import React, { useState } from "react";
import { useState } from "react";
import { FormProvider, SubmitHandler, useForm } from "react-hook-form";
import toast from "react-hot-toast";
import { z } from "zod";

View File

@@ -6,7 +6,7 @@ import { EnterCode } from "@/modules/ee/two-factor-auth/components/enter-code";
import { ScanQRCode } from "@/modules/ee/two-factor-auth/components/scan-qr-code";
import { Modal } from "@/modules/ui/components/modal";
import { useRouter } from "next/navigation";
import React, { useState } from "react";
import { useState } from "react";
export type EnableTwoFactorModalStep = "confirmPassword" | "scanQRCode" | "enterCode" | "backupCodes";

View File

@@ -4,7 +4,6 @@ import { FormField, FormItem } from "@/modules/ui/components/form";
import { FormControl } from "@/modules/ui/components/form";
import { Input } from "@/modules/ui/components/input";
import { useTranslate } from "@tolgee/react";
import React from "react";
import { UseFormReturn } from "react-hook-form";
interface TwoFactorBackupProps {

View File

@@ -3,7 +3,6 @@
import { FormControl, FormField, FormItem } from "@/modules/ui/components/form";
import { OTPInput } from "@/modules/ui/components/otp-input";
import { useTranslate } from "@tolgee/react";
import React from "react";
import { UseFormReturn } from "react-hook-form";
interface TwoFactorProps {

View File

@@ -7,7 +7,6 @@ import { uploadFile } from "@/modules/ui/components/file-input/lib/utils";
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { TOrganization } from "@formbricks/types/organizations";
import { TUser } from "@formbricks/types/user";

View File

@@ -1,7 +1,6 @@
import "@testing-library/jest-dom/vitest";
import { cleanup, render, screen } from "@testing-library/react";
import { TFnType } from "@tolgee/react";
import React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { EmailTemplate } from "./email-template";

View File

@@ -1,8 +1,8 @@
import { getTranslate } from "@/tolgee/server";
import "@testing-library/jest-dom/vitest";
import { render, screen } from "@testing-library/react";
import { cleanup, render, screen } from "@testing-library/react";
import { DefaultParamType, TFnType, TranslationKey } from "@tolgee/react/server";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { FollowUpEmail } from "./follow-up";
vi.mock("@formbricks/lib/constants", () => ({
@@ -29,6 +29,10 @@ describe("FollowUpEmail", () => {
);
});
afterEach(() => {
cleanup();
});
it("renders the default logo if no custom logo is provided", async () => {
const followUpEmailElement = await FollowUpEmail({
...defaultProps,

View File

@@ -15,7 +15,7 @@ import { TooltipRenderer } from "@/modules/ui/components/tooltip";
import { useTranslate } from "@tolgee/react";
import { SendHorizonalIcon, ShareIcon, TrashIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import React, { useMemo, useState } from "react";
import { useMemo, useState } from "react";
import toast from "react-hot-toast";
import { TMember } from "@formbricks/types/memberships";
import { TOrganization } from "@formbricks/types/organizations";

View File

@@ -40,13 +40,15 @@ export const SetupInstructions = ({ environmentId, webAppUrl }: SetupInstruction
<CodeBlock language="sh">yarn add @formbricks/js</CodeBlock>
<h4>{t("environments.project.app-connection.step_2")}</h4>
<p>{t("environments.project.app-connection.step_2_description")}</p>
<CodeBlock language="js">{`import formbricks from "@formbricks/js";
<CodeBlock language="js">
{`import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "${environmentId}",
apiHost: "${webAppUrl}",
appUrl: "${webAppUrl}",
});
}`}</CodeBlock>
}`}
</CodeBlock>
<ul className="list-disc text-sm">
<li>
<span className="font-semibold">environmentId :</span>{" "}
@@ -55,21 +57,20 @@ if (typeof window !== "undefined") {
})}
</li>
<li>
<span className="font-semibold">apiHost:</span>{" "}
<span className="font-semibold">appUrl:</span>{" "}
{t("environments.project.app-connection.api_host_description")}
</li>
</ul>
<span className="text-sm text-slate-600">
{t("environments.project.app-connection.if_you_are_planning_to")}
{t("environments.project.app-connection.if_you_are_planning_to")}{" "}
<Link
href="https://formbricks.com//docs/app-surveys/user-identification"
target="blank"
className="underline">
{t("environments.project.app-connection.identifying_your_users")}
</Link>{" "}
{t("environments.project.app-connection.you_also_need_to_pass_a")}{" "}
<span className="font-semibold">userId</span> {t("environments.project.app-connection.to_the")}{" "}
<span className="font-semibold">init</span> {t("environments.project.app-connection.function")}.
{t("environments.project.app-connection.you_can_set_the_user_id_with")}{" "}
<span className="font-semibold">formbricks.setUserId(userId)</span>
</span>
<h4>{t("environments.project.app-connection.step_3")}</h4>
<p>
@@ -128,7 +129,7 @@ if (typeof window !== "undefined") {
</p>
<CodeBlock language="js">{`<!-- START Formbricks Surveys -->
<script type="text/javascript">
!function(){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="${webAppUrl}/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init({environmentId: "${environmentId}", apiHost: "${window.location.protocol}//${window.location.host}"})},500)}();
!function(){var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src="${webAppUrl}/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.setup({environmentId: "${environmentId}", appUrl: "${window.location.protocol}//${window.location.host}"})},500)}();
</script>
<!-- END Formbricks Surveys -->`}</CodeBlock>
<h4>Step 2: Debug mode</h4>

View File

@@ -7,7 +7,7 @@ import { Button } from "@/modules/ui/components/button";
import { DeleteDialog } from "@/modules/ui/components/delete-dialog";
import { useTranslate } from "@tolgee/react";
import { useRouter } from "next/navigation";
import React, { useState } from "react";
import { useState } from "react";
import toast from "react-hot-toast";
import { FORMBRICKS_ENVIRONMENT_ID_LS } from "@formbricks/lib/localStorage";
import { truncate } from "@formbricks/lib/utils/strings";

View File

@@ -14,7 +14,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useTranslate } from "@tolgee/react";
import { PlusIcon } from "lucide-react";
import { useRouter } from "next/navigation";
import React, { useState } from "react";
import { useState } from "react";
import { useForm } from "react-hook-form";
import { toast } from "react-hot-toast";

View File

@@ -5,7 +5,7 @@ import { DeleteAccountModal } from "@/modules/account/components/DeleteAccountMo
import { Alert, AlertDescription, AlertTitle } from "@/modules/ui/components/alert";
import { Button } from "@/modules/ui/components/button";
import { useTranslate } from "@tolgee/react";
import React, { useState } from "react";
import { useState } from "react";
import { TUser } from "@formbricks/types/user";
interface RemovedFromOrganizationProps {

View File

@@ -2,7 +2,7 @@
import { LanguageIndicator } from "@/modules/ee/multi-language-surveys/components/language-indicator";
import { useTranslate } from "@tolgee/react";
import React, { ReactNode, useMemo } from "react";
import { ReactNode, useMemo } from "react";
import { getEnabledLanguages } from "@formbricks/lib/i18n/utils";
import { headlineToRecall, recallToHeadline } from "@formbricks/lib/utils/recall";
import { TI18nString, TSurvey, TSurveyRecallItem } from "@formbricks/types/surveys/types";

View File

@@ -170,7 +170,7 @@ export const LinkSurvey = ({
PRIVACY_URL={PRIVACY_URL}
isBrandingEnabled={project.linkSurveyBranding}>
<SurveyInline
apiHost={webAppUrl}
appUrl={webAppUrl}
environmentId={survey.environmentId}
isPreviewMode={isPreview}
survey={survey}

View File

@@ -1,7 +1,6 @@
import { Button } from "@/modules/ui/components/button";
import { KeyIcon } from "lucide-react";
import Link from "next/link";
import React from "react";
export type ModalButton = {
text: string;

View File

@@ -115,6 +115,7 @@
"papaparse": "5.4.1",
"posthog-js": "1.200.2",
"prismjs": "1.30.0",
"qr-code-styling": "1.9.1",
"react": "19.0.0",
"react-colorful": "5.6.1",
"react-confetti": "6.1.0",

View File

@@ -10,10 +10,9 @@ const HTML_TEMPLATE = `<head>
var e = document.getElementsByTagName("script")[0];
e.parentNode.insertBefore(t, e),
setTimeout(function () {
formbricks.init({
formbricks.setup({
environmentId: "ENVIRONMENT_ID",
userId: "RANDOM_USER_ID",
apiHost: "http://localhost:3000",
appUrl: "http://localhost:3000",
});
}, 500);
})();

View File

@@ -1,6 +1,8 @@
{
"compilerOptions": {
"baseUrl": ".",
"jsx": "preserve",
"jsxImportSource": "react",
"paths": {
"@/*": ["./*"],
"@prisma/client/*": ["@formbricks/database/client/*"]

View File

@@ -1,6 +1,7 @@
// vitest.config.ts
import { loadEnv } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
import react from "@vitejs/plugin-react";
import { defineConfig } from "vitest/config";
export default defineConfig({
@@ -26,6 +27,9 @@ export default defineConfig({
"modules/email/emails/survey/follow-up.tsx",
"app/(app)/environments/**/settings/(organization)/general/page.tsx",
"modules/ee/sso/lib/**/*.ts",
"app/(auth)/layout.tsx",
"app/(app)/layout.tsx",
"app/intercom/*.tsx",
],
exclude: [
"**/.next/**",
@@ -36,8 +40,9 @@ export default defineConfig({
"**/openapi.ts", // Exclude openapi configuration files
"**/openapi-document.ts", // Exclude openapi document files
"modules/**/types/**", // Exclude types
"**/*.tsx", // Exclude tsx files
],
},
},
plugins: [tsconfigPaths()],
plugins: [tsconfigPaths(), react()],
});

View File

@@ -18,6 +18,7 @@ services:
redis:
image: redis:7.0.11
command: "redis-server"
ports:
- 6379:6379
volumes:

View File

@@ -160,6 +160,8 @@ x-environment: &environment
# Set the below to use Redis for Next Caching (default is In-Memory from Next Cache)
# REDIS_URL:
# REDIS_DEFAULT_TTL:
# Set the below to use for Rate Limiting (default us In-Memory LRU Cache)
# REDIS_HTTP_URL:

View File

@@ -21,7 +21,6 @@ We use Mintlify to maintain our documentation. You can find more information abo
- Document parameters, return types, and potential side effects
- Example:
```typescript
/**
Creates a new user and initializes their preferences
@@ -31,26 +30,23 @@ Creates a new user and initializes their preferences
@throws {ValidationError} If name is invalid
*/
async function createUser(name: string, options: UserOptions): Promise<User> {
// implementation
// implementation
}
```
2. **TypeScript Ignore Comments**
- When using `@ts-ignore` or `@ts-expect-error`, always include a comment explaining why
- Example:
```typescript
// @ts-expect-error -- Required for dynamic function calls
void window.formbricks.init(...args);
void window.formbricks.setup(...args);
```
### API Documentation
1. **API Endpoints**
- All new API endpoints must be documented in the OpenAPI specification
- Include request/response schemas, authentication requirements, and examples
- Document both Client API and Management API endpoints
@@ -63,11 +59,12 @@ void window.formbricks.init(...args);
### Feature Documentation
- All new features must include a feature documentation file
- Document the feature's purpose, usage, and implementation details
- Include code examples and best practices
- All new features must include a feature documentation file
- Document the feature's purpose, usage, and implementation details
- Include code examples and best practices
## Working with Mintlify
We use Mintlify to write our documentation.
### File Structure
@@ -84,7 +81,6 @@ icon: "appropriate-icon"
---
```
2. **Navigation**
- Add new pages to the appropriate section in `docs/mint.json`
- Follow the existing navigation structure
@@ -104,8 +100,8 @@ Important information goes here
</Note>
```
2. **Media and Assets**
- Store images in the appropriate `/images` subdirectory
- Use descriptive alt text for all images
- Optimize images for web delivery
@@ -130,4 +126,4 @@ mintlify dev
- Verify all links and references work
- Ensure proper formatting and rendering
These documentation requirements ensure that our codebase remains maintainable, accessible, and well-documented for both current and future developers.
These documentation requirements ensure that our codebase remains maintainable, accessible, and well-documented for both current and future developers.

View File

@@ -1,5 +1,6 @@
---
title: Testing Methodology
title: "Testing Methodology"
description: "How we test Formbricks to ensure reliability, performance, and high-quality code."
icon: magnifying-glass
---

View File

@@ -1,5 +1,6 @@
---
title: Framework Usage
description: Guidelines on how Formbricks utilizes Next.js, Tailwind CSS, and Prisma ORM for efficient development and performance.
icon: book
---

View File

@@ -1,5 +1,6 @@
---
title: "Open-Source"
description: "Open-source Experience Management. Free & open source."
icon: "osi"
---

View File

@@ -1,5 +1,6 @@
---
title: "Migration"
description: "Formbricks Self-hosted version migration"
icon: "arrow-right"
---
@@ -172,7 +173,7 @@ Thats it! This new process ensures your **Formbricks** setup stays up to date
With **Formbricks 3.0**, we're making changes to ensure long-term sustainability while still supporting open source. While the **Community Edition** has gained [new features](https://formbricks.com/blog/formbricks-3-0), some [advanced capabilities](https://formbricks.com/docs/self-hosting/license) are now part of the **Enterprise Edition**.
⚠️ **No Downgrade Option:** If you upgrade to **3.0** and run the data migration, **you cannot revert to 2.7.2**. If you rely on **SSO, user identification, or cluster support**, either **stay on version 2.7.x** or [reach out](https://formbricks.com/cdn-cgi/l/email-protection#1e7671727f5e78716c737c6c777d756d307d7173) **for a custom quote**.
⚠️ **No Downgrade Option:** If you upgrade to **3.0** and run the data migration, **you cannot revert to 2.7.2**. If you rely on **SSO, user identification, or cluster support**, either **stay on version 2.7.x** or reach out to us on [**GitHub Discussions**](https://github.com/formbricks/formbricks/discussions) **for a custom quote**.
</Warning>

View File

@@ -131,6 +131,7 @@ Configure Redis by adding the following environment variables to your instances:
```sh env
REDIS_URL=redis://your-redis-host:6379
REDIS_DEFAULT_TTL=86400
REDIS_HTTP_URL=http://your-redis-host:8000
```

View File

@@ -28,34 +28,27 @@ How to deliver a specific language depends on the survey type (app or link surve
![Formbricks Home](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/survey-languages-from-home.webp)
- Click on the **Edit languages** button, to add a new language to your survey
![Survey Language Settings](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/survey-languague-settings.webp)
- Select the preferred language from the dropdown and assign an identifier Alias. Click the **Add language** button to add the language to your project.
![Add Multiple Languages to your Project](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/add-languages.webp)
You can come back to this page anytime to add more languages or remove existing ones.
- Now, return to the dashboard to create a new survey or edit an existing one.
![Surveys Home](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/surveys-home.webp)
- In the survey editor, scroll down to the **Multiple Languages** section at the bottom and enable the toggle next to it.
![Enable Multi-language for a survey](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/enable-multi-lang.webp)
- Choose a **Default Language** for your survey.
<Note>
Changing the default language will reset all the translations you have made
for the survey.
</Note>
<Note>Changing the default language will reset all the translations you have made for the survey.</Note>
1. Now, add the languages from the dropdown that you want to support in your survey.
@@ -69,28 +62,24 @@ You can come back to this page anytime to add more languages or remove existing
![Enable Multi-language for a survey](/images/xm-and-surveys/surveys/general-features/multi-language-surveys/translate-as-per-language.webp)
1. Once you are done, click on the **Publish** button to save the survey.
## App Surveys Configuration
1. When you initialise the Formbricks SDK for your user, you can pass a `language` attribute with the language code. This can be either the ISO identifier or the Alias you set when creating the language. The `language` attribute makes sure that this user only sees surveys with a translation in this specific language available.
1. After you setup the Formbricks SDK for your user, you can call the `setLanguage` function with the language code. This can be either the ISO identifier or the Alias you set when creating the language. The `language` attribute makes sure that this user only sees surveys with a translation in this specific language available.
```js javascript
Formbricks.init({
Formbricks.setup({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user_id>",
attributes: {
language: "de", // ISO identifier or Alias set when creating language
},
appUrl: "<app-url>",
});
Formbricks.setLanguage("de"); // ISO identifier or Alias set when creating language
```
<Note>
If a user has a language assigned, a survey has multi-language activate and it
is missing a translation in the language of the user, the survey will not be
displayed.
If a user has a language assigned, a survey has multi-language activate and it is missing a translation in
the language of the user, the survey will not be displayed.
</Note>
1. That's it! Now, users with the language attribute set will see the survey in their preferred language. You can start collecting responses in multiple languages and filter them by language on the summary page.

View File

@@ -1,5 +1,6 @@
---
title: "Framework Guides"
description: "Easily add the Formbricks App Survey SDK to your app with guides for different frameworks."
icon: "book"
---
@@ -16,14 +17,12 @@ Integrate the **Formbricks App Survey SDK** into your app using multiple options
</Card>
<Card title="Next.js" icon="react" href="#nextjs">
[Natively add us to your Next.js project, with support for both App and Pages
project
[Natively add us to your Next.js project, with support for both App and Pages project
structure.](https://formbricks.com/docs/app-surveys/framework-guides#next-js)
</Card>
<Card title="Vue.js" icon="vuejs" href="#vue-js">
Learn how to use Formbricks' React Native SDK to integrate your surveys into
React Native applications.
Learn how to use Formbricks' React Native SDK to integrate your surveys into React Native applications.
</Card>
<Card title="React Native" icon="react" color="lightblue" href="#react-native">
@@ -47,10 +46,9 @@ All you need to do is copy a `<script>` tag to your HTML head:
<!-- START Formbricks Surveys -->
<script type="text/javascript">
!function(){
var apiHost = "https://app.formbricks.com";
var appUrl = "https://app.formbricks.com";
var environmentId = "<your-environment-id>";
var userId = "<your-user-id>"; //optional
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=apiHost+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.init({environmentId: environmentId, apiHost: apiHost, userId: userId})},500)}();
var t=document.createElement("script");t.type="text/javascript",t.async=!0,t.src=appUrl+"/js/formbricks.umd.cjs";var e=document.getElementsByTagName("script")[0];e.parentNode.insertBefore(t,e),setTimeout(function(){window.formbricks.setup({environmentId: environmentId, appUrl: appUrl})},500)}();
</script>
<!-- END Formbricks Surveys -->
```
@@ -60,7 +58,7 @@ All you need to do is copy a `<script>` tag to your HTML head:
| Name | Type | Description |
| -------------- | ------ | -------------------------------------- |
| environment-id | string | Formbricks Environment ID. |
| api-host | string | URL of the hosted Formbricks instance. |
| app-url | string | URL of the hosted Formbricks instance. |
Now, visit the [Validate Your Setup](#validate-your-setup) section to verify your setup!
@@ -88,10 +86,9 @@ Update your `App.js/ts` file to initialize Formbricks.
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
appUrl: "<app-url>",
});
}
@@ -107,7 +104,7 @@ export default App;
| Name | Type | Description |
| -------------- | ------ | -------------------------------------- |
| environment-id | string | Formbricks Environment ID. |
| api-host | string | URL of the hosted Formbricks instance. |
| app-url | string | URL of the hosted Formbricks instance. |
Now, visit the [Validate Your Setup](#validate-your-setup) section to verify your setup!
@@ -147,10 +144,9 @@ export default function FormbricksProvider() {
const searchParams = useSearchParams();
useEffect(() => {
formbricks.init({
formbricks.setup({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
appUrl: "<app-url>",
});
}, []);
@@ -192,10 +188,9 @@ import { useEffect } from "react";
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
appUrl: "<app-url>",
});
}
@@ -220,7 +215,7 @@ export default function App({ Component, pageProps }: AppProps) {
| Name | Type | Description |
| -------------- | ------ | -------------------------------------- |
| environment-id | string | Formbricks Environment ID. |
| api-host | string | URL of the hosted Formbricks instance. |
| app-url | string | URL of the hosted Formbricks instance. |
First, initialize the Formbricks SDK to run only on the client side. To track page changes, register the route change event with the Next.js router.
@@ -246,10 +241,9 @@ yarn add @formbricks/js
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", //optional
appUrl: "<app-url>",
});
}
@@ -278,7 +272,7 @@ router.afterEach((to, from) => {
| Name | Type | Description |
| -------------- | ------ | -------------------------------------- |
| environment-id | string | Formbricks Environment ID. |
| api-host | string | URL of the hosted Formbricks instance. |
| app-url | string | URL of the hosted Formbricks instance. |
Now, visit the [Validate Your Setup](#validate-your-setup) section to verify your setup!
@@ -306,8 +300,7 @@ import Formbricks from "@formbricks/react-native";
const config = {
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user-id>", // optional
appUrl: "<app-url>",
};
export default function App() {
@@ -325,7 +318,7 @@ export default function App() {
| Name | Type | Description |
| -------------- | ------ | -------------------------------------- |
| environment-id | string | Formbricks Environment ID. |
| api-host | string | URL of the hosted Formbricks instance. |
| app-url | string | URL of the hosted Formbricks instance. |
## Validate your setup

View File

@@ -27,57 +27,44 @@ This method is recommended for applications where users are required to log in a
### Setting User ID
To enable user identification, set the `userId` in the `init()` call of Formbricks. The user will show up in the Formbricks dashboard only if the `userId` is set. Use a unique string, like a database ID or a unique email address. You can also anonymize the identifier, as long as it is unique for each user.
To enable user identification, call the `setUserId` function of Formbricks and pass the user id. The user will show up in the Formbricks dashboard. Use a unique string, like a database ID or a unique email address. You can also anonymize the identifier, as long as it is unique for each user.
```javascript
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user_id>",
});
formbricks.setUserId("<user-id>");
```
### Enhanced Initialization with User Attributes
Set user attributes in Formbricks during initialization along with the `userId`.
```javascript Enhanced Initialization with User Attributes
formbricks.init({
environmentId: "<environment-id>",
apiHost: "<api-host>",
userId: "<user_id>",
attributes: {
// your custom attributes
Plan: "premium",
},
});
```
## Setting Custom User Attributes
Use the `setAttribute` function to set custom attributes for the user (e.g., name, plan).
<Note>
**Note**: the number of different attribute classes (e.g., "Plan,"
"First Name," etc.) is currently limited to 150 attributes per environment.
</Note>
```javascript Setting Custom Attributes
formbricks.setAttribute("Plan","free");
```
```javascript Setting Custom Attributes
formbricks.setAttribute("Plan", "free");
```
The `setAttribute` function works like this:
```javascript Setting Custom Attributes
formbricks.setAttribute("attribute_key", "attribute_value");
```
You can also set multiple attributes at once by passing an object to the `setAttributes` function:
```javascript Setting Multiple Custom Attributes
formbricks.setAttributes({
attribute_key_1: "attribute_value_1",
attribute_key_2: "attribute_value_2",
});
```
<Note>
**Note**: the number of different attribute classes (e.g., "Plan," "First Name," etc.) is currently limited
to 150 attributes per environment.
</Note>
### Logging Out Users
When a user logs out of your webpage, also log them out of Formbricks to prevent activity from being linked to the wrong user. Use the logout function:
```javascript Logging out User
formbricks.logout();
```javascript Logging out User
formbricks.logout();
```

View File

@@ -97,7 +97,7 @@ spec:
protocol: {{ $config.protocol | default "TCP" | quote }}
{{- end }}
{{- end }}
{{- if or .Values.deployment.envFrom (and .Values.externalSecret.enabled (index .Values.externalSecret.files "app-secrets")) }}
{{- if or .Values.deployment.envFrom (or (and .Values.externalSecret.enabled (index .Values.externalSecret.files "app-secrets")) .Values.secret.enabled) }}
envFrom:
{{- if or .Values.secret.enabled (and .Values.externalSecret.enabled (index .Values.externalSecret.files "app-secrets")) }}
- secretRef:

View File

@@ -0,0 +1,195 @@
data "aws_ssm_parameter" "slack_notification_channel" {
name = "/prod/formbricks/slack-webhook-url"
with_decryption = true
}
resource "aws_cloudwatch_log_group" "cloudwatch_cis_benchmark" {
name = "/aws/cis-benchmark-group"
retention_in_days = 365
}
module "notify-slack" {
source = "terraform-aws-modules/notify-slack/aws"
version = "6.6.0"
slack_channel = "kubernetes"
slack_username = "formbricks-cloudwatch"
slack_webhook_url = data.aws_ssm_parameter.slack_notification_channel.value
sns_topic_name = "cloudwatch-alarms"
create_sns_topic = true
}
module "cloudwatch_cis-alarms" {
source = "terraform-aws-modules/cloudwatch/aws//modules/cis-alarms"
version = "5.7.1"
log_group_name = aws_cloudwatch_log_group.cloudwatch_cis_benchmark.name
alarm_actions = [module.notify-slack.slack_topic_arn]
}
locals {
alarms = {
ALB_HTTPCode_Target_5XX_Count = {
alarm_description = "Average API 5XX target group error code count is too high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1
period = 60
unit = "Count"
namespace = "AWS/ApplicationELB"
metric_name = "HTTPCode_Target_5XX_Count"
statistic = "Sum"
}
ALB_HTTPCode_ELB_5XX_Count = {
alarm_description = "Average API 5XX load balancer error code count is too high"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1
period = 60
unit = "Count"
namespace = "AWS/ApplicationELB"
metric_name = "HTTPCode_ELB_5XX_Count"
statistic = "Sum"
}
ALB_TargetResponseTime = {
alarm_description = format("Average API response time is greater than %s", 0.05)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 0.05
period = 60
unit = "Seconds"
namespace = "AWS/ApplicationELB"
metric_name = "TargetResponseTime"
statistic = "Average"
}
ALB_UnHealthyHostCount = {
alarm_description = format("Unhealthy host count is greater than %s", 1)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1
period = 60
unit = "Count"
namespace = "AWS/ApplicationELB"
metric_name = "UnHealthyHostCount"
statistic = "Minimum"
}
RDS_CPUUtilization = {
alarm_description = format("Average RDS CPU utilization is greater than %s", 80)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 80
period = 60
unit = "Percent"
namespace = "AWS/RDS"
metric_name = "CPUUtilization"
statistic = "Average"
}
RDS_FreeStorageSpace = {
alarm_description = format("Average RDS free storage space is less than %s", 5)
comparison_operator = "LessThanThreshold"
evaluation_periods = 5
threshold = 5
period = 60
unit = "Gigabytes"
namespace = "AWS/RDS"
metric_name = "FreeStorageSpace"
statistic = "Average"
}
RDS_FreeableMemory = {
alarm_description = format("Average RDS freeable memory is less than %s", 100)
comparison_operator = "LessThanThreshold"
evaluation_periods = 5
threshold = 100
period = 60
unit = "Megabytes"
namespace = "AWS/RDS"
metric_name = "FreeableMemory"
statistic = "Average"
}
RDS_DiskQueueDepth = {
alarm_description = format("Average RDS disk queue depth is greater than %s", 1)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1
period = 60
unit = "Count"
namespace = "AWS/RDS"
metric_name = "DiskQueueDepth"
statistic = "Average"
}
RDS_ReadIOPS = {
alarm_description = format("Average RDS read IOPS is greater than %s", 1000)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1000
period = 60
unit = "Count/Second"
namespace = "AWS/RDS"
metric_name = "ReadIOPS"
statistic = "Average"
}
RDS_WriteIOPS = {
alarm_description = format("Average RDS write IOPS is greater than %s", 1000)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1000
period = 60
unit = "Count/Second"
namespace = "AWS/RDS"
metric_name = "WriteIOPS"
statistic = "Average"
}
SQS_ApproximateAgeOfOldestMessage = {
alarm_description = format("Average SQS approximate age of oldest message is greater than %s", 300)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 300
period = 60
unit = "Seconds"
namespace = "AWS/SQS"
metric_name = "ApproximateAgeOfOldestMessage"
statistic = "Maximum"
}
DynamoDB_ConsumedReadCapacityUnits = {
alarm_description = format("Average DynamoDB consumed read capacity units is greater than %s", 90)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 90
period = 60
unit = "Count"
namespace = "AWS/DynamoDB"
metric_name = "ConsumedReadCapacityUnits"
statistic = "Average"
}
Lambda_Errors = {
alarm_description = format("Average Lambda errors is greater than %s", 1)
comparison_operator = "GreaterThanThreshold"
evaluation_periods = 5
threshold = 1
period = 60
unit = "Count"
namespace = "AWS/Lambda"
metric_name = "Errors"
statistic = "Sum"
}
}
}
module "metric_alarm" {
source = "terraform-aws-modules/cloudwatch/aws//modules/metric-alarm"
version = "5.7.1"
for_each = local.alarms
alarm_name = each.key
alarm_description = each.value.alarm_description
comparison_operator = each.value.comparison_operator
evaluation_periods = each.value.evaluation_periods
threshold = each.value.threshold
period = each.value.period
unit = each.value.unit
namespace = each.value.namespace
metric_name = each.value.metric_name
statistic = each.value.statistic
alarm_actions = [module.notify-slack.slack_topic_arn]
}

View File

@@ -1,27 +0,0 @@
data "aws_ssm_parameter" "slack_notification_channel" {
name = "/prod/formbricks/slack-webhook-url"
with_decryption = true
}
resource "aws_cloudwatch_log_group" "cloudwatch_cis_benchmark" {
name = "/aws/cis-benchmark-group"
retention_in_days = 365
}
module "notify-slack" {
source = "terraform-aws-modules/notify-slack/aws"
version = "6.6.0"
slack_channel = "kubernetes"
slack_username = "formbricks-cloudwatch"
slack_webhook_url = data.aws_ssm_parameter.slack_notification_channel.value
sns_topic_name = "cloudwatch-alarms"
create_sns_topic = true
}
module "cloudwatch_cis-alarms" {
source = "terraform-aws-modules/cloudwatch/aws//modules/cis-alarms"
version = "5.7.1"
log_group_name = aws_cloudwatch_log_group.cloudwatch_cis_benchmark.name
alarm_actions = [module.notify-slack.slack_topic_arn]
}

View File

@@ -0,0 +1,70 @@
################################################################################
# ElastiCache Module
################################################################################
resource "random_password" "valkey" {
length = 20
special = false
}
resource "random_password" "valkey_default_user" {
length = 20
special = false
}
module "valkey_sg" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.0"
name = "valkey-sg"
description = "Security group for VPC traffic"
vpc_id = module.vpc.vpc_id
ingress_cidr_blocks = [module.vpc.vpc_cidr_block]
ingress_rules = ["redis-tcp"]
tags = local.tags
}
module "elasticache_user_group" {
source = "terraform-aws-modules/elasticache/aws//modules/user-group"
version = "1.4.1"
user_group_id = "${local.name}-valkey"
create_default_user = false
default_user = {
user_id = "formbricks-default"
passwords = [random_password.valkey_default_user.result]
}
users = {
formbricks = {
access_string = "on ~* +@all"
passwords = [random_password.valkey.result]
}
}
engine = "redis"
tags = merge(local.tags, {
terraform-aws-modules = "elasticache"
})
}
module "valkey_serverless" {
source = "terraform-aws-modules/elasticache/aws//modules/serverless-cache"
version = "1.4.1"
engine = "valkey"
cache_name = "${local.name}-valkey-serverless"
cache_usage_limits = {
data_storage = {
maximum = 2
}
ecpu_per_second = {
maximum = 1000
}
}
major_engine_version = 7
subnet_ids = module.vpc.database_subnets
security_group_ids = [
module.valkey_sg.security_group_id
]
user_group_id = module.elasticache_user_group.group_id
}

View File

@@ -106,116 +106,6 @@ module "vpc_vpc-endpoints" {
tags = local.tags
}
################################################################################
# PostgreSQL Serverless v2
################################################################################
data "aws_rds_engine_version" "postgresql" {
engine = "aurora-postgresql"
version = "16.4"
}
resource "random_password" "postgres" {
length = 20
special = false
}
module "rds-aurora" {
source = "terraform-aws-modules/rds-aurora/aws"
version = "9.12.0"
name = "${local.name}-postgres"
engine = data.aws_rds_engine_version.postgresql.engine
engine_mode = "provisioned"
engine_version = data.aws_rds_engine_version.postgresql.version
storage_encrypted = true
master_username = "formbricks"
master_password = random_password.postgres.result
manage_master_user_password = false
vpc_id = module.vpc.vpc_id
db_subnet_group_name = module.vpc.database_subnet_group_name
security_group_rules = {
vpc_ingress = {
cidr_blocks = module.vpc.private_subnets_cidr_blocks
}
}
performance_insights_enabled = true
apply_immediately = true
skip_final_snapshot = true
enable_http_endpoint = true
serverlessv2_scaling_configuration = {
min_capacity = 0
max_capacity = 10
seconds_until_auto_pause = 3600
}
instance_class = "db.serverless"
instances = {
one = {}
}
tags = local.tags
}
################################################################################
# ElastiCache Module
################################################################################
resource "random_password" "valkey" {
length = 20
special = false
}
module "elasticache" {
source = "terraform-aws-modules/elasticache/aws"
version = "1.4.1"
replication_group_id = "${local.name}-valkey"
engine = "valkey"
engine_version = "7.2"
node_type = "cache.m7g.large"
transit_encryption_enabled = true
auth_token = random_password.valkey.result
maintenance_window = "sun:05:00-sun:09:00"
apply_immediately = true
# Security Group
vpc_id = module.vpc.vpc_id
security_group_rules = {
ingress_vpc = {
# Default type is `ingress`
# Default port is based on the default engine port
description = "VPC traffic"
cidr_ipv4 = module.vpc.vpc_cidr_block
}
}
# Subnet Group
subnet_group_name = "${local.name}-valkey"
subnet_group_description = "${title(local.name)} subnet group"
subnet_ids = module.vpc.database_subnets
# Parameter Group
create_parameter_group = true
parameter_group_name = "${local.name}-valkey"
parameter_group_family = "valkey7"
parameter_group_description = "${title(local.name)} parameter group"
parameters = [
{
name = "latency-tracking"
value = "yes"
}
]
tags = local.tags
}
################################################################################
# EKS Module
################################################################################
@@ -671,6 +561,7 @@ resource "helm_release" "formbricks" {
jobs:
survey-status:
schedule: "0 0 * * *"
successfulJobsHistoryLimit: 0
env:
CRON_SECRET:
valueFrom:
@@ -692,6 +583,7 @@ resource "helm_release" "formbricks" {
- 'curl -X POST -H "content-type: application/json" -H "x-api-key: $CRON_SECRET" "$WEBAPP_URL/api/cron/survey-status"'
weekely-summary:
schedule: "0 8 * * 1"
successfulJobsHistoryLimit: 0
env:
CRON_SECRET:
valueFrom:
@@ -713,6 +605,7 @@ resource "helm_release" "formbricks" {
- 'curl -X POST -H "content-type: application/json" -H "x-api-key: $CRON_SECRET" "$WEBAPP_URL/api/cron/weekly-summary"'
ping:
schedule: "0 9 * * *"
successfulJobsHistoryLimit: 0
env:
CRON_SECRET:
valueFrom:

View File

@@ -0,0 +1,54 @@
module "loki_s3_bucket" {
source = "terraform-aws-modules/s3-bucket/aws"
version = "4.6.0"
bucket_prefix = "loki-"
force_destroy = true
control_object_ownership = true
object_ownership = "BucketOwnerPreferred"
}
module "observability_loki_iam_policy" {
source = "terraform-aws-modules/iam/aws//modules/iam-policy"
version = "5.53.0"
name_prefix = "loki-"
path = "/"
description = "Policy for fombricks observability apps"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = [
"s3:*",
]
Resource = [
module.loki_s3_bucket.s3_bucket_arn,
"${module.loki_s3_bucket.s3_bucket_arn}/*"
]
}
]
})
}
module "observability_loki_iam_role" {
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
version = "5.53.0"
role_name_prefix = "loki-"
role_policy_arns = {
"formbricks" = module.observability_loki_iam_policy.arn
}
assume_role_condition_test = "StringLike"
oidc_providers = {
eks = {
provider_arn = module.eks.oidc_provider_arn
namespace_service_accounts = ["monitoring:loki*"]
}
}
}

55
infra/terraform/rds.tf Normal file
View File

@@ -0,0 +1,55 @@
################################################################################
# PostgreSQL Serverless v2
################################################################################
data "aws_rds_engine_version" "postgresql" {
engine = "aurora-postgresql"
version = "16.4"
}
resource "random_password" "postgres" {
length = 20
special = false
}
module "rds-aurora" {
source = "terraform-aws-modules/rds-aurora/aws"
version = "9.12.0"
name = "${local.name}-postgres"
engine = data.aws_rds_engine_version.postgresql.engine
engine_mode = "provisioned"
engine_version = data.aws_rds_engine_version.postgresql.version
storage_encrypted = true
master_username = "formbricks"
master_password = random_password.postgres.result
manage_master_user_password = false
vpc_id = module.vpc.vpc_id
db_subnet_group_name = module.vpc.database_subnet_group_name
security_group_rules = {
vpc_ingress = {
cidr_blocks = module.vpc.private_subnets_cidr_blocks
}
}
performance_insights_enabled = true
apply_immediately = true
skip_final_snapshot = true
enable_http_endpoint = true
serverlessv2_scaling_configuration = {
min_capacity = 0
max_capacity = 10
seconds_until_auto_pause = 3600
}
instance_class = "db.serverless"
instances = {
one = {}
}
tags = local.tags
}

View File

@@ -3,12 +3,10 @@ resource "aws_secretsmanager_secret" "formbricks_app_secrets" {
name = "prod/formbricks/secrets"
}
resource "aws_secretsmanager_secret_version" "formbricks_app_secrets" {
secret_id = aws_secretsmanager_secret.formbricks_app_secrets.id
secret_string = jsonencode({
DATABASE_URL = "postgres://formbricks:${random_password.postgres.result}@${module.rds-aurora.cluster_endpoint}/formbricks"
REDIS_URL = "rediss://:${random_password.valkey.result}@${module.elasticache.replication_group_primary_endpoint_address}:6379"
REDIS_URL = "rediss://formbricks:${random_password.valkey.result}@${module.valkey_serverless.serverless_cache_endpoint[0].address}:6379"
})
}

View File

@@ -52,6 +52,10 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
implementation(libs.androidx.fragment.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
xmlns:tools="http://schemas.android.com/tools" >
<application
android:allowBackup="true"
@@ -11,12 +11,11 @@
android:networkSecurityConfig="@xml/network_security_config"
android:supportsRtl="true"
android:theme="@style/Theme.Demo"
tools:targetApi="31">
tools:targetApi="31" >
<activity
android:name=".MainActivity"
android:exported="true"
android:windowSoftInputMode="adjustPan"
android:theme="@style/Theme.Demo">
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

View File

@@ -1,31 +1,21 @@
package com.formbricks.demo
import android.os.Bundle
import androidx.activity.compose.setContent
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity
import com.formbricks.demo.ui.theme.DemoTheme
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.formbricks.formbrickssdk.Formbricks
import com.formbricks.formbrickssdk.helper.FormbricksConfig
import java.util.UUID
class MainActivity : FragmentActivity() {
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
val config = FormbricksConfig.Builder("[API_HOST]","[ENVIRONMENT_ID]")
val config = FormbricksConfig.Builder("[appUrl]","[environmentId]")
.setLoggingEnabled(true)
.setFragmentManager(supportFragmentManager)
Formbricks.setup(this, config.build())
@@ -33,39 +23,16 @@ class MainActivity : FragmentActivity() {
Formbricks.logout()
Formbricks.setUserId(UUID.randomUUID().toString())
enableEdgeToEdge()
setContent {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
DemoTheme {
FormbricksDemo()
}
}
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
}
}
@Composable
fun FormbricksDemo() {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Button(onClick = {
val button = findViewById<Button>(R.id.button)
button.setOnClickListener {
Formbricks.track("click_demo_button")
}) {
Text(
text = "Click me!",
modifier = Modifier.padding(16.dp)
)
}
}
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
DemoTheme {
FormbricksDemo()
}
}

View File

@@ -1,11 +0,0 @@
package com.formbricks.demo.ui.theme
import androidx.compose.ui.graphics.Color
val Purple80 = Color(0xFFD0BCFF)
val PurpleGrey80 = Color(0xFFCCC2DC)
val Pink80 = Color(0xFFEFB8C8)
val Purple40 = Color(0xFF6650a4)
val PurpleGrey40 = Color(0xFF625b71)
val Pink40 = Color(0xFF7D5260)

View File

@@ -1,51 +0,0 @@
package com.formbricks.demo.ui.theme
import android.app.Activity
import android.os.Build
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.darkColorScheme
import androidx.compose.material3.dynamicDarkColorScheme
import androidx.compose.material3.dynamicLightColorScheme
import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
private val DarkColorScheme = darkColorScheme(
primary = Purple80, secondary = PurpleGrey80, tertiary = Pink80
)
private val LightColorScheme = lightColorScheme(
primary = Purple40, secondary = PurpleGrey40, tertiary = Pink40
/* Other default colors to override
background = Color(0xFFFFFBFE),
surface = Color(0xFFFFFBFE),
onPrimary = Color.White,
onSecondary = Color.White,
onTertiary = Color.White,
onBackground = Color(0xFF1C1B1F),
onSurface = Color(0xFF1C1B1F),
*/
)
@Composable
fun DemoTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
// Dynamic color is available on Android 12+
dynamicColor: Boolean = true, content: @Composable () -> Unit
) {
val colorScheme = when {
dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}
darkTheme -> DarkColorScheme
else -> LightColorScheme
}
MaterialTheme(
colorScheme = colorScheme, typography = Typography, content = content
)
}

View File

@@ -1,33 +0,0 @@
package com.formbricks.demo.ui.theme
import androidx.compose.material3.Typography
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
// Set of Material typography styles to start with
val Typography = Typography(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
)/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
*/
)

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="158dp"
tools:layout_editor_absoluteY="336dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -1,2 +1,3 @@
-keep class com.formbricks.formbrickssdk.DataBinderMapperImpl { *; }
-keep class com.formbricks.formbrickssdk.Formbricks { *; }
-keep class com.formbricks.formbrickssdk.Formbricks { *; }
-keep class com.formbricks.formbrickssdk.helper.FormbricksConfig { *; }

View File

@@ -19,9 +19,6 @@
-keep class !androidx.legacy.**,!com.google.android.**,!androidx.** { *; }
-keep class android.support.v4.app.** { *; }
# Retrofit
-dontwarn okio.**
-keep class com.squareup.okhttp.** { *; }
@@ -32,4 +29,8 @@
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
@retrofit.http.* <methods>;
}
}
-keep class com.formbricks.formbrickssdk.DataBinderMapperImpl { *; }
-keep class com.formbricks.formbrickssdk.Formbricks { *; }
-keep class com.formbricks.formbrickssdk.helper.FormbricksConfig { *; }

View File

@@ -1,13 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class ActionClass(
@SerializedName("id") val id: String?,
@SerializedName("type") val type: String?,

View File

@@ -1,13 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class ActionClassReference(
@SerializedName("name") val name: String?
)

View File

@@ -1,13 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class EnvironmentData(
@SerializedName("surveys") val surveys: List<Survey>?,
@SerializedName("actionClasses") val actionClasses: List<ActionClass>?,

View File

@@ -1,13 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class EnvironmentResponseData(
@SerializedName("data") val data: EnvironmentData,
@SerializedName("expiresAt") val expiresAt: String?

View File

@@ -1,13 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class Project(
@SerializedName("id") val id: String?,
@SerializedName("recontactDays") val recontactDays: Double?,

View File

@@ -1,14 +1,9 @@
package com.formbricks.formbrickssdk.model.environment
import com.google.gson.annotations.SerializedName
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonIgnoreUnknownKeys
@OptIn(ExperimentalSerializationApi::class)
@Serializable
@JsonIgnoreUnknownKeys
data class Survey(
@SerializedName("id") val id: String,
@SerializedName("name") val name: String,

View File

@@ -28,10 +28,10 @@ open class FormbricksApiService {
retrofit.create(FormbricksService::class.java)
.getEnvironmentState(environmentId)
}
val json = Json { ignoreUnknownKeys = true }
val resultMap = result.getOrThrow()
val resultJson = mapToJsonElement(resultMap).jsonObject
val environmentResponse = Json.decodeFromJsonElement<EnvironmentResponse>(resultJson)
val environmentResponse = json.decodeFromJsonElement<EnvironmentResponse>(resultJson)
val data = EnvironmentDataHolder(environmentResponse.data, resultMap)
return Result.success(data)
}
@@ -49,6 +49,7 @@ open class FormbricksApiService {
val body = call.body()
if (body == null) {
Result.failure(RuntimeException("Invalid response"))
} else {
Result.success(body)
}

View File

@@ -137,6 +137,8 @@ class FormbricksFragment : BottomSheetDialogFragment() {
behavior.isFitToContents = false
behavior.setState(BottomSheetBehavior.STATE_EXPANDED)
dialog?.setCancelable(false)
dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
}

View File

@@ -26,6 +26,8 @@ lifecycleLivedataKtx = "2.8.7"
lifecycleViewmodelKtx = "2.8.7"
fragmentKtx = "1.8.5"
databindingCommon = "8.8.0"
activity = "1.10.1"
constraintlayout = "2.1.4"
[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
@@ -58,6 +60,8 @@ androidx-lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecy
androidx-lifecycle-viewmodel-ktx = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-fragment-ktx = { group = "androidx.fragment", name = "fragment-ktx", version.ref = "fragmentKtx" }
androidx-databinding-common = { group = "androidx.databinding", name = "databinding-common", version.ref = "databindingCommon" }
androidx-activity = { group = "androidx.activity", name = "activity", version.ref = "activity" }
androidx-constraintlayout = { group = "androidx.constraintlayout", name = "constraintlayout", version.ref = "constraintlayout" }
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }

View File

@@ -18,7 +18,7 @@ npm install @formbricks/api
import { FormbricksAPI } from "@formbricks/api";
const api = new FormbricksAPI({
apiHost: `https://app.formbricks.com`, // If you have self-hosted Formbricks, change this to your self hosted instance's URL
appUrl: `https://app.formbricks.com`, // If you have self-hosted Formbricks, change this to your self hosted instance's URL
environmentId: "<environment-id>", // Replace this with your Formbricks environment ID
});
```

View File

@@ -1,7 +1,7 @@
{
"name": "@formbricks/api",
"license": "MIT",
"version": "3.0.0",
"version": "3.1.0",
"description": "Formbricks-api is an api wrapper for the Formbricks client API",
"keywords": [
"Formbricks",

View File

@@ -4,11 +4,11 @@ import { type ApiErrorResponse } from "@formbricks/types/errors";
import { makeRequest } from "../../utils/make-request";
export class AttributeAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(apiHost: string, environmentId: string) {
this.apiHost = apiHost;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
@@ -22,7 +22,7 @@ export class AttributeAPI {
}
return makeRequest(
this.apiHost,
this.appUrl,
`/api/v1/client/${this.environmentId}/contacts/${attributeUpdateInput.userId}/attributes`,
"PUT",
{ attributes }

View File

@@ -4,17 +4,17 @@ import { type ApiErrorResponse } from "@formbricks/types/errors";
import { makeRequest } from "../../utils/make-request";
export class DisplayAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(baseUrl: string, environmentId: string) {
this.apiHost = baseUrl;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
async create(
displayInput: Omit<TDisplayCreateInput, "environmentId">
): Promise<Result<{ id: string }, ApiErrorResponse>> {
return makeRequest(this.apiHost, `/api/v1/client/${this.environmentId}/displays`, "POST", displayInput);
return makeRequest(this.appUrl, `/api/v1/client/${this.environmentId}/displays`, "POST", displayInput);
}
}

View File

@@ -4,15 +4,15 @@ import { type TJsEnvironmentState } from "@formbricks/types/js";
import { makeRequest } from "../../utils/make-request";
export class EnvironmentAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(apiHost: string, environmentId: string) {
this.apiHost = apiHost;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
async getState(): Promise<Result<TJsEnvironmentState, ApiErrorResponse>> {
return makeRequest(this.apiHost, `/api/v1/client/${this.environmentId}/environment/`, "GET");
return makeRequest(this.appUrl, `/api/v1/client/${this.environmentId}/environment`, "GET");
}
}

View File

@@ -15,13 +15,13 @@ export class Client {
environment: EnvironmentAPI;
constructor(options: ApiConfig) {
const { apiHost, environmentId } = options;
const { appUrl, environmentId } = options;
this.response = new ResponseAPI(apiHost, environmentId);
this.display = new DisplayAPI(apiHost, environmentId);
this.attribute = new AttributeAPI(apiHost, environmentId);
this.storage = new StorageAPI(apiHost, environmentId);
this.user = new UserAPI(apiHost, environmentId);
this.environment = new EnvironmentAPI(apiHost, environmentId);
this.response = new ResponseAPI(appUrl, environmentId);
this.display = new DisplayAPI(appUrl, environmentId);
this.attribute = new AttributeAPI(appUrl, environmentId);
this.storage = new StorageAPI(appUrl, environmentId);
this.user = new UserAPI(appUrl, environmentId);
this.environment = new EnvironmentAPI(appUrl, environmentId);
}
}

View File

@@ -6,18 +6,18 @@ import { makeRequest } from "../../utils/make-request";
type TResponseUpdateInputWithResponseId = TResponseUpdateInput & { responseId: string };
export class ResponseAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(apiHost: string, environmentId: string) {
this.apiHost = apiHost;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
async create(
responseInput: Omit<TResponseInput, "environmentId">
): Promise<Result<{ id: string }, ApiErrorResponse>> {
return makeRequest(this.apiHost, `/api/v1/client/${this.environmentId}/responses`, "POST", responseInput);
return makeRequest(this.appUrl, `/api/v1/client/${this.environmentId}/responses`, "POST", responseInput);
}
async update({
@@ -29,7 +29,7 @@ export class ResponseAPI {
variables,
language,
}: TResponseUpdateInputWithResponseId): Promise<Result<object, ApiErrorResponse>> {
return makeRequest(this.apiHost, `/api/v1/client/${this.environmentId}/responses/${responseId}`, "PUT", {
return makeRequest(this.appUrl, `/api/v1/client/${this.environmentId}/responses/${responseId}`, "PUT", {
finished,
endingId,
data,

View File

@@ -2,11 +2,11 @@
import type { TUploadFileConfig, TUploadFileResponse } from "@formbricks/types/storage";
export class StorageAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(apiHost: string, environmentId: string) {
this.apiHost = apiHost;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
@@ -29,7 +29,7 @@ export class StorageAPI {
surveyId,
};
const response = await fetch(`${this.apiHost}/api/v1/client/${this.environmentId}/storage`, {
const response = await fetch(`${this.appUrl}/api/v1/client/${this.environmentId}/storage`, {
method: "POST",
headers: {
"Content-Type": "application/json",
@@ -86,7 +86,7 @@ export class StorageAPI {
let uploadResponse: Response = {} as Response;
const signedUrlCopy = signedUrl.replace("http://localhost:3000", this.apiHost);
const signedUrlCopy = signedUrl.replace("http://localhost:3000", this.appUrl);
try {
uploadResponse = await fetch(signedUrlCopy, {

View File

@@ -3,11 +3,11 @@ import { type ApiErrorResponse } from "@formbricks/types/errors";
import { makeRequest } from "../../utils/make-request";
export class UserAPI {
private apiHost: string;
private appUrl: string;
private environmentId: string;
constructor(apiHost: string, environmentId: string) {
this.apiHost = apiHost;
constructor(appUrl: string, environmentId: string) {
this.appUrl = appUrl;
this.environmentId = environmentId;
}
@@ -37,7 +37,7 @@ export class UserAPI {
attributes[key] = String(userUpdateInput.attributes[key]);
}
return makeRequest(this.apiHost, `/api/v2/client/${this.environmentId}/user`, "POST", {
return makeRequest(this.appUrl, `/api/v2/client/${this.environmentId}/user`, "POST", {
userId: userUpdateInput.userId,
attributes,
});

View File

@@ -2,7 +2,7 @@ import { type ApiErrorResponse } from "@formbricks/types/errors";
export interface ApiConfig {
environmentId: string;
apiHost: string;
appUrl: string;
}
export type ApiResponse = ApiSuccessResponse | ApiErrorResponse;

View File

@@ -3,12 +3,12 @@ import { type ApiErrorResponse } from "@formbricks/types/errors";
import { type ApiResponse, type ApiSuccessResponse } from "../types";
export const makeRequest = async <T>(
apiHost: string,
appUrl: string,
endpoint: string,
method: "GET" | "POST" | "PUT" | "DELETE",
data?: unknown
): Promise<Result<T, ApiErrorResponse>> => {
const url = new URL(apiHost + endpoint);
const url = new URL(appUrl + endpoint);
const body = data ? JSON.stringify(data) : undefined;
const res = await wrapThrowsAsync(fetch)(url.toString(), {
@@ -28,7 +28,7 @@ export const makeRequest = async <T>(
if (!response.ok) {
const errorResponse = json as ApiErrorResponse;
return err({
code: errorResponse.code === "forbidden" ? "forbidden" : "network_error",
code: errorResponse.code,
status: response.status,
message: errorResponse.message || "Something went wrong",
url,

View File

@@ -24,9 +24,9 @@ npm install -s @formbricks/js
import formbricks from "@formbricks/js";
if (typeof window !== "undefined") {
formbricks.init({
formbricks.setup({
environmentId: "your-environment-id",
apiHost: "https://app.formbricks.com",
appUrl: "https://app.formbricks.com",
});
}
```

View File

@@ -37,17 +37,19 @@
"build:dev": "tsc && vite build --mode dev",
"go": "vite build --watch --mode dev",
"lint": "eslint . --ext .ts,.js,.tsx,.jsx",
"clean": "rimraf .turbo node_modules dist coverage"
"clean": "rimraf .turbo node_modules dist coverage",
"test": "vitest",
"coverage": "vitest run --coverage"
},
"author": "Formbricks <hola@formbricks.com>",
"devDependencies": {
"@formbricks/api": "workspace:*",
"@formbricks/lib": "workspace:*",
"@formbricks/config-typescript": "workspace:*",
"@formbricks/types": "workspace:*",
"@formbricks/eslint-config": "workspace:*",
"@vitest/coverage-v8": "3.0.7",
"terser": "5.37.0",
"vite": "6.0.9",
"vite-plugin-dts": "4.3.0"
"vite-plugin-dts": "4.3.0",
"vitest": "3.0.6"
}
}

View File

@@ -1,23 +1,44 @@
/* eslint-disable import/no-default-export -- We need default exports for the js sdk */
import { type TJsConfigInput, type TJsTrackProperties } from "@formbricks/types/js";
import { trackCodeAction } from "./lib/actions";
import { getApi } from "./lib/api";
import { setAttributeInApp } from "./lib/attributes";
import { CommandQueue } from "./lib/command-queue";
import { ErrorHandler } from "./lib/errors";
import { initialize } from "./lib/initialize";
import { Logger } from "./lib/logger";
import { checkPageUrl } from "./lib/no-code-actions";
import { logoutPerson, resetPerson } from "./lib/person";
/* eslint-disable import/no-default-export -- required for default export*/
import { CommandQueue } from "@/lib/common/command-queue";
import * as Setup from "@/lib/common/setup";
import { getIsDebug } from "@/lib/common/utils";
import * as Action from "@/lib/survey/action";
import { checkPageUrl } from "@/lib/survey/no-code-action";
import * as Attribute from "@/lib/user/attribute";
import * as User from "@/lib/user/user";
import { type TConfigInput, type TLegacyConfigInput } from "@/types/config";
const logger = Logger.getInstance();
logger.debug("Create command queue");
const queue = new CommandQueue();
const init = async (initConfig: TJsConfigInput): Promise<void> => {
ErrorHandler.init(initConfig.errorHandler);
queue.add(false, initialize, initConfig);
const setup = async (setupConfig: TConfigInput): Promise<void> => {
// If the initConfig has a userId or attributes, we need to use the legacy init
if (
// @ts-expect-error -- userId and attributes were in the older type
setupConfig.userId ||
// @ts-expect-error -- attributes were in the older type
setupConfig.attributes ||
// @ts-expect-error -- apiHost was in the older type
setupConfig.apiHost
) {
const isDebug = getIsDebug();
if (isDebug) {
// eslint-disable-next-line no-console -- legacy init
console.warn("🧱 Formbricks - Warning: Using legacy init");
}
queue.add(Setup.setup, false, {
...setupConfig,
// @ts-expect-error -- apiHost was in the older type
...(setupConfig.apiHost && { appUrl: setupConfig.apiHost as string }),
} as unknown as TConfigInput);
} else {
queue.add(Setup.setup, false, setupConfig);
await queue.wait();
}
};
const setUserId = async (userId: string): Promise<void> => {
queue.add(User.setUserId, true, userId);
await queue.wait();
};
@@ -27,39 +48,47 @@ const setEmail = async (email: string): Promise<void> => {
};
const setAttribute = async (key: string, value: string): Promise<void> => {
queue.add(true, setAttributeInApp, key, value);
queue.add(Attribute.setAttributes, true, { [key]: value });
await queue.wait();
};
const setAttributes = async (attributes: Record<string, string>): Promise<void> => {
queue.add(Attribute.setAttributes, true, attributes);
await queue.wait();
};
const setLanguage = async (language: string): Promise<void> => {
queue.add(Attribute.setAttributes, true, { language });
await queue.wait();
};
const logout = async (): Promise<void> => {
queue.add(true, logoutPerson);
queue.add(User.logout, true);
await queue.wait();
};
const reset = async (): Promise<void> => {
queue.add(true, resetPerson);
await queue.wait();
};
const track = async (code: string, properties?: TJsTrackProperties): Promise<void> => {
queue.add(true, trackCodeAction, code, properties);
const track = async (code: string): Promise<void> => {
queue.add(Action.trackCodeAction, true, code);
await queue.wait();
};
const registerRouteChange = async (): Promise<void> => {
queue.add(true, checkPageUrl);
queue.add(checkPageUrl, true);
await queue.wait();
};
const formbricks = {
init,
/** @deprecated Use setup() instead. This method will be removed in a future version */
init: (initConfig: TLegacyConfigInput) => setup(initConfig as unknown as TConfigInput),
setup,
setEmail,
setAttribute,
setAttributes,
setLanguage,
setUserId,
track,
logout,
reset,
registerRouteChange,
getApi,
};
export type TFormbricks = typeof formbricks;

View File

@@ -1,58 +0,0 @@
import { type TJsTrackProperties } from "@formbricks/types/js";
import { Config } from "./config";
import { type InvalidCodeError, type NetworkError, type Result, err, okVoid } from "./errors";
import { Logger } from "./logger";
import { triggerSurvey } from "./widget";
const logger = Logger.getInstance();
const config = Config.getInstance();
export const trackAction = async (
name: string,
alias?: string,
properties?: TJsTrackProperties
): Promise<Result<void, NetworkError>> => {
const aliasName = alias ?? name;
logger.debug(`Formbricks: Action "${aliasName}" tracked`);
// get a list of surveys that are collecting insights
const activeSurveys = config.get().filteredSurveys;
if (Boolean(activeSurveys) && activeSurveys.length > 0) {
for (const survey of activeSurveys) {
for (const trigger of survey.triggers) {
if (trigger.actionClass.name === name) {
await triggerSurvey(survey, name, properties);
}
}
}
} else {
logger.debug("No active surveys to display");
}
return okVoid();
};
export const trackCodeAction = (
code: string,
properties?: TJsTrackProperties
): Promise<Result<void, NetworkError>> | Result<void, InvalidCodeError> => {
const actionClasses = config.get().environmentState.data.actionClasses;
const codeActionClasses = actionClasses.filter((action) => action.type === "code");
const action = codeActionClasses.find((codeActionClass) => codeActionClass.key === code);
if (!action) {
return err({
code: "invalid_code",
message: `${code} action unknown. Please add this action in Formbricks first in order to use it in your code.`,
});
}
return trackAction(action.name, code, properties);
};
export const trackNoCodeAction = (name: string): Promise<Result<void, NetworkError>> => {
return trackAction(name);
};

View File

@@ -1,16 +0,0 @@
import { FormbricksAPI } from "@formbricks/api";
import { Config } from "./config";
export const getApi = (): FormbricksAPI => {
const config = Config.getInstance();
const { environmentId, apiHost } = config.get();
if (!environmentId || !apiHost) {
throw new Error("formbricks.init() must be called before getApi()");
}
return new FormbricksAPI({
apiHost,
environmentId,
});
};

View File

@@ -1,203 +0,0 @@
import { FormbricksAPI } from "@formbricks/api";
import { type TAttributes } from "@formbricks/types/attributes";
import { type ApiErrorResponse } from "@formbricks/types/errors";
import { Config } from "./config";
import { type Result, err, ok, okVoid } from "./errors";
import { Logger } from "./logger";
import { fetchPersonState } from "./person-state";
import { filterSurveys } from "./utils";
const config = Config.getInstance();
const logger = Logger.getInstance();
export const updateAttribute = async (
key: string,
value: string | number
): Promise<
Result<
{
changed: boolean;
message: string;
messages?: string[];
},
ApiErrorResponse
>
> => {
const { apiHost, environmentId } = config.get();
const userId = config.get().personState.data.userId;
if (!userId) {
return err({
code: "network_error",
status: 500,
message: "Missing userId",
url: new URL(`${apiHost}/api/v1/client/${environmentId}/contacts/${userId ?? ""}/attributes`),
responseMessage: "Missing userId",
});
}
const api = new FormbricksAPI({
apiHost,
environmentId,
});
const res = await api.client.attribute.update({ userId, attributes: { [key]: value } });
if (!res.ok) {
const responseError = res.error;
if (responseError.details?.ignore) {
logger.error(responseError.message);
return {
ok: true,
value: {
changed: false,
message: res.error.message,
},
};
}
return err({
code: res.error.code,
status: res.error.status,
message: `Error updating person with userId ${userId}`,
url: new URL(`${apiHost}/api/v1/client/${environmentId}/contacts/${userId}/attributes`),
responseMessage: res.error.message,
});
}
const responseMessages = res.data.messages;
if (responseMessages && responseMessages.length > 0) {
for (const message of responseMessages) {
logger.debug(message);
}
}
if (res.data.changed) {
logger.debug("Attribute updated in Formbricks");
return {
ok: true,
value: {
changed: true,
message: "Attribute updated in Formbricks",
messages: responseMessages,
},
};
}
return {
ok: true,
value: {
changed: false,
message: "Attribute not updated in Formbricks",
messages: responseMessages,
},
};
};
export const updateAttributes = async (
apiHost: string,
environmentId: string,
userId: string,
attributes: TAttributes
): Promise<Result<TAttributes, ApiErrorResponse>> => {
// clean attributes and remove existing attributes if config already exists
const updatedAttributes = { ...attributes };
// send to backend if updatedAttributes is not empty
if (Object.keys(updatedAttributes).length === 0) {
logger.debug("No attributes to update. Skipping update.");
return ok(updatedAttributes);
}
logger.debug(`Updating attributes: ${JSON.stringify(updatedAttributes)}`);
const api = new FormbricksAPI({
apiHost,
environmentId,
});
const res = await api.client.attribute.update({ userId, attributes: updatedAttributes });
if (res.ok) {
if (res.data.messages) {
for (const message of res.data.messages) {
logger.debug(message);
}
}
return ok(updatedAttributes);
}
const responseError = res.error;
if (responseError.details?.ignore) {
logger.error(responseError.message);
return ok(updatedAttributes);
}
return err({
code: responseError.code,
status: responseError.status,
message: `Error updating person with userId ${userId}`,
url: new URL(`${apiHost}/api/v1/client/${environmentId}/people/${userId}/attributes`),
responseMessage: responseError.responseMessage,
});
};
export const setAttributeInApp = async (
key: string,
value: string
): Promise<Result<void, ApiErrorResponse>> => {
if (key === "userId") {
logger.error("Setting userId is no longer supported. Please set the userId in the init call instead.");
return okVoid();
}
const userId = config.get().personState.data.userId;
logger.debug(`Setting attribute: ${key} to value: ${value}`);
if (!userId) {
logger.error(
"UserId not provided, please provide a userId in the init method before setting attributes."
);
return okVoid();
}
const result = await updateAttribute(key, value.toString());
if (result.ok) {
if (result.value.changed) {
const personState = await fetchPersonState(
{
apiHost: config.get().apiHost,
environmentId: config.get().environmentId,
userId,
},
true
);
const filteredSurveys = filterSurveys(config.get().environmentState, personState);
config.update({
...config.get(),
personState,
filteredSurveys,
attributes: {
...config.get().attributes,
[key]: value.toString(),
},
});
}
return okVoid();
}
const error = result.error;
if (error.code === "forbidden") {
logger.error(`Authorization error: ${error.responseMessage ?? ""}`);
}
return err(result.error);
};

Some files were not shown because too many files have changed in this diff Show More