mirror of
https://github.com/Spythere/pojazdownik.git
synced 2026-05-03 19:48:11 +00:00
refactor: replaced axios with native fetch API
This commit is contained in:
@@ -12,7 +12,6 @@
|
|||||||
"format": "prettier --write src/"
|
"format": "prettier --write src/"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.4.0",
|
|
||||||
"lucide-vue-next": "^0.576.0",
|
"lucide-vue-next": "^0.576.0",
|
||||||
"pinia": "^3.0.3",
|
"pinia": "^3.0.3",
|
||||||
"prettier": "^3.0.3",
|
"prettier": "^3.0.3",
|
||||||
|
|||||||
+21
-8
@@ -1,10 +1,23 @@
|
|||||||
import axios from 'axios';
|
export class HttpClient {
|
||||||
|
constructor(private readonly baseURL: string) {}
|
||||||
|
|
||||||
const http = axios.create({
|
async get<T>(url: string, params?: Record<string, any>): Promise<T> {
|
||||||
baseURL:
|
const absoluteURL = new URL(this.baseURL + '/' + url);
|
||||||
import.meta.env.VITE_API_DEV === '1' && import.meta.env.DEV
|
|
||||||
? 'http://localhost:3001'
|
|
||||||
: 'https://stacjownik.spythere.eu',
|
|
||||||
});
|
|
||||||
|
|
||||||
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 { defineStore } from 'pinia';
|
||||||
import {
|
import {
|
||||||
acceptableWeight,
|
acceptableWeight,
|
||||||
|
additionalCargoTypes,
|
||||||
carDataList,
|
carDataList,
|
||||||
getCargoWarnings,
|
getCargoWarnings,
|
||||||
isTractionUnit,
|
isTractionUnit,
|
||||||
@@ -26,9 +27,10 @@ import {
|
|||||||
totalWeight,
|
totalWeight,
|
||||||
} from './utils/vehicleUtils';
|
} from './utils/vehicleUtils';
|
||||||
|
|
||||||
import http from './http';
|
|
||||||
|
|
||||||
import realCompositionsJSON from './data/realCompositions.json';
|
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', {
|
export const useStore = defineStore('store', {
|
||||||
state: () => ({
|
state: () => ({
|
||||||
@@ -65,6 +67,8 @@ export const useStore = defineStore('store', {
|
|||||||
chosenStorageStockString: '',
|
chosenStorageStockString: '',
|
||||||
|
|
||||||
compatibleSimulatorVersion: '2025.1.1',
|
compatibleSimulatorVersion: '2025.1.1',
|
||||||
|
|
||||||
|
httpClient: new HttpClient(baseURL),
|
||||||
}),
|
}),
|
||||||
|
|
||||||
getters: {
|
getters: {
|
||||||
@@ -89,6 +93,8 @@ export const useStore = defineStore('store', {
|
|||||||
|
|
||||||
return state.stockList
|
return state.stockList
|
||||||
.map((stock, i) => {
|
.map((stock, i) => {
|
||||||
|
// let cargoString = '';
|
||||||
|
|
||||||
let stockTypeStr = isTractionUnit(stock.vehicleRef) || !stock.cargo ? stock.vehicleRef.type : `${stock.vehicleRef.type}:${stock.cargo.id}`;
|
let stockTypeStr = isTractionUnit(stock.vehicleRef) || !stock.cargo ? stock.vehicleRef.type : `${stock.vehicleRef.type}:${stock.cargo.id}`;
|
||||||
|
|
||||||
if (i == 0 && (coldStartActive || doubleManningActive))
|
if (i == 0 && (coldStartActive || doubleManningActive))
|
||||||
@@ -125,7 +131,7 @@ export const useStore = defineStore('store', {
|
|||||||
actions: {
|
actions: {
|
||||||
async fetchVehiclesAPI() {
|
async fetchVehiclesAPI() {
|
||||||
try {
|
try {
|
||||||
const vehiclesData = (await http.get<IVehiclesAPIResponse>('/api/getVehicles')).data;
|
const vehiclesData = await this.httpClient.get<IVehiclesAPIResponse>('api/getVehicles');
|
||||||
this.vehiclesData = vehiclesData;
|
this.vehiclesData = vehiclesData;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|||||||
Reference in New Issue
Block a user