@the-node-forge/url-shortener
Version:
A URL shortener that generates and stores unique aliases for long URLs, with optional expiration and custom alias support.
48 lines (47 loc) • 1.84 kB
TypeScript
import { type ShortenOptions, type StoreAdapter } from '../types/types';
/**
* URLShortener provides methods to shorten URLs and resolve aliases.
* All methods are asynchronous and include robust error handling.
*/
export declare class URLShortener {
private readonly baseDomain;
private readonly store;
private static readonly DEFAULT_CODE_LENGTH;
private static readonly CHAR_SET;
/**
* Create a new URLShortener instance.
* @param baseDomain - Domain used for returned shortened URLs (e.g., "https://sho.rt")
* @param store - Redis-backed implementation of the StoreAdapter interface
*/
constructor(baseDomain: string, store: StoreAdapter);
/**
* Shortens a long URL into a shorter one with an alias.
* - Generates alias if not provided
* - Validates the URL
* - Checks for collisions
* - Supports optional expiration time and override flag
*
* @param longUrl - The full, original URL to shorten
* @param options - Optional config (alias, override, expiresIn)
* @returns A Promise that resolves to the full shortened URL string
*/
shorten(longUrl: string, options?: ShortenOptions): Promise<string>;
/**
* Resolves a short alias back to its original long URL.
* - If alias doesn’t exist, returns null
* - If alias exists but expired, deletes it and returns null
*
* @param alias - The short code to resolve
* @returns The original long URL or null if not found/expired
*/
resolve(alias: string): Promise<string | null>;
/**
* Helper method to validate a URL string using the built-in URL constructor.
*/
private isValidUrl;
/**
* Generates a random alphanumeric code of the specified length.
* Used for alias generation.
*/
private generateCode;
}