chore: added api mocking

This commit is contained in:
2025-01-27 02:31:08 +01:00
parent 48e8129902
commit f19a256153
9 changed files with 164 additions and 55 deletions
+66
View File
@@ -0,0 +1,66 @@
import type { AxiosInstance } from 'axios';
import axios from 'axios';
import { defineStore } from 'pinia';
import type { ActiveDataResponse, SceneriesDataResponse } from '../types/api.types';
import type { ActiveData, SceneryData } from '../types/common.types';
export const useApiStore = defineStore('api', {
state() {
return {
client: null as AxiosInstance | null,
activeData: null as ActiveData | null,
sceneryData: null as SceneryData[] | null,
};
},
actions: {
async setupAPIData() {
if (this.client != null) return;
let baseURL = 'https://stacjownik.spythere.eu';
switch (import.meta.env.VITE_API_MODE) {
case 'development':
baseURL = 'http://localhost:3001';
break;
case 'mocking':
baseURL = 'http://localhost:3123';
break;
default:
break;
}
this.client = axios.create({
baseURL,
});
this.fetchSceneriesData();
this.fetchActiveData();
setInterval(() => {
this.fetchActiveData();
}, 20000);
},
async fetchActiveData() {
try {
const response = (await this.client!.get<ActiveDataResponse>('/api/getActiveData')).data;
this.activeData = response;
} catch (error) {
console.error(error);
}
},
async fetchSceneriesData() {
try {
const response = (await this.client!.get<SceneriesDataResponse>('/api/getSceneries')).data;
this.sceneryData = response;
} catch (error) {
console.error(error);
}
},
},
});
+2 -37
View File
@@ -1,42 +1,7 @@
import { defineStore } from 'pinia';
import httpClient from '../utils/http';
import type { ActiveDataResponse, SceneriesDataResponse } from '../types/api.types';
import type { ActiveData, SceneryData } from '../types/common.types';
export const useGlobalStore = defineStore('global', {
state: () => ({
activeData: null as ActiveData | null,
sceneryData: null as SceneryData[] | null,
}),
state: () => ({}),
getters: {},
actions: {
async _fetchActiveData() {
try {
const response = (await httpClient.get<ActiveDataResponse>('/api/getActiveData')).data;
this.activeData = response;
} catch (error) {
console.error(error);
}
},
async _fetchSceneriesData() {
try {
const response = (await httpClient.get<SceneriesDataResponse>('/api/getSceneries')).data;
this.sceneryData = response;
} catch (error) {
console.error(error);
}
},
setupData() {
this._fetchActiveData();
this._fetchSceneriesData();
setInterval(() => {
this._fetchActiveData();
}, 20000);
},
},
actions: {},
});