-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.ts
More file actions
50 lines (38 loc) · 1.4 KB
/
Copy pathsearch.ts
File metadata and controls
50 lines (38 loc) · 1.4 KB
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
/**
* search.ts
* Search Threads content using the thredly API — top results, recent, and profiles.
*
* Usage:
* THREDLY_API_KEY=your-api-key QUERY="AI research" npx ts-node examples/typescript/search.ts
*/
const API_BASE = process.env.THREDLY_API_URL || 'https://thredly.dev/api';
const API_KEY = process.env.THREDLY_API_KEY || 'your-api-key';
const QUERY = process.env.QUERY || 'AI research';
const headers = { 'x-api-key': API_KEY };
async function search(endpoint: 'top' | 'recent' | 'profiles', query: string): Promise<unknown> {
const url = new URL(`${API_BASE}/search/${endpoint}`);
url.searchParams.set('q', query);
const res = await fetch(url.toString(), { headers });
if (!res.ok) {
throw new Error(`Search /${endpoint} failed: ${res.status} ${res.statusText}`);
}
return res.json();
}
async function main(): Promise<void> {
console.log(`Searching Threads for: "${QUERY}"\n`);
const [top, recent, profiles] = await Promise.all([
search('top', QUERY),
search('recent', QUERY),
search('profiles', QUERY),
]);
console.log('--- Top Results ---');
console.log(JSON.stringify(top, null, 2));
console.log('\n--- Recent Results ---');
console.log(JSON.stringify(recent, null, 2));
console.log('\n--- Profile Results ---');
console.log(JSON.stringify(profiles, null, 2));
}
main().catch((err) => {
console.error('Error:', err.message);
process.exit(1);
});