Skip to content

Commit 2bf40a8

Browse files
committed
fix XMLHttpRequest async implementation
- Change XMLHttpRequest from synchronous to asynchronous mode - Add Promise-based response handling with proper event handlers - Implement proper error handling for network issues and timeouts - Fix response parsing with consistent error structure - Ensure compatibility with existing async/await usage patterns Resolves issue with "XMLHttpRequest's timeout attribute not supported in synchronous mode"
1 parent c8e85d5 commit 2bf40a8

File tree

1 file changed

+56
-19
lines changed

1 file changed

+56
-19
lines changed

packages/vue/src/server/index.ts

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,61 @@ import { ServerRequest } from '@vue-skuilder/common';
55
const SERVER = ENV.EXPRESS_SERVER_PROTOCOL + '://' + ENV.EXPRESS_SERVER_URL;
66

77
export default async function serverRequest<T extends ServerRequest>(requestData: T): Promise<T> {
8-
try {
9-
const xml = new XMLHttpRequest();
10-
xml.withCredentials = true;
11-
xml.open('POST', SERVER, false);
12-
xml.setRequestHeader('Content-Type', 'application/json');
13-
xml.timeout = requestData.timeout || 7000;
14-
xml.ontimeout = () => {
15-
throw new Error('Request timed out');
16-
};
17-
xml.send(JSON.stringify(requestData));
8+
return new Promise<T>((resolve) => {
9+
try {
10+
const xml = new XMLHttpRequest();
11+
xml.withCredentials = true;
12+
xml.open('POST', SERVER, true);
13+
xml.setRequestHeader('Content-Type', 'application/json');
14+
xml.timeout = requestData.timeout || 7000;
1815

19-
requestData.response = JSON.parse(xml.response);
20-
} catch (error) {
21-
requestData.response = {
22-
status: Status.error,
23-
ok: false,
24-
errorText: error instanceof Error ? error.message : JSON.stringify(error),
25-
};
26-
}
27-
return requestData;
16+
// Handle the response when it completes
17+
xml.onload = function () {
18+
try {
19+
requestData.response = JSON.parse(xml.responseText);
20+
resolve(requestData);
21+
} catch (parseError) {
22+
requestData.response = {
23+
status: Status.error,
24+
ok: false,
25+
errorText: `Failed to parse response: ${
26+
parseError instanceof Error ? parseError.message : JSON.stringify(parseError)
27+
}`,
28+
};
29+
resolve(requestData);
30+
}
31+
};
32+
33+
// Handle network errors
34+
xml.onerror = function () {
35+
requestData.response = {
36+
status: Status.error,
37+
ok: false,
38+
errorText: 'Network error occurred',
39+
};
40+
resolve(requestData);
41+
};
42+
43+
// Handle timeouts
44+
xml.ontimeout = function () {
45+
requestData.response = {
46+
status: Status.error,
47+
ok: false,
48+
errorText: 'Request timed out',
49+
};
50+
resolve(requestData);
51+
};
52+
53+
// Send the request
54+
xml.send(JSON.stringify(requestData));
55+
} catch (error) {
56+
// Handle any errors that occur during setup
57+
requestData.response = {
58+
status: Status.error,
59+
ok: false,
60+
errorText: error instanceof Error ? error.message : JSON.stringify(error),
61+
};
62+
resolve(requestData);
63+
}
64+
});
2865
}

0 commit comments

Comments
 (0)