summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Matt Kane <m@mk.gg> 2024-09-04 08:16:09 +0100
committerGravatar GitHub <noreply@github.com> 2024-09-04 08:16:09 +0100
commitb5d827ba6852d046c33643f795e1542bc2818b2c (patch)
treeb9a06401eeaea16a59f594521d2de434f2e2881d
parent38047119ff454e80cddd115bff53e33b32cd9930 (diff)
downloadastro-b5d827ba6852d046c33643f795e1542bc2818b2c.tar.gz
astro-b5d827ba6852d046c33643f795e1542bc2818b2c.tar.zst
astro-b5d827ba6852d046c33643f795e1542bc2818b2c.zip
Export `LoaderContext` types (#11914)
* Export data store types * Format * Change name again!
-rw-r--r--.changeset/mean-donkeys-switch.md6
-rw-r--r--packages/astro/src/content/data-store.ts16
-rw-r--r--packages/astro/src/content/loaders/types.ts9
-rw-r--r--packages/astro/src/content/mutable-data-store.ts9
4 files changed, 25 insertions, 15 deletions
diff --git a/.changeset/mean-donkeys-switch.md b/.changeset/mean-donkeys-switch.md
new file mode 100644
index 000000000..db70086bf
--- /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 21d59363c..57df3d521 100644
--- a/packages/astro/src/content/data-store.ts
+++ b/packages/astro/src/content/data-store.ts
@@ -41,7 +41,7 @@ export interface DataEntry<TData extends Record<string, unknown> = Record<string
* To add or modify data, use {@link MutableDataStore} instead.
*/
-export class DataStore {
+export class ImmutableDataStore {
protected _collections = new Map<string, Map<string, any>>();
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<string, Map<string, any>>) {
- const store = new DataStore();
+ const store = new ImmutableDataStore();
store._collections = data;
return store;
}
}
function dataStoreSingleton() {
- let instance: Promise<DataStore> | DataStore | undefined = undefined;
+ let instance: Promise<ImmutableDataStore> | 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 32a3e929a..4c2d8a359 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<TData extends Record<string, unknown>> {
/** The ID of the entry. Unique per collection */
@@ -17,8 +19,8 @@ export interface ParseDataOptions<TData extends Record<string, unknown>> {
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<string, unknown>;
+ /** @internal */
entryTypes: Map<string, ContentEntryType>;
}
diff --git a/packages/astro/src/content/mutable-data-store.ts b/packages/astro/src/content/mutable-data-store.ts
index 29acf4506..de0591503 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: <TData extends Record<string, unknown> = Record<string, unknown>>(key: string) =>
this.get<DataEntry<TData>>(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: <TData extends Record<string, unknown> = Record<string, unknown>>(
key: string,
) => DataEntry<TData> | undefined;