mirror of
https://github.com/Spythere/srjp-td2.git
synced 2026-05-03 13:38:12 +00:00
chore: added api mocking
This commit is contained in:
@@ -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);
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user