Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekjnvk committed Dec 18, 2022
0 parents commit 0669da1
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
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/)
14 changes: 14 additions & 0 deletions example.js
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);
});
58 changes: 58 additions & 0 deletions index.js
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();
});
};
25 changes: 25 additions & 0 deletions package.json
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"
]
}

0 comments on commit 0669da1

Please sign in to comment.