42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
|
import { Accessor, createMemo, createResource } from "solid-js";
|
||
|
import { Account } from "../accounts/stores";
|
||
|
import { createRestAPIClient, mastodon } from "masto";
|
||
|
|
||
|
const restfulCache: Record<string, mastodon.rest.Client> = {}
|
||
|
|
||
|
export function createMastoClientFor(account: Account) {
|
||
|
const cacheKey = [account.site, account.accessToken].join('')
|
||
|
const cache = restfulCache[cacheKey]
|
||
|
if (cache) return cache;
|
||
|
|
||
|
const client = createRestAPIClient({
|
||
|
url: account.site,
|
||
|
accessToken: account.accessToken,
|
||
|
})
|
||
|
restfulCache[cacheKey] = client
|
||
|
|
||
|
return client
|
||
|
}
|
||
|
|
||
|
export function useMastoClientFor(account: Accessor<Account>) {
|
||
|
return createMemo(() => createMastoClientFor(account()))
|
||
|
}
|
||
|
|
||
|
export function createUnauthorizedClient(site: string) {
|
||
|
const cache = restfulCache[site]
|
||
|
if (cache) return cache;
|
||
|
|
||
|
const client = createRestAPIClient({
|
||
|
url: site
|
||
|
})
|
||
|
restfulCache[site] = client
|
||
|
|
||
|
return client
|
||
|
}
|
||
|
|
||
|
export function useInstance(client: Accessor<mastodon.rest.Client>) {
|
||
|
return createResource(client, async (client) => {
|
||
|
return await client.v2.instance.fetch()
|
||
|
})
|
||
|
}
|