@fluwa-tool/sdk 1.0.39 → 1.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -79,49 +79,133 @@ class XMLHttpRequestInterceptor {
|
|
|
79
79
|
};
|
|
80
80
|
// Logar início imediatamente
|
|
81
81
|
self.requestLogger.logStart(requestMetadata).catch(() => { });
|
|
82
|
+
// Tentar encontrar mock (com pequeno delay para WebSocket carregar)
|
|
83
|
+
const checkMockWithRetry = async () => {
|
|
84
|
+
let mockResponse = await self.mockResolver.resolve(method, url);
|
|
85
|
+
// Se não encontrou, aguardar e tentar novamente
|
|
86
|
+
if (!mockResponse) {
|
|
87
|
+
await new Promise(resolve => setTimeout(resolve, 150));
|
|
88
|
+
mockResponse = await self.mockResolver.resolve(method, url);
|
|
89
|
+
}
|
|
90
|
+
return mockResponse;
|
|
91
|
+
};
|
|
82
92
|
// Interceptar mudanças de estado
|
|
83
93
|
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
94
|
+
// Verificar mock e potencialmente interceptar
|
|
95
|
+
checkMockWithRetry().then(mockResponse => {
|
|
96
|
+
if (mockResponse) {
|
|
97
|
+
// ✅ Mock encontrado - fazer fetch interno para obter a resposta
|
|
98
|
+
requestMetadata.source = types_1.RequestSource.MOCK;
|
|
99
|
+
requestMetadata.status = mockResponse.status || 200;
|
|
100
|
+
requestMetadata.response = mockResponse.response;
|
|
101
|
+
// Aplicar delay se configurado
|
|
102
|
+
const delay = mockResponse.delay || 0;
|
|
103
|
+
setTimeout(() => {
|
|
104
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
105
|
+
requestMetadata.duration = duration;
|
|
106
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
107
|
+
// Simular que a resposta chegou (chamar onreadystatechange)
|
|
108
|
+
if (originalOnReadyStateChange) {
|
|
109
|
+
try {
|
|
110
|
+
// Injetar dados da resposta no xhr
|
|
111
|
+
xhr._fluwaStatus = mockResponse.status || 200;
|
|
112
|
+
xhr._fluwaResponse = mockResponse.response;
|
|
113
|
+
xhr._fluwaResponseText = JSON.stringify(mockResponse.response);
|
|
114
|
+
// Chamar handler como se a requisição tivesse completado
|
|
115
|
+
originalOnReadyStateChange.call(xhr);
|
|
116
|
+
}
|
|
117
|
+
catch (error) {
|
|
118
|
+
self.logger.debug('Erro ao simular resposta mockada:', error);
|
|
119
|
+
// Se der erro, deixar passar para o servidor real
|
|
120
|
+
self.originalSend.apply(xhr, [body]);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}, delay);
|
|
124
|
+
// NÃO chamar send original para requisições mockadas
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
// Sem mock - fazer request real normalmente
|
|
128
|
+
xhr.onreadystatechange = function (event) {
|
|
129
|
+
if (xhr.readyState === 4) {
|
|
130
|
+
// Requisição completou
|
|
131
|
+
requestMetadata.status = xhr.status;
|
|
132
|
+
// Tentar extrair response
|
|
133
|
+
try {
|
|
134
|
+
if (xhr.responseType === '' || xhr.responseType === 'text') {
|
|
135
|
+
if (xhr.responseText) {
|
|
136
|
+
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
else if (xhr.response) {
|
|
140
|
+
if (typeof xhr.response === 'string') {
|
|
141
|
+
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
requestMetadata.response = `[${xhr.responseType}]`;
|
|
145
|
+
}
|
|
93
146
|
}
|
|
94
147
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
148
|
+
catch (e) {
|
|
149
|
+
try {
|
|
150
|
+
if (xhr.responseText) {
|
|
151
|
+
requestMetadata.response = xhr.responseText;
|
|
152
|
+
}
|
|
98
153
|
}
|
|
99
|
-
|
|
100
|
-
requestMetadata.response =
|
|
154
|
+
catch {
|
|
155
|
+
requestMetadata.response = undefined;
|
|
101
156
|
}
|
|
102
157
|
}
|
|
158
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
159
|
+
requestMetadata.duration = duration;
|
|
160
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
103
161
|
}
|
|
104
|
-
|
|
162
|
+
// Chamar handler original
|
|
163
|
+
if (originalOnReadyStateChange) {
|
|
164
|
+
originalOnReadyStateChange.call(this, event);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
// Chamar send original
|
|
168
|
+
self.originalSend.apply(xhr, [body]);
|
|
169
|
+
}).catch(() => {
|
|
170
|
+
// Erro ao verificar mock - fazer request real
|
|
171
|
+
xhr.onreadystatechange = function (event) {
|
|
172
|
+
if (xhr.readyState === 4) {
|
|
173
|
+
requestMetadata.status = xhr.status;
|
|
105
174
|
try {
|
|
106
|
-
if (xhr.
|
|
107
|
-
|
|
175
|
+
if (xhr.responseType === '' || xhr.responseType === 'text') {
|
|
176
|
+
if (xhr.responseText) {
|
|
177
|
+
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
else if (xhr.response) {
|
|
181
|
+
if (typeof xhr.response === 'string') {
|
|
182
|
+
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
requestMetadata.response = `[${xhr.responseType}]`;
|
|
186
|
+
}
|
|
108
187
|
}
|
|
109
188
|
}
|
|
110
|
-
catch {
|
|
111
|
-
|
|
189
|
+
catch (e) {
|
|
190
|
+
try {
|
|
191
|
+
if (xhr.responseText) {
|
|
192
|
+
requestMetadata.response = xhr.responseText;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
requestMetadata.response = undefined;
|
|
197
|
+
}
|
|
112
198
|
}
|
|
199
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
200
|
+
requestMetadata.duration = duration;
|
|
201
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
113
202
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
originalOnReadyStateChange.call(this, event);
|
|
121
|
-
}
|
|
122
|
-
};
|
|
123
|
-
// Chamar send original
|
|
124
|
-
return self.originalSend.apply(this, [body]);
|
|
203
|
+
if (originalOnReadyStateChange) {
|
|
204
|
+
originalOnReadyStateChange.call(this, event);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
self.originalSend.apply(xhr, [body]);
|
|
208
|
+
});
|
|
125
209
|
};
|
|
126
210
|
this.isInstalled = true;
|
|
127
211
|
this.logger.success('XMLHttpRequest interceptor instalado');
|