Something went wrong. Try again.
Monorepo for the nibling.run platform
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495import type { Cid, Did, Nsid, RecordKey, ResourceUri } from '@atcute/lexicons';import * as v from '@atcute/lexicons/validations';import { parseResourceUri } from '@atcute/lexicons';import { isDid } from '@atcute/lexicons/syntax';import { config, microcosmClient } from './identity.ts';
const backlinkSchema = v.object({ did: v.didString(), collection: v.nsidString(), rkey: v.recordKeyString(),});
const getBacklinksSchema = v.query('blue.microcosm.links.getBacklinks', { params: v.object({ subject: v.string(), source: v.string(), limit: v.optional(v.integer()), cursor: v.optional(v.string()), }), output: { type: 'lex', schema: v.object({ total: v.integer(), records: v.array(backlinkSchema), cursor: v.optional(v.nullable(v.string())), }), },});
const getBacklinksCountSchema = v.query('blue.microcosm.links.getBacklinksCount', { params: v.object({ subject: v.string(), source: v.string() }), output: { type: 'lex', schema: v.object({ total: v.integer() }) },});
const getRecordByUriSchema = v.query('blue.microcosm.repo.getRecordByUri', { params: v.object({ at_uri: v.resourceUriString() }), output: { type: 'lex', schema: v.object({ uri: v.resourceUriString(), cid: v.cidString(), value: v.unknown() }), },});
export interface Backlink { did: Did; collection: Nsid; rkey: RecordKey;}
export interface BacklinksQuery { /** The linked-to AT-URI or DID. */ subject: string; /** `<collection>:<json path>`, such as `dev.nibling.feed.like:subject.uri`. */ source: string; limit?: number; cursor?: string;}
/** Records that link to `subject`. Undefined when neither the gateway nor Constellation is set or reachable. */export async function getBacklinks( query: BacklinksQuery,): Promise<{ total: number; records: Backlink[]; cursor?: string } | undefined> { const res = await microcosmClient(config.constellation) ?.call(getBacklinksSchema, { params: query }) .catch(() => undefined); if (!res?.ok) return undefined; const { total, records, cursor } = res.data; return cursor ? { total, records, cursor } : { total, records };}
export async function getBacklinksCount(query: Pick<BacklinksQuery, 'subject' | 'source'>): Promise<number | undefined> { const res = await microcosmClient(config.constellation) ?.call(getBacklinksCountSchema, { params: query }) .catch(() => undefined); return res?.ok ? res.data.total : undefined;}
/** A record through the gateway or Slingshot, checked against `schema` and against the URI asked for. Undefined when neither is set or up, or the record is invalid. */export async function getRecordByUri<S extends v.BaseSchema>( uri: ResourceUri, schema: S,): Promise<{ uri: ResourceUri; cid: Cid; value: v.InferOutput<S> } | undefined> { const res = await microcosmClient(config.slingshot, repoOf(uri)) ?.call(getRecordByUriSchema, { params: { at_uri: uri } }) .catch(() => undefined); if (!res?.ok) return undefined; if (res.data.uri !== uri) return undefined; const parsed = v.safeParse(schema, res.data.value); return parsed.ok ? { uri, cid: res.data.cid, value: parsed.value } : undefined;}
function repoOf(uri: ResourceUri): Did | undefined { const repo = parseResourceUri(uri).repo; return isDid(repo) ? repo : undefined;}