fix: added webhook url test for a valid URL (#4007)

Co-authored-by: Matthias Nannt <mail@matthiasnannt.com>
Co-authored-by: Johannes <72809645+jobenjada@users.noreply.github.com>
This commit is contained in:
Aditya Deshlahre
2024-10-26 02:48:38 +05:30
committed by GitHub
parent cdb8199199
commit ef7df0fc77
3 changed files with 49 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import { triggers } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/HardcodedTriggers";
import { SurveyCheckboxGroup } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/SurveyCheckboxGroup";
import { TriggerCheckboxGroup } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/TriggerCheckboxGroup";
import { validWebHookURL } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/lib/utils";
import clsx from "clsx";
import { Webhook } from "lucide-react";
import { useRouter } from "next/navigation";
@@ -42,6 +43,11 @@ export const AddWebhookModal = ({ environmentId, surveys, open, setOpen }: AddWe
const handleTestEndpoint = async (sendSuccessToast: boolean) => {
try {
const { valid, error } = validWebHookURL(testEndpointInput);
if (!valid) {
toast.error(error ?? "Something went wrong please try again!");
return;
}
setHittingEndpoint(true);
await testEndpointAction({ url: testEndpointInput });
setHittingEndpoint(false);

View File

@@ -3,6 +3,7 @@
import { triggers } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/HardcodedTriggers";
import { SurveyCheckboxGroup } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/SurveyCheckboxGroup";
import { TriggerCheckboxGroup } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/components/TriggerCheckboxGroup";
import { validWebHookURL } from "@/app/(app)/environments/[environmentId]/integrations/webhooks/lib/utils";
import clsx from "clsx";
import { TrashIcon } from "lucide-react";
import { useRouter } from "next/navigation";
@@ -46,6 +47,11 @@ export const WebhookSettingsTab = ({ webhook, surveys, setOpen }: ActionSettings
const handleTestEndpoint = async (sendSuccessToast: boolean) => {
try {
const { valid, error } = validWebHookURL(testEndpointInput);
if (!valid) {
toast.error(error ?? "Something went wrong please try again!");
return;
}
setHittingEndpoint(true);
await testEndpointAction({ url: testEndpointInput });
setHittingEndpoint(false);

View File

@@ -0,0 +1,37 @@
export const validWebHookURL = (urlInput: string) => {
const trimmedInput = urlInput.trim();
if (!trimmedInput) {
return { valid: false, error: "Please enter a URL" };
}
try {
const url = new URL(trimmedInput);
if (url.protocol !== "https:") {
return { valid: false, error: "URL must start with https://" };
}
const domainError: string =
"Please enter a complete URL with a valid domain (e.g., https://formbricks.com)";
const multipleSlashesPattern = /(?<!:)\/\/+/;
if (multipleSlashesPattern.test(trimmedInput)) {
return {
valid: false,
error: domainError,
};
}
const validDomainPattern = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!validDomainPattern.test(url.hostname)) {
return {
valid: false,
error: domainError,
};
}
return { valid: true };
} catch (error) {
return { valid: false, error: "Invalid URL format. Please enter a complete URL including https://" };
}
};