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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: Unexpected end of JSON input in Webhook Form (#4214)
Ivan Starkovab86c89cde16
diff --git a/packages/sdk/src/resource-loader.test.ts b/packages/sdk/src/resource-loader.test.tsnew file mode 100644index 0000000000..c68a97e570--- /dev/null+++ b/packages/sdk/src/resource-loader.test.ts@@ -0,0 +1,77 @@+import { describe, expect, test, beforeEach, jest } from "@jest/globals";++import { loadResource } from "./resource-loader";+import type { ResourceRequest } from "./schema/resources";++// Mock the fetch function++describe("loadResource", () => {+ let mockFetch: jest.Mock<typeof fetch>;++ beforeEach(() => {+ mockFetch = jest.fn();+ jest.clearAllMocks();+ });++ test("should successfully fetch a resource and return a JSON response", async () => {+ const mockResponse = new Response(JSON.stringify({ key: "value" }), {+ status: 200,+ });+ mockFetch.mockResolvedValue(mockResponse);++ const resourceRequest: ResourceRequest = {+ id: "1",+ name: "resource",+ url: "https://example.com/resource",+ method: "get",+ headers: [],+ body: undefined,+ };++ const result = await loadResource(mockFetch, resourceRequest);++ expect(mockFetch).toHaveBeenCalledWith("https://example.com/resource", {+ method: "get",+ headers: new Headers(),+ });++ expect(result).toEqual({+ data: {+ key: "value",+ },+ ok: true,+ status: 200,+ statusText: "",+ });+ });++ test("should fetch resource successfully with non-JSON response", async () => {+ const mockResponse = new Response("nonjson", {+ status: 200,+ });+ mockFetch.mockResolvedValue(mockResponse);++ const resourceRequest: ResourceRequest = {+ id: "1",+ name: "resource",+ url: "https://example.com/resource",+ method: "get",+ headers: [],+ body: undefined,+ };++ const result = await loadResource(mockFetch, resourceRequest);++ expect(mockFetch).toHaveBeenCalledWith("https://example.com/resource", {+ method: "get",+ headers: new Headers(),+ });++ expect(result).toEqual({+ data: "nonjson",+ ok: true,+ status: 200,+ statusText: "",+ });+ });+});diff --git a/packages/sdk/src/resource-loader.ts b/packages/sdk/src/resource-loader.tsindex bb6c5afdcc..44576caa08 100644--- a/packages/sdk/src/resource-loader.ts+++ b/packages/sdk/src/resource-loader.ts@@ -24,17 +24,22 @@ export const loadResource = async (// cloudflare workers fail when fetching url contains spaces// even though new URL suppose to trim them on parsing by specconst response = await customFetch(url.trim(), requestInit);- let data;- if (- response.ok &&- // accept json by default and when specified explicitly- (response.headers.has("content-type") === false ||- response.headers.get("content-type")?.includes("application/json"))- ) {- data = await response.json();- } else {- data = await response.text();++ let data = await response.text();++ try {+ // If it looks like JSON and quacks like JSON, then it probably is JSON.+ data = JSON.parse(data);+ } catch {+ // ignore, leave data as text}++ if (!response.ok) {+ console.error(+ `Failed to load resource: ${url} - ${response.status}: ${JSON.stringify(data).slice(0, 300)}`+ );+ }+return {ok: response.ok,data,@@ -42,6 +47,7 @@ export const loadResource = async (statusText: response.statusText,};} catch (error) {+ console.error(error);const message = (error as unknown as Error).message;return {ok: false,