From ee6e2d839e38b79b9e1fbfbca670e1f14ddf8b70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Chopin?= Date: Tue, 17 Dec 2024 20:01:11 +0100 Subject: [PATCH] fix(vectorize): return `undefined` instead of throwing in dev with no remote (#399) --- playground/server/api/vectorize/index.get.ts | 3 +-- src/runtime/vectorize/server/utils/vectorize.ts | 7 ++----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/playground/server/api/vectorize/index.get.ts b/playground/server/api/vectorize/index.get.ts index fa50ec19..63d4f5c7 100644 --- a/playground/server/api/vectorize/index.get.ts +++ b/playground/server/api/vectorize/index.get.ts @@ -2,6 +2,5 @@ export default eventHandler(async (event) => { const { query } = await getValidatedQuery(event, z.object({ query: z.array(z.number()) }).parse) - const index = hubVectorize('example') - return index.query(query) + return hubVectorize('example')?.query(query) || [] }) diff --git a/src/runtime/vectorize/server/utils/vectorize.ts b/src/runtime/vectorize/server/utils/vectorize.ts index bbb7905d..6fb7e7fb 100644 --- a/src/runtime/vectorize/server/utils/vectorize.ts +++ b/src/runtime/vectorize/server/utils/vectorize.ts @@ -30,7 +30,7 @@ type VectorizeIndexes = keyof RuntimeConfig['hub']['vectorize'] & string * * @see https://hub.nuxt.com/docs/features/vectorize */ -export function hubVectorize(index: VectorizeIndexes): Vectorize { +export function hubVectorize(index: VectorizeIndexes): Vectorize | undefined { requireNuxtHubFeature('vectorize') if (_vectorize[index]) { @@ -52,10 +52,7 @@ export function hubVectorize(index: VectorizeIndexes): Vectorize { return _vectorize[index] } if (import.meta.dev && !hub.remote) { - throw createError({ - statusCode: 500, - message: 'hubVectorize() is only supported with remote storage in development mode' - }) + return undefined } throw createError(`Missing Cloudflare Vectorize binding (${bindingName})`)