@fluwa-tool/sdk 1.0.42 → 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.
|
@@ -82,38 +82,75 @@ class XMLHttpRequestInterceptor {
|
|
|
82
82
|
};
|
|
83
83
|
checkMockWithRetry().then(mockResponse => {
|
|
84
84
|
if (mockResponse) {
|
|
85
|
-
// Mock encontrado -
|
|
85
|
+
// Mock encontrado - simular resposta do xhr
|
|
86
86
|
requestMetadata.source = types_1.RequestSource.MOCK;
|
|
87
87
|
requestMetadata.status = mockResponse.status || 200;
|
|
88
88
|
requestMetadata.response = mockResponse.response;
|
|
89
89
|
const delay = mockResponse.delay || 0;
|
|
90
|
+
// Disparar eventos de forma assíncrona para o axios processar
|
|
90
91
|
setTimeout(() => {
|
|
91
|
-
const duration = Date.now() - (xhr._fluwaStartTime || 0);
|
|
92
|
-
requestMetadata.duration = duration;
|
|
93
|
-
self.requestLogger.logComplete(requestMetadata).catch(() => { });
|
|
94
|
-
// Criar resposta fake e simular como se fosse enviada pelo servidor
|
|
95
92
|
try {
|
|
96
93
|
const responseJSON = JSON.stringify(mockResponse.response);
|
|
97
94
|
const responseStatus = mockResponse.status || 200;
|
|
98
|
-
//
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
}
|
|
109
|
-
//
|
|
110
|
-
|
|
111
|
-
xhr.
|
|
112
|
-
|
|
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) { }
|
|
104
|
+
}
|
|
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) { }
|
|
114
|
+
}
|
|
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);
|
|
113
150
|
}
|
|
114
151
|
catch (error) {
|
|
115
|
-
self.logger.debug('Erro ao simular resposta,
|
|
116
|
-
// Fallback: fazer request real
|
|
152
|
+
self.logger.debug('Erro ao simular resposta mockada:', error);
|
|
153
|
+
// Fallback: fazer request real
|
|
117
154
|
self.originalSend.apply(xhr, [body]);
|
|
118
155
|
}
|
|
119
156
|
}, delay);
|