Skip to content

Commit 01cb084

Browse files
committed
fix(XHR): typings for processResponse callback
1 parent 8b3517e commit 01cb084

3 files changed

Lines changed: 36 additions & 6 deletions

File tree

python/pythonmonkey/builtin_modules/XMLHttpRequest-internal.d.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@
55
* @date August 2023
66
*/
77

8+
/**
9+
* `processResponse` callback's argument type
10+
*/
11+
export declare interface XHRResponse {
12+
/** Response URL */
13+
url: string;
14+
/** HTTP status */
15+
status: number;
16+
/** HTTP status message */
17+
statusText: string;
18+
/** The `Content-Type` header value */
19+
contentLength: number;
20+
/** Implementation of the `xhr.getResponseHeader` method */
21+
getResponseHeader(name: string): string | undefined;
22+
/** Implementation of the `xhr.getAllResponseHeaders` method */
23+
getAllResponseHeaders(): string;
24+
}
25+
826
/**
927
* Send request
1028
*/
@@ -17,7 +35,7 @@ export declare function request(
1735
processRequestBodyChunkLength: (bytesLength: number) => void,
1836
processRequestEndOfBody: () => void,
1937
// callbacks for response progress
20-
processResponse: (response: any) => void,
38+
processResponse: (response: XHRResponse) => void,
2139
processBodyChunk: (bytes: Uint8Array) => void,
2240
processEndOfBody: () => void,
2341
): Promise<void>;

python/pythonmonkey/builtin_modules/XMLHttpRequest-internal.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
import aiohttp
77
import yarl
88
import io
9-
from typing import Union, ByteString, Callable, Any
9+
from typing import Union, ByteString, Callable, TypedDict
10+
11+
class XHRResponse(TypedDict, total=True):
12+
"""
13+
See definitions in `XMLHttpRequest-internal.d.ts`
14+
"""
15+
url: str
16+
status: int
17+
statusText: str
18+
contentLength: int
19+
getResponseHeader: Callable[[str], Union[str, None]]
20+
getAllResponseHeaders: Callable[[], str]
1021

1122
async def request(
1223
method: str,
@@ -17,7 +28,7 @@ async def request(
1728
processRequestBodyChunkLength: Callable[[int], None],
1829
processRequestEndOfBody: Callable[[], None],
1930
# callbacks for response progress
20-
processResponse: Callable[[Any], None],
31+
processResponse: Callable[[XHRResponse], None],
2132
processBodyChunk: Callable[[bytearray], None],
2233
processEndOfBody: Callable[[], None],
2334
/
@@ -52,15 +63,15 @@ def getAllResponseHeaders():
5263
return "\r\n".join(headers)
5364

5465
# readyState HEADERS_RECEIVED
55-
responseData = { # FIXME: PythonMonkey bug: the dict will be GCed if directly as an argument
66+
responseData: XHRResponse = { # FIXME: PythonMonkey bug: the dict will be GCed if directly as an argument
5667
'url': str(res.real_url),
5768
'status': res.status,
58-
'statusText': res.reason,
69+
'statusText': str(res.reason or ''),
5970

6071
'getResponseHeader': getResponseHeader,
6172
'getAllResponseHeaders': getAllResponseHeaders,
6273

63-
'contentLength': res.content_length,
74+
'contentLength': res.content_length or 0,
6475
}
6576
processResponse(responseData)
6677

python/pythonmonkey/builtin_modules/XMLHttpRequest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ class XMLHttpRequest extends XMLHttpRequestEventTarget
488488
#uploadCompleteFlag = false; // A flag, initially unset.
489489
#uploadListenerFlag = false; // A flag, initially unset.
490490
#timedOutFlag = false; // A flag, initially unset.
491+
/** @type {import('./XMLHttpRequest-internal').XHRResponse} */
491492
#response = null;
492493
/** @type {Uint8Array[]} */
493494
#receivedBytes = [];

0 commit comments

Comments
 (0)