Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1124 request restore ability to inject custom fetch instance #1535

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions docs/diagrams/architecture/http.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
```mermaid
classDiagram
class SimpleHttpClient {
number DEFAULT_TIMEOUT$
number timeout;
}
class HttpClient {
<<interface>>
string baseUrl
Expand All @@ -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
Expand Down
32 changes: 26 additions & 6 deletions packages/network/src/http/SimpleHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,41 @@ 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.
*/
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 {HeadersInit} [headers=new Headers()] - The default headers for HTTP requests.
* @param {number} [timeout=SimpleHttpClient.DEFAULT_TIMEOUT] - The timeout duration in milliseconds.
*/
constructor(
baseURL: string,
headers: HeadersInit = new Headers(),
timeout: number = SimpleHttpClient.DEFAULT_TIMEOUT
) {
this.baseURL = baseURL;
this.timeout = timeout;
this.headers = headers;
}

/**
* 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<unknown>} A promise that resolves with the response of the GET request.
*/
public async get(path: string, params?: HttpParams): Promise<unknown> {
Expand All @@ -56,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<unknown>} A promise that resolves to the response of the HTTP request.
* @throws {InvalidHTTPRequest} Throws an error if the HTTP request fails.
*/
Expand All @@ -76,6 +88,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,
Expand Down Expand Up @@ -120,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<unknown>} A promise that resolves with the response from the server.
*/
public async post(path: string, params?: HttpParams): Promise<unknown> {
Expand Down
10 changes: 8 additions & 2 deletions packages/network/tests/http/SimpleHttpClient.solo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
Expand Down Expand Up @@ -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(),
freemanzMrojo marked this conversation as resolved.
Show resolved Hide resolved
timeout
);

// Create a mock server that delays response
const mockServer = jest.fn().mockImplementation(async () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/network/tests/thor-client/ThorClient.unit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, expect, test } from '@jest/globals';
import { TESTNET_URL, ThorClient } from '../../src';

/**
* ThorClient module tests.
*
* @group integration/clients/thor-client
fabiorigam marked this conversation as resolved.
Show resolved Hide resolved
*/
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);
});
});