@unitio-code/url-shortener
Version:
A simple URL shortening library
52 lines (51 loc) • 1.65 kB
TypeScript
/**
* Generates a short URL code from a numeric ID using base62 encoding
* @param id - The numeric ID to encode
* @returns The base62 encoded string
*/
export declare function encodeId(id: number): string;
/**
* Decodes a short URL code back to its numeric ID
* @param shortCode - The base62 encoded string
* @returns The decoded numeric ID
*/
export declare function decodeShortUrl(shortCode: string): number;
/**
* Configuration options for building a short URL
*/
export interface ShortUrlOptions {
domain: string;
includeRedirectPath?: boolean;
redirectPathSegment?: string;
includeProtocol?: boolean;
protocol?: string;
pathSeparator?: string;
}
/**
* Default options for building a short URL
*/
export declare const DEFAULT_SHORT_URL_OPTIONS: ShortUrlOptions;
/**
* In-memory storage for URL mappings
* Maps numeric IDs to their original URLs
*/
export declare const urlStorage: Map<number, string>;
/**
* Stores a URL mapping
* @param id - The numeric ID
* @param originalUrl - The original URL
*/
export declare function storeUrlMapping(id: number, originalUrl: string): void;
/**
* Retrieves the original URL for a given ID
* @param id - The numeric ID
* @returns The original URL if found, undefined otherwise
*/
export declare function getOriginalUrl(id: number): string | undefined;
/**
* Builds a complete short URL with domain and customizable options
* @param id - The numeric ID to encode
* @param options - Configuration options for the short URL
* @returns The complete short URL
*/
export declare function buildShortUrl(id: number, options?: string | ShortUrlOptions): string;