|
1 | 1 | import axios from "axios"; |
| 2 | +import { isInIFrame } from "./common"; |
| 3 | +import { v4 as uuidv4 } from "uuid"; |
2 | 4 |
|
3 | 5 | export class Base44Error extends Error { |
4 | 6 | status: number; |
@@ -121,13 +123,57 @@ export function createAxiosClient({ |
121 | 123 | if (typeof window !== "undefined") { |
122 | 124 | config.headers.set("X-Origin-URL", window.location.href); |
123 | 125 | } |
| 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 | + } |
124 | 149 | return config; |
125 | 150 | }); |
126 | 151 |
|
127 | 152 | // Handle responses |
128 | 153 | if (interceptResponses) { |
129 | 154 | 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 | + }, |
131 | 177 | (error) => { |
132 | 178 | const message = |
133 | 179 | error.response?.data?.message || |
|
0 commit comments