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:
parent
38047119ff
commit
b5d827ba68
4 changed files with 25 additions and 15 deletions
6
.changeset/mean-donkeys-switch.md
Normal file
6
.changeset/mean-donkeys-switch.md
Normal 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.
|
|
@ -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;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -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>;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue