@atlaskit/node-data-provider
Version:
Node data provider for @atlaskit/editor-core plugins and @atlaskit/renderer
126 lines (125 loc) • 3.78 kB
TypeScript
import type { JSONDocNode, JSONNode } from '@atlaskit/editor-json-transformer';
import type { NodeDataProvider } from '../node-data-provider';
/**
* Represents the SSR data for a single provider.
* It's a map where each key is a unique node data key and the value is the prefetched data for that node.
*
* @example
* {
* 'node-id-1': { value: 'some data' },
* 'node-id-2': { value: 'other data' }
* }
*/
type SsrData = {
[dataKey: string]: unknown;
};
/**
* Represents the aggregated SSR data for all node data providers.
* Each key is a provider's name, and the value contains the fetch status, duration,
* and a map of node data keys to their prefetched data.
*
* @example
* ```
* {
* mentionProvider: {
* success: true,
* duration: 220,
* data: {
* 'mention-1': { id: '1', name: 'John Doe' }
* }
* },
* emojiProvider: {
* success: true,
* duration: 110,
* data: {
* 'emoji-123': { shortName: ':smile:', representation: '😊' }
* }
* }
* }
* ```
*/
type NodeDataProvidersSsrData = {
[providerName: string]: {
data: SsrData;
duration: number;
success: boolean;
};
};
interface Props {
/**
* The document for which to prefetch data.
*/
doc: JSONDocNode;
/**
* The maximum number of nodes to visit when searching for nodes to prefetch.
* This is a performance safeguard for very large documents.
*
* @default If not specified, the entire document will be traversed.
*/
maxNodesToVisit?: number;
/**
* A list of node data providers to use for prefetching, along with their
* individual configurations.
*/
providers: {
/**
* The maximum number of nodes to prefetch for this specific provider.
* This helps prevent performance issues with documents containing many
* nodes of the same type.
*
* @default If not specified, all nodes supported by the provider will be prefetched.
*/
maxNodesToPrefetch?: number;
/**
* The provider instance to use for prefetching.
*/
provider: NodeDataProvider<JSONNode, unknown>;
/**
* The timeout in milliseconds for the fetch request for this specific provider.
* If the request takes longer than this, it will be aborted and return no data.
*
* @default If not specified, the global timeout will be used.
*/
timeout?: number;
}[];
/**
* A global timeout in milliseconds for all fetch requests.
*
* Each provider will use the minimum of this value and its own specific timeout.
*/
timeout: number;
}
/**
* Fetches data for nodes in the document that are supported by the given providers.
* This function will traverse the document and call the `fetchData` method for each node that is supported by the providers.
*
* @example
* ```
* const doc = JSON.parse('{"type": "doc", "content": [...] }');
* const providers = [
* {
* provider: new EditorCardProvider(),
* maxNodesToPrefetch: 10,
* timeout: 500,
* },
* {
* provider: new EditorMentionsProvider(),
* maxNodesToPrefetch: 50,
* timeout: 500,
* },
* ];
*
* const data = await prefetchNodeDataProvidersData({
* providers,
* doc,
* timeout: 1_000,
* maxNodesToVisit: 2_000
* });
* ```
*
* @param props The properties for prefetching node data.
* @returns Record of provider names to their respective SSR data,
* success status, and duration of the fetch operation.
*/
export declare function prefetchNodeDataProvidersData({ providers, doc, timeout, maxNodesToVisit, }: Props): Promise<NodeDataProvidersSsrData>;
export {};