fix getDataEntryById to lookup by base name (#8645)

This commit is contained in:
Matthew Phillips 2023-09-23 06:12:51 +08:00 committed by GitHub
parent 421257fc1c
commit cb838b84b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fix getDataEntryById to lookup by basename

View file

@ -70,7 +70,7 @@ export const getEntryBySlug = createGetEntryBySlug({
});
export const getDataEntryById = createGetDataEntryById({
dataCollectionToEntryMap,
getEntryImport: createGlobLookup(dataCollectionToEntryMap),
});
export const getEntry = createGetEntry({

View file

@ -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.`);

View file

@ -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;

View file

@ -0,0 +1,9 @@
import { getDataEntryById } from 'astro:content';
export async function GET() {
const item = await getDataEntryById('i18n', 'en');
return {
body: JSON.stringify(item),
}
}