implement utility functions, reactive list updates
This commit is contained in:
parent
299b29adcd
commit
9a3819293c
7 changed files with 74 additions and 45 deletions
58
src/lib/util.ts
Normal file
58
src/lib/util.ts
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
import { CredentialManager, XRPC } from '@atcute/client';
|
||||
import { getPdsEndpoint } from '@atcute/identity';
|
||||
import {
|
||||
AtprotoWebDidDocumentResolver,
|
||||
CompositeDidDocumentResolver,
|
||||
PlcDidDocumentResolver,
|
||||
XrpcHandleResolver
|
||||
} from '@atcute/identity-resolver';
|
||||
|
||||
const didDocumentResolver = new CompositeDidDocumentResolver({
|
||||
methods: {
|
||||
plc: new PlcDidDocumentResolver(),
|
||||
web: new AtprotoWebDidDocumentResolver()
|
||||
}
|
||||
});
|
||||
|
||||
const handleResolver = new XrpcHandleResolver({
|
||||
serviceUrl: 'https://public.api.bsky.app'
|
||||
});
|
||||
|
||||
async function resolvePDS(did: string) {
|
||||
const doc = await didDocumentResolver.resolve(did as `did:plc:${string}` | `did:web:${string}`);
|
||||
const pds = getPdsEndpoint(doc);
|
||||
return pds;
|
||||
}
|
||||
|
||||
export async function resolveHandle(handle: string) {
|
||||
if (!handle.includes('.')) {
|
||||
throw new Error(`Invalid DID: ${handle}`);
|
||||
}
|
||||
return await handleResolver.resolve(handle as `${string}.${string}`);
|
||||
}
|
||||
|
||||
export async function createRPC(did: string) {
|
||||
if (!did.startsWith('did:')) did = await resolveHandle(did);
|
||||
|
||||
const pds = await resolvePDS(did);
|
||||
if (!pds) {
|
||||
throw new Error(`Failed to resolve PDS for DID: ${did}`);
|
||||
}
|
||||
|
||||
const manager = new CredentialManager({ service: pds });
|
||||
const rpc = new XRPC({ handler: manager });
|
||||
|
||||
return rpc;
|
||||
}
|
||||
|
||||
export async function getSessions(rpc: XRPC, did: string) {
|
||||
const {
|
||||
data: { records }
|
||||
} = await rpc.get('com.atproto.repo.listRecords', {
|
||||
params: {
|
||||
repo: did,
|
||||
collection: 'me.woach.feed.session'
|
||||
}
|
||||
});
|
||||
return records;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue