From 37861239d9d294af9c8c2868454f44724cb78a00 Mon Sep 17 00:00:00 2001 From: TechQuery Date: Tue, 10 Dec 2024 01:35:44 +0800 Subject: [PATCH] [fix] XML/HTML document response of XHR [optimize] update Upstream packages --- package.json | 8 ++++---- pnpm-lock.yaml | 32 ++++++++++++++++---------------- source/HTTPRequest.ts | 19 +++++++++++-------- test/XMLHttpRequest.ts | 2 ++ 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/package.json b/package.json index 7b33fd5..8c1c5c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "koajax", - "version": "3.1.0", + "version": "3.1.1", "license": "LGPL-3.0", "author": "shiy2008@gmail.com", "description": "HTTP Client based on Koa-like middlewares", @@ -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": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b8323fa..1805290 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -67,8 +67,8 @@ importers: specifier: ~2.13.2 version: 2.13.2(@swc/helpers@0.5.15)(typescript@5.7.2) prettier: - specifier: ^3.4.1 - version: 3.4.1 + specifier: ^3.4.2 + version: 3.4.2 ts-jest: specifier: ^29.2.5 version: 29.2.5(@babel/core@7.26.0)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.26.0))(jest@29.7.0(@types/node@20.17.9)(ts-node@10.9.2(@swc/core@1.9.3(@swc/helpers@0.5.15))(@types/node@20.17.9)(typescript@5.7.2)))(typescript@5.7.2) @@ -76,11 +76,11 @@ importers: specifier: ^10.9.2 version: 10.9.2(@swc/core@1.9.3(@swc/helpers@0.5.15))(@types/node@20.17.9)(typescript@5.7.2) typedoc: - specifier: ^0.27.2 - version: 0.27.2(typescript@5.7.2) + specifier: ^0.27.4 + version: 0.27.4(typescript@5.7.2) typedoc-plugin-mdn-links: - specifier: ^4.0.2 - version: 4.0.2(typedoc@0.27.2(typescript@5.7.2)) + specifier: ^4.0.4 + version: 4.0.4(typedoc@0.27.4(typescript@5.7.2)) typescript: specifier: ~5.7.2 version: 5.7.2 @@ -2190,8 +2190,8 @@ packages: resolution: {integrity: sha512-JcEmHlyLK/o0uGAlj65vgg+7LIms0xKXe60lcDOTU7oVX/3LuEuLwrQpW3VJ7de5TaFKiW4kWkaIpJL42FEgxQ==} engines: {node: '>=12.0.0'} - prettier@3.4.1: - resolution: {integrity: sha512-G+YdqtITVZmOJje6QkXQWzl3fSfMxFwm1tjTyo9exhkmWSqC4Yhd1+lug++IlR2mvRVAxEDDWYkQdeSztajqgg==} + prettier@3.4.2: + resolution: {integrity: sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==} engines: {node: '>=14'} hasBin: true @@ -2525,13 +2525,13 @@ packages: resolution: {integrity: sha512-RAH822pAdBgcNMAfWnCBU3CFZcfZ/i1eZjwFU/dsLKumyuuP3niueg2UAukXYF0E2AAoc82ZSSf9J0WQBinzHA==} engines: {node: '>=12.20'} - typedoc-plugin-mdn-links@4.0.2: - resolution: {integrity: sha512-AsfhvYIYsTVfIXr5ZMTjYw0LlCIPiSLglnd2Q09DVthZBa1qfYDGZgnwOekTrF8q0RSDWTCY/ZM6LERRScoA9w==} + typedoc-plugin-mdn-links@4.0.4: + resolution: {integrity: sha512-rp0qiELXDso1VTFZVbLmO06SsdePD0j+h9JurxI6F8puM/euj6WZYKL5uWSWvu0r3wdUkus3KPQQEMmZ+rn5/g==} peerDependencies: typedoc: 0.26.x || 0.27.x - typedoc@0.27.2: - resolution: {integrity: sha512-C2ima5TZJHU3ecnRIz50lKd1BsYck5LhYQIy7MRPmjuSEJreUEAt+uAVcZgY7wZsSORzEI7xW8miZIdxv/cbmw==} + typedoc@0.27.4: + resolution: {integrity: sha512-wXPQs1AYC2Crk+1XFpNuutLIkNWleokZf1UNf/X8w9KsMnirkvT+LzxTXDvfF6ug3TSLf3Xu5ZXRKGfoXPX7IA==} engines: {node: '>= 18'} hasBin: true peerDependencies: @@ -5389,7 +5389,7 @@ snapshots: posthtml-parser: 0.11.0 posthtml-render: 3.0.0 - prettier@3.4.1: {} + prettier@3.4.2: {} pretty-format@29.7.0: dependencies: @@ -5676,11 +5676,11 @@ snapshots: type-fest@2.19.0: {} - typedoc-plugin-mdn-links@4.0.2(typedoc@0.27.2(typescript@5.7.2)): + typedoc-plugin-mdn-links@4.0.4(typedoc@0.27.4(typescript@5.7.2)): dependencies: - typedoc: 0.27.2(typescript@5.7.2) + typedoc: 0.27.4(typescript@5.7.2) - typedoc@0.27.2(typescript@5.7.2): + typedoc@0.27.4(typescript@5.7.2): dependencies: '@gerrit0/mini-shiki': 1.24.1 lunr: 2.3.9 diff --git a/source/HTTPRequest.ts b/source/HTTPRequest.ts index 447d8ae..b71286a 100644 --- a/source/HTTPRequest.ts +++ b/source/HTTPRequest.ts @@ -102,12 +102,7 @@ export function requestXHR({ ...rest }: Request): RequestResult { const request = new XMLHttpRequest(); - const header_list = - headers instanceof Array - ? headers - : headers?.[Symbol.iterator] instanceof Function - ? [...(headers as Iterable)] - : Object.entries(headers); + const header = new Headers(headers); const bodyPromise = body instanceof globalThis.ReadableStream ? Array.fromAsync(body as ReadableStream).then( @@ -136,10 +131,18 @@ export function requestXHR({ }; 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); diff --git a/test/XMLHttpRequest.ts b/test/XMLHttpRequest.ts index 1b5d717..0dadc7f 100644 --- a/test/XMLHttpRequest.ts +++ b/test/XMLHttpRequest.ts @@ -21,6 +21,8 @@ export class XMLHttpRequest extends EventTarget { this.onreadystatechange?.(); } + overrideMimeType(type: string) {} + open(method: Request['method'], URI: string) { this.responseURL = URI;