@fluwa-tool/sdk 1.0.38 → 1.0.39
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.
|
@@ -27,15 +27,6 @@ class XMLHttpRequestInterceptor {
|
|
|
27
27
|
this.logger.debug('XMLHttpRequest não disponível nesse ambiente (requer navegador)');
|
|
28
28
|
return;
|
|
29
29
|
}
|
|
30
|
-
// Em React Native, desabilitar XMLHttpRequestInterceptor pois tem problemas ao simular mocks
|
|
31
|
-
// FetchInterceptor já cobre as requisições
|
|
32
|
-
const isReactNative = typeof navigator !== 'undefined' &&
|
|
33
|
-
navigator.product === 'ReactNative';
|
|
34
|
-
if (isReactNative) {
|
|
35
|
-
this.logger.debug('XMLHttpRequest interceptor desabilitado em React Native (usar FetchInterceptor)');
|
|
36
|
-
this.isInstalled = true; // Marcar como instalado para não tentar novamente
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
30
|
const self = this;
|
|
40
31
|
const originalXHR = XMLHttpRequest.prototype;
|
|
41
32
|
// Armazenar referência ao open original
|
|
@@ -88,147 +79,49 @@ class XMLHttpRequestInterceptor {
|
|
|
88
79
|
};
|
|
89
80
|
// Logar início imediatamente
|
|
90
81
|
self.requestLogger.logStart(requestMetadata).catch(() => { });
|
|
91
|
-
// Interceptar mudanças de estado
|
|
82
|
+
// Interceptar mudanças de estado
|
|
92
83
|
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
//
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
xhr._fluwaMockResponse = mockResponse;
|
|
103
|
-
xhr._fluwaMockStatus = mockResponse.status || 200;
|
|
104
|
-
// Aplicar delay se configurado
|
|
105
|
-
const delay = mockResponse.delay || 0;
|
|
106
|
-
// Simular a resposta mockada assincronamente
|
|
107
|
-
setTimeout(() => {
|
|
108
|
-
try {
|
|
109
|
-
// Injetar dados fake no xhr para simular resposta
|
|
110
|
-
Object.defineProperty(xhr, 'readyState', {
|
|
111
|
-
value: 4,
|
|
112
|
-
configurable: true
|
|
113
|
-
});
|
|
114
|
-
Object.defineProperty(xhr, 'status', {
|
|
115
|
-
value: mockResponse.status || 200,
|
|
116
|
-
configurable: true
|
|
117
|
-
});
|
|
118
|
-
Object.defineProperty(xhr, 'statusText', {
|
|
119
|
-
value: 'OK',
|
|
120
|
-
configurable: true
|
|
121
|
-
});
|
|
122
|
-
Object.defineProperty(xhr, 'responseText', {
|
|
123
|
-
value: JSON.stringify(mockResponse.response),
|
|
124
|
-
configurable: true
|
|
125
|
-
});
|
|
126
|
-
Object.defineProperty(xhr, 'response', {
|
|
127
|
-
value: mockResponse.response,
|
|
128
|
-
configurable: true
|
|
129
|
-
});
|
|
130
|
-
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
131
|
-
requestMetadata.duration = duration;
|
|
132
|
-
// Registrar completamento do mock
|
|
133
|
-
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
134
|
-
// Chamar handler original para processar a "resposta"
|
|
135
|
-
// Sem Event object (React Native não suporta)
|
|
136
|
-
if (originalOnReadyStateChange) {
|
|
137
|
-
originalOnReadyStateChange.call(xhr);
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
catch (error) {
|
|
141
|
-
self.logger.debug('Erro ao simular mock response:', error);
|
|
142
|
-
}
|
|
143
|
-
}, delay);
|
|
144
|
-
// Não chamar send original para requisições mockadas
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
// NÃO há mock - fazer request real normalmente
|
|
148
|
-
xhr.onreadystatechange = function (event) {
|
|
149
|
-
if (xhr.readyState === 4) {
|
|
150
|
-
// Requisição completou
|
|
151
|
-
requestMetadata.status = xhr.status;
|
|
152
|
-
// Tentar extrair response
|
|
153
|
-
try {
|
|
154
|
-
if (xhr.responseType === '' || xhr.responseType === 'text') {
|
|
155
|
-
if (xhr.responseText) {
|
|
156
|
-
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
else if (xhr.response) {
|
|
160
|
-
if (typeof xhr.response === 'string') {
|
|
161
|
-
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
162
|
-
}
|
|
163
|
-
else {
|
|
164
|
-
requestMetadata.response = `[${xhr.responseType}]`;
|
|
165
|
-
}
|
|
84
|
+
xhr.onreadystatechange = function (event) {
|
|
85
|
+
if (xhr.readyState === 4) {
|
|
86
|
+
// Requisição completou
|
|
87
|
+
requestMetadata.status = xhr.status;
|
|
88
|
+
// Tentar extrair response
|
|
89
|
+
try {
|
|
90
|
+
if (xhr.responseType === '' || xhr.responseType === 'text') {
|
|
91
|
+
if (xhr.responseText) {
|
|
92
|
+
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
166
93
|
}
|
|
167
94
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
requestMetadata.response = xhr.responseText;
|
|
172
|
-
}
|
|
95
|
+
else if (xhr.response) {
|
|
96
|
+
if (typeof xhr.response === 'string') {
|
|
97
|
+
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
173
98
|
}
|
|
174
|
-
|
|
175
|
-
requestMetadata.response =
|
|
99
|
+
else {
|
|
100
|
+
requestMetadata.response = `[${xhr.responseType}]`;
|
|
176
101
|
}
|
|
177
102
|
}
|
|
178
|
-
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
179
|
-
requestMetadata.duration = duration;
|
|
180
|
-
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
181
103
|
}
|
|
182
|
-
|
|
183
|
-
if (originalOnReadyStateChange) {
|
|
184
|
-
originalOnReadyStateChange.call(xhr, event);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
// Chamar send original para requisições reais
|
|
188
|
-
return self.originalSend.apply(xhr, [body]);
|
|
189
|
-
})
|
|
190
|
-
.catch(() => {
|
|
191
|
-
// Erro ao verificar mock, continua com request real
|
|
192
|
-
xhr.onreadystatechange = function (event) {
|
|
193
|
-
if (xhr.readyState === 4) {
|
|
194
|
-
requestMetadata.status = xhr.status;
|
|
104
|
+
catch (e) {
|
|
195
105
|
try {
|
|
196
|
-
if (xhr.
|
|
197
|
-
|
|
198
|
-
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
else if (xhr.response) {
|
|
202
|
-
if (typeof xhr.response === 'string') {
|
|
203
|
-
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
204
|
-
}
|
|
205
|
-
else {
|
|
206
|
-
requestMetadata.response = `[${xhr.responseType}]`;
|
|
207
|
-
}
|
|
106
|
+
if (xhr.responseText) {
|
|
107
|
+
requestMetadata.response = xhr.responseText;
|
|
208
108
|
}
|
|
209
109
|
}
|
|
210
|
-
catch
|
|
211
|
-
|
|
212
|
-
if (xhr.responseText) {
|
|
213
|
-
requestMetadata.response = xhr.responseText;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
catch {
|
|
217
|
-
requestMetadata.response = undefined;
|
|
218
|
-
}
|
|
110
|
+
catch {
|
|
111
|
+
requestMetadata.response = undefined;
|
|
219
112
|
}
|
|
220
|
-
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
221
|
-
requestMetadata.duration = duration;
|
|
222
|
-
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
223
|
-
}
|
|
224
|
-
if (originalOnReadyStateChange) {
|
|
225
|
-
originalOnReadyStateChange.call(xhr, event);
|
|
226
113
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
114
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
115
|
+
requestMetadata.duration = duration;
|
|
116
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
117
|
+
}
|
|
118
|
+
// Chamar handler original
|
|
119
|
+
if (originalOnReadyStateChange) {
|
|
120
|
+
originalOnReadyStateChange.call(this, event);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
// Chamar send original
|
|
124
|
+
return self.originalSend.apply(this, [body]);
|
|
232
125
|
};
|
|
233
126
|
this.isInstalled = true;
|
|
234
127
|
this.logger.success('XMLHttpRequest interceptor instalado');
|