diff --git a/package-lock.json b/package-lock.json index 26b32f4..16675d8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,8 @@ "license": "MIT", "dependencies": { "axios": "^1.6.2", - "socket.io-client": "^4.7.5" + "socket.io-client": "^4.7.5", + "uuid": "^13.0.0" }, "devDependencies": { "@vitest/coverage-istanbul": "^1.0.0", @@ -4025,6 +4026,19 @@ "punycode": "^2.1.0" } }, + "node_modules/uuid": { + "version": "13.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-13.0.0.tgz", + "integrity": "sha512-XQegIaBTVUjSHliKqcnFqYypAd4S+WCYt5NIeRs6w/UAry7z8Y9j5ZwRRL4kzq9U3sD6v+85er9FvkEaBpji2w==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist-node/bin/uuid" + } + }, "node_modules/vite": { "version": "5.4.19", "resolved": "https://registry.npmjs.org/vite/-/vite-5.4.19.tgz", diff --git a/package.json b/package.json index 6d50ea7..40caa79 100644 --- a/package.json +++ b/package.json @@ -20,17 +20,18 @@ }, "dependencies": { "axios": "^1.6.2", - "socket.io-client": "^4.7.5" + "socket.io-client": "^4.7.5", + "uuid": "^13.0.0" }, "devDependencies": { - "vitest": "^1.0.0", - "@vitest/ui": "^1.0.0", "@vitest/coverage-istanbul": "^1.0.0", "@vitest/coverage-v8": "^1.0.0", + "@vitest/ui": "^1.0.0", "dotenv": "^16.3.1", "eslint": "^8.54.0", "nock": "^13.4.0", - "typescript": "^5.3.2" + "typescript": "^5.3.2", + "vitest": "^1.0.0" }, "keywords": [ "base44", diff --git a/src/utils/axios-client.ts b/src/utils/axios-client.ts index f5711d1..72bf010 100644 --- a/src/utils/axios-client.ts +++ b/src/utils/axios-client.ts @@ -1,4 +1,6 @@ import axios from "axios"; +import { isInIFrame } from "./common"; +import { v4 as uuidv4 } from "uuid"; export class Base44Error extends Error { status: number; @@ -121,13 +123,57 @@ export function createAxiosClient({ if (typeof window !== "undefined") { config.headers.set("X-Origin-URL", window.location.href); } + + if (isInIFrame) { + const requestId = uuidv4(); + try { + window.parent.postMessage( + { + type: "api-request-start", + requestId, + data: { + url: baseURL + config.url, + method: config.method, + body: + config.data instanceof FormData + ? "[FormData object]" + : config.data, + }, + }, + "*" + ); + } catch { + /* skip the logging */ + } + } return config; }); // Handle responses if (interceptResponses) { client.interceptors.response.use( - (response) => response.data, + (response) => { + const requestId = (response.config as any)?.requestId; + try { + if (isInIFrame && requestId) { + window.parent.postMessage( + { + type: "api-request-end", + requestId, + data: { + statusCode: response.status, + response: response.data, + }, + }, + "*" + ); + } + } catch { + /* do nothing */ + } + + return response.data; + }, (error) => { const message = error.response?.data?.message || diff --git a/src/utils/common.ts b/src/utils/common.ts new file mode 100644 index 0000000..b3895fb --- /dev/null +++ b/src/utils/common.ts @@ -0,0 +1,2 @@ +export const isNode = typeof window === "undefined"; +export const isInIFrame = !isNode && window.self !== window.top;