import { type GetSiteResponse, LemmyHttp } from 'lemmy-js-client' import { get, writable } from 'svelte/store' import { profile } from '$lib/auth.js' import { error } from '@sveltejs/kit' import { LINKED_INSTANCE_URL, instance } from '$lib/instance.js' async function customFetch( func: ( input: RequestInfo | URL, init?: RequestInit | undefined ) => Promise, input: RequestInfo | URL, init?: RequestInit | undefined ): Promise { const res = await func(input, init) if (!res.ok) throw error(res.status, res.statusText) return res } export function getClient( instanceURL?: string, func?: ( input: RequestInfo | URL, init?: RequestInit | undefined ) => Promise ): LemmyHttp { if (!instanceURL) { instanceURL = get(instance) } return new LemmyHttp(`https://${instanceURL}`, { fetchFunction: func ? (input, init) => customFetch(func, input, init) : undefined, }) } export const getInstance = () => get(instance) export const site = writable(undefined) if (LINKED_INSTANCE_URL) { getClient(LINKED_INSTANCE_URL) .getSite({}) .then((s) => site.set(s)) } export async function validateInstance(instance: string): Promise { if (instance == '') return false try { await getClient(instance).getSite({}) return true } catch (err) { return false } } export async function uploadImage( image: File | null | undefined ): Promise { if (!image || !get(profile)?.jwt) return const formData = new FormData() formData.append('images[]', image) const response = await fetch( `${ window.location.origin }/cors/${getInstance()}/pictrs/image?${new URLSearchParams({ auth: get(profile)!.jwt!, })}`, { method: 'POST', body: formData, } ) const json = await response.json() if (json.msg == 'ok') { return `https://${get(profile)?.instance}/pictrs/image/${ json.files?.[0]?.file }` } throw new Error(`error uploading image: ${response.status}`) }