initial commit
This commit is contained in:
commit
5449e361d5
46 changed files with 8309 additions and 0 deletions
41
src/masto/clients.ts
Normal file
41
src/masto/clients.ts
Normal file
|
@ -0,0 +1,41 @@
|
|||
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()
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue