Files
csgowtf/src/utils/ApiRequests.js
2022-01-31 01:31:35 +01:00

172 lines
3.8 KiB
JavaScript

import axios from "axios";
import {errorHandling} from "@/utils/Utils";
const API_URL = process.env.VUE_APP_API_URL
// const TIMEOUT = 10_000
export const GetMatches = async () => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/matches`,
})
if (res.status === 200)
return res.data
} catch {
console.log('Could not load matches')
}
}
export const GetUser = async (id) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/player/${id}`,
})
if (res.status === 200)
return res.data
} catch {
console.log('Could not load user')
}
}
export const TrackMe = async (id64, authcode, sharecode = '') => {
let statusError = ''
let status = 202
const shareCodeRegex = /^CSGO(?:-?[ABCDEFGHJKLMNOPQRSTUVWXYZabcdefhijkmnopqrstuvwxyz23456789]{5}){5}$/
const authCodeRegex = /^[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{5}-[ABCDEFGHJKLMNOPQRSTUVWXYZ23456789]{4}$/
if (sharecode !== '' && !shareCodeRegex.test(sharecode)) {
statusError = 'Is not a valid sharecode'
status = 418
return [status, statusError]
}
if (authcode === '' || !authCodeRegex.test(authcode)) {
statusError = 'Is not a valid authcode'
status = 418
return [status, statusError]
}
try {
// const res = await axios
// .post(`${API_URL}/player/${id64}/track`, `authcode=${authcode}&sharecode=${sharecode}`)
// TODO: Needs testing
const res = await axios({
method: 'post',
url: `${API_URL}/player/${id64}/track`,
data: `authcode=${authcode}&sharecode=${sharecode}`,
})
if (res.status === 202) {
status = res.status
}
} catch (err) {
if (err.response.status === 401) {
statusError = 'Data does not match player'
status = err.response.status
} else if (err.response.status === 400) {
statusError = 'Userinput was wrong'
status = err.response.status
} else {
errorHandling(err.response.status)
}
}
return [status, statusError]
}
export const GetPlayerValue = async (match_id) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/match/${match_id}/rounds`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not get player value')
}
}
export const GetMatchDetails = async (match_id) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/match/${match_id}`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not load match details')
}
}
export const LoadMoreMatches = async (player_id, date) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/player/${player_id}/next/${date}`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not load more matches')
}
}
export const GetPlayerMeta = async (player_id, limit = 4) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/player/${player_id}/meta/${limit}`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not load player metadata')
}
}
export const GetWeaponDmg = async (match_id) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/match/${match_id}/weapons`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not calculate weapon damage')
}
}
export const LoadMoreMatchesExplore = async (date) => {
try {
const res = await axios({
method: 'get',
url: `${API_URL}/matches/next/${date}`,
})
if (res.status === 200) {
return res.data
}
} catch {
console.log('Could not load more matches')
}
}