trackswap
Version:
A powerful and flexible GPX parser and encoder library for Node.js and TypeScript. TrackSwap provides comprehensive support for GPX 1.1 format with advanced features like plugin system, middleware support, and optimized performance.
74 lines (73 loc) • 3.74 kB
TypeScript
import { ExtensionsType, Token, TokenAST } from "../types.js";
export interface IPlugin<TContext = any> {
name: string;
version?: string;
description?: string;
dependencies?: string[];
supportedTags?: string[];
initialize?(context: TContext): Promise<void> | void;
destroy?(context: TContext): Promise<void> | void;
validate?(context: TContext): Promise<boolean> | boolean;
}
export interface IConverterPlugin<TContext = any> extends IPlugin<TContext> {
convert(ast: TokenAST, context: TContext): any | undefined;
supports(tag: string): boolean;
priority?: number;
}
export interface IMiddlewarePlugin<TContext = any, TResult = any> extends IPlugin<TContext> {
priority?: number;
onTokenize?(tokens: Token[], context: TContext): Promise<Token[]> | Token[];
onAstGenerate?(ast: TokenAST, context: TContext): Promise<TokenAST> | TokenAST;
onConvert?(result: any, context: TContext): Promise<any> | any;
onComplete?(result: TResult, context: TContext): Promise<TResult> | TResult;
onError?(error: Error, context: TContext): Promise<void> | void;
}
export interface IProcessingContext {
metadata: Map<string, any>;
errors: Error[];
warnings: string[];
stats: {
startTime: number;
endTime?: number;
processedTokens: number;
convertedElements: number;
};
userData?: Record<string, any>;
}
export declare abstract class BaseConverter<TContext extends IProcessingContext = IProcessingContext> implements IConverterPlugin<TContext> {
abstract name: string;
abstract supportedTags: string[];
version: string;
priority: number;
abstract convert(ast: TokenAST, context: TContext): any | undefined;
supports(tag: string): boolean;
initialize(context: TContext): Promise<void>;
destroy(context: TContext): Promise<void>;
validate(context: TContext): Promise<boolean>;
protected parseFloat(value: string | number | undefined): number | undefined;
protected parseInt(value: string | number | undefined): number;
protected parseString(value: string | number | undefined): string;
protected parseBoolean(value: string | boolean | undefined): boolean;
protected parseDate(value: string | undefined): Date | undefined;
protected hasRequiredAttributes(ast: TokenAST, required: string[]): boolean;
protected extractAttributes<U extends Record<string, any>>(ast: TokenAST, target: U, mapping: Record<string, keyof U>): void;
protected processChildren<U extends Record<string, any>>(ast: TokenAST, target: U, processors: Record<string, (child: TokenAST, target: U) => void>): void;
protected getChildValue(ast: TokenAST, tag: string): string | undefined;
protected getChildByTag(ast: TokenAST, tag: string): TokenAST | undefined;
protected getChildrenByTag(ast: TokenAST, tag: string): TokenAST[];
protected convertExtensions(extensionsAST: TokenAST): ExtensionsType;
protected convertExtensionContent(extension: TokenAST): any;
}
export declare abstract class BaseMiddleware<TContext extends IProcessingContext = IProcessingContext, TResult = any> implements IMiddlewarePlugin<TContext, TResult> {
abstract name: string;
version: string;
description: string;
initialize(context: TContext): Promise<void>;
destroy(context: TContext): Promise<void>;
validate(context: TContext): Promise<boolean>;
onTokenize(tokens: Token[], context: TContext): Promise<Token[]>;
onAstGenerate(ast: TokenAST, context: TContext): Promise<TokenAST>;
onConvert(result: any, context: TContext): Promise<any>;
onComplete(result: TResult, context: TContext): Promise<TResult>;
onError(error: Error, context: TContext): Promise<void>;
}