Files
ts-onlinetime-ranks/frontend/src/services/UserStatsService.ts
Humenius 721421294f
Some checks failed
continuous-integration/drone/push Build is failing
feature(database-connection): Fix error handling
2021-01-28 00:03:56 +01:00

101 lines
3.2 KiB
TypeScript

import RequestError from "../models/RequestError";
import UserStatsResponse from "../models/UserStatsResponse";
export default class UserStatsService {
private static apiURL = 'https://api.tsotr.humenius.me/stats'
// private static apiURL = 'http://localhost:3500/stats'
//private static apiURL = 'https://mock.codes/501'
private static requestInit: RequestInit = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
};
public static async getStats(seasonId: string): Promise<UserStatsResponse> {
return fetch(`${this.apiURL}/season/${seasonId}`, this.requestInit)
.then(res => UserStatsService.checkResponse(res))
.then(data => data.json())
.then(data => {
data.dates.start = new Date(data.dates.start)
data.dates.end = new Date(data.dates.end)
return data
})
}
private static checkResponse(response: any): any {
if (!response.ok) {
console.log(response);
throw new RequestError(response.status, response.body);
}
return response;
}
}
// const checkResponse = <T extends Response>(response: T): T => {
// if (!response.ok) {
// console.log(response);
// let error = new RequestError(response.statusText);
// error.response = response;
// throw error;
// }
// return response;
// }
//
// const useApiService = <T>(url: string, method: string = 'GET'): { response: T | null; error: RequestError | null } => {
// const baseUrl = "https://api.tsotr.humenius.me"
// const [response, setResponse] = useState<T | null>(null)
// const [error, setError] = useState<RequestError | null>(null)
// const requestInit = {
// method: method,
// headers: {
// 'Content-Type': 'application/json'
// }
// }
//
// useEffect(() => {
// const fetchData = async (): Promise<void> => {
// const result = await fetch(`${baseUrl}${url}`, requestInit)
// .then(res => checkResponse(res))
// .then(data => data.json())
// .catch(err => setError(err))
// setResponse(result)
// }
//
// fetchData()
// }, [url])
//
// return { response, error }
// }
//
// // const seasonStatsQuery = (seasonId: number): { response: UserStatsResponse | null; error: RequestError | null } => {
// // const baseUrl = "https://api.tsotr.humenius.me/stats/season/"
// // const [response, setResponse] = useState<UserStatsResponse | null>(null)
// // const [error, setError] = useState<RequestError | null>(null)
// // const requestInit = {
// // method: 'GET',
// // headers: {
// // 'Content-Type': 'application/json'
// // }
// // }
// //
// // useEffect(() => {
// // const fetchData = async (): Promise<void> => {
// // const result = await fetch(baseUrl, requestInit)
// // .then(res => checkResponse(res))
// // .then(data => data.json())
// // .catch(err => setError(err))
// // setResponse(result)
// // }
// //
// // fetchData()
// // }, [seasonId])
// //
// // return { response, error }
// // }
//
// const currentSeasonStatsQuery = useApiService<UserStatsResponse>('/stats')
//
// export default useApiService;