|
| 1 | +import axios from 'axios'; |
| 2 | + |
| 3 | +const ALGOLIA_APP_ID = '31P3X3M1EE'; |
| 4 | +const ALGOLIA_SEARCH_API_KEY = 'fe7422b190b4ec77f8e60c80a3a3ed8a'; |
| 5 | +const ALGOLIA_INDEX_NAME = 'rocketadmin-docs'; |
| 6 | +const ALGOLIA_SEARCH_URL = `https://${ALGOLIA_APP_ID}-dsn.algolia.net/1/indexes/${ALGOLIA_INDEX_NAME}/query`; |
| 7 | + |
| 8 | +const DEFAULT_HITS_PER_PAGE = 5; |
| 9 | +const MAX_HITS_PER_PAGE = 10; |
| 10 | +const MAX_CONTENT_LENGTH = 800; |
| 11 | +const REQUEST_TIMEOUT_MS = 10000; |
| 12 | + |
| 13 | +export interface DocumentationSearchHit { |
| 14 | + title: string; |
| 15 | + url: string; |
| 16 | + content: string; |
| 17 | +} |
| 18 | + |
| 19 | +interface AlgoliaHierarchy { |
| 20 | + lvl0?: string | null; |
| 21 | + lvl1?: string | null; |
| 22 | + lvl2?: string | null; |
| 23 | + lvl3?: string | null; |
| 24 | + lvl4?: string | null; |
| 25 | + lvl5?: string | null; |
| 26 | + lvl6?: string | null; |
| 27 | +} |
| 28 | + |
| 29 | +interface AlgoliaHit { |
| 30 | + url?: string; |
| 31 | + content?: string | null; |
| 32 | + hierarchy?: AlgoliaHierarchy; |
| 33 | + type?: string; |
| 34 | +} |
| 35 | + |
| 36 | +interface AlgoliaSearchResponse { |
| 37 | + hits: AlgoliaHit[]; |
| 38 | +} |
| 39 | + |
| 40 | +export async function searchDocumentation(query: string, hitsPerPage?: number): Promise<DocumentationSearchHit[]> { |
| 41 | + const trimmedQuery = query?.trim(); |
| 42 | + if (!trimmedQuery) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + |
| 46 | + const limit = Math.min(Math.max(hitsPerPage ?? DEFAULT_HITS_PER_PAGE, 1), MAX_HITS_PER_PAGE); |
| 47 | + |
| 48 | + const response = await axios.post<AlgoliaSearchResponse>( |
| 49 | + ALGOLIA_SEARCH_URL, |
| 50 | + { |
| 51 | + query: trimmedQuery, |
| 52 | + hitsPerPage: limit, |
| 53 | + attributesToRetrieve: ['hierarchy', 'content', 'url', 'type'], |
| 54 | + attributesToSnippet: ['content:50'], |
| 55 | + }, |
| 56 | + { |
| 57 | + headers: { |
| 58 | + 'X-Algolia-Application-Id': ALGOLIA_APP_ID, |
| 59 | + 'X-Algolia-API-Key': ALGOLIA_SEARCH_API_KEY, |
| 60 | + 'Content-Type': 'application/json', |
| 61 | + }, |
| 62 | + timeout: REQUEST_TIMEOUT_MS, |
| 63 | + }, |
| 64 | + ); |
| 65 | + |
| 66 | + const hits = response.data?.hits ?? []; |
| 67 | + return hits.map(buildHit).filter((hit) => hit.url && (hit.title || hit.content)); |
| 68 | +} |
| 69 | + |
| 70 | +function buildHit(hit: AlgoliaHit): DocumentationSearchHit { |
| 71 | + const title = formatHierarchy(hit.hierarchy); |
| 72 | + const rawContent = (hit.content ?? '').replace(/\s+/g, ' ').trim(); |
| 73 | + const content = rawContent.length > MAX_CONTENT_LENGTH ? `${rawContent.slice(0, MAX_CONTENT_LENGTH)}…` : rawContent; |
| 74 | + return { |
| 75 | + title, |
| 76 | + url: hit.url ?? '', |
| 77 | + content, |
| 78 | + }; |
| 79 | +} |
| 80 | + |
| 81 | +function formatHierarchy(hierarchy: AlgoliaHierarchy | undefined): string { |
| 82 | + if (!hierarchy) { |
| 83 | + return ''; |
| 84 | + } |
| 85 | + const parts = [ |
| 86 | + hierarchy.lvl0, |
| 87 | + hierarchy.lvl1, |
| 88 | + hierarchy.lvl2, |
| 89 | + hierarchy.lvl3, |
| 90 | + hierarchy.lvl4, |
| 91 | + hierarchy.lvl5, |
| 92 | + hierarchy.lvl6, |
| 93 | + ] |
| 94 | + .filter((part): part is string => Boolean(part)) |
| 95 | + .map((part) => part.replace(/||/g, '').trim()) |
| 96 | + .filter(Boolean); |
| 97 | + return parts.join(' › '); |
| 98 | +} |
0 commit comments