|
|
@ -11,6 +11,8 @@ import type Stat from "$lib/models/stat"; |
|
|
|
import type { StatInput } from "$lib/models/stat"; |
|
|
|
import type User from "$lib/models/user"; |
|
|
|
import type { AuthResult } from "$lib/models/user"; |
|
|
|
import { Auth } from "aws-amplify"; |
|
|
|
import { getContext, setContext } from "svelte"; |
|
|
|
|
|
|
|
type FetchImpl = (info: RequestInfo, init?: RequestInit) => Promise<Response>; |
|
|
|
|
|
|
@ -158,9 +160,12 @@ export default class SL3APIClient { |
|
|
|
bodyData = JSON.stringify(body); |
|
|
|
} |
|
|
|
|
|
|
|
if (this.tokenPromise != null) { |
|
|
|
headers["Authorization"] = `Bearer ${await this.tokenPromise}`; |
|
|
|
} |
|
|
|
try { |
|
|
|
const sess = await Auth.currentSession(); |
|
|
|
const idToken = sess?.getIdToken()?.getJwtToken(); |
|
|
|
|
|
|
|
headers["Authorization"] = `Bearer ${idToken}`; |
|
|
|
} catch(_) {} |
|
|
|
|
|
|
|
const res = await this.fetcher(`${this.root}/api/v1/${path}`, { |
|
|
|
method, headers, |
|
|
@ -180,6 +185,6 @@ export default class SL3APIClient { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export function sl3(fetcher: FetchImpl, idToken?: string) { |
|
|
|
return new SL3APIClient(fetcher, idToken ? Promise.resolve(idToken) : null); |
|
|
|
export function sl3(fetcher: FetchImpl) { |
|
|
|
return new SL3APIClient(fetcher); |
|
|
|
} |