mirror of
https://github.com/withastro/astro.git
synced 2025-01-06 22:10:10 -05:00
fix: pass emulated entry to getCollection filter function (#12875)
* fix: pass emulated entry to getCollection filter function * Add test
This commit is contained in:
parent
ac00c64ee7
commit
e109002c3d
4 changed files with 32 additions and 8 deletions
5
.changeset/rotten-baboons-roll.md
Normal file
5
.changeset/rotten-baboons-roll.md
Normal file
|
@ -0,0 +1,5 @@
|
|||
---
|
||||
'astro': patch
|
||||
---
|
||||
|
||||
Fixes a bug in emulated legacy collections where the entry passed to the getCollection filter function did not include the legacy entry fields.
|
|
@ -96,15 +96,20 @@ export function createGetCollection({
|
|||
for (const rawEntry of store.values<DataEntry>(collection)) {
|
||||
const data = updateImageReferencesInData(rawEntry.data, rawEntry.filePath, imageAssetMap);
|
||||
|
||||
const entry = {
|
||||
let entry = {
|
||||
...rawEntry,
|
||||
data,
|
||||
collection,
|
||||
};
|
||||
|
||||
if (entry.legacyId) {
|
||||
entry = emulateLegacyEntry(entry);
|
||||
}
|
||||
|
||||
if (hasFilter && !filter(entry)) {
|
||||
continue;
|
||||
}
|
||||
result.push(entry.legacyId ? emulateLegacyEntry(entry) : entry);
|
||||
result.push(entry);
|
||||
}
|
||||
return result;
|
||||
} else {
|
||||
|
@ -272,19 +277,18 @@ type DataEntryResult = {
|
|||
|
||||
type EntryLookupObject = { collection: string; id: string } | { collection: string; slug: string };
|
||||
|
||||
function emulateLegacyEntry(entry: DataEntry) {
|
||||
function emulateLegacyEntry({ legacyId, ...entry }: DataEntry & { collection: string }) {
|
||||
// Define this first so it's in scope for the render function
|
||||
const legacyEntry = {
|
||||
...entry,
|
||||
id: entry.legacyId!,
|
||||
id: legacyId!,
|
||||
slug: entry.id,
|
||||
};
|
||||
delete legacyEntry.legacyId;
|
||||
return {
|
||||
...legacyEntry,
|
||||
// Define separately so the render function isn't included in the object passed to `renderEntry()`
|
||||
render: () => renderEntry(legacyEntry),
|
||||
};
|
||||
} as ContentEntryResult;
|
||||
}
|
||||
|
||||
export function createGetEntry({
|
||||
|
@ -334,7 +338,7 @@ export function createGetEntry({
|
|||
const { default: imageAssetMap } = await import('astro:asset-imports');
|
||||
entry.data = updateImageReferencesInData(entry.data, entry.filePath, imageAssetMap);
|
||||
if (entry.legacyId) {
|
||||
return { ...emulateLegacyEntry(entry), collection } as ContentEntryResult;
|
||||
return emulateLegacyEntry({ ...entry, collection });
|
||||
}
|
||||
return {
|
||||
...entry,
|
||||
|
|
|
@ -9,8 +9,17 @@ export async function GET() {
|
|||
const withUnionSchema = stripAllRenderFn(await getCollection('with-union-schema'));
|
||||
const withSymlinkedContent = stripAllRenderFn(await getCollection('with-symlinked-content'));
|
||||
const withSymlinkedData = stripAllRenderFn(await getCollection('with-symlinked-data'));
|
||||
const filtered = stripAllRenderFn(await getCollection('without-config', (entry) => entry.slug));
|
||||
|
||||
return new Response(
|
||||
devalue.stringify({ withoutConfig, withSchemaConfig, withSlugConfig, withUnionSchema, withSymlinkedContent, withSymlinkedData }),
|
||||
devalue.stringify({
|
||||
withoutConfig,
|
||||
withSchemaConfig,
|
||||
withSlugConfig,
|
||||
withUnionSchema,
|
||||
withSymlinkedContent,
|
||||
withSymlinkedData,
|
||||
filtered,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
|
|
@ -55,6 +55,12 @@ describe('Legacy Content Collections', () => {
|
|||
);
|
||||
});
|
||||
|
||||
it('Passes legacy entry to filter function', async () => {
|
||||
assert.ok(json.hasOwnProperty('filtered'));
|
||||
assert.ok(Array.isArray(json.filtered));
|
||||
assert.ok(json.filtered.length > 0);
|
||||
});
|
||||
|
||||
it('Returns `with schema` collection', async () => {
|
||||
assert.ok(json.hasOwnProperty('withSchemaConfig'));
|
||||
assert.equal(Array.isArray(json.withSchemaConfig), true);
|
||||
|
|
Loading…
Reference in a new issue