From fcc9f42769dde3e11fa100be782e2eda56b5f153 Mon Sep 17 00:00:00 2001 From: lucanicoladebiasi Date: Mon, 25 Nov 2024 15:49:00 +0000 Subject: [PATCH 1/5] fix: 1450 cyclic dependencies in thor-client.md --- packages/network/src/http/SimpleHttpClient.ts | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/packages/network/src/http/SimpleHttpClient.ts b/packages/network/src/http/SimpleHttpClient.ts index b8e8e0cbe..d469aacfa 100644 --- a/packages/network/src/http/SimpleHttpClient.ts +++ b/packages/network/src/http/SimpleHttpClient.ts @@ -20,6 +20,8 @@ class SimpleHttpClient implements HttpClient { */ public readonly baseURL: string; + public readonly headers: HeadersInit; + /** * Return the amount of time in milliseconds before a timeout occurs * when requesting with HTTP methods. @@ -27,17 +29,23 @@ class SimpleHttpClient implements HttpClient { public readonly timeout: number; /** - * Constructs an instance of SimpleHttpClient with the given base URL and timeout period. + * Constructs an instance of SimpleHttpClient with the given base URL, + * timeout period and HTTP headers. + * The HTTP headers are used each time this client send a request to the URL, + * if not overwritten by the {@link HttpParams} of the method sending the request. * - * @param {string} baseURL - The base URL for the HTTP client. - * @param {number} [timeout=SimpleHttpClient.DEFAULT_TIMEOUT] - The timeout period for requests in milliseconds. + * @param {string} baseURL - The base URL for HTTP requests. + * @param {number} [timeout=SimpleHttpClient.DEFAULT_TIMEOUT] - The timeout duration in milliseconds. + * @param {HeadersInit} [headers=new Headers()] - The default headers for HTTP requests. */ constructor( baseURL: string, - timeout: number = SimpleHttpClient.DEFAULT_TIMEOUT + timeout: number = SimpleHttpClient.DEFAULT_TIMEOUT, + headers: HeadersInit = new Headers() ) { this.baseURL = baseURL; this.timeout = timeout; + this.headers = headers; } /** @@ -76,6 +84,12 @@ class SimpleHttpClient implements HttpClient { url.searchParams.append(key, String(value)); }); } + const headers = new Headers(this.headers); + if (params?.headers !== undefined && params?.headers != null) { + Object.entries(params.headers).forEach(([key, value]) => { + headers.append(key, String(value)); + }); + } const response = await fetch(url, { method, headers: params?.headers as HeadersInit, From fc9cf27b917ff8b2cfb7bd228227f483ee814213 Mon Sep 17 00:00:00 2001 From: lucanicoladebiasi Date: Mon, 25 Nov 2024 16:23:28 +0000 Subject: [PATCH 2/5] fix: 1450 cyclic dependencies in thor-client.md --- packages/network/src/http/SimpleHttpClient.ts | 6 +++--- .../network/tests/http/SimpleHttpClient.solo.test.ts | 10 ++++++++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/network/src/http/SimpleHttpClient.ts b/packages/network/src/http/SimpleHttpClient.ts index d469aacfa..f9cbc0852 100644 --- a/packages/network/src/http/SimpleHttpClient.ts +++ b/packages/network/src/http/SimpleHttpClient.ts @@ -35,13 +35,13 @@ class SimpleHttpClient implements HttpClient { * if not overwritten by the {@link HttpParams} of the method sending the request. * * @param {string} baseURL - The base URL for HTTP requests. - * @param {number} [timeout=SimpleHttpClient.DEFAULT_TIMEOUT] - The timeout duration in milliseconds. * @param {HeadersInit} [headers=new Headers()] - The default headers for HTTP requests. + * @param {number} [timeout=SimpleHttpClient.DEFAULT_TIMEOUT] - The timeout duration in milliseconds. */ constructor( baseURL: string, - timeout: number = SimpleHttpClient.DEFAULT_TIMEOUT, - headers: HeadersInit = new Headers() + headers: HeadersInit = new Headers(), + timeout: number = SimpleHttpClient.DEFAULT_TIMEOUT ) { this.baseURL = baseURL; this.timeout = timeout; diff --git a/packages/network/tests/http/SimpleHttpClient.solo.test.ts b/packages/network/tests/http/SimpleHttpClient.solo.test.ts index 80a429e6e..eeaf9d711 100644 --- a/packages/network/tests/http/SimpleHttpClient.solo.test.ts +++ b/packages/network/tests/http/SimpleHttpClient.solo.test.ts @@ -63,7 +63,9 @@ describe('SimpleHttpClient solo tests', () => { query: {}, body: {}, headers: { - 'X-Custom-Header': 'custom-value' + 'X-Custom-Header': 'custom-value', + 'Cache-Control': + 'no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate' }, validateResponseHeader: function ( headers: Record @@ -107,7 +109,11 @@ describe('SimpleHttpClient solo tests', () => { */ test('timeout test - 100 ms', async () => { const timeout = 100; // 100ms timeout - const httpClient = new SimpleHttpClient(THOR_SOLO_URL, timeout); + const httpClient = new SimpleHttpClient( + THOR_SOLO_URL, + new Headers(), + timeout + ); // Create a mock server that delays response const mockServer = jest.fn().mockImplementation(async () => { From f4c295664ec4e200fa55bf6be02918fae524ea52 Mon Sep 17 00:00:00 2001 From: lucanicoladebiasi Date: Mon, 25 Nov 2024 16:37:21 +0000 Subject: [PATCH 3/5] fix: 1450 cyclic dependencies in thor-client.md --- packages/network/src/http/SimpleHttpClient.ts | 12 +++++++++--- .../tests/thor-client/ThorClient.unit.test.ts | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 packages/network/tests/thor-client/ThorClient.unit.test.ts diff --git a/packages/network/src/http/SimpleHttpClient.ts b/packages/network/src/http/SimpleHttpClient.ts index f9cbc0852..93009263d 100644 --- a/packages/network/src/http/SimpleHttpClient.ts +++ b/packages/network/src/http/SimpleHttpClient.ts @@ -52,7 +52,9 @@ class SimpleHttpClient implements HttpClient { * Sends an HTTP GET request to the specified path with optional query parameters. * * @param {string} path - The endpoint path to which the HTTP GET request is sent. - * @param {HttpParams} [params] - Optional query parameters to include in the request. + * @param {HttpParams} [params] - Optional parameters for the request, + * including query parameters, headers, body, and response validation. + * {@link HttpParams.headers} override {@link SimpleHttpClient.headers}. * @return {Promise} A promise that resolves with the response of the GET request. */ public async get(path: string, params?: HttpParams): Promise { @@ -64,7 +66,9 @@ class SimpleHttpClient implements HttpClient { * * @param {HttpMethod} method - The HTTP method to use for the request (e.g., GET, POST). * @param {string} path - The URL path for the request. - * @param {HttpParams} [params] - Optional parameters for the request, including query parameters, headers, body, and response validation. + * @param {HttpParams} [params] - Optional parameters for the request, + * including query parameters, headers, body, and response validation. + * {@link HttpParams.headers} override {@link SimpleHttpClient.headers}. * @return {Promise} A promise that resolves to the response of the HTTP request. * @throws {InvalidHTTPRequest} Throws an error if the HTTP request fails. */ @@ -134,7 +138,9 @@ class SimpleHttpClient implements HttpClient { * Makes an HTTP POST request to the specified path with optional parameters. * * @param {string} path - The endpoint to which the POST request is made. - * @param {HttpParams} [params] - An optional object containing query parameters or data to be sent with the request. + * @param {HttpParams} [params] - Optional parameters for the request, + * including query parameters, headers, body, and response validation. + * {@link HttpParams.headers} override {@link SimpleHttpClient.headers}. * @return {Promise} A promise that resolves with the response from the server. */ public async post(path: string, params?: HttpParams): Promise { diff --git a/packages/network/tests/thor-client/ThorClient.unit.test.ts b/packages/network/tests/thor-client/ThorClient.unit.test.ts new file mode 100644 index 000000000..f7d71eb4a --- /dev/null +++ b/packages/network/tests/thor-client/ThorClient.unit.test.ts @@ -0,0 +1,14 @@ +import { describe, expect, test } from '@jest/globals'; +import { TESTNET_URL, ThorClient } from '../../src'; + +/** + * ThorClient module tests. + */ +describe('ThorClient deprecated methods', () => { + test('ok <- fromUrl', () => { + const expected = ThorClient.at(TESTNET_URL); + // eslint-disable-next-line sonarjs/deprecation + const actual = ThorClient.fromUrl(TESTNET_URL); + expect(actual.httpClient.baseURL).toEqual(expected.httpClient.baseURL); + }); +}); From 58ac41e152de012b861b7b698b5ffad1c2a0774f Mon Sep 17 00:00:00 2001 From: lucanicoladebiasi Date: Mon, 25 Nov 2024 16:58:27 +0000 Subject: [PATCH 4/5] fix: 1450 cyclic dependencies in thor-client.md --- docs/diagrams/architecture/http.md | 9 +++++---- .../network/tests/thor-client/ThorClient.unit.test.ts | 2 ++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/docs/diagrams/architecture/http.md b/docs/diagrams/architecture/http.md index 29a76b0b6..7cca9dbe4 100644 --- a/docs/diagrams/architecture/http.md +++ b/docs/diagrams/architecture/http.md @@ -1,9 +1,5 @@ ```mermaid classDiagram - class SimpleHttpClient { - number DEFAULT_TIMEOUT$ - number timeout; - } class HttpClient { <> string baseUrl @@ -23,6 +19,11 @@ classDiagram Record~string, string~ query validateResponse(Record~string, string~ headers) } + class SimpleHttpClient { + number DEFAULT_TIMEOUT$ + HeadersInit headers + number timeout + } HttpMethod o-- HttpClient HttpParams *-- HttpClient HttpClient <|.. SimpleHttpClient diff --git a/packages/network/tests/thor-client/ThorClient.unit.test.ts b/packages/network/tests/thor-client/ThorClient.unit.test.ts index f7d71eb4a..0cad2810e 100644 --- a/packages/network/tests/thor-client/ThorClient.unit.test.ts +++ b/packages/network/tests/thor-client/ThorClient.unit.test.ts @@ -3,6 +3,8 @@ import { TESTNET_URL, ThorClient } from '../../src'; /** * ThorClient module tests. + * + * @group integration/clients/thor-client */ describe('ThorClient deprecated methods', () => { test('ok <- fromUrl', () => { From 855405fdcf3f2480aabfcf5f37028c18dbe1bcae Mon Sep 17 00:00:00 2001 From: Fabio Rigamonti <73019897+fabiorigam@users.noreply.github.com> Date: Tue, 26 Nov 2024 13:56:47 +0100 Subject: [PATCH 5/5] fix: change test type --- packages/network/tests/thor-client/ThorClient.unit.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/network/tests/thor-client/ThorClient.unit.test.ts b/packages/network/tests/thor-client/ThorClient.unit.test.ts index 0cad2810e..8aa1a5cba 100644 --- a/packages/network/tests/thor-client/ThorClient.unit.test.ts +++ b/packages/network/tests/thor-client/ThorClient.unit.test.ts @@ -4,7 +4,7 @@ import { TESTNET_URL, ThorClient } from '../../src'; /** * ThorClient module tests. * - * @group integration/clients/thor-client + * @group unit/clients/thor-client */ describe('ThorClient deprecated methods', () => { test('ok <- fromUrl', () => {