diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-04-16 10:30:11 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-04-16 10:30:11 +0200 |
commit | 39cec55bbe738a4c5f93913b9b016050a1953d10 (patch) | |
tree | efebfc255ba0d865db41d4272facf12574d8bd07 /web/pw-server/src/lib/utils.ts | |
parent | a41b4c48cb3fe56037aef62c4289435ede7d3e98 (diff) | |
download | planetwars.dev-39cec55bbe738a4c5f93913b9b016050a1953d10.tar.xz planetwars.dev-39cec55bbe738a4c5f93913b9b016050a1953d10.zip |
format & cleanup
Diffstat (limited to 'web/pw-server/src/lib/utils.ts')
-rw-r--r-- | web/pw-server/src/lib/utils.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/web/pw-server/src/lib/utils.ts b/web/pw-server/src/lib/utils.ts index aab9734..155d952 100644 --- a/web/pw-server/src/lib/utils.ts +++ b/web/pw-server/src/lib/utils.ts @@ -1,3 +1,5 @@ +import { get_session_token } from "./auth"; + export function debounce(func: Function, timeout: number = 300) { let timer: ReturnType<typeof setTimeout>; return (...args: any[]) => { @@ -7,3 +9,36 @@ export function debounce(func: Function, timeout: number = 300) { }, timeout); }; } + +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 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); +} |