@stack.thefennec.dev/telegram-export-parser
Version:
TypeScript library for parsing Telegram Desktop's data export with full type safety
107 lines • 4.51 kB
JavaScript
;
/**
* @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
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.parserFactory = exports.parseTextEntities = exports.parseMany = exports.parse = void 0;
const factory_1 = require("../core/factory");
const text_entities_1 = require("./text-entities");
const events_1 = require("./events");
const messages_1 = require("./messages");
/** Internal parser factory instance with all parsers registered */
const factory = new factory_1.ParserFactory();
exports.parserFactory = factory;
/** Combined array of all available parsers from all modules */
const parsers = [...messages_1.MESSAGE_PARSERS, ...events_1.EVENT_PARSERS, ...text_entities_1.ENTITY_PARSERS];
parsers.forEach((parser) => factory.register(parser));
// =====================================================
// PUBLIC API
// =====================================================
/**
* 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 })
* ```
*/
const parse = (raw) => factory.parse(raw);
exports.parse = parse;
/**
* 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}`))
* ```
*/
const parseMany = (rawItems) => factory.parseMany(rawItems);
exports.parseMany = parseMany;
/**
* 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.
* ```
*/
const parseTextEntities = (textEntities) => textEntities ? textEntities.map(exports.parse) : [];
exports.parseTextEntities = parseTextEntities;
//# sourceMappingURL=index.js.map