HTTP Client
The @justomx/http-client is an HTTP client designed specifically for microservices within Jüsto’s ecosystem. It is built on top of axios, a popular JavaScript library for making HTTP requests, inheriting all of its functionality such as promise support, interceptors, and request cancellation. Additionally, it introduces custom configurations specific to Jüsto’s microservice architecture, including the ability to log requests/responses and manage persistent HTTP connections.
This client simplifies the creation of instances and handling of HTTP requests, reducing repetitive configurations and adding a layer of consistency and control across services.
Installation
Section titled “Installation”Installing the client is straightforward through npm. It is recommended to also install the @justomx/context dependency, which might be necessary for handling context between microservices.
Run the following command in your terminal:
npm install --save @justomx/{http-client,context}@latestThis will install the latest version of both the HTTP client and the context library.
Basic Usage
Section titled “Basic Usage”Creating a new HttpClient instance
Section titled “Creating a new HttpClient instance”The HTTP client is initialized through the static build method. The first parameter it accepts is the name of the service you are using. This name is used internally to identify the request origin and can be useful for logging and tracing requests between microservices.
const client = HttpClient.build('MyServiceName')Standard Configuration
Section titled “Standard Configuration”HttpClient inherits all configuration options from axios, meaning you can customize aspects such as the baseURL, request timeout, custom headers, and more.
Here’s a simple configuration example:
const client = HttpClient.build('MyServiceName', { baseURL: 'http://localhost:8080/', timeout: 5000})- baseURL: Defines the base URL for all HTTP requests made with this client. This is ideal when interacting with a specific API or service.
- timeout: The maximum time (in milliseconds) the request will wait before throwing an error. In this case, it is set to 5 seconds.
This setup is particularly useful when interacting with a microservice hosted in a local or remote environment.
Additional Options
Section titled “Additional Options”Besides the standard options offered by axios, HttpClient introduces two new configurations:
1. logger
Section titled “1. logger”You can pass a logging function via the logger option. This function will be called with details of every HTTP request and response, allowing you to record network activity in your service. The logger is useful for monitoring, debugging, and runtime error analysis.
Example with a logger:
const client = HttpClient.build('MyServiceName', { baseURL: 'http://localhost:8080/', timeout: 5000, logger: getLogger() // You need to implement or import your logging function.})The getLogger() function could be a utility that implements logic to print to the console or send logs to a log analysis service.
2. keepAlive
Section titled “2. keepAlive”The client also allows you to control the keepAlive option, which determines whether HTTP connections should remain open between requests (using a reusable TCP connection). This can improve performance in microservice environments with frequent requests, although it may increase resource usage in some cases. The default value is true, meaning that persistent connections will be used.
Example configuring keepAlive:
const client = HttpClient.build('MyServiceName', { baseURL: 'http://localhost:8080/', timeout: 5000, keepAlive: false // Disable connection reuse.})Default values:
Section titled “Default values:”- timeout: 3,000 ms
- keepAlive:
true
Advanced Interaction
Section titled “Advanced Interaction”The HTTP client also supports the use of axios interceptors, which is ideal for adding custom logic before a request is sent or a response is processed. Interceptors are useful for adding authentication tokens, handling global errors, or modifying response data before it reaches your application.
Here’s an example of a basic interceptor:
export class CustomService { private readonly client: AxiosInstance constructor(basePath: string) { this.client = HttpClient.build(CustomService.name)
this.client.interceptors.request.use( (config) => { // Add an authentication token to each request config.headers.Authorization = `Bearer ${token}` return config }, (error) => { // Handle errors before the request is sent return Promise.reject(error) } ) }}References
Section titled “References”This client is built on top of axios, meaning all of axios’s advanced configurations and features are applicable. For more information on these options, check the official documentation:
Axios Documentation
If you need more details on how to leverage this HTTP client within Jüsto’s microservice environment, make sure to also review internal guides on context management and logging for microservices.