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({ export const getDataEntryById = createGetDataEntryById({
dataCollectionToEntryMap, getEntryImport: createGlobLookup(dataCollectionToEntryMap),
}); });
export const getEntry = createGetEntry({ export const getEntry = createGetEntry({

View file

@ -139,13 +139,12 @@ export function createGetEntryBySlug({
} }
export function createGetDataEntryById({ export function createGetDataEntryById({
dataCollectionToEntryMap, getEntryImport,
}: { }: {
dataCollectionToEntryMap: CollectionToEntryMap; getEntryImport: GetEntryImport;
}) { }) {
return async function getDataEntryById(collection: string, id: string) { return async function getDataEntryById(collection: string, id: string) {
const lazyImport = const lazyImport = await getEntryImport(collection, id);
dataCollectionToEntryMap[collection]?.[/*TODO: filePathToIdMap*/ id + '.json'];
// TODO: AstroError // TODO: AstroError
if (!lazyImport) throw new Error(`Entry ${collection}${id} was not found.`); 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', () => { describe('Authors Entry', () => {
for (const authorId of authorIds) { for (const authorId of authorIds) {
let json; 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),
}
}