diff --git a/.changeset/mean-donkeys-switch.md b/.changeset/mean-donkeys-switch.md new file mode 100644 index 000000000000..db70086bfe3d --- /dev/null +++ b/.changeset/mean-donkeys-switch.md @@ -0,0 +1,6 @@ +--- +'astro': patch +--- + +Exports types for all `LoaderContext` properties from `astro/loaders` to make it easier to use them in custom loaders. +The `ScopedDataStore` interface (which was previously internal) is renamed to `DataStore`, to reflect the fact that it's the only public API for the data store. diff --git a/packages/astro/src/content/data-store.ts b/packages/astro/src/content/data-store.ts index 21d59363c022..57df3d521596 100644 --- a/packages/astro/src/content/data-store.ts +++ b/packages/astro/src/content/data-store.ts @@ -41,7 +41,7 @@ export interface DataEntry = Record>(); constructor() { @@ -92,31 +92,31 @@ export class DataStore { // @ts-expect-error - this is a virtual module const data = await import('astro:data-layer-content'); if (data.default instanceof Map) { - return DataStore.fromMap(data.default); + return ImmutableDataStore.fromMap(data.default); } const map = devalue.unflatten(data.default); - return DataStore.fromMap(map); + return ImmutableDataStore.fromMap(map); } catch {} - return new DataStore(); + return new ImmutableDataStore(); } static async fromMap(data: Map>) { - const store = new DataStore(); + const store = new ImmutableDataStore(); store._collections = data; return store; } } function dataStoreSingleton() { - let instance: Promise | DataStore | undefined = undefined; + let instance: Promise | ImmutableDataStore | undefined = undefined; return { get: async () => { if (!instance) { - instance = DataStore.fromModule(); + instance = ImmutableDataStore.fromModule(); } return instance; }, - set: (store: DataStore) => { + set: (store: ImmutableDataStore) => { instance = store; }, }; diff --git a/packages/astro/src/content/loaders/types.ts b/packages/astro/src/content/loaders/types.ts index 32a3e929ad87..4c2d8a3598f1 100644 --- a/packages/astro/src/content/loaders/types.ts +++ b/packages/astro/src/content/loaders/types.ts @@ -3,7 +3,9 @@ import type { ZodSchema } from 'zod'; import type { AstroIntegrationLogger } from '../../core/logger/core.js'; import type { AstroConfig } from '../../types/public/config.js'; import type { ContentEntryType } from '../../types/public/content.js'; -import type { MetaStore, ScopedDataStore } from '../mutable-data-store.js'; +import type { DataStore, MetaStore } from '../mutable-data-store.js'; + +export type { DataStore, MetaStore }; export interface ParseDataOptions> { /** The ID of the entry. Unique per collection */ @@ -17,8 +19,8 @@ export interface ParseDataOptions> { export interface LoaderContext { /** The unique name of the collection */ collection: string; - /** A database abstraction to store the actual data */ - store: ScopedDataStore; + /** A database to store the actual data */ + store: DataStore; /** A simple KV store, designed for things like sync tokens */ meta: MetaStore; logger: AstroIntegrationLogger; @@ -35,6 +37,7 @@ export interface LoaderContext { /** If the loader has been triggered by an integration, this may optionally contain extra data set by that integration */ refreshContextData?: Record; + /** @internal */ entryTypes: Map; } diff --git a/packages/astro/src/content/mutable-data-store.ts b/packages/astro/src/content/mutable-data-store.ts index 29acf4506bf7..de0591503c66 100644 --- a/packages/astro/src/content/mutable-data-store.ts +++ b/packages/astro/src/content/mutable-data-store.ts @@ -4,7 +4,7 @@ import { Traverse } from 'neotraverse/modern'; import { imageSrcToImportId, importIdToSymbolName } from '../assets/utils/resolveImports.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { IMAGE_IMPORT_PREFIX } from './consts.js'; -import { type DataEntry, DataStore, type RenderedContent } from './data-store.js'; +import { type DataEntry, ImmutableDataStore, type RenderedContent } from './data-store.js'; import { contentModuleToId } from './utils.js'; const SAVE_DEBOUNCE_MS = 500; @@ -13,7 +13,7 @@ const SAVE_DEBOUNCE_MS = 500; * Extends the DataStore with the ability to change entries and write them to disk. * This is kept as a separate class to avoid needing node builtins at runtime, when read-only access is all that is needed. */ -export class MutableDataStore extends DataStore { +export class MutableDataStore extends ImmutableDataStore { #file?: PathLike; #assetsFile?: PathLike; @@ -190,7 +190,7 @@ export default new Map([\n${lines.join(',\n')}]); } } - scopedStore(collectionName: string): ScopedDataStore { + scopedStore(collectionName: string): DataStore { return { get: = Record>(key: string) => this.get>(collectionName, key), @@ -329,7 +329,8 @@ export default new Map([\n${lines.join(',\n')}]); } } -export interface ScopedDataStore { +// This is the scoped store for a single collection. It's a subset of the MutableDataStore API, and is the only public type. +export interface DataStore { get: = Record>( key: string, ) => DataEntry | undefined;