UNPKG

@stack.thefennec.dev/telegram-export-parser

Version:

TypeScript library for parsing Telegram Desktop's data export with full type safety

116 lines 4.47 kB
/** * @fileoverview Universal parser system for Telegram export data processing. * * Transforms raw Telegram export data (messages, events, text entities) into typed TypeScript * objects using priority-based parser selection. Handles batch processing with error resilience. * * @example * ```typescript * import { parse, parseMany } from './parsers' * * // Auto-detects and parses any Telegram export data * const message = parse(rawTelegramMessage) // → TextMessage | PhotoMessage | etc. * const entity = parse(rawTextEntity) // → BoldEntity | LinkEntity | etc. * * // Batch processing with error handling * const { parsed, failed } = parseMany(rawDataArray) * console.log(`✅ ${parsed.length} parsed, ❌ ${failed.length} failed`) * ``` * * @module parsers */ import { ParserFactory } from '../core/factory'; import type { RawTextEntity, TextEntity } from '../types'; /** Internal parser factory instance with all parsers registered */ declare const factory: ParserFactory; /** * Universal parser for raw Telegram export data → typed TypeScript objects. * * Auto-detects data type and applies the appropriate parser using priority-based selection. * Supports all Telegram export formats: messages, events, text entities. * * @template TOutput - Expected output type (auto-inferred) * @param raw - Raw Telegram export data object * @returns Parsed and typed object * @throws {TelegramExportParseError} Invalid input or no suitable parser found * * @example * ```typescript * // Messages, events, entities - all handled automatically * const message = parse({ id: 123, type: 'message', text: 'Hello!' }) * const entity = parse({ type: 'bold', text: 'Important' }) * const event = parse({ type: 'service', action: 'phone_call', duration: 120 }) * ``` */ export declare const parse: <TOutput = unknown>(raw: unknown) => TOutput; /** * Batch parser with error resilience - processes arrays without stopping on failures. * * Essential for large Telegram exports where some items may be malformed. Continues * processing and collects both successes and failures with debugging context. * * @template TOutput - Expected output type for successful parses * @param rawItems - Array of raw data objects to parse * @returns Object with parsed results and failure details * @returns {TOutput[]} returns.parsed - Successfully parsed items * @returns {Array<{index: number, data: unknown, error: Error}>} returns.failed - Failed items * * @example * ```typescript * const rawData = [ * { id: 1, type: 'message', text: 'Valid' }, // ✅ * { invalid: 'data' }, // ❌ * { id: 3, type: 'message', text: 'Also valid' } // ✅ * ] * * const { parsed, failed } = parseMany(rawData) * console.log(`Success rate: ${(parsed.length / rawData.length * 100).toFixed(1)}%`) * * // Debug failures * failed.forEach(({ index, error }) => * console.log(`Item ${index}: ${error.message}`)) * ``` */ export declare const parseMany: <TOutput = unknown>(rawItems: unknown[]) => { parsed: TOutput[]; failed: Array<{ index: number; data: unknown; error: Error; }>; }; /** * Parse text formatting entities (bold, links, mentions, etc.) from message data. * * Null-safe helper that returns empty array for undefined/falsy inputs. Essential for * processing Telegram message formatting without manual null checks. * * @param textEntities - Raw text entities array (may be undefined) * @returns Parsed TextEntity objects (empty array if input is falsy) * * @example * ```typescript * const entities = parseTextEntities(rawMessage.text_entities) * entities.forEach(entity => console.log(entity.toMarkdown())) * // **bold text**, [link](https://example.com), @username, etc. * ``` */ export declare const parseTextEntities: (textEntities: RawTextEntity[] | null | undefined) => TextEntity[]; /** * Direct access to parser factory for advanced use cases. * * For parser introspection, custom registration, or debugging. Most applications * should use the high-level `parse()` and `parseMany()` functions instead. * * @example * ```typescript * // Registry introspection * const parsers = parserFactory.getRegisteredParsers() * console.log(`${parsers.length} parsers registered`) * * // Custom parser registration * parserFactory.register(myCustomParser) * ``` */ export { factory as parserFactory }; //# sourceMappingURL=index.d.ts.map