UNPKG

@developers-joyride/shortify

Version:

High performance URL shortener library with multi-database support (MongoDB, SQLite, PostgreSQL, MySQL)

49 lines (48 loc) 1.28 kB
import { IUrl, IDatabaseAdapter } from "../interfaces/database.interface"; export interface ShortifyOptions { baseUrl: string; urlLength?: number; customUrlId?: string; expiresInDays?: number; } export interface ShortifyResult { originalUrl: string; shortUrl: string; urlId: string; expiresAt?: Date; } export declare class ShortenerService { private defaultUrlLength; private baseUrl; private dbAdapter; constructor(baseUrl: string, dbAdapter: IDatabaseAdapter); /** * Validate if a URL is properly formatted */ private isValidUrl; /** * Generate a short URL ID */ private generateUrlId; /** * Check if a URL ID already exists in the database * For performance, we use this to avoid duplicate URL IDs */ private findExistingUrlId; /** * Shorten a URL */ shorten(originalUrl: string, options?: Partial<ShortifyOptions>): Promise<ShortifyResult>; /** * Resolve a short URL to its original URL */ resolve(urlId: string): Promise<string | null>; /** * Get URL stats */ getUrlStats(urlId: string): Promise<Partial<IUrl> | null>; /** * Delete a shortened URL */ deleteUrl(urlId: string): Promise<boolean>; }