Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ascorbic committed Jun 26, 2024
1 parent 4b7f8f7 commit 20bee9a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 5 deletions.
28 changes: 27 additions & 1 deletion packages/astro/src/content/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ZodSchema } from 'zod';
import type { AstroSettings } from '../@types/astro.js';
import type { AstroIntegrationLogger, Logger } from '../core/logger/core.js';
import { DataStore, globalDataStore, type MetaStore, type ScopedDataStore } from './data-store.js';
import { globalContentConfigObserver } from './utils.js';
import { getEntryData, globalContentConfigObserver } from './utils.js';
import { promises as fs, existsSync } from 'fs';

export interface LoaderContext {
Expand All @@ -14,6 +14,11 @@ export interface LoaderContext {
logger: AstroIntegrationLogger;

settings: AstroSettings;

parseData<T extends Record<string, unknown> = Record<string, unknown>>(
id: string,
data: T
): T;
}

export interface Loader<S extends ZodSchema = ZodSchema> {
Expand Down Expand Up @@ -45,12 +50,33 @@ export async function syncDataLayer({
if (collection.type !== 'experimental_data') {
return;
}

function parseData<T extends Record<string, unknown> = Record<string, unknown>>(
id: string,
data: T
): T {
return getEntryData(
{
id,
collection: name,
unvalidatedData: data,
_internal: {
rawData: undefined,
filePath: '#invalid',
},
},
collection,
false
) as unknown as T;
}

return collection.loader.load({
collection: name,
store: store.scopedStore(name),
meta: store.metaStore(name),
logger: logger.forkIntegrationLogger('content'),
settings,
parseData
});
})
);
Expand Down
23 changes: 20 additions & 3 deletions packages/astro/src/content/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const collectionConfigParser = z.union([
meta: z.any(),
logger: z.any(),
settings: z.any(),
parseData: z.any(),
}),
],
z.unknown()
Expand Down Expand Up @@ -104,18 +105,34 @@ export async function getEntryData(
},
collectionConfig: CollectionConfig,
shouldEmitFile: boolean,
pluginContext: PluginContext
pluginContext?: PluginContext
) {
let data;
if (collectionConfig.type === 'data') {
if (collectionConfig.type === 'data' || collectionConfig.type === 'experimental_data') {
data = entry.unvalidatedData;
} else {
const { slug, ...unvalidatedData } = entry.unvalidatedData;
data = unvalidatedData;
}

let schema = collectionConfig.schema;
if (typeof schema === 'function') {
if (collectionConfig.type === 'experimental_data') {
if (!schema) {
// Experimental data loaders can define their own schema
schema = collectionConfig.loader.schema;
}
if (typeof schema === 'function') {
// TODO: images!
schema = schema({
image: () => {
throw new Error('Images not supported in experimental data loaders');
},
});
}
} else if (typeof schema === 'function') {
if (!pluginContext) {
throw new Error('Plugin context required for dynamic schema');
}
schema = schema({
image: createImage(pluginContext, shouldEmitFile, entry._internal.filePath),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const dogs = defineCollection({
size: z.string(),
origin: z.string(),
lifespan: z.string(),
temperament: z.array(z.string()),
temperament: z.array(z.string())
}),
})
export const collections = { blog, dogs };

0 comments on commit 20bee9a

Please sign in to comment.