diff --git a/package.json b/package.json index 287e967..09d2d6b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "format": "prettier --write src/" }, "dependencies": { - "axios": "^1.4.0", "lucide-vue-next": "^0.576.0", "pinia": "^3.0.3", "prettier": "^3.0.3", diff --git a/src/http.ts b/src/http.ts index 9b1d8be..ca54329 100644 --- a/src/http.ts +++ b/src/http.ts @@ -1,10 +1,23 @@ -import axios from 'axios'; +export class HttpClient { + constructor(private readonly baseURL: string) {} -const http = axios.create({ - baseURL: - import.meta.env.VITE_API_DEV === '1' && import.meta.env.DEV - ? 'http://localhost:3001' - : 'https://stacjownik.spythere.eu', -}); + async get(url: string, params?: Record): Promise { + const absoluteURL = new URL(this.baseURL + '/' + url); -export default http; + if (params) { + Object.keys(params).forEach((key) => { + if (params[key] === undefined) return; + + absoluteURL.searchParams.append(key, params[key]); + }); + } + + const data = await fetch(absoluteURL); + + if (!data.ok) { + throw new Error(`Cannot fetch: ${absoluteURL}`); + } + + return data.json(); + } +} diff --git a/src/managers/apiManager.ts b/src/managers/apiManager.ts deleted file mode 100644 index 9cdf477..0000000 --- a/src/managers/apiManager.ts +++ /dev/null @@ -1,16 +0,0 @@ -import http from '../http'; -import { API } from '../types/api.types'; - -export class ApiManager { - static async fetchActiveData() { - try { - const responseData = (await http.get('/api/getActiveData')).data; - - return responseData; - } catch (error) { - console.error('Nie udało się pobrać zdalnej zawartości', error); - } - - return null; - } -} diff --git a/src/store.ts b/src/store.ts index 995980c..2cd98a9 100644 --- a/src/store.ts +++ b/src/store.ts @@ -14,6 +14,7 @@ import { import { defineStore } from 'pinia'; import { acceptableWeight, + additionalCargoTypes, carDataList, getCargoWarnings, isTractionUnit, @@ -26,9 +27,10 @@ import { totalWeight, } from './utils/vehicleUtils'; -import http from './http'; - import realCompositionsJSON from './data/realCompositions.json'; +import { HttpClient } from './http'; + +const baseURL = import.meta.env.VITE_API_DEV === '1' && import.meta.env.DEV ? 'http://localhost:3001' : 'https://stacjownik.spythere.eu'; export const useStore = defineStore('store', { state: () => ({ @@ -65,6 +67,8 @@ export const useStore = defineStore('store', { chosenStorageStockString: '', compatibleSimulatorVersion: '2025.1.1', + + httpClient: new HttpClient(baseURL), }), getters: { @@ -89,6 +93,8 @@ export const useStore = defineStore('store', { return state.stockList .map((stock, i) => { + // let cargoString = ''; + let stockTypeStr = isTractionUnit(stock.vehicleRef) || !stock.cargo ? stock.vehicleRef.type : `${stock.vehicleRef.type}:${stock.cargo.id}`; if (i == 0 && (coldStartActive || doubleManningActive)) @@ -125,7 +131,7 @@ export const useStore = defineStore('store', { actions: { async fetchVehiclesAPI() { try { - const vehiclesData = (await http.get('/api/getVehicles')).data; + const vehiclesData = await this.httpClient.get('api/getVehicles'); this.vehiclesData = vehiclesData; } catch (error) { console.error(error);