-
Notifications
You must be signed in to change notification settings - Fork 2
API module
The project contains the base layer for axios
which can be configured in axiosBase.ts.
The main methods (get
, post
, put
) from the HTTP
protocol are implemented. The other methods could be also implemented based on demand.
If the API you're using has a different business logic part then you need to separate them between different folders. Let's overview the files from the countries
folder.
All the API
requests should be implemented in countriesApi.ts using the methods from the base layer.
All the remote types should be specified in types.ts.
All the API-related constants should be kept in config.ts.
The base URL can be configured in axiosBase.ts. But the best practice is to keep all the URL files in the env file.
Using one more layer between the remote API and the components allows us to resolve the next problems:
- Not keeping the business logic in the components. Instead, we're going to call only abstract methods.
- Having the ability to combine the different API requests.
- If the remote API has been updated then we will be processing all the changes in a separate layer and the components will be untouchable.
It's a good practice to have the local models for the data due to the next benefits:
- Remote models don't always have the needed structure;
- You are able to move all the business logic to a separate layer and components will just display the data without any additional calculations. It's easier to debug, extend and maintain the code;
- You have every chance of creating as few redundant as possible models.
The local API should be described and kept in the services
folder inside the modules. In this layer, we're going to accept the different params from the components, log them and process the errors and of course, call the remote API. Check an example for the countriesApi.
The parsers are needed to parse the data from the remote types to local and vise-versa. We can specify the convenient format of the data, and parse different data types such as date and time, etc. Check an example for the countriesParsers.