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: load inspected external resources in Builder (#6548)

Oleg Isonencb0cc5c1254c
diff --git a/apps/builder/app/builder/features/settings-panel/variable-editors/http-resource-refresh.test.tsx b/apps/builder/app/builder/features/settings-panel/variable-editors/http-resource-refresh.test.tsx
new file mode 100644
index 0000000000..3cd596dfbd
--- /dev/null
+++ b/apps/builder/app/builder/features/settings-panel/variable-editors/http-resource-refresh.test.tsx
@@ -0,0 +1,122 @@
+import { createRef } from "react";
+import { createRoot, type Root } from "react-dom/client";
+import { act } from "react-dom/test-utils";
+import { userEvent } from "@vitest/browser/context";
+import { Dialog, TooltipProvider } from "@webstudio-is/design-system";
+import type { ResourceRequest } from "@webstudio-is/sdk";
+import { afterEach, expect, test, vi } from "vitest";
+
+import { $resources } from "~/shared/sync/data-stores";
+
+(
+ globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }
+).IS_REACT_ACT_ENVIRONMENT = true;
+
+let root: Root | undefined;
+const initialResources = $resources.get();
+
+afterEach(async () => {
+ act(() => root?.unmount());
+ root = undefined;
+ $resources.set(initialResources);
+ const { __testing__ } = await import("~/shared/resources");
+ __testing__.reset();
+ document.body.innerHTML = "";
+ vi.unstubAllGlobals();
+});
+
+test("Resource editor Refresh sends its computed GET to the inspected loader", async () => {
+ const url = "https://api.tilmaplatform.com/api/orgs/holyredeemer/en/";
+ const loaderCalls: Array<{ input: string; init?: RequestInit }> = [];
+ const nativeFetch = globalThis.fetch;
+ vi.stubGlobal(
+ "fetch",
+ async (input: RequestInfo | URL, init?: RequestInit) => {
+ if (String(input).startsWith("/rest/resources-loader")) {
+ loaderCalls.push({ input: String(input), init });
+ return Response.json({ resources: [] });
+ }
+ return nativeFetch(input, init);
+ }
+ );
+ // fetch.client captures fetch at import time.
+ const [{ HttpResourceEditor }, { updateCsrfToken }] = await Promise.all([
+ import("./http-resource-editor"),
+ import("~/shared/csrf.client"),
+ ]);
+ updateCsrfToken("test-token");
+
+ const variable = {
+ id: "holy-redeemer-variable",
+ type: "resource" as const,
+ name: "Holy Redeemer",
+ resourceId: "holy-redeemer-resource",
+ scopeInstanceId: "instance",
+ };
+ $resources.set(
+ new Map([
+ [
+ variable.resourceId,
+ {
+ id: variable.resourceId,
+ name: variable.name,
+ method: "get" as const,
+ url: JSON.stringify(url),
+ headers: [],
+ },
+ ],
+ ])
+ );
+ const container = document.createElement("div");
+ document.body.appendChild(container);
+ root = createRoot(container);
+ await act(async () => {
+ root?.render(
+ <TooltipProvider>
+ <Dialog open>
+ <HttpResourceEditor
+ variable={variable}
+ formRef={createRef<HTMLFormElement>()}
+ onSubmit={vi.fn()}
+ disabled={false}
+ commonFields={
+ <input type="hidden" name="name" value={variable.name} />
+ }
+ title="Edit Resource"
+ titleActions={(options) => (
+ <button
+ type="button"
+ data-testid="refresh-resource"
+ onClick={options?.onRefresh}
+ >
+ Refresh
+ </button>
+ )}
+ value={undefined}
+ onValueChange={vi.fn()}
+ variableType="resource"
+ />
+ </Dialog>
+ </TooltipProvider>
+ );
+ });
+
+ await act(async () => {
+ await userEvent.click(
+ document.querySelector('[data-testid="refresh-resource"]')!
+ );
+ });
+ await vi.waitFor(() => expect(loaderCalls).toHaveLength(1));
+ const [{ input, init }] = loaderCalls;
+ expect(input).toMatch(/^\/rest\/resources-loader\?inspect=/);
+ expect(init?.method).toBe("POST");
+ expect(JSON.parse(String(init?.body)) as ResourceRequest[]).toEqual([
+ {
+ name: variable.name,
+ method: "get",
+ url,
+ searchParams: [],
+ headers: [],
+ },
+ ]);
+});
diff --git a/apps/builder/app/services/preview-form-inspection.server.test.ts b/apps/builder/app/services/preview-form-inspection.server.test.ts
index d6de90964f..9c542219b3 100644
--- a/apps/builder/app/services/preview-form-inspection.server.test.ts
+++ b/apps/builder/app/services/preview-form-inspection.server.test.ts
@@ -146,6 +146,105 @@ test("multipart inspection reports file metadata without upload bytes", async ()
expect(JSON.stringify(snapshot)).not.toContain("private-upload-bytes");
});
+test("prepared multipart inspection does not invent a content-type boundary", async () => {
+ const body = new FormData();
+ body.set("email", "[email protected]");
+ body.set(
+ "file",
+ new File(["private-upload-bytes"], "hello.txt", { type: "text/plain" })
+ );
+ const snapshot = await capturePreviewResourceExchange(
+ "resource",
+ {
+ kind: "http",
+ request: {
+ type: "prepared-http",
+ url: "https://example.com/send",
+ method: "POST",
+ headers: new Headers(),
+ body,
+ },
+ response: {
+ status: 200,
+ statusText: "OK",
+ headers: new Headers(),
+ data: "accepted",
+ },
+ },
+ { publicValues: new Set(["[email protected]"]), privateValues: new Set() }
+ );
+ expect(snapshot.request.headers).toEqual([]);
+ expect(snapshot.request.body).toEqual({
+ email: "[email protected]",
+ file: { name: "hello.txt", type: "text/plain", size: 20 },
+ });
+ expect(JSON.stringify(snapshot)).not.toContain("private-upload-bytes");
+});
+
+test("prepared URL-encoded inspection retains repeated fields", async () => {
+ const snapshot = await capturePreviewResourceExchange(
+ "resource",
+ {
+ kind: "http",
+ request: {
+ type: "prepared-http",
+ url: "https://example.com/send",
+ method: "POST",
+ headers: new Headers({
+ "content-type": "application/x-www-form-urlencoded",
+ }),
+ body: "tag=one&tag=two",
+ },
+ response: {
+ status: 200,
+ statusText: "OK",
+ headers: new Headers(),
+ data: "accepted",
+ },
+ },
+ { publicValues: new Set(["one", "two"]), privateValues: new Set() }
+ );
+ expect(snapshot.request.body).toEqual({ tag: ["one", "two"] });
+});
+
+test.each(["native", "prepared"] as const)(
+ "%s HTTP inspection preserves malformed JSON as text",
+ async (source) => {
+ const body = '{"message":';
+ const url = "https://example.com/send";
+ const headers = new Headers({ "content-type": "application/json" });
+ const request =
+ source === "native"
+ ? new Request(url, { method: "POST", headers, body })
+ : {
+ type: "prepared-http" as const,
+ url,
+ method: "POST",
+ headers,
+ body,
+ };
+ const snapshot = await capturePreviewResourceExchange(
+ "resource",
+ {
+ request,
+ response: {
+ status: 200,
+ statusText: "OK",
+ headers: new Headers(),
+ data: "accepted",
+ },
+ },
+ {
+ publicValues: new Set(),
+ privateValues: new Set(),
+ allowRequestBody: true,
+ }
+ );
+
+ expect(snapshot.request.body).toBe(body);
+ }
+);
+
test("inspection preserves repeated query parameters and sanitizes relative Location credentials", async () => {
const snapshot = await capturePreviewResourceExchange(
"resource",
diff --git a/apps/builder/app/services/preview-form-route.server.test.ts b/apps/builder/app/services/preview-form-route.server.test.ts
index c4600f3c96..83d4c571e6 100644
--- a/apps/builder/app/services/preview-form-route.server.test.ts
+++ b/apps/builder/app/services/preview-form-route.server.test.ts
@@ -194,9 +194,12 @@ test("an unpublished project's current draft executes its HTTP Resource", async
});
const resourceFetch = vi.mocked(createNodeProtectedResourceFetch).mock
.results[0].value;
- expect(resourceFetch).toHaveBeenCalledWith(expect.any(Request));
- const outgoing = resourceFetch.mock.calls[0][0] as Request;
- expect(outgoing.url).toContain("https://example.com/contact");
+ expect(resourceFetch).toHaveBeenCalledWith(
+ "https://example.com/contact",
+ expect.objectContaining({ method: "post" })
+ );
+ const [input, init] = resourceFetch.mock.calls[0];
+ const outgoing = new Request(input, init);
expect(outgoing.method).toBe("POST");
expect(loadDevBuildByProjectId).toHaveBeenCalledWith(
expect.anything(),
@@ -336,7 +339,13 @@ test("Builder Preview executes a legacy Action with an enabled key", async () =>
expect((await response.json()).success).toBe(true);
expect(
vi.mocked(createNodeProtectedResourceFetch).mock.results[0].value
- ).toHaveBeenCalledWith(expect.any(Request));
+ ).toHaveBeenCalledWith(
+ "https://example.com/contact",
+ expect.objectContaining({
+ method: "post",
+ body: '{"email":"[email protected]"}',
+ })
+ );
});
test.each([
@@ -471,7 +480,8 @@ test("Preview Actions resolve the matched page's legacy System binding", async (
const fetch = vi.mocked(createNodeProtectedResourceFetch).mock.results[0]
.value;
expect(fetch).toHaveBeenCalledOnce();
- const outgoing = fetch.mock.calls[0][0] as Request;
+ const [input, init] = fetch.mock.calls[0];
+ const outgoing = new Request(input, init);
expect(await outgoing.json()).toEqual({
pathname: "/contact",
origin: "https://site.wstd.work",
@@ -785,9 +795,9 @@ test.each([false, true])(
"server-only-test-token"
);
} else {
- expect(resourceFetch).toHaveBeenCalledWith(expect.any(Request));
- expect((resourceFetch.mock.calls[0][0] as Request).url).toContain(
- "https://example.com/contact"
+ expect(resourceFetch).toHaveBeenCalledWith(
+ "https://example.com/contact",
+ expect.objectContaining({ method: "post" })
);
}
} finally {
@@ -882,9 +892,12 @@ test("Preview uses changed draft Resource settings without republishing", async
expect(((await response.json()) as { success: boolean }).success).toBe(true);
const resourceFetch = vi.mocked(createNodeProtectedResourceFetch).mock
.results[0].value;
- expect(resourceFetch).toHaveBeenCalledWith(expect.any(Request));
- expect((resourceFetch.mock.calls[0][0] as Request).url).toContain(
- "https://new.example/updated"
+ expect(resourceFetch).toHaveBeenCalledWith(
+ "https://new.example/updated",
+ expect.objectContaining({
+ method: "post",
+ body: '{"email":"[email protected]"}',
+ })
);
});
@@ -932,8 +945,12 @@ test("Preview binds trusted server browser info without forwarding Referer crede
expect(JSON.stringify(responseBody)).not.toContain("local-test-token");
const resourceFetch = vi.mocked(createNodeProtectedResourceFetch).mock
.results[0].value;
- expect(resourceFetch).toHaveBeenCalledWith(expect.any(Request));
- const outgoing = resourceFetch.mock.calls[0][0] as Request;
+ expect(resourceFetch).toHaveBeenCalledWith(
+ "https://example.com/contact",
+ expect.objectContaining({ method: "post" })
+ );
+ const [input, init] = resourceFetch.mock.calls[0];
+ const outgoing = new Request(input, init);
expect(await outgoing.json()).toEqual({
ip: "203.0.113.77",
referrer: `https://p-${projectId}.localhost/`,
@@ -1058,7 +1075,8 @@ test("Preview binds each page's submitted values and browser context to its own
.mocked(createNodeProtectedResourceFetch)
.mock.results.at(-1)?.value;
expect(resourceFetch).toHaveBeenCalledTimes(index + 1);
- const outgoing = resourceFetch.mock.calls[index][0] as Request;
+ const [input, init] = resourceFetch.mock.calls[index];
+ const outgoing = new Request(input, init);
expect(outgoing.url).toBe(`https://example.com/${page.formId}`);
expect(await outgoing.json()).toEqual({
email: page.email,
@@ -1447,7 +1465,8 @@ test("a parent-scope Action with no Body sends entered Form values in the actual
const fetch = vi.mocked(createNodeProtectedResourceFetch).mock.results[0]
.value;
expect(fetch).toHaveBeenCalledTimes(1);
- const outgoing = vi.mocked(fetch).mock.calls[0][0] as Request;
+ const [input, init] = vi.mocked(fetch).mock.calls[0];
+ const outgoing = new Request(input, init);
expect(outgoing.method).toBe("POST");
expect(outgoing.headers.get("content-type")).toBe("application/json");
expect(await outgoing.clone().json()).toEqual({ email: "[email protected]" });
diff --git a/apps/builder/app/services/preview-resource-inspection.server.ts b/apps/builder/app/services/preview-resource-inspection.server.ts
index cd4b2e65f1..58444015ed 100644
--- a/apps/builder/app/services/preview-resource-inspection.server.ts
+++ b/apps/builder/app/services/preview-resource-inspection.server.ts
@@ -1,4 +1,7 @@
-import type { ResourceExchange } from "@webstudio-is/sdk/runtime";
+import type {
+ PreparedHttpRequest,
+ ResourceExchange,
+} from "@webstudio-is/sdk/runtime";
import type { PreviewResourceExchange } from "~/shared/preview-resource-inspection";
const sensitive =
@@ -8,6 +11,20 @@ const publicHeader =
const redacted = "[redacted]";
const maxBodyLength = 64 * 1024;
+const isPreparedHttpRequest = (
+ request: ResourceExchange["request"]
+): request is PreparedHttpRequest =>
+ "type" in request && request.type === "prepared-http";
+
+const formEntries = (fields: FormData | URLSearchParams) => {
+ const body: Record<string, unknown> = {};
+ for (const name of new Set(fields.keys())) {
+ const values = fields.getAll(name);
+ body[name] = values.length === 1 ? values[0] : values;
+ }
+ return body;
+};
+
export const capturePreviewResourceExchange = async (
resourceId: string,
exchange: ResourceExchange,
@@ -73,12 +90,14 @@ export const capturePreviewResourceExchange = async (
const request = exchange.request;
const kind =
exchange.kind ??
- (!(request instanceof Request) && request.control === "email"
+ (!(request instanceof Request) &&
+ !isPreparedHttpRequest(request) &&
+ request.control === "email"
? "email"
: "http");
const visibleValues = new Set([...publicValues, ...publicValueAliases]);
const requestHeaders =
- request instanceof Request
+ request instanceof Request || isPreparedHttpRequest(request)
? request.headers
: new Headers(
request.headers.map(({ name, value }) => [name, String(value)])
@@ -213,23 +232,24 @@ export const capturePreviewResourceExchange = async (
: { body, truncated: limit.truncated };
};
let requestBody: unknown;
- if (request instanceof Request) {
+ if (request instanceof Request || isPreparedHttpRequest(request)) {
const contentType = request.headers.get("content-type") ?? "";
- if (
- /multipart\/form-data|application\/x-www-form-urlencoded/i.test(
+ let body: unknown;
+ if (request instanceof Request) {
+ body = /multipart\/form-data|application\/x-www-form-urlencoded/i.test(
contentType
)
- ) {
- const fields = await request.formData();
- const body: Record<string, unknown> = {};
- for (const name of new Set(fields.keys())) {
- const values = fields.getAll(name);
- body[name] = values.length === 1 ? values[0] : values;
- }
- requestBody = body;
+ ? await request.formData()
+ : await request.text();
} else {
- const body = await request.text();
- if (/application\/json/i.test(contentType)) {
+ body = request.body;
+ }
+ if (body instanceof FormData || body instanceof URLSearchParams) {
+ requestBody = formEntries(body);
+ } else if (typeof body === "string") {
+ if (/application\/x-www-form-urlencoded/i.test(contentType)) {
+ requestBody = formEntries(new URLSearchParams(body));
+ } else if (/application\/json/i.test(contentType)) {
try {
requestBody = JSON.parse(body);
} catch {
@@ -238,6 +258,8 @@ export const capturePreviewResourceExchange = async (
} else {
requestBody = body;
}
+ } else {
+ requestBody = body === undefined ? "" : "[unavailable]";
}
} else {
requestBody = request.email ?? request.body;
diff --git a/apps/builder/app/services/safe-resource-fetch.live.server.test.ts b/apps/builder/app/services/safe-resource-fetch.live.server.test.ts
new file mode 100644
index 0000000000..a68d5c21db
--- /dev/null
+++ b/apps/builder/app/services/safe-resource-fetch.live.server.test.ts
@@ -0,0 +1,275 @@
+import { createServer } from "node:http";
+import { expect, test, vi } from "vitest";
+import type { ResourceRequest } from "@webstudio-is/sdk";
+
+import { safeResourceFetch } from "./safe-resource-fetch.server";
+import {
+ loadResourceRequestList,
+ resourceRequestListSchema,
+} from "./resource-list-loader.server";
+import { getResourceKey } from "~/shared/resource-utils";
+
+// Match the external URL branch in rest.resources-loader.ts, including its
+// string-only transport contract and existing public-address validation.
+const externalResourceFetch: typeof fetch = (input, init) => {
+ if (typeof input !== "string") {
+ return Promise.reject(
+ new TypeError("Resource loader requires a URL string")
+ );
+ }
+ return safeResourceFetch(input, init);
+};
+
+test.each([307, 308])(
+ "inspected Resource POST and PUT bodies survive a %i redirect",
+ async (redirectStatus) => {
+ const originalAllowLocal = process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS;
+ process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS = "true";
+ const received: Array<{ path: string; method: string; body: string }> = [];
+ const server = createServer(async (incoming, outgoing) => {
+ const chunks: Buffer[] = [];
+ for await (const chunk of incoming) {
+ chunks.push(chunk);
+ }
+ const exchange = {
+ path: incoming.url ?? "",
+ method: incoming.method ?? "",
+ body: Buffer.concat(chunks).toString(),
+ };
+ received.push(exchange);
+ if (incoming.url === "/redirect") {
+ outgoing.writeHead(redirectStatus, { location: "/receive" });
+ outgoing.end();
+ } else {
+ outgoing.setHeader("content-type", "application/json");
+ outgoing.end(JSON.stringify(exchange));
+ }
+ });
+ try {
+ await new Promise<void>((resolve) =>
+ server.listen(0, "127.0.0.1", resolve)
+ );
+ const address = server.address();
+ if (address === null || typeof address === "string") {
+ throw new Error("Expected a local test server port");
+ }
+ for (const method of ["post", "put"] as const) {
+ received.length = 0;
+ const resourceRequest = {
+ name: "Redirect inspection",
+ method,
+ url: `http://127.0.0.1:${address.port}/redirect`,
+ searchParams: [],
+ headers: [],
+ body: { id: 42 },
+ } satisfies ResourceRequest;
+ const customFetch = vi.fn(externalResourceFetch);
+ const output = await loadResourceRequestList({
+ request: new Request(
+ "https://p-test.localhost/rest/resources-loader",
+ {
+ method: "POST",
+ }
+ ),
+ requestList: [resourceRequest],
+ sourceOrigin: "https://source.example",
+ includeDiagnostics: false,
+ inspectResourceKey: getResourceKey(resourceRequest),
+ customFetch,
+ });
+ expect(customFetch).toHaveBeenCalledWith(
+ resourceRequest.url,
+ expect.objectContaining({ method, body: '{"id":42}' })
+ );
+ expect(received).toEqual([
+ {
+ path: "/redirect",
+ method: method.toUpperCase(),
+ body: '{"id":42}',
+ },
+ { path: "/receive", method: method.toUpperCase(), body: '{"id":42}' },
+ ]);
+ expect(output.resources[0]?.[1]).toMatchObject({
+ ok: true,
+ status: 200,
+ data: received[1],
+ });
+ expect(output.inspection).toMatchObject({
+ request: { method: method.toUpperCase(), url: resourceRequest.url },
+ response: { status: 200 },
+ });
+ }
+ } finally {
+ await new Promise<void>((resolve, reject) =>
+ server.close((error) => (error ? reject(error) : resolve()))
+ );
+ if (originalAllowLocal === undefined) {
+ delete process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS;
+ } else {
+ process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS = originalAllowLocal;
+ }
+ }
+ }
+);
+
+test("inspected Resource GET and POST requests preserve their methods and bodies", async () => {
+ const originalAllowLocal = process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS;
+ process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS = "true";
+ const server = createServer(async (incoming, outgoing) => {
+ const chunks: Buffer[] = [];
+ for await (const chunk of incoming) {
+ chunks.push(chunk);
+ }
+ outgoing.setHeader("content-type", "application/json");
+ outgoing.end(
+ JSON.stringify({
+ method: incoming.method,
+ header: incoming.headers["x-resource-test"],
+ body: Buffer.concat(chunks).toString(),
+ })
+ );
+ });
+ try {
+ await new Promise<void>((resolve) =>
+ server.listen(0, "127.0.0.1", resolve)
+ );
+ const address = server.address();
+ if (address === null || typeof address === "string") {
+ throw new Error("Expected a local test server port");
+ }
+ const resourceRequest = {
+ name: "Local inspection",
+ method: "post",
+ url: `http://127.0.0.1:${address.port}/receive`,
+ searchParams: [],
+ headers: [{ name: "x-resource-test", value: "preserved" }],
+ body: { id: 42 },
+ } satisfies ResourceRequest;
+ const key = getResourceKey(resourceRequest);
+ const customFetch = vi.fn(externalResourceFetch);
+ const output = await loadResourceRequestList({
+ request: new Request("https://p-test.localhost/rest/resources-loader", {
+ method: "POST",
+ }),
+ requestList: [resourceRequest],
+ sourceOrigin: "https://source.example",
+ includeDiagnostics: false,
+ inspectResourceKey: key,
+ customFetch,
+ });
+ expect(customFetch).toHaveBeenCalledWith(
+ resourceRequest.url,
+ expect.objectContaining({
+ method: "post",
+ headers: expect.any(Headers),
+ body: '{"id":42}',
+ })
+ );
+ expect(output.resources[0]?.[1]).toMatchObject({
+ ok: true,
+ status: 200,
+ data: {
+ method: "POST",
+ header: "preserved",
+ body: '{"id":42}',
+ },
+ });
+ expect(output.inspection).toMatchObject({
+ request: { method: "POST", url: resourceRequest.url },
+ response: { status: 200 },
+ });
+ const getRequest = {
+ name: "Local GET inspection",
+ method: "get",
+ url: resourceRequest.url,
+ searchParams: [],
+ headers: [],
+ } satisfies ResourceRequest;
+ const getFetch = vi.fn(externalResourceFetch);
+ const getOutput = await loadResourceRequestList({
+ request: new Request("https://p-test.localhost/rest/resources-loader", {
+ method: "POST",
+ }),
+ requestList: [getRequest],
+ sourceOrigin: "https://source.example",
+ includeDiagnostics: false,
+ inspectResourceKey: getResourceKey(getRequest),
+ customFetch: getFetch,
+ });
+ expect(getFetch).toHaveBeenCalledWith(
+ getRequest.url,
+ expect.objectContaining({ method: "get", headers: expect.any(Headers) })
+ );
+ expect(getOutput.resources[0]?.[1]).toMatchObject({
+ ok: true,
+ status: 200,
+ data: { method: "GET", body: "" },
+ });
+ expect(getOutput.inspection).toMatchObject({
+ request: { method: "GET", url: getRequest.url },
+ response: { status: 200 },
+ });
+ } finally {
+ await new Promise<void>((resolve, reject) =>
+ server.close((error) => (error ? reject(error) : resolve()))
+ );
+ if (originalAllowLocal === undefined) {
+ delete process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS;
+ } else {
+ process.env.E2E_ALLOW_LOCAL_RESOURCE_URLS = originalAllowLocal;
+ }
+ }
+});
+
+test.skipIf(process.env.WEBSTUDIO_LIVE_RESOURCE_GET !== "1")(
+ "uses the Resource editor request in the inspected server loader path",
+ async () => {
+ const url = "https://api.tilmaplatform.com/api/orgs/holyredeemer/en/";
+ const resourceRequest = {
+ name: "Holy Redeemer",
+ method: "get",
+ url,
+ searchParams: [],
+ headers: [],
+ } satisfies ResourceRequest;
+
+ const key = getResourceKey(resourceRequest);
+ const request = new Request(
+ `https://p-090e6e14-ae50-4b2e-bd22-71733cec05bb.localhost/rest/resources-loader?inspect=${encodeURIComponent(key)}`,
+ { method: "POST", body: JSON.stringify([resourceRequest]) }
+ );
+ const requestList = resourceRequestListSchema.parse(await request.json());
+ const customFetch = vi.fn(externalResourceFetch);
+ const output = await loadResourceRequestList({
+ request,
+ requestList,
+ sourceOrigin: "https://source.example",
+ includeDiagnostics: false,
+ inspectResourceKey: key,
+ customFetch,
+ });
+ expect(customFetch).toHaveBeenCalledWith(
+ url,
+ expect.objectContaining({ method: "get", headers: expect.any(Headers) })
+ );
+ const result = output.resources[0]?.[1];
+ console.info("Inspected Resource editor GET outcome", {
+ ok: (result as { ok?: unknown })?.ok,
+ status: (result as { status?: unknown })?.status,
+ inspectionStatus: output.inspection?.response.status,
+ });
+ expect(result).toMatchObject({
+ ok: true,
+ status: 200,
+ data: {
+ name: expect.any(String),
+ slug: expect.any(String),
+ },
+ });
+ expect(output.inspection).toMatchObject({
+ request: { method: "GET", url },
+ response: { status: 200 },
+ });
+ },
+ 20_000
+);
diff --git a/packages/sdk/src/resource-loader.test.ts b/packages/sdk/src/resource-loader.test.ts
index 3cee970fe7..8c9f720e79 100644
--- a/packages/sdk/src/resource-loader.test.ts
+++ b/packages/sdk/src/resource-loader.test.ts
@@ -19,6 +19,7 @@ import {
isLocalResource,
loadResource,
loadResources,
+ type ResourceExchange,
type ResourceRequestGraph,
} from "./resource-loader";
import type { ResourceRequest } from "./schema/resources";
@@ -617,6 +618,7 @@ test("sends file bodies as multipart and preserves repeated fields and bytes", a
type: "application/octet-stream",
});
let submitted: Request | undefined;
+ let preparedRequest: ResourceExchange["request"] | undefined;
const fetch = vi.fn<typeof globalThis.fetch>(async (input, init) => {
submitted = new Request(input, init);
return Response.json({ accepted: true });
@@ -643,9 +645,28 @@ test("sends file bodies as multipart and preserves repeated fields and bytes", a
rootIds: ["upload"],
};
- await expect(loadResources(fetch, graph)).resolves.toMatchObject({
+ await expect(
+ loadResources(fetch, graph, undefined, {
+ onResourceExchange: (_id, exchange) => {
+ preparedRequest = exchange.request;
+ },
+ })
+ ).resolves.toMatchObject({
Upload: { ok: true, data: { accepted: true } },
});
+ expect(preparedRequest).toMatchObject({
+ type: "prepared-http",
+ method: "POST",
+ url: "https://example.com/upload",
+ body: expect.any(FormData),
+ });
+ if (
+ preparedRequest &&
+ "type" in preparedRequest &&
+ preparedRequest.type === "prepared-http"
+ ) {
+ expect(preparedRequest.headers.has("content-type")).toBe(false);
+ }
expect(submitted?.headers.get("content-type")).toMatch(
/^multipart\/form-data; boundary=/
);
@@ -1837,8 +1858,12 @@ test("optional exchange observer records the same serialized fetch and each retr
}> = [];
const fetch = vi
.fn<typeof globalThis.fetch>()
- .mockImplementation(async (input) => {
- expect(input).toBeInstanceOf(Request);
+ .mockImplementation(async (input, init) => {
+ expect(input).toBe("https://example.com/send?q=submitted");
+ expect(init).toMatchObject({
+ method: "post",
+ body: '{"email":"[email protected]"}',
+ });
const response = Response.json(
{ accepted: fetch.mock.calls.length > 1 },
{
@@ -1872,14 +1897,17 @@ test("optional exchange observer records the same serialized fetch and each retr
await loadResources(fetch, graph, undefined, {
shouldRetryFailedRoot: () => true,
onResourceExchange: async (id, exchange) => {
- if (!(exchange.request instanceof Request)) {
+ if (
+ !("type" in exchange.request) ||
+ exchange.request.type !== "prepared-http"
+ ) {
throw Error("Expected HTTP transport");
}
observed.push({
id,
method: exchange.request.method,
url: exchange.request.url,
- body: await exchange.request.text(),
+ body: String(exchange.request.body),
status: exchange.response.status,
responseHeader: exchange.response.headers.get("X-Response"),
finalUrl: exchange.response.url,
diff --git a/packages/sdk/src/resource-loader.ts b/packages/sdk/src/resource-loader.ts
index 52fde6d17f..9e7c98486d 100644
--- a/packages/sdk/src/resource-loader.ts
+++ b/packages/sdk/src/resource-loader.ts
@@ -157,10 +157,18 @@ export const assetsFieldCatalogApiUrl = `${assetsApiUrl}/field-catalog`;
export const assetsOpenApiUrl = `${assetsApiUrl}/openapi.json`;
export const assetsQuerySchemaApiUrl = `${assetsApiUrl}/query-schema.json`;
+export type PreparedHttpRequest = {
+ type: "prepared-http";
+ url: string;
+ method: string;
+ headers: Headers;
+ body?: RequestInit["body"];
+};
+
export type ResourceExchange = {
/** The configured Resource type; never infer this from the request URL. */
kind?: "http" | "email";
- request: Request | ResourceRequest;
+ request: Request | ResourceRequest | PreparedHttpRequest;
response: {
status: number;
statusText: string;
@@ -429,7 +437,7 @@ export const loadResource = async (
controller.abort();
}, options.timeoutMs);
- let inspectionRequest: Request | undefined;
+ let inspectionRequest: PreparedHttpRequest | undefined;
let inspectionResponse: ResourceExchange["response"] | undefined;
try {
const { method, searchParams, headers, body } = resourceRequest;
@@ -527,16 +535,17 @@ export const loadResource = async (
requestInit.body = serializeValue(body);
}
}
- const outgoing =
- options.onExchange && !isLocalResource(href)
- ? new Request(href, requestInit)
- : undefined;
- // Cloudflare adds generic metadata to clone(); inspection uses Web Request fields.
- inspectionRequest = outgoing?.clone() as Request | undefined;
+ if (options.onExchange && !isLocalResource(href)) {
+ inspectionRequest = {
+ type: "prepared-http",
+ url: href,
+ method: method.toUpperCase(),
+ headers: new Headers(requestHeaders),
+ body: requestInit.body,
+ };
+ }
const response = await awaitWithSignal(
- outgoing === undefined
- ? customFetch(href, requestInit)
- : customFetch(outgoing),
+ customFetch(href, requestInit),
signal
);