updated utils from js to ts
This commit is contained in:
499
src/utils/ApiRequests.ts
Normal file
499
src/utils/ApiRequests.ts
Normal file
@@ -0,0 +1,499 @@
|
||||
import axios from "axios";
|
||||
import { StatusCodes as STATUS } from "http-status-codes";
|
||||
import { AUTHCODE_REGEX, SHARECODE_REGEX } from "@/constants";
|
||||
import type { Store } from "vuex";
|
||||
import type { Match, MatchChat, MatchWeapons, Player, PlayerMeta } from "@/api";
|
||||
import type { RootState } from "@/stores/matchDetails";
|
||||
|
||||
const API_URL = import.meta.env.VITE_API_URL;
|
||||
|
||||
// /player/<id> GET returns player <id> details (last 10 matches)
|
||||
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
|
||||
): Promise<Player | null> => {
|
||||
let response: Player | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/player/${id}`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Player not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get meta-stats or player";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /player/<id>/meta/<limit> GET returns player <id> meta-stats with <limit>
|
||||
export const GetPlayerMeta = async (
|
||||
store: Store<never>,
|
||||
player_id: string,
|
||||
limit = 4
|
||||
): Promise<PlayerMeta | null> => {
|
||||
let response: PlayerMeta | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/player/${player_id}/meta/${limit}`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Player not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get player meta";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /player/<id>/next/<unix> GET returns 20 matches after <unix> for player <id>
|
||||
export const LoadMoreMatches = async (
|
||||
store: Store<never>,
|
||||
player_id: string,
|
||||
date: number
|
||||
): Promise<Player | null> => {
|
||||
let response: Player | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/player/${player_id}/next/${date}`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Player not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get meta-stats or player";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /player/<id>/track POST Track player <id> FORM_DATA: authcode, [sharecode]
|
||||
export const TrackMe = async (
|
||||
store: Store<never>,
|
||||
id64: string,
|
||||
authcode: string,
|
||||
sharecode = ""
|
||||
): Promise<number | null> => {
|
||||
let status: number | null = null;
|
||||
let message = "";
|
||||
|
||||
if (sharecode !== "" && !SHARECODE_REGEX.test(sharecode)) {
|
||||
status = STATUS.IM_A_TEAPOT;
|
||||
message = "Sharecode is invalid";
|
||||
}
|
||||
if (authcode === "" || !AUTHCODE_REGEX.test(authcode.toUpperCase())) {
|
||||
status = STATUS.IM_A_TEAPOT;
|
||||
message = "Authcode is invalid";
|
||||
}
|
||||
|
||||
if (status === null && message === "") {
|
||||
await axios
|
||||
.post(
|
||||
`${API_URL}/player/${id64}/track`,
|
||||
`authcode=${authcode.toUpperCase()}&sharecode=${sharecode}`
|
||||
)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.ACCEPTED) {
|
||||
status = STATUS.ACCEPTED;
|
||||
message = "Tracking successful";
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Invalid arguments";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Player not found";
|
||||
break;
|
||||
case STATUS.SERVICE_UNAVAILABLE:
|
||||
message = "Service currently unavailable - Please try again later";
|
||||
break;
|
||||
case STATUS.UNAUTHORIZED:
|
||||
message = "Authcode is invalid";
|
||||
break;
|
||||
case STATUS.PRECONDITION_FAILED:
|
||||
message = "Sharecode is invalid or missing";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message =
|
||||
"Service is currently unavailable - Please try again later";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
status = err.response.status;
|
||||
});
|
||||
}
|
||||
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
return status;
|
||||
};
|
||||
|
||||
// /match/<id> GET returns details for match <id>
|
||||
export const GetMatchDetails = async (
|
||||
store: Store<never>,
|
||||
match_id: string
|
||||
): Promise<Match | null> => {
|
||||
let response: Match | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/match/${match_id}`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Error parsing matchID";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Match not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get match data";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
type MatchRounds = {
|
||||
rounds: {
|
||||
[round: string]: {
|
||||
[player: string]: Array<{ equip: number; spent: number; bank: number }>;
|
||||
};
|
||||
};
|
||||
};
|
||||
// /match/<id>/rounds GET returns round-stats for match <id>
|
||||
export const GetPlayerValue = async (
|
||||
store: Store<never>,
|
||||
match_id: string
|
||||
): Promise<MatchRounds | null> => {
|
||||
let response: MatchRounds | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/match/${match_id}/rounds`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Error parsing matchID";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Match not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get match data";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /match/<id>/weapons GET returns weapon-stats for match <id>
|
||||
export const GetWeaponDmg = async (
|
||||
store: Store<never>,
|
||||
match_id: string
|
||||
): Promise<MatchWeapons | null> => {
|
||||
let response: MatchWeapons | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/match/${match_id}/weapons`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Weapon damage not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get weapon damage";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /match/<id>/chat GET returns chat history for match <id>
|
||||
export const GetChatHistory = async (
|
||||
store: Store<never>,
|
||||
match_id: string
|
||||
): Promise<MatchChat | null> => {
|
||||
let response: MatchChat | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/match/${match_id}/chat`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Weapon damage not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get weapon damage";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /matches/<id>/chat/<langCode> GET returns chat history for match <id> with translated sections
|
||||
export const GetChatHistoryTranslated = async (
|
||||
store: Store<never>,
|
||||
match_id: string
|
||||
): Promise<MatchChat | null> => {
|
||||
let response: MatchChat | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/match/${match_id}/chat?translate=1`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.NOT_FOUND:
|
||||
message = "Chat was not found";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to get chat";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /matches GET returns last 20 matches in DB
|
||||
export const GetMatches = async (
|
||||
// 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;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/matches`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to marshal JSON";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
||||
|
||||
// /matches/next/<unix> GET returns 20 matches after time <unix>
|
||||
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
|
||||
): Promise<Match[] | null> => {
|
||||
let response: Match[] | null = null;
|
||||
|
||||
await axios
|
||||
.get(`${API_URL}/matches/next/${date}`)
|
||||
.then((res) => {
|
||||
if (res.status === STATUS.OK) response = res.data;
|
||||
})
|
||||
.catch((err) => {
|
||||
let message = "";
|
||||
|
||||
switch (err.response.status) {
|
||||
case STATUS.BAD_REQUEST:
|
||||
message = "Bad request";
|
||||
break;
|
||||
case STATUS.INTERNAL_SERVER_ERROR:
|
||||
message = "Unable to load more matches";
|
||||
break;
|
||||
default:
|
||||
message = "An unknown error occurred";
|
||||
}
|
||||
store.commit({
|
||||
type: "changeInfoState",
|
||||
data: {
|
||||
statuscode: err.response.status,
|
||||
message,
|
||||
type: "error",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return response;
|
||||
};
|
Reference in New Issue
Block a user