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