UNPKG

spidio-url-shortener

Version:
150 lines (109 loc) 3.29 kB
# Spidio URL Shortener A Node.js client library for the Spid.io URL shortening service. ## Installation ```bash npm install spidio-url-shortener ``` ## Usage ### Basic Usage ```javascript 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 ```javascript 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 object - `host` (string): API host - `key` (string): API key - `auth` (string): Bearer token - `project` (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 URL - `resolve()`: Resolves a shortened URL ## Error Handling The library includes proper error handling. Failed requests will throw errors that you can catch and handle appropriately. ```javascript 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 1. Fork the repository 2. Create your feature branch (`git checkout -b feature/amazing-feature`) 3. Commit your changes (`git commit -m 'Add some amazing feature'`) 4. Push to the branch (`git push origin feature/amazing-feature`) 5. 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.