mirror of
https://github.com/Spythere/pojazdownik.git
synced 2026-05-03 05:18:10 +00:00
refactor: replaced axios with native fetch API
This commit is contained in:
+21
-8
@@ -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<T>(url: string, params?: Record<string, any>): Promise<T> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.ActiveData>('/api/getActiveData')).data;
|
||||
|
||||
return responseData;
|
||||
} catch (error) {
|
||||
console.error('Nie udało się pobrać zdalnej zawartości', error);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+9
-3
@@ -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<IVehiclesAPIResponse>('/api/getVehicles')).data;
|
||||
const vehiclesData = await this.httpClient.get<IVehiclesAPIResponse>('api/getVehicles');
|
||||
this.vehiclesData = vehiclesData;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
|
||||
Reference in New Issue
Block a user