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.

fix: copy paste search params in resources (#5412)

Bogdan Chadkinde444a350132
diff --git a/apps/builder/app/shared/copy-paste.test.tsx b/apps/builder/app/shared/copy-paste.test.tsx
index 510e8ea8e0..d12e028744 100644
--- a/apps/builder/app/shared/copy-paste.test.tsx
+++ b/apps/builder/app/shared/copy-paste.test.tsx
@@ -578,6 +578,7 @@ describe("resources", () => {
const resourceVariable = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
@@ -594,6 +595,7 @@ describe("resources", () => {
expect(fragment.resources).toEqual([
expect.objectContaining({
url: "$ws$dataSource$1",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$1" }],
headers: [{ name: "auth", value: "$ws$dataSource$1" }],
body: "$ws$dataSource$1",
}),
@@ -605,6 +607,7 @@ describe("resources", () => {
const resourceVariable = new ResourceValue("Box Resource", {
url: expression`${bodyVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -620,6 +623,7 @@ describe("resources", () => {
expect(fragment.resources).toEqual([
expect.objectContaining({
url: "Body$32$Variable",
+ searchParams: [{ name: "filter", value: "Body$32$Variable" }],
headers: [{ name: "auth", value: "Body$32$Variable" }],
body: "Body$32$Variable",
}),
@@ -631,6 +635,7 @@ describe("resources", () => {
const resourceVariable = new ResourceValue("Box Resource", {
url: expression`${bodyVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -654,11 +659,13 @@ describe("resources", () => {
expect(Array.from(data.resources.values())).toEqual([
expect.objectContaining({
url: "$ws$dataSource$0",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$0" }],
headers: [{ name: "auth", value: "$ws$dataSource$0" }],
body: "$ws$dataSource$0",
}),
expect.objectContaining({
url: "$ws$dataSource$0",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$0" }],
headers: [{ name: "auth", value: "$ws$dataSource$0" }],
body: "$ws$dataSource$0",
}),
@@ -670,6 +677,7 @@ describe("resources", () => {
const resourceProp = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
@@ -685,6 +693,7 @@ describe("resources", () => {
expect(fragment.resources).toEqual([
expect.objectContaining({
url: "$ws$dataSource$1",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$1" }],
headers: [{ name: "auth", value: "$ws$dataSource$1" }],
body: "$ws$dataSource$1",
}),
@@ -696,6 +705,7 @@ describe("resources", () => {
const resourceProp = new ResourceValue("Box Resource", {
url: expression`${bodyVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -709,6 +719,7 @@ describe("resources", () => {
expect(fragment.resources).toEqual([
expect.objectContaining({
url: "Body$32$Variable",
+ searchParams: [{ name: "filter", value: "Body$32$Variable" }],
headers: [{ name: "auth", value: "Body$32$Variable" }],
body: "Body$32$Variable",
}),
@@ -720,6 +731,7 @@ describe("resources", () => {
const resourceProp = new ResourceValue("Box Resource", {
url: expression`${bodyVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -743,11 +755,13 @@ describe("resources", () => {
expect(Array.from(data.resources.values())).toEqual([
expect.objectContaining({
url: "$ws$dataSource$0",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$0" }],
headers: [{ name: "auth", value: "$ws$dataSource$0" }],
body: "$ws$dataSource$0",
}),
expect.objectContaining({
url: "$ws$dataSource$0",
+ searchParams: [{ name: "filter", value: "$ws$dataSource$0" }],
headers: [{ name: "auth", value: "$ws$dataSource$0" }],
body: "$ws$dataSource$0",
}),
@@ -759,12 +773,14 @@ describe("resources", () => {
const resourceProp = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
const resourceVariable = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
@@ -798,12 +814,14 @@ describe("resources", () => {
expect.objectContaining({
id: expect.toSatisfy((value) => value !== fragment.resources[0].id),
url: newVariableIdentifier,
+ searchParams: [{ name: "filter", value: newVariableIdentifier }],
headers: [{ name: "auth", value: newVariableIdentifier }],
body: newVariableIdentifier,
}),
expect.objectContaining({
id: expect.toSatisfy((value) => value !== fragment.resources[1].id),
url: newVariableIdentifier,
+ searchParams: [{ name: "filter", value: newVariableIdentifier }],
headers: [{ name: "auth", value: newVariableIdentifier }],
body: newVariableIdentifier,
}),
@@ -822,12 +840,14 @@ describe("resources", () => {
const resourceProp = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
const resourceVariable = new ResourceValue("Box Resource", {
url: expression`${boxVariable}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${boxVariable}` }],
headers: [{ name: "auth", value: expression`${boxVariable}` }],
body: expression`${boxVariable}`,
});
@@ -865,12 +885,14 @@ describe("resources", () => {
expect.objectContaining({
id: fragment.resources[0].id,
url: oldVariableIdentifier,
+ searchParams: [{ name: "filter", value: oldVariableIdentifier }],
headers: [{ name: "auth", value: oldVariableIdentifier }],
body: oldVariableIdentifier,
}),
expect.objectContaining({
id: fragment.resources[1].id,
url: oldVariableIdentifier,
+ searchParams: [{ name: "filter", value: oldVariableIdentifier }],
headers: [{ name: "auth", value: oldVariableIdentifier }],
body: oldVariableIdentifier,
}),
diff --git a/apps/builder/app/shared/data-variables.test.tsx b/apps/builder/app/shared/data-variables.test.tsx
index 8720bc6369..a34599eff9 100644
--- a/apps/builder/app/shared/data-variables.test.tsx
+++ b/apps/builder/app/shared/data-variables.test.tsx
@@ -216,14 +216,16 @@ test("find unset variable names", () => {
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`six`,
method: "post",
- headers: [{ name: "auth", value: expression`seven` }],
- body: expression`eight`,
+ searchParams: [{ name: "filter", value: expression`seven` }],
+ headers: [{ name: "auth", value: expression`eight` }],
+ body: expression`nine`,
});
const resourceProp = new ResourceValue("resourceProp", {
- url: expression`nine`,
+ url: expression`ten`,
method: "post",
- headers: [{ name: "auth", value: expression`ten` }],
- body: expression`eleven`,
+ searchParams: [{ name: "filter", value: expression`eleven` }],
+ headers: [{ name: "auth", value: expression`twelve` }],
+ body: expression`thirteen`,
});
const data = renderData(
<$.Body ws:id="body" data-prop={expression`two`}>
@@ -245,11 +247,13 @@ test("find unset variable names", () => {
"three",
"four",
"six",
- "seven",
"eight",
+ "seven",
"nine",
"ten",
+ "twelve",
"eleven",
+ "thirteen",
]);
});
@@ -403,12 +407,14 @@ test("restore tree variables in resources", () => {
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`one + 1`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`one + 1` }],
headers: [{ name: "auth", value: expression`one + 1` }],
body: expression`one + 1`,
});
const resourceProp = new ResourceValue("resourceProp", {
url: expression`one + 2`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`one + 2` }],
headers: [{ name: "auth", value: expression`one + 2` }],
body: expression`one + 2`,
});
@@ -439,12 +445,14 @@ test("restore tree variables in resources", () => {
expect.objectContaining({
url: `${boxIdentifier} + 1`,
method: "post",
+ searchParams: [{ name: "filter", value: `${boxIdentifier} + 1` }],
headers: [{ name: "auth", value: `${boxIdentifier} + 1` }],
body: `${boxIdentifier} + 1`,
}),
expect.objectContaining({
url: `${boxIdentifier} + 2`,
method: "post",
+ searchParams: [{ name: "filter", value: `${boxIdentifier} + 2` }],
headers: [{ name: "auth", value: `${boxIdentifier} + 2` }],
body: `${boxIdentifier} + 2`,
}),
@@ -457,12 +465,14 @@ test("rebind tree variables in resources", () => {
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`${bodyVariable}`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
const resourceProp = new ResourceValue("resourceProp", {
url: expression`${bodyVariable}`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -493,12 +503,14 @@ test("rebind tree variables in resources", () => {
expect.objectContaining({
url: boxIdentifier,
method: "post",
+ searchParams: [{ name: "filter", value: boxIdentifier }],
headers: [{ name: "auth", value: boxIdentifier }],
body: boxIdentifier,
}),
expect.objectContaining({
url: boxIdentifier,
method: "post",
+ searchParams: [{ name: "filter", value: boxIdentifier }],
headers: [{ name: "auth", value: boxIdentifier }],
body: boxIdentifier,
}),
@@ -663,12 +675,14 @@ test("delete variable and unset it in resources", () => {
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`${bodyVariable}`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
const resourceProp = new ResourceValue("resourceProp", {
url: expression`${bodyVariable}`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`${bodyVariable}` }],
headers: [{ name: "auth", value: expression`${bodyVariable}` }],
body: expression`${bodyVariable}`,
});
@@ -691,12 +705,14 @@ test("delete variable and unset it in resources", () => {
expect.objectContaining({
url: "bodyVariable",
method: "post",
+ searchParams: [{ name: "filter", value: "bodyVariable" }],
headers: [{ name: "auth", value: "bodyVariable" }],
body: "bodyVariable",
}),
expect.objectContaining({
url: "bodyVariable",
method: "post",
+ searchParams: [{ name: "filter", value: "bodyVariable" }],
headers: [{ name: "auth", value: "bodyVariable" }],
body: "bodyVariable",
}),
diff --git a/apps/builder/app/shared/data-variables.ts b/apps/builder/app/shared/data-variables.ts
index 9df59f68a6..f9a9c49b81 100644
--- a/apps/builder/app/shared/data-variables.ts
+++ b/apps/builder/app/shared/data-variables.ts
@@ -406,6 +406,12 @@ const traverseExpressions = ({
for (const header of resource.headers) {
header.value = update(header.value, instanceId) ?? header.value;
}
+ if (resource.searchParams) {
+ for (const searchParam of resource.searchParams) {
+ searchParam.value =
+ update(searchParam.value, instanceId) ?? searchParam.value;
+ }
+ }
if (resource.body) {
resource.body = update(resource.body, instanceId) ?? resource.body;
}
diff --git a/apps/builder/app/shared/instance-utils.test.tsx b/apps/builder/app/shared/instance-utils.test.tsx
index a6db4c0d71..4b6a71d162 100644
--- a/apps/builder/app/shared/instance-utils.test.tsx
+++ b/apps/builder/app/shared/instance-utils.test.tsx
@@ -932,6 +932,7 @@ describe("delete instance", () => {
const myResource = new ResourceValue("My Resource", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
const data = renderData(
@@ -953,6 +954,7 @@ describe("delete instance", () => {
const myResource = new ResourceValue("My Resource", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
const data = renderData(
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 0519e9bff4..a251e81521 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -826,6 +826,14 @@ export const extractWebstudioFragment = (
unsetNameById,
});
}
+ if (newResource.searchParams) {
+ for (const searchParam of newResource.searchParams) {
+ searchParam.value = unsetExpressionVariables({
+ expression: searchParam.value,
+ unsetNameById,
+ });
+ }
+ }
if (newResource.body) {
newResource.body = unsetExpressionVariables({
expression: newResource.body,
@@ -1174,6 +1182,18 @@ export const insertWebstudioFragmentCopy = ({
});
header.value = replaceDataSources(header.value, newDataSourceIds);
}
+ if (resource.searchParams) {
+ for (const searchParam of resource.searchParams) {
+ searchParam.value = restoreExpressionVariables({
+ expression: searchParam.value,
+ maskedIdByName,
+ });
+ searchParam.value = replaceDataSources(
+ searchParam.value,
+ newDataSourceIds
+ );
+ }
+ }
if (resource.body) {
resource.body = restoreExpressionVariables({
expression: resource.body,
diff --git a/apps/builder/app/shared/nano-states/props.test.tsx b/apps/builder/app/shared/nano-states/props.test.tsx
index 0df4b0e583..507ddafc73 100644
--- a/apps/builder/app/shared/nano-states/props.test.tsx
+++ b/apps/builder/app/shared/nano-states/props.test.tsx
@@ -797,6 +797,7 @@ test("compute resource variable values", () => {
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
const data = renderData(
@@ -868,6 +869,7 @@ test("compute parameter and resource variables without values to make it availab
const resourceVariable = new ResourceValue("resourceVariable", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
const parameterVariable = new Parameter("parameterVariable");
diff --git a/packages/react-sdk/src/component-generator.test.tsx b/packages/react-sdk/src/component-generator.test.tsx
index 8f1520e419..19cb23f238 100644
--- a/packages/react-sdk/src/component-generator.test.tsx
+++ b/packages/react-sdk/src/component-generator.test.tsx
@@ -707,6 +707,7 @@ test("generate resources loading", () => {
const dataResource = new ResourceValue("data", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
expect(
@@ -747,6 +748,7 @@ test("avoid generating unused variables", () => {
const unusedResource = new ResourceValue("Unused Resource Name", {
url: expression`""`,
method: "get",
+ searchParams: [],
headers: [],
});
const data = renderData(
@@ -883,11 +885,13 @@ test("generate resource prop", () => {
const myResource = new ResourceValue("myResource", {
url: expression`"https://my-url.com?with-secret"`,
method: "get",
+ searchParams: [],
headers: [],
});
const anotherResource = new ResourceValue("anotherResource", {
url: expression`"https://another-url.com?with-secret"`,
method: "get",
+ searchParams: [],
headers: [],
});
expect(
diff --git a/packages/sdk/src/resources-generator.test.tsx b/packages/sdk/src/resources-generator.test.tsx
index 9e621ac58e..85d50867e0 100644
--- a/packages/sdk/src/resources-generator.test.tsx
+++ b/packages/sdk/src/resources-generator.test.tsx
@@ -199,6 +199,7 @@ test("generate global system variable and use in resources loader", () => {
const myResource = new ResourceValue("My Resource", {
url: expression`"https://my-json.com/" + $ws$system.params.slug`,
method: "post",
+ searchParams: [{ name: "filter", value: expression`{search:'term'}` }],
headers: [{ name: "Content-Type", value: expression`"application/json"` }],
body: expression`{ body: true }`,
});
@@ -220,6 +221,7 @@ test("generate global system variable and use in resources loader", () => {
name: "My Resource",
url: "https://my-json.com/" + system?.params?.slug,
searchParams: [
+ { name: "filter", value: {search:'term'} },
],
method: "post",
headers: [
diff --git a/packages/template/src/jsx.test.tsx b/packages/template/src/jsx.test.tsx
index 1ea217f940..0bddf27cb6 100644
--- a/packages/template/src/jsx.test.tsx
+++ b/packages/template/src/jsx.test.tsx
@@ -517,6 +517,7 @@ test("render resource variable", () => {
const myResource = new ResourceValue("myResource", {
url: expression`"https://my-url.com/" + ${value}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${value}` }],
headers: [{ name: "auth", value: expression`${value}` }],
body: expression`${value}`,
});
@@ -545,6 +546,7 @@ test("render resource variable", () => {
name: "myResource",
url: `"https://my-url.com/" + $ws$dataSource$1`,
method: "get",
+ searchParams: [{ name: "filter", value: `$ws$dataSource$1` }],
headers: [{ name: "auth", value: `$ws$dataSource$1` }],
body: `$ws$dataSource$1`,
},
@@ -556,6 +558,7 @@ test("render resource prop", () => {
const myResource = new ResourceValue("myResource", {
url: expression`"https://my-url.com/" + ${value}`,
method: "get",
+ searchParams: [{ name: "filter", value: expression`${value}` }],
headers: [{ name: "auth", value: expression`${value}` }],
body: expression`${value}`,
});
@@ -586,6 +589,7 @@ test("render resource prop", () => {
name: "myResource",
url: `"https://my-url.com/" + $ws$dataSource$1`,
method: "get",
+ searchParams: [{ name: "filter", value: `$ws$dataSource$1` }],
headers: [{ name: "auth", value: `$ws$dataSource$1` }],
body: `$ws$dataSource$1`,
},
diff --git a/packages/template/src/jsx.ts b/packages/template/src/jsx.ts
index a55e6af1c8..3189da1f9d 100644
--- a/packages/template/src/jsx.ts
+++ b/packages/template/src/jsx.ts
@@ -35,6 +35,7 @@ export class Parameter {
type ResourceConfig = {
url: Expression;
method: Resource["method"];
+ searchParams: Array<{ name: string; value: Expression }>;
headers: Array<{ name: string; value: Expression }>;
body?: Expression;
};
@@ -218,6 +219,12 @@ export const renderTemplate = (
name: resourceValue.name,
url: compileExpression(instanceId, resourceValue.config.url),
method: resourceValue.config.method,
+ searchParams: resourceValue.config.searchParams.map(
+ ({ name, value }) => ({
+ name,
+ value: compileExpression(instanceId, value),
+ })
+ ),
headers: resourceValue.config.headers.map(({ name, value }) => ({
name,
value: compileExpression(instanceId, value),