import axios, {AxiosPromise, AxiosResponse} from 'axios' import { JoinCreateGameObject, IsStartedObject, ApiObject, GameStateObject, MyTurnObject, GuessAction, GameRules } from "@/objects/objects"; export default class PerudoApi { private static baseUrl = process.env.VUE_APP_BASE_URL; private static objectInstance?: PerudoApi; public static get instance(): PerudoApi { if (typeof PerudoApi.objectInstance === 'undefined') { PerudoApi.objectInstance = new PerudoApi(); } return PerudoApi.objectInstance; } public createGame(name: string): Promise { return this.get('game/create?name=' + name) .then((response: AxiosResponse) => { return response.data; }) } public joinGame(name: string, code: string): Promise { return this.get('game/join/' + code + '?name=' + name) .then((response: AxiosResponse) => { return response.data; }) } public setGameRules(playerId: string, rules: GameRules): Promise { return this.post('game/rules/' + playerId, rules) .then((response: AxiosResponse) => { return response.data; }); } public gameStarted(playerId: string): Promise { return this.get('game/started/' + playerId) .then((response: AxiosResponse) => { return response.data; }) } public startGame(playerId: string): Promise { return this.get('game/start/' + playerId) .then((response: AxiosResponse) => { return response.data; }); } public myTurn(playerId: string): Promise { return this.get('player/turn/' + playerId) .then((response: AxiosResponse) => { return response.data; }) } public makeGuess(playerId: string, guess: GuessAction): Promise { return this.post('player/guess/' + playerId, guess) .then((response: AxiosResponse) => { return response.data; }); } public callBluff(playerId: string): Promise { return this.post('player/call/' + playerId) .then((response: AxiosResponse) => { return response.data; }); } private get(url: string, getParameters?: Record): AxiosPromise { return axios.get(PerudoApi.baseUrl + url).then((response: AxiosResponse) => { // @ts-ignore if (response?.data?.errors?.length > 0) { throw response.data.errors?.join(); } return response; }) } private post(url: string, data?: any,): AxiosPromise { return axios.post(PerudoApi.baseUrl + url, data).then((response: AxiosResponse) => { // @ts-ignore if (response?.data?.errors?.length > 0) { throw response.data.errors?.join(); } return response; }); } }