From 8c20f4b27946679267bb44255721a9f14ae1077a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 11 Sep 2023 15:07:55 -0300 Subject: new login token --- packages/web-util/src/utils/request.ts | 75 +++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 10 deletions(-) (limited to 'packages/web-util') diff --git a/packages/web-util/src/utils/request.ts b/packages/web-util/src/utils/request.ts index 1464eca98..8ce21b0e1 100644 --- a/packages/web-util/src/utils/request.ts +++ b/packages/web-util/src/utils/request.ts @@ -25,6 +25,8 @@ export enum ErrorType { UNEXPECTED, } + + /** * * @param baseUrl URL where the service is located @@ -60,10 +62,27 @@ export async function defaultRequestHandler( const requestPreventCache = options.preventCache ?? false; const requestPreventCors = options.preventCors ?? false; - const _url = new URL(`${baseUrl}${endpoint}`); + const validURL = validateURL(baseUrl, endpoint); + + if (!validURL) { + const error: HttpResponseUnexpectedError = { + info: { + url: `${baseUrl}${endpoint}`, + payload: {}, + hasToken: !!options.token, + status: 0, + options, + }, + type: ErrorType.UNEXPECTED, + exception: undefined, + loading: false, + message: `invalid URL: "${validURL}"`, + }; + throw new RequestError(error) + } Object.entries(requestParams).forEach(([key, value]) => { - _url.searchParams.set(key, String(value)); + validURL.searchParams.set(key, String(value)); }); let payload: BodyInit | undefined = undefined; @@ -77,7 +96,20 @@ export async function defaultRequestHandler( } else if (typeof requestBody === "object") { payload = JSON.stringify(requestBody); } else { - throw Error("unsupported request body type"); + const error: HttpResponseUnexpectedError = { + info: { + url: validURL.href, + payload: {}, + hasToken: !!options.token, + status: 0, + options, + }, + type: ErrorType.UNEXPECTED, + exception: undefined, + loading: false, + message: `unsupported request body type: "${typeof requestBody}"`, + }; + throw new RequestError(error) } } @@ -88,7 +120,7 @@ export async function defaultRequestHandler( let response; try { - response = await fetch(_url.href, { + response = await fetch(validURL.href, { headers: requestHeaders, method: requestMethod, credentials: "omit", @@ -100,15 +132,29 @@ export async function defaultRequestHandler( } catch (ex) { const info: RequestInfo = { payload, - url: _url.href, + url: validURL.href, hasToken: !!options.token, status: 0, options, }; - const error: HttpRequestTimeoutError = { + + if (ex instanceof Error) { + if (ex.message === "HTTP_REQUEST_TIMEOUT") { + const error: HttpRequestTimeoutError = { + info, + type: ErrorType.TIMEOUT, + message: "request timeout", + }; + throw new RequestError(error); + } + } + + const error: HttpResponseUnexpectedError = { info, - type: ErrorType.TIMEOUT, - message: "Request timeout", + type: ErrorType.UNEXPECTED, + exception: ex, + loading: false, + message: (ex instanceof Error ? ex.message : ""), }; throw new RequestError(error); } @@ -124,7 +170,7 @@ export async function defaultRequestHandler( if (response.ok) { const result = await buildRequestOk( response, - _url.href, + validURL.href, payload, !!options.token, options, @@ -133,7 +179,7 @@ export async function defaultRequestHandler( } else { const dataTxt = await response.text(); const error = buildRequestFailed( - _url.href, + validURL.href, dataTxt, response.status, payload, @@ -377,3 +423,12 @@ export function buildRequestFailed( return error; } } + +function validateURL(baseUrl: string, endpoint: string): URL | undefined { + try { + return new URL(`${baseUrl}${endpoint}`) + } catch (ex) { + return undefined + } + +} \ No newline at end of file -- cgit v1.2.3