This repository has no description
1import type { BobbinContext, XrpcRequestInit } from "./client";
2import type { Nsid } from "@atcute/lexicons/syntax";
3import { jsonPost } from "./_request";
4
5export type RecordPath = string & {};
6export type EnvelopePath = ".repo" | ".collection" | ".rkey" | ".";
7export type LinkSource = `${Nsid}:${RecordPath | EnvelopePath}`;
8
9export interface LinkDescriptor {
10 source: LinkSource;
11 type: "count" | "distinctAuthors" | "viewer";
12}
13
14// ref is did or at-uri, source echoes the descriptor string, viewer holds the caller's own record uri if requested, null if none exists
15export interface StatsLeaf {
16 count?: number;
17 distinctAuthors?: number;
18 viewer?: string | null;
19}
20export type Stats = Record<string, Record<LinkSource, StatsLeaf>>;
21export interface Enriched<O> {
22 output: O;
23 stats: Stats;
24}
25
26export interface EnrichRequest {
27 xrpc: string;
28 params?: Record<string, unknown>;
29 enrich: LinkDescriptor[];
30 sources?: string[];
31 viewer?: string;
32}
33
34export const enrich = <O>(
35 ctx: BobbinContext,
36 req: EnrichRequest,
37 init?: XrpcRequestInit
38): Promise<Enriched<O>> => jsonPost<Enriched<O>>(ctx, "sh.tangled.query.enrichResponse", req, init);
39
40export const countOf = (stats: Stats, ref: string | undefined, source: LinkSource): number =>
41 (ref !== undefined && stats[ref]?.[source]?.count) || 0;
42
43// undefined means no viewer or inapplicable descriptor, null means no matching record
44export const viewerUriOf = (
45 stats: Stats,
46 ref: string | undefined,
47 source: LinkSource
48): string | null | undefined => (ref !== undefined ? stats[ref]?.[source]?.viewer : undefined);