diff options
author | Paul Frazee <pfrazee@gmail.com> | 2023-03-21 17:58:50 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-21 17:58:50 -0500 |
commit | a7e3ce25854d6186b77e68c155a9a8bcdbd896ec (patch) | |
tree | 55c1a86575876c50824be7175a047c3e409ff7e6 /src/lib/api | |
parent | 48e18662f69530d5c201d08014a039126c88e7dd (diff) | |
download | voidsky-a7e3ce25854d6186b77e68c155a9a8bcdbd896ec.tar.zst |
* Refactor mobile search screen * Remove 'staleness' fetch trigger on search * Implement a temporary fulltext search solution * Add missing key from profile search result * A few UI & UX improvements to the search suggestions * Update web search suggestions * Implement search in web build
Diffstat (limited to 'src/lib/api')
-rw-r--r-- | src/lib/api/search.ts | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/lib/api/search.ts b/src/lib/api/search.ts new file mode 100644 index 000000000..dfe9b688b --- /dev/null +++ b/src/lib/api/search.ts @@ -0,0 +1,69 @@ +/** + * This is a temporary off-spec search endpoint + * TODO removeme when we land this in proto! + */ +import {AppBskyFeedPost} from '@atproto/api' + +const PROFILES_ENDPOINT = 'https://search.bsky.social/search/profiles' +const POSTS_ENDPOINT = 'https://search.bsky.social/search/posts' + +export interface ProfileSearchItem { + $type: string + avatar: { + cid: string + mimeType: string + } + banner: { + cid: string + mimeType: string + } + description: string | undefined + displayName: string | undefined + did: string +} + +export interface PostSearchItem { + tid: string + cid: string + user: { + did: string + handle: string + } + post: AppBskyFeedPost.Record +} + +export async function searchProfiles( + query: string, +): Promise<ProfileSearchItem[]> { + return await doFetch<ProfileSearchItem[]>(PROFILES_ENDPOINT, query) +} + +export async function searchPosts(query: string): Promise<PostSearchItem[]> { + return await doFetch<PostSearchItem[]>(POSTS_ENDPOINT, query) +} + +async function doFetch<T>(endpoint: string, query: string): Promise<T> { + const controller = new AbortController() + const to = setTimeout(() => controller.abort(), 15e3) + + const uri = new URL(endpoint) + uri.searchParams.set('q', query) + + const res = await fetch(String(uri), { + method: 'get', + headers: { + accept: 'application/json', + }, + signal: controller.signal, + }) + + const resHeaders: Record<string, string> = {} + res.headers.forEach((value: string, key: string) => { + resHeaders[key] = value + }) + let resBody = await res.json() + + clearTimeout(to) + + return resBody as unknown as T +} |