Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/astro/test/fixtures/live-loaders space/astro.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @ts-check
import { defineConfig } from 'astro/config';

import node from '@astrojs/node';

// https://astro.build/config
export default defineConfig({
adapter: node({
mode: 'standalone'
}),
experimental: {
liveContentCollections: true
}
});
15 changes: 15 additions & 0 deletions packages/astro/test/fixtures/live-loaders space/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "@test/live-loaders-space",
"type": "module",
"version": "0.0.1",
"scripts": {
"dev": "astro dev",
"build": "astro build",
"preview": "astro preview",
"astro": "astro"
},
"dependencies": {
"@astrojs/node": "workspace:*",
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { defineCollection } from "astro:content";
const something = defineCollection({
loader: () => ([])
})
export const collections = { something };
86 changes: 86 additions & 0 deletions packages/astro/test/fixtures/live-loaders space/src/live.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { defineLiveCollection } from 'astro:content';
import { z } from 'astro/zod';
import type { LiveLoader } from 'astro/loaders';

type Entry = {
title: string;
age?: number;
};

interface CollectionFilter {
addToAge?: number;
returnInvalid?: boolean;
}

type EntryFilter = {
id: keyof typeof entries;
addToAge?: number;
};

const entries = {
'123': { id: '123', data: { title: 'Page 123', age: 10 } },
'456': { id: '456', data: { title: 'Page 456', age: 20 } },
'789': { id: '789', data: { title: 'Page 789', age: 30 } },
};

class CustomError extends Error {
constructor(message: string) {
super(message);
this.name = 'CustomError';
}
}

const loader: LiveLoader<Entry, EntryFilter, CollectionFilter, CustomError> = {
name: 'test-loader',
loadEntry: async ({ filter }) => {
const entry = entries[filter.id];
if (!entry) {
return {
error: new CustomError(`Entry ${filter.id} not found`),
};
}
return {
...entry,
data: {
title: entry.data.title,
age: filter?.addToAge
? entry.data.age
? entry.data.age + filter.addToAge
: filter.addToAge
: entry.data.age,
},
cacheHint: {
tags: [`page:${filter.id}`],
maxAge: 60,
},
};
},
loadCollection: async ({filter}) => {
return {
entries: filter?.addToAge
? Object.values(entries).map((entry) => ({
...entry,
data: {
title: filter.returnInvalid ? 99 as any : entry.data.title,
age: entry.data.age ? entry.data.age + filter!.addToAge! : undefined,
},
}))
: Object.values(entries),
cacheHint: {
tags: ['page'],
maxAge: 60,
},
};
},
};

const liveStuff = defineLiveCollection({
type: 'live',
loader,
schema: z.object({
title: z.string(),
age: z.number().optional(),
}),
});

export const collections = { liveStuff };
23 changes: 23 additions & 0 deletions packages/astro/test/fixtures/live-loaders space/src/pages/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { APIRoute } from 'astro';
import { getLiveCollection, getLiveEntry } from 'astro:content';

export const prerender = false;

export const GET: APIRoute = async ({ url }) => {
const addToAge = url.searchParams.get('addToAge');
const returnInvalid = url.searchParams.has('returnInvalid');
const filter = addToAge ? { addToAge: parseInt(addToAge), returnInvalid } : undefined;
const { error, entries, cacheHint } = await getLiveCollection('liveStuff', filter);
const entryByString = await getLiveEntry('liveStuff', '123');
const entryByObject = await getLiveEntry('liveStuff', { id: '456', ...filter });

return Response.json({
collection: {
cacheHint,
entries,
error: error ? { ...error, message: error.message } : undefined,
},
entryByObject,
entryByString,
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
import { getLiveCollection } from "astro:content";

const collection = await getLiveCollection("liveStuff")

if(collection.error) {
throw collection.error;
}

export const prerender = false;

---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>
<h1>Astro</h1>
<ul>
{collection.entries?.map((item) => (
<li>
{item.data.title}
</li>
))}
</ul>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
import { getLiveEntry } from "astro:content";

const { entry } = await getLiveEntry("liveStuff", "123")
export const prerender = false;

---

<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
<meta name="viewport" content="width=device-width" />
<meta name="generator" content={Astro.generator} />
<title>Astro</title>
</head>
<body>
<h1>{entry?.data.title}</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { APIRoute } from 'astro';
import { getCollection } from 'astro:content';

export const prerender = false;

export const GET: APIRoute = async () => {
try {
// @ts-ignore This should throw an error because liveStuff is a live collection
const collection = await getCollection('liveStuff');
return Response.json({ collection });
} catch (error: any) {
return Response.json(
{
error: error.message
},
{
status: 500,
},
);
}
};
5 changes: 5 additions & 0 deletions packages/astro/test/fixtures/live-loaders space/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "astro/tsconfigs/strict",
"include": [".astro/types.d.ts", "**/*"],
"exclude": ["dist"]
}
Loading
Loading