updated ApiRequests.ts

This commit is contained in:
2022-03-24 10:19:18 +01:00
parent 640eddc365
commit 0c9d6e7975
3 changed files with 191 additions and 228 deletions

View File

@@ -1,21 +1,29 @@
import axios from "axios"; import axios from "axios";
import { StatusCodes as STATUS } from "http-status-codes"; import { StatusCodes as STATUS } from "http-status-codes";
import { AUTHCODE_REGEX, SHARECODE_REGEX } from "@/constants"; import { AUTHCODE_REGEX, SHARECODE_REGEX } from "@/constants";
import type { Store } from "vuex"; import type {
import type { Match, MatchChat, MatchWeapons, Player, PlayerMeta } from "@/api"; Match,
import type { RootState } from "@/stores/matchDetails"; MatchChat,
MatchRounds,
MatchWeapons,
Player,
PlayerMeta,
} from "@/types";
import type { infoState } from "@/stores/infoState";
import { reactive } from "vue";
const API_URL = import.meta.env.VITE_API_URL; const API_URL = import.meta.env.VITE_API_URL;
// /player/<id> GET returns player <id> details (last 10 matches) // /player/<id> GET returns player <id> details (last 10 matches)
export const GetUser = async ( export const GetUser = async (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-types
store: Store<"playerDetails", RootState, {}, {}>,
id: string id: string
): Promise<Player | null> => { ): Promise<[Player | null, infoState]> => {
let response: Player | null = null; let response: Player | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/player/${id}`) .get(`${API_URL}/player/${id}`)
@@ -23,41 +31,37 @@ export const GetUser = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Player not found"; info.message = "Player not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get meta-stats or player"; info.message = "Unable to get meta-stats or player";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /player/<id>/meta/<limit> GET returns player <id> meta-stats with <limit> // /player/<id>/meta/<limit> GET returns player <id> meta-stats with <limit>
export const GetPlayerMeta = async ( export const GetPlayerMeta = async (
store: Store<never>,
player_id: string, player_id: string,
limit = 4 limit = 4
): Promise<PlayerMeta | null> => { ): Promise<[PlayerMeta | null, infoState]> => {
let response: PlayerMeta | null = null; let response: PlayerMeta | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/player/${player_id}/meta/${limit}`) .get(`${API_URL}/player/${player_id}/meta/${limit}`)
@@ -65,41 +69,37 @@ export const GetPlayerMeta = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Player not found"; info.message = "Player not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get player meta"; info.message = "Unable to get player meta";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /player/<id>/next/<unix> GET returns 20 matches after <unix> for player <id> // /player/<id>/next/<unix> GET returns 20 matches after <unix> for player <id>
export const LoadMoreMatches = async ( export const LoadMoreMatches = async (
store: Store<never>,
player_id: string, player_id: string,
date: number date: number
): Promise<Player | null> => { ): Promise<[Player | null, infoState]> => {
let response: Player | null = null; let response: Player | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/player/${player_id}/next/${date}`) .get(`${API_URL}/player/${player_id}/next/${date}`)
@@ -107,54 +107,50 @@ export const LoadMoreMatches = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Player not found"; info.message = "Player not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get meta-stats or player"; info.message = "Unable to get meta-stats or player";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /player/<id>/track POST Track player <id> FORM_DATA: authcode, [sharecode] // /player/<id>/track POST Track player <id> FORM_DATA: authcode, [sharecode]
export const TrackMe = async ( export const TrackMe = async (
store: Store<never>,
id64: string, id64: string,
authcode: string, authcode: string,
sharecode = "" sharecode = ""
): Promise<number | null> => { ): Promise<infoState> => {
let status: number | null = null; const info = reactive<infoState>({
let message = ""; statusCode: 0,
message: "",
type: "success",
});
if (sharecode !== "" && !SHARECODE_REGEX.test(sharecode)) { if (sharecode !== "" && !SHARECODE_REGEX.test(sharecode)) {
status = STATUS.IM_A_TEAPOT; info.statusCode = STATUS.IM_A_TEAPOT;
message = "Sharecode is invalid"; info.message = "Sharecode is invalid";
info.type = "error";
} }
if (authcode === "" || !AUTHCODE_REGEX.test(authcode.toUpperCase())) { if (authcode === "" || !AUTHCODE_REGEX.test(authcode.toUpperCase())) {
status = STATUS.IM_A_TEAPOT; info.statusCode = STATUS.IM_A_TEAPOT;
message = "Authcode is invalid"; info.message = "Authcode is invalid";
info.type = "error";
} }
if (status === null && message === "") { if (info.statusCode === 0 && info.message === "") {
await axios await axios
.post( .post(
`${API_URL}/player/${id64}/track`, `${API_URL}/player/${id64}/track`,
@@ -162,55 +158,52 @@ export const TrackMe = async (
) )
.then((res) => { .then((res) => {
if (res.status === STATUS.ACCEPTED) { if (res.status === STATUS.ACCEPTED) {
status = STATUS.ACCEPTED; info.statusCode = STATUS.ACCEPTED;
message = "Tracking successful"; info.message = "Tracking successful";
} }
}) })
.catch((err) => { .catch((err) => {
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Invalid arguments"; info.message = "Invalid arguments";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Player not found"; info.message = "Player not found";
break; break;
case STATUS.SERVICE_UNAVAILABLE: case STATUS.SERVICE_UNAVAILABLE:
message = "Service currently unavailable - Please try again later"; info.message =
"Service currently unavailable - Please try again later";
break; break;
case STATUS.UNAUTHORIZED: case STATUS.UNAUTHORIZED:
message = "Authcode is invalid"; info.message = "Authcode is invalid";
break; break;
case STATUS.PRECONDITION_FAILED: case STATUS.PRECONDITION_FAILED:
message = "Sharecode is invalid or missing"; info.message = "Sharecode is invalid or missing";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = info.message =
"Service is currently unavailable - Please try again later"; "Service is currently unavailable - Please try again later";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
status = err.response.status; info.statusCode = err.response.status;
info.type = "error";
}); });
} }
return info;
store.commit({
type: "changeInfoState",
data: {
statuscode: status,
message,
type: "error",
},
});
return status;
}; };
// /match/<id> GET returns details for match <id> // /match/<id> GET returns details for match <id>
export const GetMatchDetails = async ( export const GetMatchDetails = async (
store: Store<never>,
match_id: string match_id: string
): Promise<Match | null> => { ): Promise<[Match | null, infoState]> => {
let response: Match | null = null; let response: Match | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/match/${match_id}`) .get(`${API_URL}/match/${match_id}`)
@@ -218,47 +211,36 @@ export const GetMatchDetails = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Error parsing matchID"; info.message = "Error parsing matchID";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Match not found"; info.message = "Match not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get match data"; info.message = "Unable to get match data";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
type MatchRounds = {
rounds: {
[round: string]: {
[player: string]: Array<{ equip: number; spent: number; bank: number }>;
};
};
};
// /match/<id>/rounds GET returns round-stats for match <id> // /match/<id>/rounds GET returns round-stats for match <id>
export const GetPlayerValue = async ( export const GetPlayerValue = async (
store: Store<never>,
match_id: string match_id: string
): Promise<MatchRounds | null> => { ): Promise<[MatchRounds | null, infoState]> => {
let response: MatchRounds | null = null; let response: MatchRounds | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/match/${match_id}/rounds`) .get(`${API_URL}/match/${match_id}/rounds`)
@@ -266,40 +248,36 @@ export const GetPlayerValue = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Error parsing matchID"; info.message = "Error parsing matchID";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Match not found"; info.message = "Match not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get match data"; info.message = "Unable to get match data";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /match/<id>/weapons GET returns weapon-stats for match <id> // /match/<id>/weapons GET returns weapon-stats for match <id>
export const GetWeaponDmg = async ( export const GetWeaponDmg = async (
store: Store<never>,
match_id: string match_id: string
): Promise<MatchWeapons | null> => { ): Promise<[MatchWeapons | null, infoState]> => {
let response: MatchWeapons | null = null; let response: MatchWeapons | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/match/${match_id}/weapons`) .get(`${API_URL}/match/${match_id}/weapons`)
@@ -307,40 +285,36 @@ export const GetWeaponDmg = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Weapon damage not found"; info.message = "Weapon damage not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get weapon damage"; info.message = "Unable to get weapon damage";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /match/<id>/chat GET returns chat history for match <id> // /match/<id>/chat GET returns chat history for match <id>
export const GetChatHistory = async ( export const GetChatHistory = async (
store: Store<never>,
match_id: string match_id: string
): Promise<MatchChat | null> => { ): Promise<[MatchChat | null, infoState]> => {
let response: MatchChat | null = null; let response: MatchChat | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/match/${match_id}/chat`) .get(`${API_URL}/match/${match_id}/chat`)
@@ -348,40 +322,36 @@ export const GetChatHistory = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Weapon damage not found"; info.message = "Weapon damage not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get weapon damage"; info.message = "Unable to get weapon damage";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /matches/<id>/chat/<langCode> GET returns chat history for match <id> with translated sections // /matches/<id>/chat/<langCode> GET returns chat history for match <id> with translated sections
export const GetChatHistoryTranslated = async ( export const GetChatHistoryTranslated = async (
store: Store<never>,
match_id: string match_id: string
): Promise<MatchChat | null> => { ): Promise<[MatchChat | null, infoState]> => {
let response: MatchChat | null = null; let response: MatchChat | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/match/${match_id}/chat?translate=1`) .get(`${API_URL}/match/${match_id}/chat?translate=1`)
@@ -389,42 +359,34 @@ export const GetChatHistoryTranslated = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.NOT_FOUND: case STATUS.NOT_FOUND:
message = "Chat was not found"; info.message = "Chat was not found";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to get chat"; info.message = "Unable to get chat";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };
// /matches GET returns last 20 matches in DB // /matches GET returns last 20 matches in DB
export const GetMatches = async ( export const GetMatches = async (): Promise<[Match[] | null, infoState]> => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-types
store: Store<"matchDetails", RootState, {}, {}>
): Promise<Match[] | null> => {
let response: Match[] | null = null; let response: Match[] | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/matches`) .get(`${API_URL}/matches`)
@@ -432,40 +394,32 @@ export const GetMatches = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to marshal JSON"; info.message = "Unable to marshal JSON";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return [response, info];
return response;
}; };
// /matches/next/<unix> GET returns 20 matches after time <unix> // /matches/next/<unix> GET returns 20 matches after time <unix>
export const LoadMoreMatchesExplore = async ( export const LoadMoreMatchesExplore = async (
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/ban-types
store: Store<"matchDetails", RootState, {}, {}>,
date: number date: number
): Promise<Match[] | null> => { ): Promise<[Match[] | null, infoState]> => {
let response: Match[] | null = null; let response: Match[] | null = null;
const info = reactive<infoState>({
statusCode: 0,
message: "",
type: "success",
});
await axios await axios
.get(`${API_URL}/matches/next/${date}`) .get(`${API_URL}/matches/next/${date}`)
@@ -473,27 +427,19 @@ export const LoadMoreMatchesExplore = async (
if (res.status === STATUS.OK) response = res.data; if (res.status === STATUS.OK) response = res.data;
}) })
.catch((err) => { .catch((err) => {
let message = "";
switch (err.response.status) { switch (err.response.status) {
case STATUS.BAD_REQUEST: case STATUS.BAD_REQUEST:
message = "Bad request"; info.message = "Bad request";
break; break;
case STATUS.INTERNAL_SERVER_ERROR: case STATUS.INTERNAL_SERVER_ERROR:
message = "Unable to load more matches"; info.message = "Unable to load more matches";
break; break;
default: default:
message = "An unknown error occurred"; info.message = "An unknown error occurred";
} }
store.commit({ info.statusCode = err.response.status;
type: "changeInfoState", info.type = "error";
data: {
statuscode: err.response.status,
message,
type: "error",
},
});
}); });
return response; return [response, info];
}; };

View File

@@ -1,30 +1,37 @@
import type { Player } from "@/api"; export type LSPlayer = {
steamId64: string;
vanityUrl: string;
name: string;
avatar: string;
};
export const SaveLastVisitedToLocalStorage = (data: Player): void => { export const SaveLastVisitedToLocalStorage = (data: LSPlayer): void => {
const a = JSON.parse(localStorage.getItem("recent-visited") || "") || []; const a = JSON.parse(localStorage.getItem("recent-visited") || "");
console.log("hello");
console.log("a: ", a);
if (a.length === 0) { if (a.length === 0) {
a.unshift(data); a.unshift(data);
} else if (a.length === 9) { } else if (a.length === 9) {
if (a.find((p: Player) => p.steamid64 === data.steamid64)) { if (a.find((p: LSPlayer) => p.steamId64 === data.steamId64)) {
a.shift(); a.shift();
a.splice( a.splice(
a.findIndex((p: Player) => p.steamid64 === data.steamid64), a.findIndex((p: LSPlayer) => p.steamId64 === data.steamId64),
1 1
); );
a.unshift(data); a.unshift(data);
} else if (!a.find((p: Player) => p.steamid64 === data.steamid64)) { } else if (!a.find((p: LSPlayer) => p.steamId64 === data.steamId64)) {
a.shift(); a.shift();
a.unshift(data); a.unshift(data);
} }
} else if (a.length > 0 && a.length < 9) { } else if (a.length > 0 && a.length < 9) {
if (a.find((p: Player) => p.steamid64 === data.steamid64)) { if (a.find((p: LSPlayer) => p.steamId64 === data.steamId64)) {
a.splice( a.splice(
a.findIndex((p: Player) => p.steamid64 === data.steamid64), a.findIndex((p: LSPlayer) => p.steamId64 === data.steamId64),
1 1
); );
a.unshift(data); a.unshift(data);
} else if (!a.find((p: Player) => p.steamid64 === data.steamid64)) { } else if (!a.find((p: LSPlayer) => p.steamId64 === data.steamId64)) {
a.unshift(data); a.unshift(data);
} }
} }

View File

@@ -96,7 +96,7 @@ export const getPlayerArr = (
return arr; return arr;
}; };
export const constructAvatarUrl = (hash: string, size: number): string => { export const constructAvatarUrl = (hash: string, size: string): string => {
let output = ""; let output = "";
const base = const base =
"https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars"; "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars";
@@ -158,3 +158,13 @@ export const StripControlCodes = (str = ""): string => {
export const ProcessName = (str = ""): string => { export const ProcessName = (str = ""): string => {
return StripControlCodes(str).trim(); return StripControlCodes(str).trim();
}; };
export const setBgImgDisplay = (value: string, type: any): void => {
const bgImg = document.querySelector(".bg-img") as typeof type;
bgImg.style.display = value;
};
export const setAppDivBackground = (value: string, type: any): void => {
const appDiv = document.getElementById("app") as typeof type;
appDiv.style.background = value;
};