diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-08-06 15:23:02 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-08-06 15:23:02 +0200 |
commit | 4672a0846297cea8184c2c3f52b4dc05f9c79fd2 (patch) | |
tree | 82f6b32f46d34cb22025d6400f648ec08a217bfb /web/pw-server/src/lib/utils.ts | |
parent | 70c79646aed56344e2e21f3b738443c135c8211d (diff) | |
download | planetwars.dev-4672a0846297cea8184c2c3f52b4dc05f9c79fd2.tar.xz planetwars.dev-4672a0846297cea8184c2c3f52b4dc05f9c79fd2.zip |
introduce ApiClient
Diffstat (limited to 'web/pw-server/src/lib/utils.ts')
-rw-r--r-- | web/pw-server/src/lib/utils.ts | 37 |
1 files changed, 7 insertions, 30 deletions
diff --git a/web/pw-server/src/lib/utils.ts b/web/pw-server/src/lib/utils.ts index 155d952..ab1faa5 100644 --- a/web/pw-server/src/lib/utils.ts +++ b/web/pw-server/src/lib/utils.ts @@ -1,4 +1,4 @@ -import { get_session_token } from "./auth"; +import { ApiClient, FetchFn } from "./api_client"; export function debounce(func: Function, timeout: number = 300) { let timer: ReturnType<typeof setTimeout>; @@ -10,35 +10,12 @@ export function debounce(func: Function, timeout: number = 300) { }; } -export async function get(url: string, fetch_fn: Function = fetch) { - const headers = { "Content-Type": "application/json" }; - - const token = get_session_token(); - if (token) { - headers["Authorization"] = `Bearer ${token}`; - } - - const response = await fetch_fn(url, { - method: "GET", - headers, - }); - - return JSON.parse(response); +export async function get(url: string, params?: Record<string, string>, fetch_fn: FetchFn = fetch) { + const client = new ApiClient(fetch_fn); + return await client.get(url, params); } -export async function post(url: string, data: any, fetch_fn: Function = fetch) { - const headers = { "Content-Type": "application/json" }; - - const token = get_session_token(); - if (token) { - headers["Authorization"] = `Bearer ${token}`; - } - - const response = await fetch_fn(url, { - method: "POST", - headers, - body: JSON.stringify(data), - }); - - return JSON.parse(response); +export async function post(url: string, data: any, fetch_fn: FetchFn = fetch) { + const client = new ApiClient(fetch_fn); + return await client.post(url, data); } |