spidio-url-shortener
v1.1.0
Published
A Node.js client for Spid.io URL shortening service
Maintainers
Readme
Spidio URL Shortener
A Node.js client library for the Spid.io URL shortening service.
Installation
npm install spidio-url-shortenerUsage
Basic Usage
const { resolveUrl, createShortUrl } = require('spidio-url-shortener');
const options = {
host: 'api.spid.io',
key: 'your-api-key',
auth: 'your-bearer-token',
project: 'your-project-id'
};
// Create a short URL
const urlData = {
url: 'https://example.com/very-long-url',
// Add other required fields as per Spid.io API
};
createShortUrl(options, urlData)
.then(result => {
console.log('Short URL created:', result.data);
})
.catch(error => {
console.error('Error:', error.message);
});
// Resolve a short URL
resolveUrl(options)
.then(result => {
console.log('Original URL:', result.data);
})
.catch(error => {
console.error('Error:', error.message);
});Class-based Usage
const { SpidioUrlShortener } = require('spidio-url-shortener');
const shortener = new SpidioUrlShortener({
host: 'api.spid.io',
key: 'your-api-key',
auth: 'your-bearer-token',
project: 'your-project-id'
});
// Create a short URL
const urlData = {
url: 'https://example.com/very-long-url'
};
async function example() {
try {
const shortResult = await shortener.shorten(urlData);
console.log('Short URL:', shortResult.data);
const resolveResult = await shortener.resolve();
console.log('Original URL:', resolveResult.data);
} catch (error) {
console.error('Error:', error.message);
}
}
example();API Reference
createShortUrl(options, data)
Creates a shortened URL.
Parameters:
options(Object): Configuration objecthost(string): API hostkey(string): API keyauth(string): Bearer tokenproject(string): Project identifier
data(Object): URL data to shorten
Returns: Promise that resolves to an object containing the shortened URL data.
resolveUrl(options)
Resolves a shortened URL to get the original URL.
Parameters:
options(Object): Configuration object (same as above)
Returns: Promise that resolves to an object containing the original URL data.
SpidioUrlShortener
A class wrapper for easier usage.
Constructor:
options(Object): Configuration object
Methods:
shorten(data): Creates a shortened URLresolve(): Resolves a shortened URL
Error Handling
The library includes proper error handling. Failed requests will throw errors that you can catch and handle appropriately.
try {
const result = await createShortUrl(options, data);
console.log(result.data);
} catch (error) {
console.error('Failed to create short URL:', error.message);
}Requirements
- Node.js >= 14.0.0
- Valid Spid.io API credentials
License
MIT
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Support
For issues related to this package, please open an issue on GitHub. For Spid.io API documentation and support, visit their official documentation.
