url-shortener-js
v1.0.0
Published
A lightweight JavaScript URL shortener library
Maintainers
Readme
URL Shortener
A lightweight JavaScript library for shortening URLs with a simple and consistent API. The library currently uses TinyURL and is designed to be easy to extend with additional providers in future releases.
Features
- Validate URLs before shortening them.
- Generate short links using TinyURL.
- Simple API with modern ES Modules.
- Modular structure that makes adding new providers straightforward.
Installation
npm install url-shortener-js Usage
import { shorten, validate } from "url-shortener";const url = "https://github.com";
if (validate(url)) { const shortUrl = await shorten(url); console.log(shortUrl); }
API
"validate(url)"
Returns "true" if the given URL is valid. Otherwise, it returns "false".
validate("https://github.com"); // true
validate("github"); // false
"shorten(url, options)"
Creates a shortened URL and returns it as a string.
By default, the library uses TinyURL.
const shortUrl = await shorten("https://github.com");
You can also choose a provider.
const shortUrl = await shorten("https://github.com", { provider: "tinyurl" });
Error Handling
The library throws custom errors when an invalid URL is provided or when an unsupported provider is selected.
try { const shortUrl = await shorten(url); } catch (error) { console.error(error); }
Project Structure
src/
├── constants/
├── errors/
├── providers/
├── utils/
├── index.js
├── shorten.js
└── validate.jsLicense
This project is licensed under the MIT License. See the LICENSE file for details.
