import IOSchema from '../schema/schema.js'; import '../schema/types/memberdef.js'; import '../schema/schema-types.js'; /** * Represents a stored definition value with metadata. */ type IODefinitionValue = { /** True if this is a schema definition (key starts with $) */ isSchema: boolean; /** True if this is a variable definition (key starts with @) */ isVariable: boolean; /** The actual definition value */ value: any; }; /** * IODefinitions manages schema definitions, variables, and metadata for Internet Object documents. * * Key Types: * - Schema definitions: Keys starting with `$` (e.g., `$person`, `$schema`) * - Variables: Keys starting with `@` (e.g., `@yes`, `@baseUrl`) * - Metadata: Regular keys (e.g., `version`, `page`) * * Features: * - Preserves insertion order (definitions can reference earlier definitions) * - O(1) key-based access * - Forward reference validation (throws for undefined variables/schemas) * - Merge support for document composition * - Iterable for processing all definitions * * Definition Order Rules (per Internet Object spec): * - Variables and schemas defined earlier can be used in later definitions * - Forward references (using definitions not yet defined) throw errors * * @example * ```typescript * const defs = new IODefinitions(); * defs.set('@baseUrl', 'https://api.example.com'); * defs.set('$person', personSchema); * defs.set('$schema', mainSchema); * * // Variables are accessed via getV() * const url = defs.getV('@baseUrl'); // 'https://api.example.com' * * // Schemas are accessed via get() * const schema = defs.get('$person'); // Schema instance * ``` */ declare class IODefinitions { /** * The default schema, if defined. Reset when $schema is deleted or updated. */ private _defaultSchema; /** * Internal storage for definitions. Order is preserved as per insertion sequence. */ private _definitions; /** * Returns the number of definitions in the collection. * @returns Number of definitions. */ get length(): number; /** * Returns an array of definition keys, in insertion order. * @returns Array of keys. */ get keys(): string[]; /** * Returns the key-value pair at the specified index, preserving definition order. * @param index Index of the definition. * @returns Object with key and value. */ at(index: number): { key: string; value: IODefinitionValue; }; /** * Returns the default schema, if defined. * Resolves schema variable references (e.g., $schema: $otherSchema). * @returns The default Schema instance or null. */ get defaultSchema(): IOSchema | null; /** * Checks if there are any definitions beyond just the default $schema. * Used to determine serialization format: * - false: Output bare schema line (schema-only mode) * - true: Output ~ definitions format (has variables, metadata, or multiple schemas) * @returns True if there are any definitions other than a single $schema, false otherwise */ get defaultSchemaOnly(): boolean; /** * Gets a definition value by key, regardless of whether it's a variable, schema, or regular definition. * @param key The definition key * @returns The value associated with the key, or undefined if not found */ get(key: string): any; /** * Gets the variable value. This function is intended to be used internally * for quickly fetching the variable value, hence it accepts any key to keep the * consumer code free from type checking. The function validates the key and * returns the associated value, if available. Otherwise returns undefined. * @param key {any} The variable key starting with $ * @returns The value associated with the variable */ getV(k: any): any; set(k: string, v: any): void; /** * Removes a definition by key. * @param key The key of the definition to remove * @returns True if the key existed and was deleted, false otherwise */ delete(key: string): boolean; /** * Pushes a new definition to the definitions list. * @param key The key of the definition * @param value The value of the definition */ push(key: string, value: any, isSchema?: boolean, isVariable?: boolean): void; /** * Merges the definitions with the other definitions. * @param other The other definitions to merge with * @param override If true, the other definitions will override the current * definitions */ merge(other: IODefinitions, override?: boolean): void; toObject(): any; toJSON(): any; /** * Returns an iterator of definition keys. */ /** * Returns an iterator of definition keys (useful for for...of). */ keyIterator(): IterableIterator; /** * Returns an iterator of [key, value] pairs. */ entries(): IterableIterator<[string, IODefinitionValue]>; /** * Default iterator for [key, value] pairs. */ [Symbol.iterator](): IterableIterator<[string, IODefinitionValue]>; } export { IODefinitions as default };