UNPKG

staticql

Version:

Type-safe query engine for static content including Markdown, YAML, JSON, and more.

125 lines (124 loc) 3.5 kB
import { PrefixIndexDefinition, PrefixIndexDepth } from "./utils/typs.js"; import { JSONSchema7 } from "./validator/Validator.js"; /** * Represents a single content record, identified by a slug. */ export type SourceRecord = { slug: string; raw: string; [key: string]: any; }; /** * Supported content types. */ export type SourceType = string; /** * Configuration for a single source (as defined in user config). */ export interface SourceConfig { type: SourceType; pattern: string; schema: JSONSchema7; relations?: Record<string, Relation>; index?: IndexDefinition; customIndex?: IndexDefinition; } type IndexDefinition = Record<string, { indexDepth?: PrefixIndexDepth; }>; /** * Internally resolved and enriched source configuration. */ export interface ResolvedSourceConfig { name: string; type: SourceType; pattern: string; schema: JSONSchema7; relations?: Record<string, Relation>; indexes?: PrefixIndexDefinition; } /** * Direct relation to another source. */ export type DirectRelation = { to: string; localKey: string; foreignKey: string; type: "hasOne" | "hasMany" | "belongsTo" | "belongsToMany"; }; /** * Through (intermediate) relation to another source. */ export type ThroughRelation = { to: string; through: string; sourceLocalKey: string; throughForeignKey: string; throughLocalKey: string; targetForeignKey: string; type: "hasOneThrough" | "hasManyThrough"; }; /** * Any supported relation type. */ export type Relation = DirectRelation | ThroughRelation; /** * Resolves user-defined source configurations into a normalized internal format. */ export declare class SourceConfigResolver { private readonly sources; private cache; constructor(sources: Record<string, SourceConfig>); /** * Resolves all sources and returns the enriched configurations. * * @returns */ resolveAll(): ResolvedSourceConfig[]; /** * Resolves a single source by name. * * @param sourceName - The name of the source. * @returns Resolved configuration. * @throws If the source does not exist. */ resolveOne(sourceName: string): ResolvedSourceConfig; /** * Determines whether a relation is a through (indirect) relation. * * @param rel * @returns */ isThroughRelation(rel: Relation): rel is ThroughRelation; /** * Check depth in range. */ private isDepthInRange; /** * Converts a list of slugs into full paths based on a glob pattern. */ static getSourcePathsBySlugs(pattern: string, slugs: string[]): string[]; /** * Converts a slug (with `--`) to a file path (`/`). */ static slugToPath(slug: string): string; /** * Converts a path (`/`) to a slug (with `--`). */ static pathToSlug(path: string): string; /** * Extracts the base directory from a glob pattern (up to the first wildcard). */ static extractBaseDir(globPath: string): string; /** * Resolves a logical file path from a glob source and a relative path. */ static resolveFilePath(sourceGlob: string, relativePath: string): string; /** * Extracts the slug from a full file path using the source glob. */ static getSlugFromPath(sourcePath: string, filePath: string): string; static patternTest(pattern: string, filePath: string): boolean; private static globToRegExp; } export {};