Skip to content

Commit

Permalink
Add fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
istarkov committed May 13, 2024
1 parent b7a5d49 commit 85e0e59
Show file tree
Hide file tree
Showing 13 changed files with 858 additions and 650 deletions.
116 changes: 66 additions & 50 deletions fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from "@remix-run/server-runtime";
import { useLoaderData } from "@remix-run/react";
import { ReactSdkContext } from "@webstudio-is/react-sdk";
import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import {
n8nHandler,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/form-handlers";
import {
Page,
siteName,
Expand Down Expand Up @@ -241,65 +245,77 @@ const getMethod = (value: string | undefined) => {
};

export const action = async ({ request, context }: ActionFunctionArgs) => {
const formData = await request.formData();
try {
const formData = await request.formData();

const formId = getFormId(formData);
if (formId === undefined) {
// We're throwing rather than returning { success: false }
// because this isn't supposed to happen normally: bug or malicious user
throw json("Form not found", { status: 404 });
}
const formId = formData.get(formIdFieldName);

const formProperties = formsProperties.get(formId);
if (formId == null || typeof formId !== "string") {
throw new Error("No form id in FormData");
}

// form properties are not defined when defaults are used
const { action, method } = formProperties ?? {};
const formBotValue = formData.get(formBotFieldName);

if (contactEmail === undefined) {
return { success: false };
}
if (formBotValue == null || typeof formBotValue !== "string") {
throw new Error("Form bot field not found");
}

// wrapped in try/catch just in cases new URL() throws
// (should not happen)
let pageUrl: URL;
try {
pageUrl = new URL(request.url);
const submitTime = parseInt(formBotValue, 16);
// 5 minutes
if (Math.abs(Date.now() - submitTime) > 1000 * 60 * 5) {
throw new Error(`Form bot value invalid ${formBotValue}`);
}

const formProperties = formsProperties.get(formId);

// form properties are not defined when defaults are used
const { action, method } = formProperties ?? {};

if (contactEmail === undefined) {
throw new Error("Contact email not found");
}

const pageUrl = new URL(request.url);
pageUrl.host = getRequestHost(request);
} catch {
return { success: false };
}

if (action !== undefined) {
try {
// Test that action is full URL
new URL(action);
} catch {
return json(
{
success: false,
error: "Invalid action URL, must be valid http/https protocol",
},
{ status: 200 }
);
if (action !== undefined) {
try {
// Test that action is full URL
new URL(action);
} catch {
throw new Error(
"Invalid action URL, must be valid http/https protocol"
);
}
}
}

const formInfo = {
formData,
projectId,
action: action ?? null,
method: getMethod(method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
} as const;

const result = await n8nHandler({
formInfo,
hookUrl: context.N8N_FORM_EMAIL_HOOK,
});
const formInfo = {
formData,
projectId,
action: action ?? null,
method: getMethod(method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
} as const;

const result = await n8nHandler({
formInfo,
hookUrl: context.N8N_FORM_EMAIL_HOOK,
});

return result;
return result;
} catch (error) {
console.error(error);

return json(
{
success: false,
error: error instanceof Error ? error.message : "Unknown error",
},
{ status: 200 }
);
}
};

const Outlet = () => {
Expand Down
116 changes: 66 additions & 50 deletions fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import {
} from "@remix-run/server-runtime";
import { useLoaderData } from "@remix-run/react";
import { ReactSdkContext } from "@webstudio-is/react-sdk";
import { n8nHandler, getFormId } from "@webstudio-is/form-handlers";
import {
n8nHandler,
formIdFieldName,
formBotFieldName,
} from "@webstudio-is/form-handlers";
import {
Page,
siteName,
Expand Down Expand Up @@ -241,65 +245,77 @@ const getMethod = (value: string | undefined) => {
};

export const action = async ({ request, context }: ActionFunctionArgs) => {
const formData = await request.formData();
try {
const formData = await request.formData();

const formId = getFormId(formData);
if (formId === undefined) {
// We're throwing rather than returning { success: false }
// because this isn't supposed to happen normally: bug or malicious user
throw json("Form not found", { status: 404 });
}
const formId = formData.get(formIdFieldName);

const formProperties = formsProperties.get(formId);
if (formId == null || typeof formId !== "string") {
throw new Error("No form id in FormData");
}

// form properties are not defined when defaults are used
const { action, method } = formProperties ?? {};
const formBotValue = formData.get(formBotFieldName);

if (contactEmail === undefined) {
return { success: false };
}
if (formBotValue == null || typeof formBotValue !== "string") {
throw new Error("Form bot field not found");
}

// wrapped in try/catch just in cases new URL() throws
// (should not happen)
let pageUrl: URL;
try {
pageUrl = new URL(request.url);
const submitTime = parseInt(formBotValue, 16);
// 5 minutes
if (Math.abs(Date.now() - submitTime) > 1000 * 60 * 5) {
throw new Error(`Form bot value invalid ${formBotValue}`);
}

const formProperties = formsProperties.get(formId);

// form properties are not defined when defaults are used
const { action, method } = formProperties ?? {};

if (contactEmail === undefined) {
throw new Error("Contact email not found");
}

const pageUrl = new URL(request.url);
pageUrl.host = getRequestHost(request);
} catch {
return { success: false };
}

if (action !== undefined) {
try {
// Test that action is full URL
new URL(action);
} catch {
return json(
{
success: false,
error: "Invalid action URL, must be valid http/https protocol",
},
{ status: 200 }
);
if (action !== undefined) {
try {
// Test that action is full URL
new URL(action);
} catch {
throw new Error(
"Invalid action URL, must be valid http/https protocol"
);
}
}
}

const formInfo = {
formData,
projectId,
action: action ?? null,
method: getMethod(method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
} as const;

const result = await n8nHandler({
formInfo,
hookUrl: context.N8N_FORM_EMAIL_HOOK,
});
const formInfo = {
formData,
projectId,
action: action ?? null,
method: getMethod(method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
} as const;

const result = await n8nHandler({
formInfo,
hookUrl: context.N8N_FORM_EMAIL_HOOK,
});

return result;
return result;
} catch (error) {
console.error(error);

return json(
{
success: false,
error: error instanceof Error ? error.message : "Unknown error",
},
{ status: 200 }
);
}
};

const Outlet = () => {
Expand Down
Loading

0 comments on commit 85e0e59

Please sign in to comment.