Lanyards is a dedicated profile for researchers, built on the AT Protocol.
at main 64 lines 1.8 kB view raw
1/** 2 * Factory for creating Lanyard API clients with authentication 3 * Bridges AtpAgent session management with XrpcClient 4 */ 5 6import { AtpAgent } from '@atproto/api'; 7import { AtpBaseClient } from '@/types/generated'; 8 9/** 10 * Creates a Lanyard API client from an authenticated AtpAgent 11 * 12 * @param agent - Authenticated AtpAgent with active session 13 * @returns Configured AtpBaseClient ready to make authenticated requests 14 * @throws Error if agent has no active session 15 * 16 * @example 17 * ```typescript 18 * const agent = new AtpAgent({ service: 'https://bsky.social' }); 19 * await agent.login({ identifier: 'user', password: 'pass' }); 20 * 21 * const client = createLanyardClient(agent); 22 * const result = await client.at.lanyard.profile.get({ 23 * repo: agent.session.did, 24 * rkey: 'self' 25 * }); 26 * ``` 27 */ 28export function createLanyardClient(agent: AtpAgent): AtpBaseClient { 29 if (!agent.session) { 30 throw new Error('AtpAgent must have an active session to create client'); 31 } 32 33 const session = agent.session; 34 35 return new AtpBaseClient({ 36 service: agent.service.toString(), 37 headers: { 38 authorization: `Bearer ${session.accessJwt}`, 39 }, 40 }); 41} 42 43/** 44 * Creates a Lanyard API client for unauthenticated read operations 45 * 46 * @param serviceUrl - AT Protocol service URL (e.g., 'https://bsky.social') 47 * @returns AtpBaseClient without authentication 48 * 49 * @example 50 * ```typescript 51 * const client = createPublicLanyardClient('https://bsky.social'); 52 * const profile = await client.at.lanyard.profile.get({ 53 * repo: 'did:plc:abc123', 54 * rkey: 'self' 55 * }); 56 * ``` 57 */ 58export function createPublicLanyardClient( 59 serviceUrl: string = 'https://bsky.social' 60): AtpBaseClient { 61 return new AtpBaseClient({ 62 service: serviceUrl, 63 }); 64}