extend api_client for loading match data
This commit is contained in:
parent
4672a08462
commit
6f0c1093ac
2 changed files with 46 additions and 45 deletions
|
@ -24,23 +24,15 @@ export class ApiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
async get(url: string, params?: Record<string, string>): Promise<any> {
|
async get(url: string, params?: Record<string, string>): Promise<any> {
|
||||||
const headers = { "Content-Type": "application/json" };
|
const response = await this.getRequest(url, params);
|
||||||
|
this.checkResponse(response);
|
||||||
|
return await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
if (this.sessionToken) {
|
async getText(url: string, params?: Record<string, string>): Promise<any> {
|
||||||
headers["Authorization"] = `Bearer ${this.sessionToken}`;
|
const response = await this.getRequest(url, params);
|
||||||
}
|
this.checkResponse(response);
|
||||||
|
return await response.text();
|
||||||
if (params) {
|
|
||||||
let searchParams = new URLSearchParams(params);
|
|
||||||
url = `${url}?${searchParams}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await this.fetch_fn(url, {
|
|
||||||
method: "GET",
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
return await this.getJsonResponse(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async post(url: string, data: any): Promise<any> {
|
async post(url: string, data: any): Promise<any> {
|
||||||
|
@ -57,13 +49,30 @@ export class ApiClient {
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
});
|
});
|
||||||
|
|
||||||
return await this.getJsonResponse(response);
|
this.checkResponse(response);
|
||||||
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getJsonResponse(response: Response): Promise<any> {
|
private async getRequest(url: string, params: Record<string, string>): Promise<Response> {
|
||||||
if (response.ok) {
|
const headers = { "Content-Type": "application/json" };
|
||||||
return await response.json();
|
|
||||||
} else {
|
if (this.sessionToken) {
|
||||||
|
headers["Authorization"] = `Bearer ${this.sessionToken}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params) {
|
||||||
|
let searchParams = new URLSearchParams(params);
|
||||||
|
url = `${url}?${searchParams}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return await this.fetch_fn(url, {
|
||||||
|
method: "GET",
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private checkResponse(response: Response) {
|
||||||
|
if (!response.ok) {
|
||||||
throw new ApiError(response.status, response.statusText);
|
throw new ApiError(response.status, response.statusText);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,25 @@
|
||||||
<script lang="ts" context="module">
|
<script lang="ts" context="module">
|
||||||
function fetchJson(url: string): Promise<Response> {
|
import { ApiClient } from "$lib/api_client";
|
||||||
return fetch(url, {
|
export async function load({ params, fetch }) {
|
||||||
headers: {
|
try {
|
||||||
"Content-Type": "application/json",
|
const matchId = params["match_id"];
|
||||||
},
|
const apiClient = new ApiClient(fetch);
|
||||||
});
|
const [matchData, matchLog] = await Promise.all([
|
||||||
}
|
apiClient.get(`/api/matches/${matchId}`),
|
||||||
|
apiClient.getText(`/api/matches/${matchId}/log`),
|
||||||
export async function load({ params }) {
|
]);
|
||||||
// TODO: handle failure cases better
|
|
||||||
const matchId = params["match_id"];
|
|
||||||
const matchDataResponse = await fetchJson(`/api/matches/${matchId}`);
|
|
||||||
if (!matchDataResponse.ok) {
|
|
||||||
}
|
|
||||||
const matchLogResponse = await fetchJson(`/api/matches/${matchId}/log`);
|
|
||||||
|
|
||||||
if (matchDataResponse.ok && matchLogResponse.ok) {
|
|
||||||
return {
|
return {
|
||||||
props: {
|
props: {
|
||||||
matchData: await matchDataResponse.json(),
|
matchData: matchData,
|
||||||
matchLog: await matchLogResponse.text(),
|
matchLog: matchLog,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
status: error.status,
|
||||||
|
error: error,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
|
||||||
status: matchDataResponse.status,
|
|
||||||
error: new Error("failed to load match"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue