-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0669da1
Showing
4 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<!-- Write a readme file --> | ||
# tiny-url-generator | ||
|
||
A simple URL shortener written in node.js using [srtn.ga](https://srtn.ga/) as the API. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm install tiny-url-generator | ||
``` | ||
|
||
## Usage | ||
|
||
```javascript | ||
const ls = require('tiny-url-generator'); | ||
|
||
ls.generate({ | ||
url: "https://www.google.com", | ||
title: "Google" | ||
}) | ||
.then((res) => { | ||
console.log("Response: ", res); | ||
}) | ||
.catch((err) => { | ||
console.log("Error: ", err); | ||
}); | ||
``` | ||
|
||
### sample output | ||
```json | ||
{ | ||
"message": "OK", | ||
"status": true, | ||
"data": { | ||
"link": "http://srtn.ga/go/6znljT", | ||
"title": "Google", | ||
"timestamp": 1671390933, | ||
"expiry": 1671403277, | ||
"isAnalyticsEnabled": 0 | ||
} | ||
} | ||
``` | ||
|
||
|
||
## API Reference | ||
|
||
### generate() | ||
Generates a short URL | ||
#### Parameters | ||
* `title`<br/> | ||
The title of the URL. | ||
* `url`<br/> | ||
The URL to shorten. | ||
* `expiry` (optional)<br/> | ||
The expiry time of the URL. (0 = never) | ||
* `analytics_password` (Optional)<br/> | ||
The password for the analytics page (optional). if not provided, analytics will be disabled. | ||
#### Response | ||
The response is a JSON object with the following properties: | ||
* `link`<br/> | ||
The shortened URL. | ||
* `title`<br/> | ||
The title of the URL. | ||
* `expiry`<br/> | ||
The expiry time of the URL. | ||
* `analytics_link`<br/> | ||
The URL to the analytics page (optional) | ||
|
||
|
||
# Rate Limiting | ||
The API has a rate limit of 40 requests per minute. If you exceed this limit, you will receive a 429 Too Many Requests response. | ||
|
||
|
||
# Contributing | ||
Pull requests are welcome. For major changes, please open an issue. | ||
|
||
|
||
# License | ||
[MIT](https://choosealicense.com/licenses/mit/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const ls = require("./index"); | ||
|
||
ls.generate({ | ||
url: "https://www.google.com", | ||
title: "Google", | ||
expiry: 0, | ||
analytics_password: "password", | ||
}) | ||
.then((res) => { | ||
console.log("Response", res); | ||
}) | ||
.catch((err) => { | ||
console.log("Error", err); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const http = require("node:https"); | ||
const HOSTNAME = "srtn.ga"; | ||
const PORT = 443; | ||
const PATHNAME = "/API/Shorten"; | ||
|
||
module.exports.generate = async function (opts) { | ||
const { url, title = "", expiry = 0, analytics_password } = opts; | ||
if (!url) throw new Error("No URL provided"); | ||
if (!title) throw new Error("No Title provided"); | ||
|
||
const postData = JSON.stringify({ | ||
url, | ||
expiry, | ||
link_title: title, | ||
analytics_password, | ||
}); | ||
|
||
const options = { | ||
hostname: HOSTNAME, | ||
port: PORT, | ||
path: PATHNAME, | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
"Content-Length": Buffer.byteLength(postData), | ||
}, | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
const req = http.request(options, (res) => { | ||
res.setEncoding("utf8"); | ||
res.on("data", (chunk) => { | ||
try { | ||
let body = JSON.parse(chunk); | ||
if (body.status) { | ||
resolve(body); | ||
} else { | ||
reject(body); | ||
} | ||
} catch (e) { | ||
console.log(chunk); | ||
reject(e); | ||
} | ||
}); | ||
|
||
res.on("end", () => { | ||
resolve(res); | ||
}); | ||
|
||
req.on("error", (e) => { | ||
reject(e); | ||
}); | ||
}); | ||
|
||
req.write(postData); | ||
req.end(); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"name": "tiny-url-generator", | ||
"version": "1.0.0", | ||
"description": "A simple tiny url generator", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"no test specified\" && exit 0" | ||
}, | ||
"author": "abhishekjnvk", | ||
"license": "ISC", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/abhishekjnvk/tiny-url-generator" | ||
}, | ||
"keywords": [ | ||
"url", | ||
"url-shortener", | ||
"link-shortener", | ||
"link", | ||
"short-url", | ||
"tiny-url-generator", | ||
"tiny-url", | ||
"tinyurl" | ||
] | ||
} |