fix URLSearchParams and HTTP typing issue
This commit is contained in:
parent
233a354b47
commit
f09a502fe7
@ -82,9 +82,12 @@ export interface URLCtor {
|
||||
delete Object.prototype.__magic__;
|
||||
})();
|
||||
|
||||
// Use native or pure JS URL implementation?
|
||||
const useOwnUrlImp = true;
|
||||
|
||||
// @ts-ignore
|
||||
let _URL = globalThis.URL;
|
||||
if (!_URL) {
|
||||
if (useOwnUrlImp || !_URL) {
|
||||
// @ts-ignore
|
||||
globalThis.URL = _URL = URLImpl;
|
||||
// @ts-ignore
|
||||
@ -96,7 +99,7 @@ export const URL: URLCtor = _URL;
|
||||
// @ts-ignore
|
||||
let _URLSearchParams = globalThis.URLSearchParams;
|
||||
|
||||
if (!_URLSearchParams) {
|
||||
if (useOwnUrlImp || !_URLSearchParams) {
|
||||
// @ts-ignore
|
||||
globalThis.URLSearchParams = URLSearchParamsImpl;
|
||||
// @ts-ignore
|
||||
|
@ -347,8 +347,7 @@ function isASCIIHex(c: number) {
|
||||
export class URLSearchParamsImpl {
|
||||
_list: any[];
|
||||
_url: any;
|
||||
constructor(constructorArgs: any[], { doNotStripQMark = false }: any) {
|
||||
let init = constructorArgs[0];
|
||||
constructor(init: any, { doNotStripQMark = false }: any = {}) {
|
||||
this._list = [];
|
||||
this._url = null;
|
||||
|
||||
@ -425,6 +424,19 @@ export class URLSearchParamsImpl {
|
||||
return output;
|
||||
}
|
||||
|
||||
forEach(
|
||||
callbackfn: (
|
||||
value: string,
|
||||
key: string,
|
||||
parent: URLSearchParamsImpl,
|
||||
) => void,
|
||||
thisArg?: any,
|
||||
): void {
|
||||
for (const tuple of this._list) {
|
||||
callbackfn.call(thisArg, tuple[1], tuple[0], this);
|
||||
}
|
||||
}
|
||||
|
||||
has(name: string) {
|
||||
for (const tuple of this._list) {
|
||||
if (tuple[0] === name) {
|
||||
@ -1916,7 +1928,7 @@ export class URLImpl {
|
||||
|
||||
// We cannot invoke the "new URLSearchParams object" algorithm without going through the constructor, which strips
|
||||
// question mark by default. Therefore the doNotStripQMark hack is used.
|
||||
this._query = new URLSearchParamsImpl([query], {
|
||||
this._query = new URLSearchParamsImpl(query, {
|
||||
doNotStripQMark: true,
|
||||
});
|
||||
this._query._url = this;
|
||||
@ -2104,4 +2116,4 @@ export class URLImpl {
|
||||
// FIXME: type!
|
||||
_url: any;
|
||||
_query: any;
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import {
|
||||
import {
|
||||
Logger,
|
||||
RequestThrottler,
|
||||
stringToBytes,
|
||||
TalerErrorCode,
|
||||
} from "@gnu-taler/taler-util";
|
||||
|
||||
@ -70,7 +71,15 @@ export class BrowserHttpLib implements HttpRequestLibrary {
|
||||
}
|
||||
myRequest.responseType = "arraybuffer";
|
||||
if (requestBody) {
|
||||
myRequest.send(requestBody);
|
||||
if (requestBody instanceof ArrayBuffer) {
|
||||
myRequest.send(requestBody);
|
||||
} else if (ArrayBuffer.isView(requestBody)) {
|
||||
myRequest.send(requestBody);
|
||||
} else if (typeof requestBody === "string") {
|
||||
myRequest.send(requestBody);
|
||||
} else {
|
||||
myRequest.send(JSON.stringify(requestBody));
|
||||
}
|
||||
} else {
|
||||
myRequest.send();
|
||||
}
|
||||
|
@ -55,9 +55,24 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary {
|
||||
);
|
||||
}
|
||||
|
||||
let myBody: BodyInit | undefined = undefined;
|
||||
if (requestBody != null) {
|
||||
if (typeof requestBody === "string") {
|
||||
myBody = requestBody;
|
||||
} else if (requestBody instanceof ArrayBuffer) {
|
||||
myBody = requestBody;
|
||||
} else if (ArrayBuffer.isView(requestBody)) {
|
||||
myBody = requestBody;
|
||||
} else if (typeof requestBody === "object") {
|
||||
myBody = JSON.stringify(myBody);
|
||||
} else {
|
||||
throw Error("unsupported request body type");
|
||||
}
|
||||
}
|
||||
|
||||
const response = await fetch(requestUrl, {
|
||||
headers: requestHeader,
|
||||
body: requestBody,
|
||||
body: myBody,
|
||||
method: requestMethod,
|
||||
// timeout: options?.timeout
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user