@unitio-code/url-shortener
Version:
A simple URL shortening library
92 lines (91 loc) • 3.08 kB
TypeScript
/**
* Storage interface for URL mappings
*/
export interface StorageInterface {
get(id: number): Promise<string | undefined>;
set(id: number, url: string, ttl?: number): Promise<void>;
has(id: number): Promise<boolean>;
}
/**
* In-memory storage implementation
*/
export declare class MemoryStorage implements StorageInterface {
private storage;
get(id: number): Promise<string | undefined>;
set(id: number, url: string, ttl?: number): Promise<void>;
has(id: number): Promise<boolean>;
}
/**
* Redis storage implementation (optional)
*/
export declare class RedisStorage implements StorageInterface {
private client;
constructor(client: any);
get(id: number): Promise<string | undefined>;
set(id: number, url: string, ttl?: number): Promise<void>;
has(id: number): Promise<boolean>;
}
/**
* Validates if a string is a proper URL
*/
export declare function isValidUrl(url: string): boolean;
/**
* 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;
/**
* Default storage instance (in-memory)
*/
export declare const defaultStorage: MemoryStorage;
/**
* Stores a URL mapping using the specified storage
* @param id - The numeric ID
* @param originalUrl - The original URL
* @param storage - Storage instance to use
* @param ttl - Time to live in seconds (optional)
*/
export declare function storeUrlMapping(id: number, originalUrl: string, storage?: StorageInterface, ttl?: number): Promise<void>;
/**
* Retrieves the original URL for a given ID
* @param id - The numeric ID
* @param storage - Storage instance to use
* @returns The original URL if found, undefined otherwise
*/
export declare function getOriginalUrl(id: number, storage?: StorageInterface): Promise<string | undefined>;
/**
* Checks if a URL mapping exists
* @param id - The numeric ID
* @param storage - Storage instance to use
* @returns True if the mapping exists, false otherwise
*/
export declare function hasUrlMapping(id: number, storage?: StorageInterface): Promise<boolean>;
/**
* 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;