File tree Expand file tree Collapse file tree 4 files changed +176
-0
lines changed
Expand file tree Collapse file tree 4 files changed +176
-0
lines changed Original file line number Diff line number Diff line change 1+ <!-- Write a readme file -->
2+ # tiny-url-generator
3+
4+ A simple URL shortener written in node.js using [ srtn.ga] ( https://srtn.ga/ ) as the API.
5+
6+ ## Installation
7+
8+ ``` bash
9+ npm install tiny-url-generator
10+ ```
11+
12+ ## Usage
13+
14+ ``` javascript
15+ const ls = require (' tiny-url-generator' );
16+
17+ ls .generate ({
18+ url: " https://www.google.com" ,
19+ title: " Google"
20+ })
21+ .then ((res ) => {
22+ console .log (" Response: " , res);
23+ })
24+ .catch ((err ) => {
25+ console .log (" Error: " , err);
26+ });
27+ ```
28+
29+ ### sample output
30+ ``` json
31+ {
32+ "message" : " OK" ,
33+ "status" : true ,
34+ "data" : {
35+ "link" : " http://srtn.ga/go/6znljT" ,
36+ "title" : " Google" ,
37+ "timestamp" : 1671390933 ,
38+ "expiry" : 1671403277 ,
39+ "isAnalyticsEnabled" : 0
40+ }
41+ }
42+ ```
43+
44+
45+ ## API Reference
46+
47+ ### generate()
48+ Generates a short URL
49+ #### Parameters
50+ * ` title ` <br />
51+ The title of the URL.
52+ * ` url ` <br />
53+ The URL to shorten.
54+ * ` expiry ` (optional)<br />
55+ The expiry time of the URL. (0 = never)
56+ * ` analytics_password ` (Optional)<br />
57+ The password for the analytics page (optional). if not provided, analytics will be disabled.
58+ #### Response
59+ The response is a JSON object with the following properties:
60+ * ` link ` <br />
61+ The shortened URL.
62+ * ` title ` <br />
63+ The title of the URL.
64+ * ` expiry ` <br />
65+ The expiry time of the URL.
66+ * ` analytics_link ` <br />
67+ The URL to the analytics page (optional)
68+
69+
70+ # Rate Limiting
71+ 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.
72+
73+
74+ # Contributing
75+ Pull requests are welcome. For major changes, please open an issue.
76+
77+
78+ # License
79+ [ MIT] ( https://choosealicense.com/licenses/mit/ )
Original file line number Diff line number Diff line change 1+ const ls = require ( "./index" ) ;
2+
3+ ls . generate ( {
4+ url : "https://www.google.com" ,
5+ title : "Google" ,
6+ expiry : 0 ,
7+ analytics_password : "password" ,
8+ } )
9+ . then ( ( res ) => {
10+ console . log ( "Response" , res ) ;
11+ } )
12+ . catch ( ( err ) => {
13+ console . log ( "Error" , err ) ;
14+ } ) ;
Original file line number Diff line number Diff line change 1+ const http = require ( "node:https" ) ;
2+ const HOSTNAME = "srtn.ga" ;
3+ const PORT = 443 ;
4+ const PATHNAME = "/API/Shorten" ;
5+
6+ module . exports . generate = async function ( opts ) {
7+ const { url, title = "" , expiry = 0 , analytics_password } = opts ;
8+ if ( ! url ) throw new Error ( "No URL provided" ) ;
9+ if ( ! title ) throw new Error ( "No Title provided" ) ;
10+
11+ const postData = JSON . stringify ( {
12+ url,
13+ expiry,
14+ link_title : title ,
15+ analytics_password,
16+ } ) ;
17+
18+ const options = {
19+ hostname : HOSTNAME ,
20+ port : PORT ,
21+ path : PATHNAME ,
22+ method : "POST" ,
23+ headers : {
24+ "Content-Type" : "application/json" ,
25+ "Content-Length" : Buffer . byteLength ( postData ) ,
26+ } ,
27+ } ;
28+
29+ return new Promise ( ( resolve , reject ) => {
30+ const req = http . request ( options , ( res ) => {
31+ res . setEncoding ( "utf8" ) ;
32+ res . on ( "data" , ( chunk ) => {
33+ try {
34+ let body = JSON . parse ( chunk ) ;
35+ if ( body . status ) {
36+ resolve ( body ) ;
37+ } else {
38+ reject ( body ) ;
39+ }
40+ } catch ( e ) {
41+ console . log ( chunk ) ;
42+ reject ( e ) ;
43+ }
44+ } ) ;
45+
46+ res . on ( "end" , ( ) => {
47+ resolve ( res ) ;
48+ } ) ;
49+
50+ req . on ( "error" , ( e ) => {
51+ reject ( e ) ;
52+ } ) ;
53+ } ) ;
54+
55+ req . write ( postData ) ;
56+ req . end ( ) ;
57+ } ) ;
58+ } ;
Original file line number Diff line number Diff line change 1+ {
2+ "name" : " tiny-url-generator" ,
3+ "version" : " 1.0.0" ,
4+ "description" : " A simple tiny url generator" ,
5+ "main" : " index.js" ,
6+ "scripts" : {
7+ "test" : " echo \" no test specified\" && exit 0"
8+ },
9+ "author" : " abhishekjnvk" ,
10+ "license" : " ISC" ,
11+ "repository" : {
12+ "type" : " git" ,
13+ "url" : " https://github.com/abhishekjnvk/tiny-url-generator"
14+ },
15+ "keywords" : [
16+ " url" ,
17+ " url-shortener" ,
18+ " link-shortener" ,
19+ " link" ,
20+ " short-url" ,
21+ " tiny-url-generator" ,
22+ " tiny-url" ,
23+ " tinyurl"
24+ ]
25+ }
You can’t perform that action at this time.
0 commit comments