diff options
Diffstat (limited to 'packages/web-util/src')
-rw-r--r-- | packages/web-util/src/utils/http-impl.browser.ts | 4 | ||||
-rw-r--r-- | packages/web-util/src/utils/http-impl.sw.ts | 41 |
2 files changed, 16 insertions, 29 deletions
diff --git a/packages/web-util/src/utils/http-impl.browser.ts b/packages/web-util/src/utils/http-impl.browser.ts index 5d65c3903..1e5496071 100644 --- a/packages/web-util/src/utils/http-impl.browser.ts +++ b/packages/web-util/src/utils/http-impl.browser.ts @@ -41,8 +41,10 @@ const logger = new Logger("browserHttpLib"); /** * An implementation of the [[HttpRequestLibrary]] using the * browser's XMLHttpRequest. + * + * @deprecated use BrowserFetchHttpLib */ -export class BrowserHttpLib implements HttpRequestLibrary { +export class BrowserHttpLibDepreacted implements HttpRequestLibrary { private throttle = new RequestThrottler(); private throttlingEnabled = true; private requireTls = false; diff --git a/packages/web-util/src/utils/http-impl.sw.ts b/packages/web-util/src/utils/http-impl.sw.ts index 2ae4ccd86..316b75dfd 100644 --- a/packages/web-util/src/utils/http-impl.sw.ts +++ b/packages/web-util/src/utils/http-impl.sw.ts @@ -39,7 +39,7 @@ import { * An implementation of the [[HttpRequestLibrary]] using the * browser's XMLHttpRequest. */ -export class ServiceWorkerHttpLib implements HttpRequestLibrary { +export class BrowserFetchHttpLib implements HttpRequestLibrary { private throttle = new RequestThrottler(); private throttlingEnabled = true; private requireTls = false; @@ -58,6 +58,7 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { const requestHeader = options?.headers; const requestTimeout = options?.timeout ?? Duration.fromMilliseconds(DEFAULT_REQUEST_TIMEOUT_MS); + const requestCancel = options?.cancellationToken; const parsedUrl = new URL(requestUrl); if (this.throttlingEnabled && this.throttle.applyThrottle(requestUrl)) { @@ -82,8 +83,10 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { ); } - let myBody: ArrayBuffer | undefined = - requestMethod === "POST" ? encodeBody(requestBody) : undefined; + const myBody: ArrayBuffer | undefined = + requestMethod === "POST" || requestMethod === "PUT" || requestMethod === "PATCH" + ? encodeBody(requestBody) + : undefined; const requestHeadersMap = getDefaultHeaders(requestMethod); if (requestHeader) { @@ -94,12 +97,17 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { } const controller = new AbortController(); - let timeoutId: any | undefined; + let timeoutId: ReturnType<typeof setTimeout> | undefined; if (requestTimeout.d_ms !== "forever") { timeoutId = setTimeout(() => { - controller.abort(TalerErrorCode.WALLET_HTTP_REQUEST_GENERIC_TIMEOUT); + controller.abort(TalerErrorCode.GENERIC_TIMEOUT); }, requestTimeout.d_ms); } + if (requestCancel) { + requestCancel.onCancelled(() => { + controller.abort(TalerErrorCode.GENERIC_CLIENT_INTERNAL_ERROR) + }); + } try { const response = await fetch(requestUrl, { @@ -142,29 +150,6 @@ export class ServiceWorkerHttpLib implements HttpRequestLibrary { } } - get(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> { - return this.fetch(url, { - method: "GET", - ...opt, - }); - } - - postJson( - url: string, - body: any, - opt?: HttpRequestOptions, - ): Promise<HttpResponse> { - return this.fetch(url, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(body), - ...opt, - }); - } - - stop(): void { - // Nothing to do - } } function makeTextHandler( |