diff --git a/.changeset/spicy-walls-wave.md b/.changeset/spicy-walls-wave.md new file mode 100644 index 000000000..c5069dd3a --- /dev/null +++ b/.changeset/spicy-walls-wave.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fix getDataEntryById to lookup by basename diff --git a/packages/astro/content-module.template.mjs b/packages/astro/content-module.template.mjs index e0ac7a564..9ce06960f 100644 --- a/packages/astro/content-module.template.mjs +++ b/packages/astro/content-module.template.mjs @@ -70,7 +70,7 @@ export const getEntryBySlug = createGetEntryBySlug({ }); export const getDataEntryById = createGetDataEntryById({ - dataCollectionToEntryMap, + getEntryImport: createGlobLookup(dataCollectionToEntryMap), }); export const getEntry = createGetEntry({ diff --git a/packages/astro/src/content/runtime.ts b/packages/astro/src/content/runtime.ts index 96d6c1141..2dd805208 100644 --- a/packages/astro/src/content/runtime.ts +++ b/packages/astro/src/content/runtime.ts @@ -139,13 +139,12 @@ export function createGetEntryBySlug({ } export function createGetDataEntryById({ - dataCollectionToEntryMap, + getEntryImport, }: { - dataCollectionToEntryMap: CollectionToEntryMap; + getEntryImport: GetEntryImport; }) { return async function getDataEntryById(collection: string, id: string) { - const lazyImport = - dataCollectionToEntryMap[collection]?.[/*TODO: filePathToIdMap*/ id + '.json']; + const lazyImport = await getEntryImport(collection, id); // TODO: AstroError if (!lazyImport) throw new Error(`Entry ${collection} → ${id} was not found.`); diff --git a/packages/astro/test/data-collections.test.js b/packages/astro/test/data-collections.test.js index 104284031..e30f49ac2 100644 --- a/packages/astro/test/data-collections.test.js +++ b/packages/astro/test/data-collections.test.js @@ -41,6 +41,17 @@ describe('Content Collections - data collections', () => { }); }); + describe('getDataEntryById', () => { + let json; + before(async () => { + const rawJson = await fixture.readFile('/translations/by-id.json'); + json = JSON.parse(rawJson); + }); + it('Grabs the item by the base file name', () => { + expect(json.id).to.equal('en'); + }); + }); + describe('Authors Entry', () => { for (const authorId of authorIds) { let json; diff --git a/packages/astro/test/fixtures/data-collections/src/pages/translations/by-id.json.js b/packages/astro/test/fixtures/data-collections/src/pages/translations/by-id.json.js new file mode 100644 index 000000000..f7b84752d --- /dev/null +++ b/packages/astro/test/fixtures/data-collections/src/pages/translations/by-id.json.js @@ -0,0 +1,9 @@ +import { getDataEntryById } from 'astro:content'; + +export async function GET() { + const item = await getDataEntryById('i18n', 'en'); + + return { + body: JSON.stringify(item), + } +}