~james/core

core/web/src/lib/api/search.ts -rw-r--r-- 2.1 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
	);