@fluwa-tool/sdk 1.0.33 → 1.0.35
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.
|
@@ -115,6 +115,10 @@ class FetchInterceptor {
|
|
|
115
115
|
requestMetadata.source = types_1.RequestSource.MOCK;
|
|
116
116
|
requestMetadata.response = mockResponse.response;
|
|
117
117
|
requestMetadata.status = mockResponse.status;
|
|
118
|
+
this.logger.debug(`✅ Retornando mock para ${method} ${url}`, {
|
|
119
|
+
status: mockResponse.status,
|
|
120
|
+
response: mockResponse.response
|
|
121
|
+
});
|
|
118
122
|
// Aplicar delay se configurado
|
|
119
123
|
if (mockResponse.delay) {
|
|
120
124
|
await this.delay(mockResponse.delay);
|
|
@@ -124,11 +128,16 @@ class FetchInterceptor {
|
|
|
124
128
|
// Registrar completamento
|
|
125
129
|
await this.requestLogger.logComplete(requestMetadata);
|
|
126
130
|
// Retornar mock como Response
|
|
127
|
-
|
|
131
|
+
const mockResponseObj = new Response(JSON.stringify(mockResponse.response), {
|
|
128
132
|
status: mockResponse.status || 200,
|
|
129
133
|
statusText: 'Mock',
|
|
130
134
|
headers: { 'Content-Type': 'application/json' },
|
|
131
135
|
});
|
|
136
|
+
this.logger.debug(`📤 Mock Response criada:`, {
|
|
137
|
+
status: mockResponseObj.status,
|
|
138
|
+
statusText: mockResponseObj.statusText
|
|
139
|
+
});
|
|
140
|
+
return mockResponseObj;
|
|
132
141
|
}
|
|
133
142
|
// Fazer request real
|
|
134
143
|
const response = await this.originalFetch.apply(globalThis, args);
|
|
@@ -79,69 +79,86 @@ class XMLHttpRequestInterceptor {
|
|
|
79
79
|
};
|
|
80
80
|
// Logar início imediatamente
|
|
81
81
|
self.requestLogger.logStart(requestMetadata).catch(() => { });
|
|
82
|
-
//
|
|
82
|
+
// Interceptar mudanças de estado (para request real)
|
|
83
|
+
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
84
|
+
// Verificar se há mock ANTES de enviar a requisição
|
|
83
85
|
self.mockResolver.resolve(method, url)
|
|
84
86
|
.then((mockResponse) => {
|
|
85
87
|
if (mockResponse) {
|
|
86
|
-
// ✅ Mock encontrado -
|
|
88
|
+
// ✅ Mock encontrado - simular resposta ao invés de fazer request real
|
|
87
89
|
requestMetadata.source = types_1.RequestSource.MOCK;
|
|
88
90
|
requestMetadata.status = mockResponse.status || 200;
|
|
89
91
|
requestMetadata.response = mockResponse.response;
|
|
92
|
+
// Guardar no xhr para depois extrair no handler
|
|
93
|
+
xhr._fluwaMockResponse = mockResponse;
|
|
94
|
+
xhr._fluwaMockStatus = mockResponse.status || 200;
|
|
90
95
|
// Aplicar delay se configurado
|
|
91
|
-
const
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
96
|
+
const delay = mockResponse.delay || 0;
|
|
97
|
+
// Simular a resposta mockada assincronamente
|
|
98
|
+
setTimeout(() => {
|
|
99
|
+
try {
|
|
100
|
+
// Simular readyState = 4 (completo)
|
|
101
|
+
// Criar um evento fake que faz parecer que a resposta chegou
|
|
102
|
+
const mockEvent = new Event('readystatechange');
|
|
103
|
+
// Injetar dados fake no xhr para simular resposta
|
|
104
|
+
Object.defineProperty(xhr, 'readyState', {
|
|
105
|
+
value: 4,
|
|
106
|
+
configurable: true
|
|
107
|
+
});
|
|
108
|
+
Object.defineProperty(xhr, 'status', {
|
|
109
|
+
value: mockResponse.status || 200,
|
|
110
|
+
configurable: true
|
|
111
|
+
});
|
|
112
|
+
Object.defineProperty(xhr, 'statusText', {
|
|
113
|
+
value: 'OK',
|
|
114
|
+
configurable: true
|
|
115
|
+
});
|
|
116
|
+
Object.defineProperty(xhr, 'responseText', {
|
|
117
|
+
value: JSON.stringify(mockResponse.response),
|
|
118
|
+
configurable: true
|
|
119
|
+
});
|
|
120
|
+
Object.defineProperty(xhr, 'response', {
|
|
121
|
+
value: mockResponse.response,
|
|
122
|
+
configurable: true
|
|
123
|
+
});
|
|
124
|
+
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
125
|
+
requestMetadata.duration = duration;
|
|
126
|
+
// Registrar completamento do mock
|
|
127
|
+
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
128
|
+
// Chamar handler original para processar a "resposta"
|
|
129
|
+
if (originalOnReadyStateChange) {
|
|
130
|
+
originalOnReadyStateChange.call(xhr, mockEvent);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
self.logger.debug('Erro ao simular mock response:', error);
|
|
114
135
|
}
|
|
115
|
-
});
|
|
136
|
+
}, delay);
|
|
137
|
+
// Não chamar send original para requisições mockadas
|
|
138
|
+
return;
|
|
116
139
|
}
|
|
117
140
|
// NÃO há mock - fazer request real normalmente
|
|
118
|
-
|
|
119
|
-
const originalOnReadyStateChange = xhr.onreadystatechange;
|
|
120
|
-
xhr.onreadystatechange = function () {
|
|
141
|
+
xhr.onreadystatechange = function (event) {
|
|
121
142
|
if (xhr.readyState === 4) {
|
|
122
143
|
// Requisição completou
|
|
123
144
|
requestMetadata.status = xhr.status;
|
|
124
145
|
// Tentar extrair response
|
|
125
146
|
try {
|
|
126
|
-
// responseText só está disponível para responseType 'text' ou '' (padrão)
|
|
127
147
|
if (xhr.responseType === '' || xhr.responseType === 'text') {
|
|
128
148
|
if (xhr.responseText) {
|
|
129
149
|
requestMetadata.response = self.safeParseBody(xhr.responseText);
|
|
130
150
|
}
|
|
131
151
|
}
|
|
132
152
|
else if (xhr.response) {
|
|
133
|
-
// Para blob, arraybuffer, document, etc., usar xhr.response
|
|
134
153
|
if (typeof xhr.response === 'string') {
|
|
135
154
|
requestMetadata.response = self.safeParseBody(xhr.response);
|
|
136
155
|
}
|
|
137
156
|
else {
|
|
138
|
-
// Não consegue serializar blob/arraybuffer, apenas registra tipo
|
|
139
157
|
requestMetadata.response = `[${xhr.responseType}]`;
|
|
140
158
|
}
|
|
141
159
|
}
|
|
142
160
|
}
|
|
143
161
|
catch (e) {
|
|
144
|
-
// Em caso de erro, tenta responseText como fallback
|
|
145
162
|
try {
|
|
146
163
|
if (xhr.responseText) {
|
|
147
164
|
requestMetadata.response = xhr.responseText;
|
|
@@ -157,16 +174,15 @@ class XMLHttpRequestInterceptor {
|
|
|
157
174
|
}
|
|
158
175
|
// Chamar handler original
|
|
159
176
|
if (originalOnReadyStateChange) {
|
|
160
|
-
originalOnReadyStateChange.call(
|
|
177
|
+
originalOnReadyStateChange.call(xhr, event);
|
|
161
178
|
}
|
|
162
179
|
};
|
|
163
|
-
// Chamar send original
|
|
180
|
+
// Chamar send original para requisições reais
|
|
164
181
|
return self.originalSend.apply(xhr, [body]);
|
|
165
182
|
})
|
|
166
183
|
.catch(() => {
|
|
167
184
|
// Erro ao verificar mock, continua com request real
|
|
168
|
-
|
|
169
|
-
xhr.onreadystatechange = function () {
|
|
185
|
+
xhr.onreadystatechange = function (event) {
|
|
170
186
|
if (xhr.readyState === 4) {
|
|
171
187
|
requestMetadata.status = xhr.status;
|
|
172
188
|
try {
|
|
@@ -199,12 +215,12 @@ class XMLHttpRequestInterceptor {
|
|
|
199
215
|
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
200
216
|
}
|
|
201
217
|
if (originalOnReadyStateChange) {
|
|
202
|
-
originalOnReadyStateChange.call(
|
|
218
|
+
originalOnReadyStateChange.call(xhr, event);
|
|
203
219
|
}
|
|
204
220
|
};
|
|
205
221
|
return self.originalSend.apply(xhr, [body]);
|
|
206
222
|
});
|
|
207
|
-
// Retornar undefined (
|
|
223
|
+
// Retornar undefined (send é síncrono mas processamento é async)
|
|
208
224
|
return;
|
|
209
225
|
};
|
|
210
226
|
this.isInstalled = true;
|