Skip to content

Commit c9e5ab7

Browse files
committed
Network logging in iframe
1 parent f738ff7 commit c9e5ab7

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

package-lock.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
},
2121
"dependencies": {
2222
"axios": "^1.6.2",
23-
"socket.io-client": "^4.7.5"
23+
"socket.io-client": "^4.7.5",
24+
"uuid": "^13.0.0"
2425
},
2526
"devDependencies": {
26-
"vitest": "^1.0.0",
27-
"@vitest/ui": "^1.0.0",
2827
"@vitest/coverage-istanbul": "^1.0.0",
2928
"@vitest/coverage-v8": "^1.0.0",
29+
"@vitest/ui": "^1.0.0",
3030
"dotenv": "^16.3.1",
3131
"eslint": "^8.54.0",
3232
"nock": "^13.4.0",
33-
"typescript": "^5.3.2"
33+
"typescript": "^5.3.2",
34+
"vitest": "^1.0.0"
3435
},
3536
"keywords": [
3637
"base44",

src/utils/axios-client.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import axios from "axios";
2+
import { isInIFrame } from "./common";
3+
import { v4 as uuidv4 } from "./common";
24

35
export class Base44Error extends Error {
46
status: number;
@@ -121,13 +123,57 @@ export function createAxiosClient({
121123
if (typeof window !== "undefined") {
122124
config.headers.set("X-Origin-URL", window.location.href);
123125
}
126+
127+
if (isInIFrame) {
128+
const requestId = uuidv4();
129+
try {
130+
window.parent.postMessage(
131+
{
132+
type: "api-request-start",
133+
requestId,
134+
data: {
135+
url: baseURL + config.url,
136+
method: config.method,
137+
body:
138+
config.data instanceof FormData
139+
? "[FormData object]"
140+
: config.data,
141+
},
142+
},
143+
"*"
144+
);
145+
} catch {
146+
/* skip the logging */
147+
}
148+
}
124149
return config;
125150
});
126151

127152
// Handle responses
128153
if (interceptResponses) {
129154
client.interceptors.response.use(
130-
(response) => response.data,
155+
(response) => {
156+
const requestId = (response.config as any)?.requestId;
157+
try {
158+
if (isInIFrame && requestId) {
159+
window.parent.postMessage(
160+
{
161+
type: "api-request-end",
162+
requestId,
163+
data: {
164+
statusCode: response.status,
165+
response: response.data,
166+
},
167+
},
168+
"*"
169+
);
170+
}
171+
} catch {
172+
/* do nothing */
173+
}
174+
175+
return response.data;
176+
},
131177
(error) => {
132178
const message =
133179
error.response?.data?.message ||

src/utils/common.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export const inNode = typeof window === "undefined";
2+
export const isInIFrame = window !== undefined && window.self !== window.top;

0 commit comments

Comments
 (0)