0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix: add refresh context to schema for loader args (#11960)

* fix: add refresh context to schema for loader args

* fix negative match test
This commit is contained in:
Matt Kane 2024-09-10 13:18:18 +01:00 committed by GitHub
parent f13c357753
commit 4410130df7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 3 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where the refresh context data was not passed correctly to content layer loaders

View file

@ -80,6 +80,7 @@ const collectionConfigParser = z.union([
parseData: z.any(),
generateDigest: z.function(z.tuple([z.any()], z.string())),
watcher: z.any().optional(),
refreshContextData: z.record(z.unknown()).optional(),
}),
],
z.unknown(),

View file

@ -87,7 +87,10 @@ describe('Content Layer', () => {
assert.ok(json.hasOwnProperty('probes'));
assert.ok(Array.isArray(json.probes));
assert.equal(json.probes.length, 5);
assert.equal(json.probes.at(-1).id, 'philae-lander', 'Voyager probes should not be included');
assert.ok(
json.probes.every(({ id }) => !id.startsWith('voyager')),
'Voyager probes should not be included',
);
});
it('Returns data entry by id', async () => {
@ -290,16 +293,18 @@ describe('Content Layer', () => {
const rawJsonResponse = await fixture.fetch('/collections.json');
const initialJson = devalue.parse(await rawJsonResponse.text());
assert.equal(initialJson.increment.data.lastValue, 1);
const now = new Date().toISOString();
const refreshResponse = await fixture.fetch('/_refresh', {
method: 'POST',
body: JSON.stringify({}),
body: JSON.stringify({ now }),
});
const refreshData = await refreshResponse.json();
assert.equal(refreshData.message, 'Content refreshed successfully');
const updatedJsonResponse = await fixture.fetch('/collections.json');
const updated = devalue.parse(await updatedJsonResponse.text());
assert.equal(updated.increment.data.lastValue, 2);
assert.deepEqual(updated.increment.data.refreshContextData, { webhookBody: { now } });
});
it('updates collection when data file is changed', async () => {

View file

@ -123,7 +123,7 @@ const images = defineCollection({
const increment = defineCollection({
loader: {
name: 'increment-loader',
load: async ({ store }) => {
load: async ({ store, refreshContextData }) => {
const entry = store.get<{ lastValue: number }>('value');
const lastValue = entry?.data.lastValue ?? 0;
store.set({
@ -131,6 +131,7 @@ const increment = defineCollection({
data: {
lastValue: lastValue + 1,
lastUpdated: new Date(),
refreshContextData
},
});
},
@ -139,6 +140,7 @@ const increment = defineCollection({
z.object({
lastValue: z.number(),
lastUpdated: z.date(),
refreshContextData: z.record(z.unknown()),
}),
},
});