import { Client } from '@atcute/client'; import type {} from '@atcute/atproto'; import * as NetworkCosmikCard from './lexicons/types/network/cosmik/card.js'; import * as NetworkCosmikConnection from './lexicons/types/network/cosmik/connection.js'; export interface UrlMetadata { title?: string; description?: string; author?: string; siteName?: string; imageUrl?: string; type?: string; publishedDate?: string; } export async function createCard( client: Client, did: string, url: string, metadata?: UrlMetadata, ): Promise { const now = new Date().toISOString(); const urlMetadata: NetworkCosmikCard.UrlMetadata | undefined = metadata ? { $type: 'network.cosmik.card#urlMetadata', title: metadata.title, description: metadata.description, author: metadata.author, siteName: metadata.siteName, type: metadata.type, publishedDate: metadata.publishedDate, } : undefined; const record: NetworkCosmikCard.Main = { $type: 'network.cosmik.card', type: 'URL', content: { $type: 'network.cosmik.card#urlContent', url: url as NetworkCosmikCard.UrlContent['url'], metadata: urlMetadata, }, createdAt: now, }; const response = await client.post('com.atproto.repo.createRecord', { input: { repo: did as `did:${string}:${string}`, collection: 'network.cosmik.card', record, }, }); if (!response.ok) { throw new Error(`Failed to create card: ${(response.data as { error?: string; message?: string }).error ?? 'unknown'}`); } return (response.data as { uri: string }).uri; } export async function createConnection( client: Client, did: string, source: string, target: string, connectionType = 'RELATED', ): Promise { const now = new Date().toISOString(); const record: NetworkCosmikConnection.Main = { $type: 'network.cosmik.connection', source, target, connectionType, createdAt: now, updatedAt: now, }; const response = await client.post('com.atproto.repo.createRecord', { input: { repo: did as `did:${string}:${string}`, collection: 'network.cosmik.connection', record, }, }); if (!response.ok) { throw new Error( `Failed to create connection: ${(response.data as { error?: string; message?: string }).error ?? 'unknown'}`, ); } return (response.data as { uri: string }).uri; }