mirror of
https://github.com/withastro/astro.git
synced 2024-12-16 21:46:22 -05:00
fix getDataEntryById to lookup by base name (#8645)
This commit is contained in:
parent
421257fc1c
commit
cb838b84b4
5 changed files with 29 additions and 5 deletions
5
.changeset/spicy-walls-wave.md
Normal file
5
.changeset/spicy-walls-wave.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fix getDataEntryById to lookup by basename
|
|
@ -70,7 +70,7 @@ export const getEntryBySlug = createGetEntryBySlug({
|
|||
});
|
||||
|
||||
export const getDataEntryById = createGetDataEntryById({
|
||||
dataCollectionToEntryMap,
|
||||
getEntryImport: createGlobLookup(dataCollectionToEntryMap),
|
||||
});
|
||||
|
||||
export const getEntry = createGetEntry({
|
||||
|
|
|
@ -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.`);
|
||||
|
|
|
@ -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;
|
||||
|
|
9
packages/astro/test/fixtures/data-collections/src/pages/translations/by-id.json.js
vendored
Normal file
9
packages/astro/test/fixtures/data-collections/src/pages/translations/by-id.json.js
vendored
Normal 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),
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue