import type * as OrgTangledActorSearchActorsTypeahead from "#lib/api/lexicons/types/org/tangled/actor/searchActorsTypeahead.ts"; import type { BobbinContext, XrpcRequestInit } from "#lib/api/client.ts"; import { jsonGet } from "#lib/api/_request.ts"; import { paginateBy } from "#lib/api/pagination.ts"; import * as OrgTangledTempSearchSearchCode from "#lib/api/lexicons/types/org/tangled/temp/search/searchCode.ts"; export interface SearchHit { uri: string; cid?: string; nsid: string; score: number; /** the indexed record; shape varies by `nsid`. */ value: unknown; } export interface SearchPage { hits: SearchHit[]; cursor: string | null; total?: number; } export interface SearchParams { q: string; nsid?: string; author?: string; repo?: string; since?: string; until?: string; cursor?: string; limit?: number; } export const search = (ctx: BobbinContext, params: SearchParams, init?: XrpcRequestInit) => jsonGet(ctx, "org.tangled.search.query", { ...params }, init); export type ActorSuggestion = OrgTangledActorSearchActorsTypeahead.Actor; export const searchActorsTypeahead = ( ctx: BobbinContext, params: OrgTangledActorSearchActorsTypeahead.$params, init?: XrpcRequestInit ) => jsonGet( ctx, "org.tangled.actor.searchActorsTypeahead", params, init ); export interface SearchAllOptions extends XrpcRequestInit { maxPages?: number; } /** Async-iterate every search hit across cursor pages. */ export async function* searchAll( ctx: BobbinContext, params: Omit, options: SearchAllOptions = {} ): AsyncGenerator { yield* paginateBy(async (cursor) => { const page = await search(ctx, { ...params, cursor }, options); return { items: page.hits, cursor: page.cursor }; }, options); } export const searchCode = ( ctx: BobbinContext, params: OrgTangledTempSearchSearchCode.$params, init?: XrpcRequestInit ): Promise => jsonGet( ctx, OrgTangledTempSearchSearchCode.mainSchema.nsid, { ...params }, init );