Skip to content

Commit 3786123

Browse files
committed
[fix] XML/HTML document response of XHR
[optimize] update Upstream packages
1 parent ce37db3 commit 3786123

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "koajax",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"license": "LGPL-3.0",
55
"author": "[email protected]",
66
"description": "HTTP Client based on Koa-like middlewares",
@@ -50,11 +50,11 @@
5050
"lint-staged": "^15.2.10",
5151
"open-cli": "^8.0.0",
5252
"parcel": "~2.13.2",
53-
"prettier": "^3.4.1",
53+
"prettier": "^3.4.2",
5454
"ts-jest": "^29.2.5",
5555
"ts-node": "^10.9.2",
56-
"typedoc": "^0.27.2",
57-
"typedoc-plugin-mdn-links": "^4.0.2",
56+
"typedoc": "^0.27.4",
57+
"typedoc-plugin-mdn-links": "^4.0.4",
5858
"typescript": "~5.7.2"
5959
},
6060
"prettier": {

pnpm-lock.yaml

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/HTTPRequest.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,7 @@ export function requestXHR<B>({
102102
...rest
103103
}: Request): RequestResult<B> {
104104
const request = new XMLHttpRequest();
105-
const header_list =
106-
headers instanceof Array
107-
? headers
108-
: headers?.[Symbol.iterator] instanceof Function
109-
? [...(headers as Iterable<string[]>)]
110-
: Object.entries(headers);
105+
const header = new Headers(headers);
111106
const bodyPromise =
112107
body instanceof globalThis.ReadableStream
113108
? Array.fromAsync(body as ReadableStream).then(
@@ -136,10 +131,18 @@ export function requestXHR<B>({
136131
};
137132
request.onerror = request.ontimeout = reject;
138133

134+
const [MIMEType] = header.get('Accept')?.split(',') || [
135+
rest.responseType === 'document'
136+
? 'application/xhtml+xml'
137+
: rest.responseType === 'json'
138+
? 'application/json'
139+
: ''
140+
];
141+
if (MIMEType) request.overrideMimeType(MIMEType);
142+
139143
request.open(method, path + '');
140144

141-
for (const [key, value] of header_list)
142-
request.setRequestHeader(key, value);
145+
for (const [key, value] of header) request.setRequestHeader(key, value);
143146

144147
Object.assign(request, rest);
145148

test/XMLHttpRequest.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export class XMLHttpRequest extends EventTarget {
2121
this.onreadystatechange?.();
2222
}
2323

24+
overrideMimeType(type: string) {}
25+
2426
open(method: Request['method'], URI: string) {
2527
this.responseURL = URI;
2628

0 commit comments

Comments
 (0)