Tico4Chain-Coders / POC-Trustless-Work

POC of a decentralized application (dApp) to enhance trust in agency-client relationships using blockchain. It enables trustless payments via smart contracts, securing funds in escrow until milestones are approved by clients. Stablecoins like USDC are used to ensure stability and ease of use.
6 stars 12 forks source link

feat: adding axios instance #30

Closed KevinLatino closed 5 days ago

KevinLatino commented 6 days ago

CLR-S (2)

Pull Request | Trustless Work

1. https://github.com/Tico4Chain-Coders/POC-Trustless-Work/issues/23


2. Brief Description of the Issue

Currently, the project lacks a centralized Axios instance configured with the environment variable for the base URL. This results in repetitive code and inconsistent implementation across different API calls. This pull request introduces a reusable Axios instance and replaces all direct Axios usage with this new instance, ensuring better maintainability and adherence to best practices.


3. Type of Change

Mark with an x all the checkboxes that apply (like [x]).


4. Changes Made

1) I added a new directory (src/core/axios/http.ts), where there is an axios instance

2) In services, I started using the axios instance

5. Evidence Before Solution

[!NOTE] I think an image is better for you to understand.

Before the axios instance:

image

  1. On line 3 a constant is declared that has the environment variable as its value

  2. Then, on line 13, axios is used and the variable "API_URL" is used, which contains the value of the environment variable that is the base of the URL to make http requests

[!IMPORTANT] This is not the best practice, because we are repeating code many times, ignoring the concept of DRY (Don't Repeat Yourself). Centralizing the axios instance helps avoid this repetition and promotes maintainability.

6. Evidence After Solution

[!NOTE] I think an image is better for you to understand.

image

[!NOTE] Here we have the axios instance, with the value of the environment variable, which will be used as the base URL for http requests

image

[!IMPORTANT] Here we no longer have to be declaring a constant with the value of the environment variable, in addition to the fact that the axios instance supports the "get", "post", "put" and "delete" methods


7. Important Notes

[!NOTE] Ensure that the environment variable NEXT_PUBLIC_API_URL is correctly set up in your .env file for the axios instance to work properly. Without this variable, the base URL will default to an empty string, which could cause request failures.

[!NOTE] When updating or adding new services, remember to use the centralized axios instance located in src/core/axios/http.ts to maintain consistency and adhere to the DRY principle.

[!TIP] In the future, interceptors can be implemented in the axios instance to handle tasks such as adding authentication tokens, logging requests and responses, or retrying failed requests automatically. This would further enhance maintainability and reduce boilerplate code.