@cardog/corgi
Version:
⚡ The fastest and most lightweight open-source VIN decoding library. Fully offline, TypeScript-first, with comprehensive NHTSA VPIC database integration for Node.js, browsers, and Cloudflare Workers.
205 lines (198 loc) • 6.76 kB
TypeScript
import { D as DatabaseAdapter, Q as QueryResult, a as DatabaseAdapterFactory, b as DecodeOptions, c as DecodeResult } from './d1-adapter-tXarWrfX.js';
export { B as BodyStyle, p as BrowserDatabaseAdapter, q as BrowserDatabaseAdapterFactory, C as CheckDigitResult, r as CloudflareD1Adapter, i as DatabaseError, f as DecodeError, n as DiagnosticInfo, E as EngineInfo, k as ErrorCategory, j as ErrorCode, l as ErrorSeverity, L as LookupError, M as ModelYearResult, h as PatternError, e as PatternMatch, P as PlantInfo, m as Position, S as StructureError, V as VINComponents, o as VINDecoder, g as ValidationError, d as VehicleInfo, W as WMIResult, s as createD1Adapter } from './d1-adapter-tXarWrfX.js';
import '@cloudflare/workers-types';
/**
* Node.js implementation of the DatabaseAdapter using better-sqlite3
*/
declare class NodeDatabaseAdapter implements DatabaseAdapter {
private db;
private queryCount;
/**
* Create a new database adapter for Node.js environment
*
* @param dbPath - Path to the SQLite database file
*/
constructor(dbPath: string);
/**
* Execute a SQL query with parameters
*
* @param query - SQL query to execute
* @param params - Parameters to bind to the query
* @returns Query results
*/
exec(query: string, params?: any[]): Promise<QueryResult[]>;
/**
* Close the database connection
*/
close(): Promise<void>;
}
/**
* Factory for creating Node.js database adapters
*/
declare class NodeDatabaseAdapterFactory implements DatabaseAdapterFactory {
/**
* Create a new database adapter for the given path
*
* @param pathOrUrl - Path to the SQLite database file
* @returns Initialized database adapter
*/
createAdapter(pathOrUrl: string): Promise<DatabaseAdapter>;
}
/**
* Gets the path to the database, handling decompression if needed
*
* @param options - Optional configuration
* @returns Path to usable database file
*/
declare function getDatabasePath(options?: {
forceFresh?: boolean;
databasePath?: string;
}): Promise<string>;
interface Logger {
info: (obj: unknown, message?: string) => void;
error: (obj: unknown, message?: string) => void;
warn: (obj: unknown, message?: string) => void;
debug: (obj: unknown, message?: string) => void;
trace: (obj: unknown, message?: string) => void;
}
declare function createLogger(component: string, meta?: Record<string, any>): Logger;
/**
* CORGI - Comprehensive Open Registry for Global Identification
* A TypeScript library for decoding and validating Vehicle Identification Numbers (VINs)
*
* @packageDocumentation
*/
/**
* Configuration options for creating a VIN decoder
*/
interface DecoderConfig {
/**
* Path to the VPIC database (optional - will use bundled database if not provided)
*/
databasePath?: string;
/**
* Force fresh database setup (ignore cache)
*/
forceFresh?: boolean;
/**
* Optional default decode options
*/
defaultOptions?: DecodeOptions;
/**
* Runtime environment (automatic detection if not specified)
*/
runtime?: 'node' | 'browser' | 'cloudflare';
}
/**
* Create a VIN decoder with the appropriate adapter for the current environment
*
* @param config - Decoder configuration (optional)
* @returns VIN decoder instance
*
* @example
* ```typescript
* import { createDecoder } from '@crdg/corgi';
*
* // Uses bundled database automatically
* const decoder = await createDecoder();
*
* // Or with explicit options
* const customDecoder = await createDecoder({
* databasePath: '/path/to/vpic.db',
* defaultOptions: {
* includePatternDetails: true
* }
* });
*
* const result = await decoder.decode('1HGCM82633A123456');
* ```
*/
declare function createDecoder(config?: DecoderConfig): Promise<VINDecoderWrapper>;
/**
* Wrapper for VIN decoder with simplified API
*/
declare class VINDecoderWrapper {
private decoder;
private defaultOptions;
/**
* Create a new VIN decoder wrapper
*
* @param adapter - Database adapter
* @param defaultOptions - Default decode options
*/
constructor(adapter: DatabaseAdapter, defaultOptions?: DecodeOptions);
/**
* Decode a VIN
*
* @param vin - The VIN to decode
* @param options - Optional decode options
* @returns Decoded VIN information
*/
decode(vin: string, options?: DecodeOptions): Promise<DecodeResult>;
/**
* Close the decoder and release resources
*/
close(): Promise<void>;
}
/**
* Get or create the shared decoder instance with default configuration
*
* @param config - Optional configuration overrides
* @returns Shared decoder instance
*
* @example
* ```typescript
* import { getDecoder } from '@crdg/corgi';
*
* // Uses default shared instance
* const decoder = await getDecoder();
* const result = await decoder.decode('1HGCM82633A123456');
* ```
*/
declare function getDecoder(config?: DecoderConfig): Promise<VINDecoderWrapper>;
/**
* Convenient way to decode a VIN using the shared decoder instance
*
* @param vin - The VIN to decode
* @param options - Optional decoding options
* @returns The decode result
*
* @example
* ```typescript
* import { quickDecode } from '@crdg/corgi';
*
* // Simple one-line decoding
* const result = await quickDecode('1HGCM82633A123456');
* console.log(result.components.vehicle);
* ```
*/
declare function quickDecode(vin: string, options?: DecodeOptions): Promise<DecodeResult>;
/**
* Decode a VIN using a provided database adapter
* This is a lower-level function for advanced uses
*
* @param vin The VIN to decode
* @param adapter The database adapter to use
* @param options Optional decoding options
* @returns The decode result
*
* @example
* ```typescript
* import { decodeVIN, NodeDatabaseAdapterFactory } from '@crdg/corgi';
*
* // Initialize the adapter
* const factory = new NodeDatabaseAdapterFactory();
* const adapter = await factory.createAdapter('/path/to/vpic.db');
*
* // Decode a VIN
* const result = await decodeVIN('1HGCM82633A123456', adapter, {
* includePatternDetails: true
* });
* ```
*/
declare function decodeVIN(vin: string, adapter: DatabaseAdapter, options?: DecodeOptions): Promise<DecodeResult>;
declare function initD1Adapter(d1: any): void;
declare global {
var __D1_FACTORY: ((db: string) => Promise<DatabaseAdapter>) | undefined;
}
export { DatabaseAdapter, DatabaseAdapterFactory, DecodeOptions, DecodeResult, type DecoderConfig, NodeDatabaseAdapter, NodeDatabaseAdapterFactory, QueryResult, VINDecoderWrapper, createDecoder, createLogger, decodeVIN, getDatabasePath, getDecoder, initD1Adapter, quickDecode };