@fluwa-tool/sdk 1.0.41 → 1.0.43
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.
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Interceptador de XMLHttpRequest
|
|
3
|
-
*
|
|
3
|
+
* Usa fetch interno quando há mock para evitar problemas em React Native
|
|
4
4
|
*/
|
|
5
5
|
import { ILogger } from '../../core/Logger';
|
|
6
6
|
import { IRequestLogger } from './RequestLogger';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/**
|
|
3
3
|
* Interceptador de XMLHttpRequest
|
|
4
|
-
*
|
|
4
|
+
* Usa fetch interno quando há mock para evitar problemas em React Native
|
|
5
5
|
*/
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.XMLHttpRequestInterceptor = void 0;
|
|
@@ -56,7 +56,7 @@ class XMLHttpRequestInterceptor {
|
|
|
56
56
|
const requestId = xhr._fluwaRequestId;
|
|
57
57
|
const headers = xhr._fluwaHeaders || {};
|
|
58
58
|
if (self.urlFilter && !self.urlFilter.shouldIntercept(url)) {
|
|
59
|
-
self.logger.debug(`🔧 [XMLHttpRequestInterceptor] Ignorando URL excluída: ${url}
|
|
59
|
+
self.logger.debug(`🔧 [XMLHttpRequestInterceptor] Ignorando URL excluída: ${url}`);
|
|
60
60
|
return self.originalSend.apply(this, [body]);
|
|
61
61
|
}
|
|
62
62
|
const requestMetadata = {
|
|
@@ -71,7 +71,6 @@ class XMLHttpRequestInterceptor {
|
|
|
71
71
|
source: types_1.RequestSource.REAL,
|
|
72
72
|
};
|
|
73
73
|
self.requestLogger.logStart(requestMetadata).catch(() => { });
|
|
74
|
-
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
75
74
|
// Verificar mock com retry
|
|
76
75
|
const checkMockWithRetry = async () => {
|
|
77
76
|
let mockResponse = await self.mockResolver.resolve(method, url);
|
|
@@ -83,39 +82,82 @@ class XMLHttpRequestInterceptor {
|
|
|
83
82
|
};
|
|
84
83
|
checkMockWithRetry().then(mockResponse => {
|
|
85
84
|
if (mockResponse) {
|
|
86
|
-
// Mock encontrado - simular resposta
|
|
85
|
+
// Mock encontrado - simular resposta do xhr
|
|
87
86
|
requestMetadata.source = types_1.RequestSource.MOCK;
|
|
88
87
|
requestMetadata.status = mockResponse.status || 200;
|
|
89
88
|
requestMetadata.response = mockResponse.response;
|
|
90
89
|
const delay = mockResponse.delay || 0;
|
|
90
|
+
// Disparar eventos de forma assíncrona para o axios processar
|
|
91
91
|
setTimeout(() => {
|
|
92
|
-
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
93
|
-
requestMetadata.duration = duration;
|
|
94
|
-
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
95
|
-
// Simular readyState = 4 chamando onreadystatechange 3x
|
|
96
92
|
try {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
93
|
+
const responseJSON = JSON.stringify(mockResponse.response);
|
|
94
|
+
const responseStatus = mockResponse.status || 200;
|
|
95
|
+
// Simular progresso: LOADING → DONE
|
|
96
|
+
// readyState 1 = OPENED
|
|
97
|
+
setTimeout(() => {
|
|
98
|
+
xhr.readyState = 1;
|
|
99
|
+
if (xhr.onreadystatechange) {
|
|
100
|
+
try {
|
|
101
|
+
xhr.onreadystatechange();
|
|
102
|
+
}
|
|
103
|
+
catch (e) { }
|
|
105
104
|
}
|
|
106
|
-
|
|
107
|
-
|
|
105
|
+
}, 0);
|
|
106
|
+
// readyState 2 = HEADERS_RECEIVED
|
|
107
|
+
setTimeout(() => {
|
|
108
|
+
xhr.readyState = 2;
|
|
109
|
+
if (xhr.onreadystatechange) {
|
|
110
|
+
try {
|
|
111
|
+
xhr.onreadystatechange();
|
|
112
|
+
}
|
|
113
|
+
catch (e) { }
|
|
108
114
|
}
|
|
109
|
-
}
|
|
115
|
+
}, 10);
|
|
116
|
+
// readyState 3 = LOADING
|
|
117
|
+
setTimeout(() => {
|
|
118
|
+
xhr.readyState = 3;
|
|
119
|
+
if (xhr.onreadystatechange) {
|
|
120
|
+
try {
|
|
121
|
+
xhr.onreadystatechange();
|
|
122
|
+
}
|
|
123
|
+
catch (e) { }
|
|
124
|
+
}
|
|
125
|
+
}, 20);
|
|
126
|
+
// readyState 4 = DONE (injetar resposta aqui)
|
|
127
|
+
setTimeout(() => {
|
|
128
|
+
xhr.status = responseStatus;
|
|
129
|
+
xhr.statusText = responseStatus >= 200 && responseStatus < 300 ? 'OK' : 'Error';
|
|
130
|
+
xhr.responseText = responseJSON;
|
|
131
|
+
xhr.response = mockResponse.response;
|
|
132
|
+
xhr.readyState = 4;
|
|
133
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
134
|
+
requestMetadata.duration = duration;
|
|
135
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
136
|
+
// Chamar evento final
|
|
137
|
+
if (xhr.onreadystatechange) {
|
|
138
|
+
try {
|
|
139
|
+
xhr.onreadystatechange();
|
|
140
|
+
}
|
|
141
|
+
catch (e) { }
|
|
142
|
+
}
|
|
143
|
+
if (xhr.onload) {
|
|
144
|
+
try {
|
|
145
|
+
xhr.onload();
|
|
146
|
+
}
|
|
147
|
+
catch (e) { }
|
|
148
|
+
}
|
|
149
|
+
}, 30);
|
|
110
150
|
}
|
|
111
151
|
catch (error) {
|
|
112
|
-
self.logger.debug('Erro ao simular resposta mockada
|
|
152
|
+
self.logger.debug('Erro ao simular resposta mockada:', error);
|
|
153
|
+
// Fallback: fazer request real
|
|
113
154
|
self.originalSend.apply(xhr, [body]);
|
|
114
155
|
}
|
|
115
156
|
}, delay);
|
|
116
157
|
return;
|
|
117
158
|
}
|
|
118
159
|
// Sem mock - fazer request real
|
|
160
|
+
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
119
161
|
xhr.onreadystatechange = function (event) {
|
|
120
162
|
if (xhr.readyState === 4) {
|
|
121
163
|
requestMetadata.status = xhr.status;
|