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<SearchPage>(ctx, "org.tangled.search.query", { ...params }, init);
export type ActorSuggestion = OrgTangledActorSearchActorsTypeahead.Actor;
export const searchActorsTypeahead = (
ctx: BobbinContext,
params: OrgTangledActorSearchActorsTypeahead.$params,
init?: XrpcRequestInit
) =>
jsonGet<OrgTangledActorSearchActorsTypeahead.$output>(
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<SearchParams, "cursor">,
options: SearchAllOptions = {}
): AsyncGenerator<SearchHit, void, unknown> {
yield* paginateBy<SearchHit>(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<OrgTangledTempSearchSearchCode.$output> =>
jsonGet<OrgTangledTempSearchSearchCode.$output>(
ctx,
OrgTangledTempSearchSearchCode.mainSchema.nsid,
{ ...params },
init
);