Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: manage forms with resources (#3803)

Bogdan Chadkind7531b73b7d0
diff --git a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
index 471d13aba1..d9814fa1ef 100644
--- a/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
+++ b/apps/builder/app/builder/features/settings-panel/controls/combined.tsx
@@ -210,6 +210,12 @@ export const renderControl = ({
);
}
+ if (prop.type === "resource") {
+ throw new Error(
+ `Cannot render a fallback control for prop "${rest.propName}" with type resource`
+ );
+ }
+
prop satisfies never;
}
diff --git a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
index 9c70d4d32a..440d854dac 100644
--- a/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
+++ b/apps/builder/app/builder/features/settings-panel/props-section/use-props-logic.ts
@@ -107,6 +107,10 @@ const getDefaultMetaForType = (type: Prop["type"]): PropMeta => {
throw new Error(
"A prop with type parameter must have a meta, we can't provide a default one because we need a list of options"
);
+ case "resource":
+ throw new Error(
+ "A prop with type resource must have a meta, we can't provide a default one because we need a list of options"
+ );
default:
throw new Error(`Usupported data type: ${type satisfies never}`);
}
diff --git a/fixtures/ssg/.gitignore b/fixtures/ssg/.gitignore
new file mode 100644
index 0000000000..1521c8b765
--- /dev/null
+++ b/fixtures/ssg/.gitignore
@@ -0,0 +1 @@
+dist
diff --git a/fixtures/ssg/app/__generated__/[another-page]._index.server.tsx b/fixtures/ssg/app/__generated__/[another-page]._index.server.tsx
index 7848d01d46..dc6510942c 100644
--- a/fixtures/ssg/app/__generated__/[another-page]._index.server.tsx
+++ b/fixtures/ssg/app/__generated__/[another-page]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/ssg/app/__generated__/_index.server.tsx b/fixtures/ssg/app/__generated__/_index.server.tsx
index ec7f6f9e62..83d760a977 100644
--- a/fixtures/ssg/app/__generated__/_index.server.tsx
+++ b/fixtures/ssg/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/ssg/package.json b/fixtures/ssg/package.json
index d235e3bd35..b9f8f6a7d0 100644
--- a/fixtures/ssg/package.json
+++ b/fixtures/ssg/package.json
@@ -9,7 +9,7 @@
"cli": "NODE_OPTIONS='--conditions=webstudio --import=tsx' webstudio",
"fixtures:link": "pnpm cli link --link \"https://main.development.webstudio.is/builder/d845c167-ea07-4875-b08d-83e97c09dcce?authToken=e9d1343f-9298-4fd3-a66e-f89a5af2dd93\"",
"fixtures:sync": "pnpm cli sync && pnpm prettier --write ./.webstudio/",
- "fixtures:build": "pnpm cli build --template ssg --template internal --preview && pnpm prettier --write ./app/ ./package.json tsconfig.json"
+ "fixtures:build": "rm -rf pages && pnpm cli build --template ssg --template internal --preview && pnpm prettier --write ."
},
"engines": {
"node": ">=20.0.0"
diff --git a/fixtures/ssg/pages/another-page/+data.ts b/fixtures/ssg/pages/another-page/+data.ts
index c2dbbe3f59..bb0883e596 100644
--- a/fixtures/ssg/pages/another-page/+data.ts
+++ b/fixtures/ssg/pages/another-page/+data.ts
@@ -20,7 +20,7 @@ export const data = async (pageContext: PageContextServer) => {
origin: url.origin,
};
- const resources = await loadResources(fetch, getResources({ system }));
+ const resources = await loadResources(fetch, getResources({ system }).data);
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
diff --git a/fixtures/ssg/pages/index/+data.ts b/fixtures/ssg/pages/index/+data.ts
index ea262488b3..c6c77c36bb 100644
--- a/fixtures/ssg/pages/index/+data.ts
+++ b/fixtures/ssg/pages/index/+data.ts
@@ -20,7 +20,7 @@ export const data = async (pageContext: PageContextServer) => {
origin: url.origin,
};
- const resources = await loadResources(fetch, getResources({ system }));
+ const resources = await loadResources(fetch, getResources({ system }).data);
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
diff --git a/fixtures/webstudio-cloudflare-template/app/__generated__/[another-page]._index.server.tsx b/fixtures/webstudio-cloudflare-template/app/__generated__/[another-page]._index.server.tsx
index 7848d01d46..dc6510942c 100644
--- a/fixtures/webstudio-cloudflare-template/app/__generated__/[another-page]._index.server.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/__generated__/[another-page]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-cloudflare-template/app/__generated__/_index.server.tsx b/fixtures/webstudio-cloudflare-template/app/__generated__/_index.server.tsx
index ec7f6f9e62..83d760a977 100644
--- a/fixtures/webstudio-cloudflare-template/app/__generated__/_index.server.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
index 6607c5547c..8622c19e2b 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/[another-page]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[another-page]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
index aa8db5a3c4..65b10c483a 100644
--- a/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-cloudflare-template/app/routes/_index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/_index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-custom-template/app/__generated__/[script-test]._index.server.tsx b/fixtures/webstudio-custom-template/app/__generated__/[script-test]._index.server.tsx
index 4a9f69fd8a..d714e6a4a6 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/[script-test]._index.server.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/[script-test]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-custom-template/app/__generated__/[sitemap.xml]._index.server.tsx b/fixtures/webstudio-custom-template/app/__generated__/[sitemap.xml]._index.server.tsx
index 01e71fb611..5cc00f4ec5 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/[sitemap.xml]._index.server.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/[sitemap.xml]._index.server.tsx
@@ -11,7 +11,11 @@ export const getResources = (_props: { system: System }) => {
method: "get",
headers: [],
};
- return new Map<string, ResourceRequest>([["sitemapxml_1", sitemapxml_1]]);
+ const _data = new Map<string, ResourceRequest>([
+ ["sitemapxml_1", sitemapxml_1],
+ ]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -34,9 +38,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-custom-template/app/__generated__/[world]._index.server.tsx b/fixtures/webstudio-custom-template/app/__generated__/[world]._index.server.tsx
index ca27831095..db57a8a7f4 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/[world]._index.server.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/[world]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-custom-template/app/__generated__/_index.server.tsx b/fixtures/webstudio-custom-template/app/__generated__/_index.server.tsx
index 24ab7bf214..5fd4bcf60b 100644
--- a/fixtures/webstudio-custom-template/app/__generated__/_index.server.tsx
+++ b/fixtures/webstudio-custom-template/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -28,9 +30,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
index 759b546853..9265e97c6d 100644
--- a/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[script-test]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[script-test]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
index 505eb68346..e26898b022 100644
--- a/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[sitemap.xml]._index.tsx
@@ -44,7 +44,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
diff --git a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
index 6bf8247607..99b6ccf3dc 100644
--- a/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/[world]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[world]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-custom-template/app/routes/_index.tsx b/fixtures/webstudio-custom-template/app/routes/_index.tsx
index aa8db5a3c4..65b10c483a 100644
--- a/fixtures/webstudio-custom-template/app/routes/_index.tsx
+++ b/fixtures/webstudio-custom-template/app/routes/_index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/_index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/[another-page]._index.server.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/[another-page]._index.server.tsx
index 7848d01d46..dc6510942c 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/[another-page]._index.server.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/[another-page]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.server.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.server.tsx
index ec7f6f9e62..83d760a977 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.server.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
index 6607c5547c..8622c19e2b 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/[another-page]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[another-page]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
index aa8db5a3c4..65b10c483a 100644
--- a/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-edge-functions/app/routes/_index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/_index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-netlify-functions/app/__generated__/[another-page]._index.server.tsx b/fixtures/webstudio-remix-netlify-functions/app/__generated__/[another-page]._index.server.tsx
index 7848d01d46..dc6510942c 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/__generated__/[another-page]._index.server.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/__generated__/[another-page]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.server.tsx b/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.server.tsx
index ec7f6f9e62..83d760a977 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.server.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
index 6607c5547c..8622c19e2b 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/[another-page]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[another-page]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
index aa8db5a3c4..65b10c483a 100644
--- a/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-netlify-functions/app/routes/_index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/_index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.server.tsx
index d6ece3c487..5d8bac2505 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[_route_with_symbols_]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.server.tsx
index d38af95560..de88d2e4ca 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.server.tsx
@@ -4,7 +4,16 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const action: ResourceRequest = {
+ id: "isNSM3wXcnHFikwNPlEOL",
+ name: "action",
+ url: "/custom",
+ method: "get",
+ headers: [],
+ };
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([["action", action]]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,11 +36,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([
- ["isNSM3wXcnHFikwNPlEOL", { method: "get", action: "/custom" }],
-]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
index 29da662d05..541c01958b 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[form]._index.tsx
@@ -130,8 +130,7 @@ const Page = ({}: { system: any }) => {
formState_1 = state;
set$formState_1(formState_1);
}}
- method={"get"}
- action={"/custom"}
+ action={"action"}
className="w-webhook-form"
>
{(formState_1 === "initial" || formState_1 === "error") && (
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.server.tsx
index b1fe579917..f8f29b5b95 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[heading-with-id]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[nested].[nested-page]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[nested].[nested-page]._index.server.tsx
index 8806493692..04569b1786 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[nested].[nested-page]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[nested].[nested-page]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -27,9 +29,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.server.tsx
index a73dbfece5..5e54def7c2 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[radix]._index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -29,9 +31,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx
index a3b7721772..7277a98955 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/[resources]._index.server.tsx
@@ -11,7 +11,9 @@ export const getResources = (_props: { system: System }) => {
method: "get",
headers: [],
};
- return new Map<string, ResourceRequest>([["list_1", list_1]]);
+ const _data = new Map<string, ResourceRequest>([["list_1", list_1]]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -34,9 +36,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/__generated__/_index.server.tsx b/fixtures/webstudio-remix-vercel/app/__generated__/_index.server.tsx
index a6a608da39..ffa0217c9f 100644
--- a/fixtures/webstudio-remix-vercel/app/__generated__/_index.server.tsx
+++ b/fixtures/webstudio-remix-vercel/app/__generated__/_index.server.tsx
@@ -4,7 +4,9 @@
import type { PageMeta } from "@webstudio-is/sdk";
import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([]);
+ const _data = new Map<string, ResourceRequest>([]);
+ const _action = new Map<string, ResourceRequest>([]);
+ return { data: _data, action: _action };
};
export const getPageMeta = ({
@@ -34,9 +36,6 @@ export const getPageMeta = ({
};
};
-type FormProperties = { method?: string; action?: string };
-export const formsProperties = new Map<string, FormProperties>([]);
-
type Params = Record<string, string | undefined>;
export const getRemixParams = ({ ...params }: Params): Params => {
return params;
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
index 1c00c3fc97..b9ce6211a2 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[_route_with_symbols_]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[_route_with_symbols_]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
index 0ea540821f..345ae907e0 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[form]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[form]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
index 6f23b0c6ea..490c3162b6 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[heading-with-id]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[heading-with-id]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
index afdf134191..0f4e81134f 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[nested].[nested-page]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[nested].[nested-page]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
index c6a247b6f6..f451918590 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[radix]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[radix]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
index dc0a58dc48..a8555e29c9 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/[resources]._index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/[resources]._index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
index aa8db5a3c4..65b10c483a 100644
--- a/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
+++ b/fixtures/webstudio-remix-vercel/app/routes/_index.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "../__generated__/_index";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/packages/cli/src/prebuild.ts b/packages/cli/src/prebuild.ts
index c503e92162..85ff5e8383 100644
--- a/packages/cli/src/prebuild.ts
+++ b/packages/cli/src/prebuild.ts
@@ -42,10 +42,10 @@ import {
findTreeInstanceIds,
getPagePath,
parseComponentName,
- generateFormsProperties,
generateResources,
generatePageMeta,
getStaticSiteMapXml,
+ replaceFormActionsWithResources,
} from "@webstudio-is/sdk";
import type { Data } from "@webstudio-is/http-client";
import { createImageLoader } from "@webstudio-is/image";
@@ -580,6 +580,11 @@ export const prebuild = async (options: {
const props = new Map(pageData.build.props);
const dataSources = new Map(pageData.build.dataSources);
const resources = new Map(pageData.build.resources);
+ replaceFormActionsWithResources({
+ instances,
+ resources,
+ props,
+ });
const pageComponent = generateWebstudioComponent({
scope,
name: "Page",
@@ -679,6 +684,7 @@ export const prebuild = async (options: {
scope,
page: pageData.page,
dataSources,
+ props,
resources,
})}
@@ -689,8 +695,6 @@ export const prebuild = async (options: {
assets,
})}
- ${generateFormsProperties(props)}
-
${generateRemixParams(pageData.page.path)}
export const projectId = "${siteData.build.projectId}";
diff --git a/packages/cli/templates/defaults/app/route-templates/html.tsx b/packages/cli/templates/defaults/app/route-templates/html.tsx
index 8b3eb46eee..565aec5d09 100644
--- a/packages/cli/templates/defaults/app/route-templates/html.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/html.tsx
@@ -25,7 +25,6 @@ import {
pageBackgroundImageAssets,
} from "__CLIENT__";
import {
- formsProperties,
getResources,
getPageMeta,
getRemixParams,
@@ -67,7 +66,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
@@ -258,14 +260,25 @@ export const action = async ({
{ success: true } | { success: false; errors: string[] }
> => {
try {
+ const pageUrl = new URL(request.url);
+ pageUrl.host = getRequestHost(request);
+
const formData = await request.formData();
- const formId = formData.get(formIdFieldName);
+ const system = {
+ params: {},
+ search: {},
+ origin: pageUrl.origin,
+ };
+
+ const resourceName = formData.get(formIdFieldName);
- if (formId == null || typeof formId !== "string") {
+ if (resourceName == null || typeof resourceName !== "string") {
throw new Error("No form id in FormData");
}
+ const resource = getResources({ system }).action.get(resourceName);
+
const formBotValue = formData.get(formBotFieldName);
if (formBotValue == null || typeof formBotValue !== "string") {
@@ -284,34 +297,15 @@ export const action = async ({
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);
-
- 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),
+ action: resource?.url ?? null,
+ method: getMethod(resource?.method),
pageUrl: pageUrl.toString(),
toEmail: contactEmail,
fromEmail: pageUrl.hostname + "@webstudio.email",
diff --git a/packages/cli/templates/defaults/app/route-templates/xml.tsx b/packages/cli/templates/defaults/app/route-templates/xml.tsx
index 5dec649902..5b8629545f 100644
--- a/packages/cli/templates/defaults/app/route-templates/xml.tsx
+++ b/packages/cli/templates/defaults/app/route-templates/xml.tsx
@@ -40,7 +40,10 @@ export const loader = async (arg: LoaderFunctionArgs) => {
origin: url.origin,
};
- const resources = await loadResources(customFetch, getResources({ system }));
+ const resources = await loadResources(
+ customFetch,
+ getResources({ system }).data
+ );
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
diff --git a/packages/cli/templates/ssg/app/route-templates/html/+data.ts b/packages/cli/templates/ssg/app/route-templates/html/+data.ts
index 6d2d1daab5..30a19f54f6 100644
--- a/packages/cli/templates/ssg/app/route-templates/html/+data.ts
+++ b/packages/cli/templates/ssg/app/route-templates/html/+data.ts
@@ -17,7 +17,7 @@ export const data = async (pageContext: PageContextServer) => {
origin: url.origin,
};
- const resources = await loadResources(fetch, getResources({ system }));
+ const resources = await loadResources(fetch, getResources({ system }).data);
const pageMeta = getPageMeta({ system, resources });
if (pageMeta.redirect) {
diff --git a/packages/react-sdk/placeholder.d.ts b/packages/react-sdk/placeholder.d.ts
index 0d03e139f4..1c0a9af14e 100644
--- a/packages/react-sdk/placeholder.d.ts
+++ b/packages/react-sdk/placeholder.d.ts
@@ -30,20 +30,16 @@ declare module "__CLIENT__" {
declare module "__SERVER__" {
import type { PageMeta, System, ResourceRequest } from "@webstudio-is/sdk";
- export const getResources: (props: {
- system: System;
- }) => Map<string, ResourceRequest>;
+ export const getResources: (props: { system: System }) => {
+ data: Map<string, ResourceRequest>;
+ action: Map<string, ResourceRequest>;
+ };
export const getPageMeta: (props: {
system: System;
resources: Record<string, any>;
}) => PageMeta;
- export const formsProperties: Map<
- string,
- { method?: string; action?: string }
- >;
-
type Params = Record<string, string | undefined>;
export const getRemixParams: ({ ...params }: Params) => Params;
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index 904ddfe81f..70dfb231d0 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -8,6 +8,7 @@ import {
ExpressionValue,
PageValue,
ParameterValue,
+ ResourceValue,
createProxy,
renderJsx,
ws,
@@ -840,3 +841,45 @@ test("generate conditional collection", () => {
"
`);
});
+
+test("generate resource prop", () => {
+ expect(
+ generateWebstudioComponent({
+ classesMap: new Map(),
+ scope: createScope(),
+ name: "Page",
+ rootInstanceId: "body",
+ parameters: [],
+ dataSources: new Map(),
+ indexesWithinAncestors: new Map(),
+ ...renderJsx(
+ <$.Body ws:id="body">
+ <$.Form
+ ws:id="form1"
+ action={new ResourceValue("https://my-url.com?with-secret")}
+ ></$.Form>
+ <$.Form
+ ws:id="form2"
+ action={new ResourceValue("https://another-url.com?with-secret")}
+ ></$.Form>
+ </$.Body>
+ ),
+ })
+ ).toMatchInlineSnapshot(`
+ "const Page = () => {
+ return <Body
+ data-ws-id="body"
+ data-ws-component="Body">
+ <Form
+ data-ws-id="form1"
+ data-ws-component="Form"
+ action={"action"} />
+ <Form
+ data-ws-id="form2"
+ data-ws-component="Form"
+ action={"action_1"} />
+ </Body>
+ }
+ "
+ `);
+});
diff --git a/packages/react-sdk/src/component-generator.ts b/packages/react-sdk/src/component-generator.ts
index d0abcae385..5bb99b1ef0 100644
--- a/packages/react-sdk/src/component-generator.ts
+++ b/packages/react-sdk/src/component-generator.ts
@@ -131,6 +131,9 @@ const generatePropValue = ({
if (prop.type === "action") {
return generateAction({ scope, prop, dataSources, usedDataSources });
}
+ if (prop.type === "resource") {
+ return JSON.stringify(scope.getName(prop.value, prop.name));
+ }
prop satisfies never;
};
diff --git a/packages/react-sdk/src/props.ts b/packages/react-sdk/src/props.ts
index b9e4c0379f..04ead58a68 100644
--- a/packages/react-sdk/src/props.ts
+++ b/packages/react-sdk/src/props.ts
@@ -130,12 +130,6 @@ export const indexAttribute = "data-ws-index" as const;
export const collapsedAttribute = "data-ws-collapsed" as const;
export const textContentAttribute = "data-ws-text-content" as const;
-export const getInstanceIdFromComponentProps = (
- props: Record<string, unknown>
-) => {
- return props[idAttribute] as string;
-};
-
export const getIndexWithinAncestorFromComponentProps = (
props: Record<string, unknown>
) => {
diff --git a/packages/sdk-components-react-remix/src/webhook-form.tsx b/packages/sdk-components-react-remix/src/webhook-form.tsx
index 0f244bae57..71b1fd1e03 100644
--- a/packages/sdk-components-react-remix/src/webhook-form.tsx
+++ b/packages/sdk-components-react-remix/src/webhook-form.tsx
@@ -7,7 +7,6 @@ import {
} from "react";
import { useFetcher, type Fetcher, type FormProps } from "@remix-run/react";
import { formIdFieldName } from "@webstudio-is/form-handlers";
-import { getInstanceIdFromComponentProps } from "@webstudio-is/react-sdk";
import { formBotFieldName } from "../../form-handlers/src/shared";
export const defaultTag = "form";
@@ -93,8 +92,6 @@ export const WebhookForm = forwardRef<
) => {
const fetcher = useFetcher<{ success: boolean }>();
- const instanceId = getInstanceIdFromComponentProps(rest);
-
useOnFetchEnd(fetcher, (data) => {
const state: State = data?.success === true ? "success" : "error";
onStateChange?.(state);
@@ -123,7 +120,7 @@ export const WebhookForm = forwardRef<
ref={ref}
onSubmit={handleSubmitAndAddHiddenJsField}
>
- <input type="hidden" name={formIdFieldName} value={instanceId} />
+ <input type="hidden" name={formIdFieldName} value={action} />
{children}
</fetcher.Form>
);
diff --git a/packages/sdk/src/forms-generator.test.ts b/packages/sdk/src/forms-generator.test.ts
deleted file mode 100644
index 11d3496073..0000000000
--- a/packages/sdk/src/forms-generator.test.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-import { expect, test } from "@jest/globals";
-import { generateFormsProperties } from "./forms-generator";
-
-test("generates forms properties", () => {
- expect(
- generateFormsProperties(
- new Map([
- [
- "method1Id",
- {
- id: "method1Id",
- instanceId: "1",
- name: "method",
- type: "string",
- value: "post",
- },
- ],
-
- [
- "method2Id",
- {
- id: "method2Id",
- instanceId: "2",
- name: "method",
- type: "string",
- value: "get",
- },
- ],
-
- [
- "action1Id",
- {
- id: "action1Id",
- instanceId: "2",
- name: "action",
- type: "string",
- value: "/index.php",
- },
- ],
- ])
- )
- ).toMatchInlineSnapshot(`
-"type FormProperties = { method?: string, action?: string }
-export const formsProperties = new Map<string, FormProperties>([["1",{"method":"post"}],["2",{"method":"get","action":"/index.php"}]])
-"
-`);
-
- expect(generateFormsProperties(new Map())).toMatchInlineSnapshot(`
-"type FormProperties = { method?: string, action?: string }
-export const formsProperties = new Map<string, FormProperties>([])
-"
-`);
-});
diff --git a/packages/sdk/src/forms-generator.ts b/packages/sdk/src/forms-generator.ts
deleted file mode 100644
index b813a05ca4..0000000000
--- a/packages/sdk/src/forms-generator.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import type { Props } from "./schema/props";
-
-/**
- * Generates data based utilities at build time
- */
-export const generateFormsProperties = (props: Props) => {
- // method and action per instance extracted from props
- const formsProperties = new Map<
- string,
- { method?: string; action?: string }
- >();
- for (const prop of props.values()) {
- if (prop.type === "string") {
- if (prop.name === "action" || prop.name === "method") {
- let properties = formsProperties.get(prop.instanceId);
- if (properties === undefined) {
- properties = {};
- }
- properties[prop.name] = prop.value;
- formsProperties.set(prop.instanceId, properties);
- }
- }
- }
- const entriesString = JSON.stringify(Array.from(formsProperties.entries()));
- let generated = "";
- generated += `type FormProperties = { method?: string, action?: string }\n`;
- generated += `export const formsProperties = new Map<string, FormProperties>(${entriesString})\n`;
- return generated;
-};
diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts
index d736d3bc93..f7b3284bd5 100644
--- a/packages/sdk/src/index.ts
+++ b/packages/sdk/src/index.ts
@@ -16,7 +16,6 @@ export * from "./page-utils";
export * from "./scope";
export * from "./resource-loader";
export * from "./expression";
-export * from "./forms-generator";
export * from "./resources-generator";
export * from "./page-meta-generator";
export * from "./url-pattern";
diff --git a/packages/sdk/src/jsx.ts b/packages/sdk/src/jsx.ts
index b7de825bc3..d2ea45bc09 100644
--- a/packages/sdk/src/jsx.ts
+++ b/packages/sdk/src/jsx.ts
@@ -16,6 +16,13 @@ export class ParameterValue {
}
}
+export class ResourceValue {
+ value;
+ constructor(resourceId: string) {
+ this.value = resourceId;
+ }
+}
+
export class ActionValue {
value;
constructor(args: string[], code: string) {
@@ -89,6 +96,10 @@ export const renderJsx = (root: JSX.Element) => {
props.set(propId, { ...base, type: "parameter", value: value.value });
continue;
}
+ if (value instanceof ResourceValue) {
+ props.set(propId, { ...base, type: "resource", value: value.value });
+ continue;
+ }
if (value instanceof ActionValue) {
props.set(propId, { ...base, type: "action", value: [value.value] });
continue;
diff --git a/packages/sdk/src/resources-generator.test.ts b/packages/sdk/src/resources-generator.test.ts
index d3119875e5..7d085c7f6e 100644
--- a/packages/sdk/src/resources-generator.test.ts
+++ b/packages/sdk/src/resources-generator.test.ts
@@ -30,11 +30,12 @@ test("generate resources loader", () => {
body: `{ body: true }`,
},
]),
+ props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- const variableName: ResourceRequest = {
+ const resourceName: ResourceRequest = {
id: "resourceId",
name: "resourceName",
url: "https://my-json.com",
@@ -44,9 +45,12 @@ test("generate resources loader", () => {
],
body: { body: true },
}
- return new Map<string, ResourceRequest>([
- ["variableName", variableName],
+ const _data = new Map<string, ResourceRequest>([
+ ["resourceName", resourceName],
])
+ const _action = new Map<string, ResourceRequest>([
+ ])
+ return { data: _data, action: _action }
}
"
`);
@@ -91,12 +95,13 @@ test("generate variable and use in resources loader", () => {
body: `{ body: true }`,
},
]),
+ props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
let AccessToken = "my-token"
- const variableName: ResourceRequest = {
+ const resourceName: ResourceRequest = {
id: "resourceId",
name: "resourceName",
url: "https://my-json.com/",
@@ -106,9 +111,12 @@ test("generate variable and use in resources loader", () => {
],
body: { body: true },
}
- return new Map<string, ResourceRequest>([
- ["variableName", variableName],
+ const _data = new Map<string, ResourceRequest>([
+ ["resourceName", resourceName],
+ ])
+ const _action = new Map<string, ResourceRequest>([
])
+ return { data: _data, action: _action }
}
"
`);
@@ -147,12 +155,13 @@ test("generate system variable and use in resources loader", () => {
body: `{ body: true }`,
},
]),
+ props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
const system = _props.system
- const variableName: ResourceRequest = {
+ const resourceName: ResourceRequest = {
id: "resourceId",
name: "resourceName",
url: "https://my-json.com/" + system?.params?.slug,
@@ -162,9 +171,12 @@ test("generate system variable and use in resources loader", () => {
],
body: { body: true },
}
- return new Map<string, ResourceRequest>([
- ["variableName", variableName],
+ const _data = new Map<string, ResourceRequest>([
+ ["resourceName", resourceName],
+ ])
+ const _action = new Map<string, ResourceRequest>([
])
+ return { data: _data, action: _action }
}
"
`);
@@ -177,12 +189,16 @@ test("generate empty resources loader", () => {
page: { rootInstanceId: "body" } as Page,
dataSources: new Map(),
resources: new Map(),
+ props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([
+ const _data = new Map<string, ResourceRequest>([
])
+ const _action = new Map<string, ResourceRequest>([
+ ])
+ return { data: _data, action: _action }
}
"
`);
@@ -203,12 +219,16 @@ test("prevent generating unused variables", () => {
},
]),
resources: new Map(),
+ props: new Map(),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([
+ const _data = new Map<string, ResourceRequest>([
+ ])
+ const _action = new Map<string, ResourceRequest>([
])
+ return { data: _data, action: _action }
}
"
`);
@@ -231,12 +251,66 @@ test("prevent generating unused system variable", () => {
},
]),
resources: new Map(),
+ props: new Map(),
+ })
+ ).toMatchInlineSnapshot(`
+ "import type { System, ResourceRequest } from "@webstudio-is/sdk";
+ export const getResources = (_props: { system: System }) => {
+ const _data = new Map<string, ResourceRequest>([
+ ])
+ const _action = new Map<string, ResourceRequest>([
+ ])
+ return { data: _data, action: _action }
+ }
+ "
+ `);
+});
+
+test("generate action resource", () => {
+ expect(
+ generateResources({
+ scope: createScope(),
+ page: {
+ rootInstanceId: "body",
+ systemDataSourceId: "variableParamsId",
+ } as Page,
+ dataSources: new Map(),
+ resources: toMap([
+ {
+ id: "resourceId",
+ name: "resourceName",
+ url: `"https://my-url.com"`,
+ method: "post",
+ headers: [],
+ },
+ ]),
+ props: toMap([
+ {
+ id: "propId",
+ instanceId: "body",
+ name: "myProp",
+ type: "resource",
+ value: "resourceId",
+ },
+ ]),
})
).toMatchInlineSnapshot(`
"import type { System, ResourceRequest } from "@webstudio-is/sdk";
export const getResources = (_props: { system: System }) => {
- return new Map<string, ResourceRequest>([
+ const resourceName: ResourceRequest = {
+ id: "resourceId",
+ name: "resourceName",
+ url: "https://my-url.com",
+ method: "post",
+ headers: [
+ ],
+ }
+ const _data = new Map<string, ResourceRequest>([
+ ])
+ const _action = new Map<string, ResourceRequest>([
+ ["resourceName", resourceName],
])
+ return { data: _data, action: _action }
}
"
`);
diff --git a/packages/sdk/src/resources-generator.ts b/packages/sdk/src/resources-generator.ts
index 2be03de987..ed70e172f5 100644
--- a/packages/sdk/src/resources-generator.ts
+++ b/packages/sdk/src/resources-generator.ts
@@ -1,6 +1,8 @@
import type { DataSources } from "./schema/data-sources";
import type { Page } from "./schema/pages";
import type { Resources } from "./schema/resources";
+import type { Props } from "./schema/props";
+import type { Instance, Instances } from "./schema/instances";
import type { Scope } from "./scope";
import { generateExpression } from "./expression";
@@ -8,29 +10,22 @@ export const generateResources = ({
scope,
page,
dataSources,
+ props,
resources,
}: {
scope: Scope;
page: Page;
dataSources: DataSources;
+ props: Props;
resources: Resources;
}) => {
- let generatedRequests = "";
- let generatedPairs = "";
-
const usedDataSources: DataSources = new Map();
- for (const dataSource of dataSources.values()) {
- if (dataSource.type !== "resource") {
- continue;
- }
- const resource = resources.get(dataSource.resourceId);
- if (resource === undefined) {
- continue;
- }
+ let generatedRequests = "";
+ for (const resource of resources.values()) {
let generatedRequest = "";
// call resource by bound variable name
- const resourceName = scope.getName(resource.id, dataSource.name);
+ const resourceName = scope.getName(resource.id, resource.name);
generatedRequest += ` const ${resourceName}: ResourceRequest = {\n`;
generatedRequest += ` id: "${resource.id}",\n`;
generatedRequest += ` name: ${JSON.stringify(resource.name)},\n`;
@@ -65,7 +60,6 @@ export const generateResources = ({
}
generatedRequest += ` }\n`;
generatedRequests += generatedRequest;
- generatedPairs += ` ["${resourceName}", ${resourceName}],\n`;
}
let generatedVariables = "";
@@ -91,9 +85,104 @@ export const generateResources = ({
generated += `export const getResources = (_props: { system: System }) => {\n`;
generated += generatedVariables;
generated += generatedRequests;
- generated += ` return new Map<string, ResourceRequest>([\n`;
- generated += generatedPairs;
+
+ generated += ` const _data = new Map<string, ResourceRequest>([\n`;
+ for (const dataSource of dataSources.values()) {
+ if (dataSource.type === "resource") {
+ const name = scope.getName(dataSource.resourceId, dataSource.name);
+ generated += ` ["${name}", ${name}],\n`;
+ }
+ }
+ generated += ` ])\n`;
+
+ generated += ` const _action = new Map<string, ResourceRequest>([\n`;
+ for (const prop of props.values()) {
+ if (prop.type === "resource") {
+ const name = scope.getName(prop.value, prop.name);
+ generated += ` ["${name}", ${name}],\n`;
+ }
+ }
generated += ` ])\n`;
+
+ generated += ` return { data: _data, action: _action }\n`;
generated += `}\n`;
+
return generated;
};
+
+const getMethod = (value: string | undefined) => {
+ switch (value?.toLowerCase()) {
+ case "get":
+ return "get";
+ case "delete":
+ return "delete";
+ case "put":
+ return "put";
+ default:
+ return "post";
+ }
+};
+
+/**
+ * migrate webhook forms to resource action
+ * @todo move to client migrations eventually
+ */
+export const replaceFormActionsWithResources = ({
+ props,
+ instances,
+ resources,
+}: {
+ props: Props;
+ instances: Instances;
+ resources: Resources;
+}) => {
+ const formProps = new Map<
+ Instance["id"],
+ { method?: string; action?: string }
+ >();
+ for (const prop of props.values()) {
+ if (
+ prop.name === "method" &&
+ prop.type === "string" &&
+ instances.get(prop.instanceId)?.component === "Form"
+ ) {
+ let data = formProps.get(prop.instanceId);
+ if (data === undefined) {
+ data = {};
+ formProps.set(prop.instanceId, data);
+ }
+ data.method = prop.value;
+ props.delete(prop.id);
+ }
+ if (
+ prop.name === "action" &&
+ prop.type === "string" &&
+ instances.get(prop.instanceId)?.component === "Form"
+ ) {
+ let data = formProps.get(prop.instanceId);
+ if (data === undefined) {
+ data = {};
+ formProps.set(prop.instanceId, data);
+ }
+ data.action = prop.value;
+ props.set(prop.id, {
+ id: prop.id,
+ instanceId: prop.instanceId,
+ name: prop.name,
+ type: "resource",
+ value: prop.instanceId,
+ });
+ }
+ }
+ for (const [instanceId, { action, method }] of formProps) {
+ if (action) {
+ resources.set(instanceId, {
+ id: instanceId,
+ name: "action",
+ method: getMethod(method),
+ url: JSON.stringify(action),
+ headers: [],
+ });
+ }
+ }
+};
diff --git a/packages/sdk/src/schema/props.ts b/packages/sdk/src/schema/props.ts
index 9269bfeca6..6848538b12 100644
--- a/packages/sdk/src/schema/props.ts
+++ b/packages/sdk/src/schema/props.ts
@@ -57,6 +57,12 @@ export const Prop = z.union([
// data source id
value: z.string(),
}),
+ z.object({
+ ...baseProp,
+ type: z.literal("resource"),
+ // resource id
+ value: z.string(),
+ }),
z.object({
...baseProp,
type: z.literal("expression"),