| 1 |
import { KB_HOST } from '@constants'; |
| 2 |
|
| 3 |
export default async ({ query }) => { |
| 4 |
const fallback = { documentation: 'No relevant documentation found' }; |
| 5 |
const urlParams = new URLSearchParams({ search: query }); |
| 6 |
const r = await fetch(`${KB_HOST}/api/posts?${urlParams.toString()}`, { |
| 7 |
method: 'POST', |
| 8 |
headers: { 'Content-Type': 'application/json' }, |
| 9 |
}).catch(() => ({ ok: false })); |
| 10 |
if (!r.ok) return fallback; |
| 11 |
|
| 12 |
const articles = await r.json(); |
| 13 |
if (!articles?.[0]?.slug) return fallback; |
| 14 |
const r2 = await fetch(`${KB_HOST}/api/posts/${articles?.[0]?.slug}`, { |
| 15 |
method: 'GET', |
| 16 |
headers: { 'Content-Type': 'application/json' }, |
| 17 |
}).catch(() => ({ ok: false })); |
| 18 |
if (!r2.ok) return fallback; |
| 19 |
const article = await r2.json(); |
| 20 |
return article?.content ? { documentation: article.content } : fallback; |
| 21 |
}; |
| 22 |
|