@fluwa-tool/sdk 1.0.32 → 1.0.33

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.
@@ -77,72 +77,135 @@ class XMLHttpRequestInterceptor {
77
77
  appName: self.appName,
78
78
  source: types_1.RequestSource.REAL,
79
79
  };
80
- // Logar início imediatamente (não esperar mockResolver)
80
+ // Logar início imediatamente
81
81
  self.requestLogger.logStart(requestMetadata).catch(() => { });
82
- // Verificar se há mock (não afeta logging)
82
+ // Verificar se há mock ANTES de enviar a requisição (assíncrono)
83
83
  self.mockResolver.resolve(method, url)
84
84
  .then((mockResponse) => {
85
85
  if (mockResponse) {
86
- // mock disponível
86
+ // Mock encontrado - retornar mock ao invés de fazer request real
87
87
  requestMetadata.source = types_1.RequestSource.MOCK;
88
88
  requestMetadata.status = mockResponse.status || 200;
89
89
  requestMetadata.response = mockResponse.response;
90
90
  // Aplicar delay se configurado
91
- if (mockResponse.delay) {
92
- return new Promise(resolve => setTimeout(resolve, mockResponse.delay));
93
- }
91
+ const delayPromise = mockResponse.delay
92
+ ? new Promise(resolve => setTimeout(resolve, mockResponse.delay))
93
+ : Promise.resolve();
94
+ return delayPromise.then(() => {
95
+ const duration = Date.now() - (xhr._fluwaStartTime || 0);
96
+ requestMetadata.duration = duration;
97
+ // Registrar completamento do mock
98
+ self.requestLogger.logComplete(requestMetadata).catch(() => { });
99
+ // Simular resposta bem-sucedida do XMLHttpRequest com dados do mock
100
+ // Usar propriedades writable em vez de Object.defineProperty
101
+ xhr._fluwaStatus = mockResponse.status || 200;
102
+ xhr._fluwaStatusText = 'Mock';
103
+ xhr._fluwaResponseText = JSON.stringify(mockResponse.response);
104
+ xhr._fluwaResponse = mockResponse.response;
105
+ // Simular mudança de readyState para 4 (completo)
106
+ Object.defineProperty(xhr, 'readyState', { value: 4, writable: true, configurable: true });
107
+ Object.defineProperty(xhr, 'status', { value: mockResponse.status || 200, writable: true, configurable: true });
108
+ Object.defineProperty(xhr, 'statusText', { value: 'Mock', writable: true, configurable: true });
109
+ Object.defineProperty(xhr, 'responseText', { value: JSON.stringify(mockResponse.response), writable: true, configurable: true });
110
+ Object.defineProperty(xhr, 'response', { value: mockResponse.response, writable: true, configurable: true });
111
+ // Chamar handler original com estado final
112
+ if (xhr.onreadystatechange) {
113
+ xhr.onreadystatechange.call(xhr);
114
+ }
115
+ });
94
116
  }
95
- })
96
- .catch(() => {
97
- // Erro ao verificar mock, continua com request real
98
- });
99
- // Interceptar mudanças de estado
100
- const originalOnReadyStateChange = xhr.onreadystatechange;
101
- xhr.onreadystatechange = function () {
102
- if (xhr.readyState === 4) {
103
- // Requisição completou
104
- requestMetadata.status = xhr.status;
105
- // Tentar extrair response
106
- try {
107
- // responseText só está disponível para responseType 'text' ou '' (padrão)
108
- if (xhr.responseType === '' || xhr.responseType === 'text') {
109
- if (xhr.responseText) {
110
- requestMetadata.response = self.safeParseBody(xhr.responseText);
117
+ // NÃO há mock - fazer request real normalmente
118
+ // Interceptar mudanças de estado
119
+ const originalOnReadyStateChange = xhr.onreadystatechange;
120
+ xhr.onreadystatechange = function () {
121
+ if (xhr.readyState === 4) {
122
+ // Requisição completou
123
+ requestMetadata.status = xhr.status;
124
+ // Tentar extrair response
125
+ try {
126
+ // responseText só está disponível para responseType 'text' ou '' (padrão)
127
+ if (xhr.responseType === '' || xhr.responseType === 'text') {
128
+ if (xhr.responseText) {
129
+ requestMetadata.response = self.safeParseBody(xhr.responseText);
130
+ }
131
+ }
132
+ else if (xhr.response) {
133
+ // Para blob, arraybuffer, document, etc., usar xhr.response
134
+ if (typeof xhr.response === 'string') {
135
+ requestMetadata.response = self.safeParseBody(xhr.response);
136
+ }
137
+ else {
138
+ // Não consegue serializar blob/arraybuffer, apenas registra tipo
139
+ requestMetadata.response = `[${xhr.responseType}]`;
140
+ }
111
141
  }
112
142
  }
113
- else if (xhr.response) {
114
- // Para blob, arraybuffer, document, etc., usar xhr.response
115
- if (typeof xhr.response === 'string') {
116
- requestMetadata.response = self.safeParseBody(xhr.response);
143
+ catch (e) {
144
+ // Em caso de erro, tenta responseText como fallback
145
+ try {
146
+ if (xhr.responseText) {
147
+ requestMetadata.response = xhr.responseText;
148
+ }
117
149
  }
118
- else {
119
- // Não consegue serializar blob/arraybuffer, apenas registra tipo
120
- requestMetadata.response = `[${xhr.responseType}]`;
150
+ catch {
151
+ requestMetadata.response = undefined;
121
152
  }
122
153
  }
154
+ const duration = Date.now() - (xhr._fluwaStartTime || 0);
155
+ requestMetadata.duration = duration;
156
+ self.requestLogger.logComplete(requestMetadata).catch(() => { });
157
+ }
158
+ // Chamar handler original
159
+ if (originalOnReadyStateChange) {
160
+ originalOnReadyStateChange.call(this);
123
161
  }
124
- catch (e) {
125
- // Em caso de erro, tenta responseText como fallback
162
+ };
163
+ // Chamar send original
164
+ return self.originalSend.apply(xhr, [body]);
165
+ })
166
+ .catch(() => {
167
+ // Erro ao verificar mock, continua com request real
168
+ const originalOnReadyStateChange = xhr.onreadystatechange;
169
+ xhr.onreadystatechange = function () {
170
+ if (xhr.readyState === 4) {
171
+ requestMetadata.status = xhr.status;
126
172
  try {
127
- if (xhr.responseText) {
128
- requestMetadata.response = xhr.responseText;
173
+ if (xhr.responseType === '' || xhr.responseType === 'text') {
174
+ if (xhr.responseText) {
175
+ requestMetadata.response = self.safeParseBody(xhr.responseText);
176
+ }
177
+ }
178
+ else if (xhr.response) {
179
+ if (typeof xhr.response === 'string') {
180
+ requestMetadata.response = self.safeParseBody(xhr.response);
181
+ }
182
+ else {
183
+ requestMetadata.response = `[${xhr.responseType}]`;
184
+ }
129
185
  }
130
186
  }
131
- catch {
132
- requestMetadata.response = undefined;
187
+ catch (e) {
188
+ try {
189
+ if (xhr.responseText) {
190
+ requestMetadata.response = xhr.responseText;
191
+ }
192
+ }
193
+ catch {
194
+ requestMetadata.response = undefined;
195
+ }
133
196
  }
197
+ const duration = Date.now() - (xhr._fluwaStartTime || 0);
198
+ requestMetadata.duration = duration;
199
+ self.requestLogger.logComplete(requestMetadata).catch(() => { });
134
200
  }
135
- const duration = Date.now() - (xhr._fluwaStartTime || 0);
136
- requestMetadata.duration = duration;
137
- self.requestLogger.logComplete(requestMetadata).catch(() => { });
138
- }
139
- // Chamar handler original
140
- if (originalOnReadyStateChange) {
141
- originalOnReadyStateChange.call(this);
142
- }
143
- };
144
- // Chamar send original
145
- return self.originalSend.apply(this, [body]);
201
+ if (originalOnReadyStateChange) {
202
+ originalOnReadyStateChange.call(this);
203
+ }
204
+ };
205
+ return self.originalSend.apply(xhr, [body]);
206
+ });
207
+ // Retornar undefined (envio da requisição é assíncrono)
208
+ return;
146
209
  };
147
210
  this.isInstalled = true;
148
211
  this.logger.success('XMLHttpRequest interceptor instalado');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fluwa-tool/sdk",
3
- "version": "1.0.32",
3
+ "version": "1.0.33",
4
4
  "description": "Fluwa DevTools SDK for network interception and mocking",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",