HEX
Server: LiteSpeed
System: Linux server318.web-hosting.com 4.18.0-513.18.1.lve.el8.x86_64 #1 SMP Thu Feb 22 12:55:50 UTC 2024 x86_64
User: sahadove (2893)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: /home/sahadove//wp-content/plugins/extendify/src/Agent/workflows/extermal/tools/search-wp-docs.js
import { KB_HOST } from '@constants';

export default async ({ query }) => {
	const fallback = { documentation: 'No relevant documentation found' };
	const urlParams = new URLSearchParams({ search: query });
	const r = await fetch(`${KB_HOST}/api/posts?${urlParams.toString()}`, {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
	}).catch(() => ({ ok: false }));
	if (!r.ok) return fallback;

	const articles = await r.json();
	if (!articles?.[0]?.slug) return fallback;
	const r2 = await fetch(`${KB_HOST}/api/posts/${articles?.[0]?.slug}`, {
		method: 'GET',
		headers: { 'Content-Type': 'application/json' },
	}).catch(() => ({ ok: false }));
	if (!r2.ok) return fallback;
	const article = await r2.json();
	return article?.content ? { documentation: article.content } : fallback;
};