aboutsummaryrefslogtreecommitdiff
path: root/web/pw-server/src/lib
diff options
context:
space:
mode:
authorIlion Beyst <ilion.beyst@gmail.com>2022-08-06 16:12:00 +0200
committerIlion Beyst <ilion.beyst@gmail.com>2022-08-06 16:12:00 +0200
commit6f0c1093ac65064c9c19a4137ff897137f74c694 (patch)
tree28f3470513c62f3983189654c80ddb802863d4c5 /web/pw-server/src/lib
parent4672a0846297cea8184c2c3f52b4dc05f9c79fd2 (diff)
downloadplanetwars.dev-6f0c1093ac65064c9c19a4137ff897137f74c694.tar.xz
planetwars.dev-6f0c1093ac65064c9c19a4137ff897137f74c694.zip
extend api_client for loading match data
Diffstat (limited to 'web/pw-server/src/lib')
-rw-r--r--web/pw-server/src/lib/api_client.ts51
1 files changed, 30 insertions, 21 deletions
diff --git a/web/pw-server/src/lib/api_client.ts b/web/pw-server/src/lib/api_client.ts
index 93135ab..2eaa762 100644
--- a/web/pw-server/src/lib/api_client.ts
+++ b/web/pw-server/src/lib/api_client.ts
@@ -24,23 +24,15 @@ export class ApiClient {
}
async get(url: string, params?: Record<string, string>): Promise<any> {
- const headers = { "Content-Type": "application/json" };
-
- if (this.sessionToken) {
- headers["Authorization"] = `Bearer ${this.sessionToken}`;
- }
-
- if (params) {
- let searchParams = new URLSearchParams(params);
- url = `${url}?${searchParams}`;
- }
-
- const response = await this.fetch_fn(url, {
- method: "GET",
- headers,
- });
+ const response = await this.getRequest(url, params);
+ this.checkResponse(response);
+ return await response.json();
+ }
- return await this.getJsonResponse(response);
+ async getText(url: string, params?: Record<string, string>): Promise<any> {
+ const response = await this.getRequest(url, params);
+ this.checkResponse(response);
+ return await response.text();
}
async post(url: string, data: any): Promise<any> {
@@ -57,13 +49,30 @@ export class ApiClient {
body: JSON.stringify(data),
});
- return await this.getJsonResponse(response);
+ this.checkResponse(response);
+ return await response.json();
}
- private async getJsonResponse(response: Response): Promise<any> {
- if (response.ok) {
- return await response.json();
- } else {
+ private async getRequest(url: string, params: Record<string, string>): Promise<Response> {
+ const headers = { "Content-Type": "application/json" };
+
+ 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);
}
}