Package Exports
- @developers-joyride/shortify
- @developers-joyride/shortify/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@developers-joyride/shortify) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Shortify
A high-performance URL shortener library for Node.js, written in TypeScript with MongoDB as the backend.
Features
- ⚡️ High performance: Optimized for high throughput
- 🔒 Type safety: Full TypeScript support
- 📈 Analytics: Track clicks and usage stats for your shortened URLs
- ⌛️ Expiration support: Set expiration dates for your shortened URLs
- 🛠️ Customizable: Use custom URL IDs or auto-generated ones
- 🔄 Automatic retry: Built-in MongoDB connection retry with exponential backoff
- 🧠 Caching: Automatically avoids duplicating shortened URLs
Installation
# Using npm
npm install shortify
# Using yarn
yarn add shortifyUsage
Basic Usage
import Shortify from "shortify";
import dotenv from "dotenv";
// Load environment variables
dotenv.config();
// Create a new Shortify instance
const shortify = new Shortify(
"https://your-domain.com/", // Base URL for your shortened links
process.env.MONGODB_URI || "mongodb://localhost:27017/shortify" // MongoDB connection URI
);
// Connect to MongoDB
await shortify.connect();
// Shorten a URL
const result = await shortify.shorten(
"https://example.com/very-long-url-that-needs-shortening"
);
console.log(`Shortened URL: ${result.shortUrl}`); // https://your-domain.com/abc123
// Resolve a shortened URL
const originalUrl = await shortify.resolve("abc123");
console.log(`Original URL: ${originalUrl}`); // https://example.com/very-long-url-that-needs-shortening
// Get statistics for a shortened URL
const stats = await shortify.getStats("abc123");
console.log(`Clicks: ${stats?.clicks}`);
// Delete a shortened URL
await shortify.delete("abc123");
// Disconnect when done
await shortify.disconnect();Advanced Options
// Shorten a URL with custom options
const result = await shortify.shorten("https://example.com", {
customUrlId: "custom-id", // Use a custom ID instead of auto-generated one
urlLength: 6, // Set length of auto-generated ID (ignored if customUrlId is set)
expiresInDays: 30, // URL will expire after 30 days
baseUrl: "https://short.ly/", // Override the base URL for this specific link
});
// Create with connection options
const shortify = new Shortify(
"https://your-domain.com/",
"mongodb://localhost:27017/shortify",
{
maxRetries: 5, // Maximum number of connection retry attempts
retryDelay: 5000, // Initial delay between retries in ms (doubles with each retry)
}
);API Reference
Shortify Class
Constructor
constructor(baseUrl: string, mongoUri: string, options?: { maxRetries?: number, retryDelay?: number })Methods
connect(): Connect to MongoDBdisconnect(): Disconnect from MongoDBisReady(): Check if the connection is establishedshorten(url: string, options?: ShortifyOptions): Shorten a URLresolve(urlId: string): Resolve a short URL to its original URLgetStats(urlId: string): Get statistics for a shortened URLdelete(urlId: string): Delete a shortened URL
ShortifyOptions Interface
interface ShortifyOptions {
baseUrl?: string; // Override the base URL
urlLength?: number; // Length of the generated URL ID
customUrlId?: string; // Custom URL ID
expiresInDays?: number; // Number of days until the URL expires
}License
MIT