UNPKG

@developers-joyride/shortify

Version:

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

102 lines (101 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseFactory = exports.Shortify = void 0; const shortener_service_1 = require("./services/shortener.service"); const database_factory_1 = require("./factories/database.factory"); // Main class that bundles all functionality class Shortify { /** * Create a new Shortify instance * @param baseUrl - Base URL for shortened links * @param dbConfig - Database configuration */ constructor(baseUrl, dbConfig) { this.isConnected = false; this.dbAdapter = database_factory_1.DatabaseFactory.createAdapter(dbConfig); this.shortenerService = new shortener_service_1.ShortenerService(baseUrl, this.dbAdapter); // Handle DB connection events this.dbAdapter.on("connected", () => { this.isConnected = true; }); this.dbAdapter.on("disconnected", () => { this.isConnected = false; }); this.dbAdapter.on("error", (err) => { console.error("Shortify DB error:", err); }); } /** * Create a new Shortify instance with MongoDB (backward compatibility) * @param baseUrl - Base URL for shortened links * @param mongoUri - MongoDB connection URI * @param options - Additional options for MongoDB connection */ static createWithMongo(baseUrl, mongoUri, options = {}) { return new Shortify(baseUrl, { type: "mongodb", connectionString: mongoUri, maxRetries: options.maxRetries, retryDelay: options.retryDelay, collectionName: options.collectionName, }); } /** * Connect to database */ async connect() { return this.dbAdapter.connect(); } /** * Disconnect from database */ async disconnect() { return this.dbAdapter.disconnect(); } /** * Check connection status */ isReady() { return this.isConnected; } /** * Shorten a URL */ async shorten(url, options = {}) { if (!this.isConnected) { throw new Error("Database connection not established"); } return this.shortenerService.shorten(url, options); } /** * Resolve a short URL ID to its original URL */ async resolve(urlId) { if (!this.isConnected) { throw new Error("Database connection not established"); } return this.shortenerService.resolve(urlId); } /** * Get statistics for a shortened URL */ async getStats(urlId) { if (!this.isConnected) { throw new Error("Database connection not established"); } return this.shortenerService.getUrlStats(urlId); } /** * Delete a shortened URL */ async delete(urlId) { if (!this.isConnected) { throw new Error("Database connection not established"); } return this.shortenerService.deleteUrl(urlId); } } exports.Shortify = Shortify; var database_factory_2 = require("./factories/database.factory"); Object.defineProperty(exports, "DatabaseFactory", { enumerable: true, get: function () { return database_factory_2.DatabaseFactory; } }); exports.default = Shortify;