From ef8ad6436adeea1005f089d6bfbc001cf147392d Mon Sep 17 00:00:00 2001 From: R2m1liA <15258427350@163.com> Date: Tue, 21 Oct 2025 08:52:04 +0000 Subject: [PATCH] =?UTF-8?q?feat(endpoint):=20=E6=B7=BB=E5=8A=A0=E8=B7=AF?= =?UTF-8?q?=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加/connection-test用于测试meilisearch连接 --- src/meilisearch_endpoint/index.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/meilisearch_endpoint/index.ts b/src/meilisearch_endpoint/index.ts index fda64bb..a1962e5 100644 --- a/src/meilisearch_endpoint/index.ts +++ b/src/meilisearch_endpoint/index.ts @@ -3,7 +3,7 @@ import { defineEndpoint } from '@directus/extensions-sdk'; export default defineEndpoint({ id: 'meilisearch', handler: (router, context) => { - router.get('/', (_req, res) => res.send('Hello, World!')); + router.get('/', (_req, res) => res.send('Hello, MeiliSearch!')); router.get('/collections', async (_req, res) => { const { services, getSchema } = context; const { CollectionsService } = services; @@ -22,6 +22,29 @@ export default defineEndpoint({ })); res.json(result); - }) + }); + router.post('/connection-test', async (req, res) => { + const { host, apiKey } = req.body; + console.log(req.body); + try { + const response = await fetch(`${host}/health`, { + method: 'GET', + headers: { + 'Authorization': `Bearer ${apiKey}`, + }, + }); + if (!response.ok) { + throw new Error(`MeiliSearch health check failed with status ${response.status}`); + } + const data = await response.json(); + if (data.status === 'available') { + return res.json({ success: true, message: '连接成功' }); + } else { + return res.json({ success: false, message: `返回状态异常:${JSON.stringify(data)}` }); + } + } catch (error) { + return res.json({ success: false, message: `连接失败:${(error as Error).message}` }); + } + }); } });