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.
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]).
[ ] 📝 Documentation (updates to README, docs, or comments)
[ ] 🐛 Bug fix (non-breaking change which fixes an issue)
[x] 👌 Enhancement (non-breaking change which adds functionality)
[ ] 💥 Breaking change (fix or feature that would cause existing functionality to change)
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:
On line 3 a constant is declared that has the environment variable as its value
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.
[!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
[!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.
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
Before the axios instance:
On line 3 a constant is declared that has the environment variable as its value
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
6. Evidence After Solution
7. Important Notes