Skip to content

Commit

Permalink
[fix] XML/HTML document response of XHR
Browse files Browse the repository at this point in the history
[optimize] update Upstream packages
  • Loading branch information
TechQuery committed Dec 9, 2024
1 parent ce37db3 commit 3786123
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "koajax",
"version": "3.1.0",
"version": "3.1.1",
"license": "LGPL-3.0",
"author": "[email protected]",
"description": "HTTP Client based on Koa-like middlewares",
Expand Down Expand Up @@ -50,11 +50,11 @@
"lint-staged": "^15.2.10",
"open-cli": "^8.0.0",
"parcel": "~2.13.2",
"prettier": "^3.4.1",
"prettier": "^3.4.2",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typedoc": "^0.27.2",
"typedoc-plugin-mdn-links": "^4.0.2",
"typedoc": "^0.27.4",
"typedoc-plugin-mdn-links": "^4.0.4",
"typescript": "~5.7.2"
},
"prettier": {
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions source/HTTPRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ export function requestXHR<B>({
...rest
}: Request): RequestResult<B> {
const request = new XMLHttpRequest();
const header_list =
headers instanceof Array
? headers
: headers?.[Symbol.iterator] instanceof Function
? [...(headers as Iterable<string[]>)]
: Object.entries(headers);
const header = new Headers(headers);
const bodyPromise =
body instanceof globalThis.ReadableStream
? Array.fromAsync(body as ReadableStream).then(
Expand Down Expand Up @@ -136,10 +131,18 @@ export function requestXHR<B>({
};
request.onerror = request.ontimeout = reject;

const [MIMEType] = header.get('Accept')?.split(',') || [
rest.responseType === 'document'
? 'application/xhtml+xml'
: rest.responseType === 'json'
? 'application/json'
: ''
];
if (MIMEType) request.overrideMimeType(MIMEType);

request.open(method, path + '');

for (const [key, value] of header_list)
request.setRequestHeader(key, value);
for (const [key, value] of header) request.setRequestHeader(key, value);

Object.assign(request, rest);

Expand Down
2 changes: 2 additions & 0 deletions test/XMLHttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export class XMLHttpRequest extends EventTarget {
this.onreadystatechange?.();
}

overrideMimeType(type: string) {}

open(method: Request['method'], URI: string) {
this.responseURL = URI;

Expand Down

0 comments on commit 3786123

Please sign in to comment.