diff --git a/src/lib/lemmy.ts b/src/lib/lemmy.ts index e615346c..77a219f2 100644 --- a/src/lib/lemmy.ts +++ b/src/lib/lemmy.ts @@ -5,15 +5,37 @@ import { instance } from '$lib/instance.js' import { instanceToURL } from '$lib/util.js' import { profile } from '$lib/auth.js' +const isURL = (input: RequestInfo | URL): input is URL => + typeof input == 'object' && 'searchParams' in input + +const toURL = (input: RequestInfo | URL): URL | undefined => { + if (isURL(input)) return input + + try { + return new URL(input.toString()) + } catch (e) { + return + } +} + async function customFetch( - func: ( - input: RequestInfo | URL, - init?: RequestInit | undefined - ) => Promise, + func: + | (( + input: RequestInfo | URL, + init?: RequestInit | undefined + ) => Promise) + | undefined, input: RequestInfo | URL, - init?: RequestInit | undefined + init?: RequestInit | undefined, + auth?: string ): Promise { - const res = await func(input, init) + const f = func ? func : fetch + + const url = toURL(input) + + if (auth && url) url.searchParams.set('auth', auth) + + const res = await f(url ?? input, init) if (!res.ok) throw error(res.status, await res.text()) return res } @@ -37,7 +59,7 @@ export function client({ let headers = jwt ? { Authorization: `Bearer ${jwt}` } : { Authorization: '' } return new LemmyHttp(instanceToURL(instanceURL), { - fetchFunction: func, + fetchFunction: (input, init) => customFetch(func, input, init, jwt), headers: headers, }) } diff --git a/src/lib/version.ts b/src/lib/version.ts index af294fe7..589a34b3 100644 --- a/src/lib/version.ts +++ b/src/lib/version.ts @@ -1,4 +1,4 @@ -export const MINIMUM_VERSION = '0.19.0' +export const MINIMUM_VERSION = '0.18.1' export const MAXIMUM_VERSION = '0.999' export const versionIsSupported = (v: string, minimum: string) => {