From b95cc1876b7263913a3a18df9234fd0122f25cf2 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 5 Apr 2024 18:11:15 -0300 Subject: fix #8655 --- packages/aml-backoffice-ui/src/hooks/useBackend.ts | 72 +++++----------------- .../merchant-backoffice-ui/src/hooks/testing.tsx | 18 +++++- packages/web-util/src/context/api.ts | 3 + packages/web-util/src/tests/mock.ts | 7 ++- packages/web-util/src/tests/swr.ts | 1 + packages/web-util/src/utils/request.ts | 43 +++++++++++++ 6 files changed, 83 insertions(+), 61 deletions(-) diff --git a/packages/aml-backoffice-ui/src/hooks/useBackend.ts b/packages/aml-backoffice-ui/src/hooks/useBackend.ts index 0615c9c99..7b55568c8 100644 --- a/packages/aml-backoffice-ui/src/hooks/useBackend.ts +++ b/packages/aml-backoffice-ui/src/hooks/useBackend.ts @@ -1,65 +1,21 @@ -import { canonicalizeBaseUrl } from "@gnu-taler/taler-util"; -import { - HttpResponseOk, - RequestOptions, - useApiContext, -} from "@gnu-taler/web-util/browser"; -import { useCallback } from "preact/hooks"; -import { uiSettings } from "../settings.js"; +/* + This file is part of GNU Taler + (C) 2022 Taler Systems S.A. -interface useBackendType { - request: ( - path: string, - options?: RequestOptions, - ) => Promise>; - fetcher: (args: [string, string]) => Promise>; - paginatedFetcher: ( - args: [string, number, number, string], - ) => Promise>; -} -export function usePublicBackend(): useBackendType { - const { request: requestHandler } = useApiContext(); + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. - const baseUrl = getInitialBackendBaseURL(); + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. - const request = useCallback( - function requestImpl( - path: string, - options: RequestOptions = {}, - ): Promise> { - return requestHandler(baseUrl, path, options); - }, - [baseUrl], - ); + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see + */ + import { canonicalizeBaseUrl } from "@gnu-taler/taler-util"; +import { uiSettings } from "../settings.js"; - const fetcher = useCallback( - function fetcherImpl([endpoint, talerAmlOfficerSignature]: [string, string]): Promise> { - return requestHandler(baseUrl, endpoint, { - talerAmlOfficerSignature - }); - }, - [baseUrl], - ); - const paginatedFetcher = useCallback( - function fetcherImpl([endpoint, page, size, talerAmlOfficerSignature]: [ - string, - number, - number, - string, - ]): Promise> { - return requestHandler(baseUrl, endpoint, { - params: { page: page || 1, size }, - talerAmlOfficerSignature, - }); - }, - [baseUrl], - ); - return { - request, - fetcher, - paginatedFetcher, - }; -} export function getInitialBackendBaseURL(): string { const overrideUrl = diff --git a/packages/merchant-backoffice-ui/src/hooks/testing.tsx b/packages/merchant-backoffice-ui/src/hooks/testing.tsx index 68b0479dd..fc78f6c58 100644 --- a/packages/merchant-backoffice-ui/src/hooks/testing.tsx +++ b/packages/merchant-backoffice-ui/src/hooks/testing.tsx @@ -24,9 +24,23 @@ import { ComponentChildren, FunctionalComponent, h, VNode } from "preact"; import { HttpRequestLibrary, HttpRequestOptions, HttpResponse } from "@gnu-taler/taler-util/http"; import { SWRConfig } from "swr"; import { ApiContextProvider } from "@gnu-taler/web-util/browser"; -import { HttpResponseOk, RequestOptions } from "@gnu-taler/web-util/browser"; import { TalerBankIntegrationHttpClient, TalerCoreBankHttpClient, TalerRevenueHttpClient, TalerWireGatewayHttpClient } from "@gnu-taler/taler-util"; +interface RequestOptions { + method?: "GET" | "POST" | "HEAD", + params?: any, + token?: string | undefined, + data?: any, +} +interface HttpResponseOk { + ok: true, + data: T, + loading: boolean, + clientError: boolean, + serverError: boolean, + info: any, +} + export class ApiMockEnvironment extends MockEnvironment { constructor(debug = false) { super(debug); @@ -156,7 +170,7 @@ export class ApiMockEnvironment extends MockEnvironment { // changeToken: () => null, // }} // > - + = { method: HttpMethod; url: string; @@ -69,6 +71,9 @@ type MockedResponse = { expectedQuery?: ExpectationValues; }; +/** + * @deprecated do not use it, it will be removed + */ export abstract class MockEnvironment { expectations: Array = []; queriesMade: Array = []; diff --git a/packages/web-util/src/tests/swr.ts b/packages/web-util/src/tests/swr.ts index 903cd48d8..d5f4341f3 100644 --- a/packages/web-util/src/tests/swr.ts +++ b/packages/web-util/src/tests/swr.ts @@ -28,6 +28,7 @@ const logger = new Logger("tests/swr.ts"); * * buildTestingContext() will return a testing context * + * @deprecated do not use it, it will be removed */ export class SwrMockEnvironment extends MockEnvironment { constructor(debug = false) { diff --git a/packages/web-util/src/utils/request.ts b/packages/web-util/src/utils/request.ts index 70f943540..23d3af468 100644 --- a/packages/web-util/src/utils/request.ts +++ b/packages/web-util/src/utils/request.ts @@ -17,6 +17,9 @@ import { HttpStatusCode } from "@gnu-taler/taler-util"; import { base64encode } from "./base64.js"; +/** + * @deprecated do not use it, it will be removed + */ export enum ErrorType { CLIENT, SERVER, @@ -32,6 +35,7 @@ export enum ErrorType { * @param baseUrl URL where the service is located * @param endpoint endpoint of the service to be called * @param options auth, method and params + * @deprecated do not use it, it will be removed * @returns */ export async function defaultRequestHandler( @@ -189,16 +193,25 @@ export async function defaultRequestHandler( } } +/** + * @deprecated do not use it, it will be removed + */ export type HttpResponse = | HttpResponseOk | HttpResponseLoading | HttpError; +/** + * @deprecated do not use it, it will be removed + */ export type HttpResponsePaginated = | HttpResponseOkPaginated | HttpResponseLoading | HttpError; +/** + * @deprecated do not use it, it will be removed + */ export interface RequestInfo { url: string; hasToken: boolean; @@ -215,6 +228,9 @@ interface HttpResponseLoading { data?: T; } +/** + * @deprecated do not use it, it will be removed + */ export interface HttpResponseOk { ok: true; loading?: false; @@ -225,8 +241,14 @@ export interface HttpResponseOk { info?: RequestInfo; } +/** + * @deprecated do not use it, it will be removed + */ export type HttpResponseOkPaginated = HttpResponseOk & WithPagination; +/** + * @deprecated do not use it, it will be removed + */ export interface WithPagination { loadMore: () => void; loadMorePrev: () => void; @@ -234,6 +256,9 @@ export interface WithPagination { isReachingStart?: boolean; } +/** + * @deprecated do not use it, it will be removed + */ export type HttpError = | HttpRequestTimeoutError | HttpResponseClientError @@ -241,6 +266,9 @@ export type HttpError = | HttpResponseUnreadableError | HttpResponseUnexpectedError; +/** + * @deprecated do not use it, it will be removed + */ export interface HttpResponseServerError { ok?: false; loading?: false; @@ -292,6 +320,9 @@ interface HttpResponseUnreadableError { body: string; message: string; } +/** + * @deprecated do not use it, it will be removed + */ export class RequestError extends Error { /** * @deprecated use cause @@ -307,6 +338,9 @@ export class RequestError extends Error { type Methods = "GET" | "POST" | "PATCH" | "DELETE" | "PUT"; +/** + * @deprecated do not use it, it will be removed + */ export interface RequestOptions { method?: Methods; token?: string; @@ -323,6 +357,9 @@ export interface RequestOptions { talerAmlOfficerSignature?: string; } +/** + * @deprecated do not use it, it will be removed + */ async function buildRequestOk( response: Response, url: string, @@ -345,6 +382,9 @@ async function buildRequestOk( }; } +/** + * @deprecated do not use it, it will be removed + */ export function buildRequestFailed( url: string, dataTxt: string, @@ -424,6 +464,9 @@ export function buildRequestFailed( } } +/** + * @deprecated do not use it, it will be removed + */ function validateURL(baseUrl: string, endpoint: string): URL | undefined { try { return new URL(`${baseUrl}${endpoint}`) -- cgit v1.2.3