From ede361085aa1fb9b91f7da9e971ff6d8712601f1 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 7 Aug 2024 09:31:09 +0100 Subject: [PATCH] Throw when using deprecated getEntryByX functions with content layer (#11637) --- packages/astro/src/content/runtime.ts | 40 ++++++------------- packages/astro/src/core/errors/errors-data.ts | 12 ++++++ .../content-layer/src/pages/index.astro | 4 +- 3 files changed, 27 insertions(+), 29 deletions(-) diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 0b79eae0b7d5..f1934d09a63f 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -92,7 +92,7 @@ export function createGetCollection({ data, collection, }; - if(hasFilter && !filter(entry)) { + if (hasFilter && !filter(entry)) { continue; } result.push(entry); @@ -170,20 +170,13 @@ export function createGetEntryBySlug({ return async function getEntryBySlug(collection: string, slug: string) { const store = await globalDataStore.get(); - if (store.hasCollection(collection)) { - const data = store.get(collection, slug); - if (!data) { - throw new Error(`Entry ${collection} → ${slug} was not found.`); - } - - const entry = store.get(collection, slug); - - return { - ...entry, - collection, - }; - } if (!collectionNames.has(collection)) { + if (store.hasCollection(collection)) { + throw new AstroError({ + ...AstroErrorData.GetEntryDeprecationError, + message: AstroErrorData.GetEntryDeprecationError.message(collection, 'getEntryBySlug'), + }); + } // eslint-disable-next-line no-console console.warn(`The collection ${JSON.stringify(collection)} does not exist.`); return undefined; @@ -221,20 +214,13 @@ export function createGetDataEntryById({ return async function getDataEntryById(collection: string, id: string) { const store = await globalDataStore.get(); - if (store.hasCollection(collection)) { - const data = store.get(collection, id); - if (!data) { - throw new Error(`Entry ${collection} → ${id} was not found.`); - } - - const entry = store.get(collection, id); - - return { - ...entry, - collection, - }; - } if (!collectionNames.has(collection)) { + if (store.hasCollection(collection)) { + throw new AstroError({ + ...AstroErrorData.GetEntryDeprecationError, + message: AstroErrorData.GetEntryDeprecationError.message(collection, 'getDataEntryById'), + }); + } // eslint-disable-next-line no-console console.warn(`The collection ${JSON.stringify(collection)} does not exist.`); return undefined; diff --git a/packages/astro/src/core/errors/errors-data.ts b/packages/astro/src/core/errors/errors-data.ts index 15f2ad769e1d..263a8a16aabc 100644 --- a/packages/astro/src/core/errors/errors-data.ts +++ b/packages/astro/src/core/errors/errors-data.ts @@ -1482,6 +1482,18 @@ export const ContentLayerWriteError = { title: 'Could not write content layer data.', } satisfies ErrorData; +/** + * @docs + * @description + * The `getDataEntryById` and `getEntryBySlug` functions are deprecated and cannot be used with content layer collections. Use the `getEntry` function instead. + */ +export const GetEntryDeprecationError = { + name: 'GetEntryDeprecationError', + title: 'Invalid use of `getDataEntryById` or `getEntryBySlug` function.', + message: (collection: string, method: string) => `The \`${method}\` function is deprecated and cannot be used to query the "${collection}" collection. Use \`getEntry\` instead.`, + hint: 'Use the `getEntry` or `getCollection` functions to query content layer collections.', +} satisfies ErrorData; + /** * @docs * @message diff --git a/packages/astro/test/fixtures/content-layer/src/pages/index.astro b/packages/astro/test/fixtures/content-layer/src/pages/index.astro index 7036de032fdf..970dffaf003d 100644 --- a/packages/astro/test/fixtures/content-layer/src/pages/index.astro +++ b/packages/astro/test/fixtures/content-layer/src/pages/index.astro @@ -1,8 +1,8 @@ --- -import { getCollection, getDataEntryById } from 'astro:content'; +import { getCollection, getEntry } from 'astro:content'; const blog = await getCollection('blog'); -const first = await getDataEntryById('blog', 1); +const first = await getEntry('blog', 1); const dogs = await getCollection('dogs'); ---