0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

Export LoaderContext types (#11914)

* Export data store types

* Format

* Change name again!
This commit is contained in:
Matt Kane 2024-09-04 08:16:09 +01:00 committed by GitHub
parent 38047119ff
commit b5d827ba68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 25 additions and 15 deletions

View file

@ -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.

View file

@ -41,7 +41,7 @@ export interface DataEntry<TData extends Record<string, unknown> = Record<string
* To add or modify data, use {@link MutableDataStore} instead. * To add or modify data, use {@link MutableDataStore} instead.
*/ */
export class DataStore { export class ImmutableDataStore {
protected _collections = new Map<string, Map<string, any>>(); protected _collections = new Map<string, Map<string, any>>();
constructor() { constructor() {
@ -92,31 +92,31 @@ export class DataStore {
// @ts-expect-error - this is a virtual module // @ts-expect-error - this is a virtual module
const data = await import('astro:data-layer-content'); const data = await import('astro:data-layer-content');
if (data.default instanceof Map) { if (data.default instanceof Map) {
return DataStore.fromMap(data.default); return ImmutableDataStore.fromMap(data.default);
} }
const map = devalue.unflatten(data.default); const map = devalue.unflatten(data.default);
return DataStore.fromMap(map); return ImmutableDataStore.fromMap(map);
} catch {} } catch {}
return new DataStore(); return new ImmutableDataStore();
} }
static async fromMap(data: Map<string, Map<string, any>>) { static async fromMap(data: Map<string, Map<string, any>>) {
const store = new DataStore(); const store = new ImmutableDataStore();
store._collections = data; store._collections = data;
return store; return store;
} }
} }
function dataStoreSingleton() { function dataStoreSingleton() {
let instance: Promise<DataStore> | DataStore | undefined = undefined; let instance: Promise<ImmutableDataStore> | ImmutableDataStore | undefined = undefined;
return { return {
get: async () => { get: async () => {
if (!instance) { if (!instance) {
instance = DataStore.fromModule(); instance = ImmutableDataStore.fromModule();
} }
return instance; return instance;
}, },
set: (store: DataStore) => { set: (store: ImmutableDataStore) => {
instance = store; instance = store;
}, },
}; };

View file

@ -3,7 +3,9 @@ import type { ZodSchema } from 'zod';
import type { AstroIntegrationLogger } from '../../core/logger/core.js'; import type { AstroIntegrationLogger } from '../../core/logger/core.js';
import type { AstroConfig } from '../../types/public/config.js'; import type { AstroConfig } from '../../types/public/config.js';
import type { ContentEntryType } from '../../types/public/content.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>> { export interface ParseDataOptions<TData extends Record<string, unknown>> {
/** The ID of the entry. Unique per collection */ /** The ID of the entry. Unique per collection */
@ -17,8 +19,8 @@ export interface ParseDataOptions<TData extends Record<string, unknown>> {
export interface LoaderContext { export interface LoaderContext {
/** The unique name of the collection */ /** The unique name of the collection */
collection: string; collection: string;
/** A database abstraction to store the actual data */ /** A database to store the actual data */
store: ScopedDataStore; store: DataStore;
/** A simple KV store, designed for things like sync tokens */ /** A simple KV store, designed for things like sync tokens */
meta: MetaStore; meta: MetaStore;
logger: AstroIntegrationLogger; 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 */ /** If the loader has been triggered by an integration, this may optionally contain extra data set by that integration */
refreshContextData?: Record<string, unknown>; refreshContextData?: Record<string, unknown>;
/** @internal */
entryTypes: Map<string, ContentEntryType>; entryTypes: Map<string, ContentEntryType>;
} }

View file

@ -4,7 +4,7 @@ import { Traverse } from 'neotraverse/modern';
import { imageSrcToImportId, importIdToSymbolName } from '../assets/utils/resolveImports.js'; import { imageSrcToImportId, importIdToSymbolName } from '../assets/utils/resolveImports.js';
import { AstroError, AstroErrorData } from '../core/errors/index.js'; import { AstroError, AstroErrorData } from '../core/errors/index.js';
import { IMAGE_IMPORT_PREFIX } from './consts.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'; import { contentModuleToId } from './utils.js';
const SAVE_DEBOUNCE_MS = 500; 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. * 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. * 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; #file?: PathLike;
#assetsFile?: PathLike; #assetsFile?: PathLike;
@ -190,7 +190,7 @@ export default new Map([\n${lines.join(',\n')}]);
} }
} }
scopedStore(collectionName: string): ScopedDataStore { scopedStore(collectionName: string): DataStore {
return { return {
get: <TData extends Record<string, unknown> = Record<string, unknown>>(key: string) => get: <TData extends Record<string, unknown> = Record<string, unknown>>(key: string) =>
this.get<DataEntry<TData>>(collectionName, key), 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>>( get: <TData extends Record<string, unknown> = Record<string, unknown>>(
key: string, key: string,
) => DataEntry<TData> | undefined; ) => DataEntry<TData> | undefined;