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