0

Perform a search query

by
Published Oct 17, 2025

Searches the ReadMe project.

Script readme Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
/**
3
 * Perform a search query
4
 * Searches the ReadMe project.
5
 */
6
export async function main(
7
	auth: RT.Readme,
8
	query: string | undefined,
9
	section?:
10
		| 'guides'
11
		| 'reference'
12
		| 'recipes'
13
		| 'custom_pages'
14
		| 'discuss'
15
		| 'changelog'
16
		| undefined,
17
	version?: string | undefined,
18
	projects?: string | undefined
19
) {
20
	const url = new URL(`https://api.readme.com/v2/search`)
21
	for (const [k, v] of [
22
		['query', query],
23
		['section', section],
24
		['version', version],
25
		['projects', projects]
26
	]) {
27
		if (v !== undefined && v !== '') {
28
			url.searchParams.append(k, v)
29
		}
30
	}
31
	const response = await fetch(url, {
32
		method: 'GET',
33
		headers: {
34
			Authorization: 'Bearer ' + auth.apiKey
35
		},
36
		body: undefined
37
	})
38
	if (!response.ok) {
39
		const text = await response.text()
40
		throw new Error(`${response.status} ${text}`)
41
	}
42
	return await response.json()
43
}
44