feat: add v0.19.0 support

This commit is contained in:
Xyphyn committed 2023-09-28 17:09:09 -07:00
1 parent b3b8a10e44
commit 119ab53946
62 files changed
+188 -157

No files matched your search

+31 -13
View File
@@ -2,6 +2,8 @@ import { type GetSiteResponse, LemmyHttp } from 'lemmy-js-client'
import { get, writable } from 'svelte/store'
import { error } from '@sveltejs/kit'
import { instance } from '$lib/instance.js'
import { instanceToURL } from '$lib/util.js'
import { profile } from '$lib/auth.js'
async function customFetch(
func: (
@@ -16,33 +18,49 @@ async function customFetch(
return res
}
export function client({
instanceURL,
func,
auth,
}: {
instanceURL?: string
func?: (
input: RequestInfo | URL,
init?: RequestInit | undefined
) => Promise<Response>
auth?: string
}) {
if (!instanceURL) instanceURL = get(instance)
let jwt = auth ? auth : get(profile)?.jwt
let headers = jwt ? { Authorization: `Bearer ${jwt}` } : { Authorization: '' }
return new LemmyHttp(instanceToURL(instanceURL), {
fetchFunction: func,
headers: headers,
})
}
export function getClient(
instanceURL?: string,
func?: (
input: RequestInfo | URL,
init?: RequestInit | undefined
) => Promise<Response>
) => Promise<Response>,
auth?: string
): LemmyHttp {
if (!instanceURL) {
instanceURL = get(instance)
}
return new LemmyHttp(`https://${instanceURL}`, {
fetchFunction: func
? (input, init) => customFetch(func, input, init)
: undefined,
})
return client({ instanceURL, func, auth })
}
export const getInstance = () => get(instance)
export const getInstance = () => encodeURIComponent(get(instance))
export const site = writable<GetSiteResponse | undefined>(undefined)
export async function validateInstance(instance: string): Promise<boolean> {
if (instance == '') return false
try {
await getClient(instance).getSite({})
await getClient(instance).getSite()
return true
} catch (err) {