1- import axios from 'axios' ;
2-
3-
4-
5- type providers = 'isgd' | 'cdpt' | 'vgd' | '4hnet' | 'tinube' | 'rbgy' | 'vurl' | 'haha' | 'pwm' | 'cya' | 'hideuri' | 'tnyim' ;
1+ import axios from 'axios'
2+
3+ type providers =
4+ | 'isgd'
5+ | 'cdpt'
6+ | 'vgd'
7+ | '4hnet'
8+ | 'tinube'
9+ | 'rbgy'
10+ | 'vurl'
11+ | 'haha'
12+ | 'pwm'
13+ | 'cya'
14+ | 'hideuri'
15+ | 'tnyim'
616
717/**
818 *
919 *
1020 * @interface IResponse
1121 */
1222interface IResponse {
13- longUrl : string ;
14- shortUrl : string ;
23+ longUrl : string
24+ shortUrl : string
1525}
1626
1727/**
@@ -20,8 +30,8 @@ interface IResponse {
2030 * @interface IOptions
2131 */
2232interface IOptions {
23- provider : providers ;
24- timeout ?: number ;
33+ provider : providers
34+ timeout ?: number
2535}
2636
2737/**
@@ -31,56 +41,42 @@ interface IOptions {
3141 * @interface IProviders
3242 */
3343interface IProviders {
34- url : string ;
35- method : string ;
36- body ?: any ;
37- formData ?: boolean ;
44+ url : string
45+ method : string
46+ body ?: any
47+ formData ?: boolean
3848}
3949
40-
41-
4250/**
4351 *
4452 *
4553 * @param {Record<any, any> } response
4654 * @param {string } longUrl
4755 * @return {* } {IResponse}
4856 */
49- function responseMap ( response : Record < any , any > , longUrl : string , provider : providers ) : IResponse {
50- const responseType = response . headers [ 'content-type' ] . split ( ';' ) [ 0 ] ;
51- if ( ! [ 'text/plain' , 'text/html' ] . includes ( responseType ) ) {
52- if ( response . data ?. url ) {
53- return { longUrl, shortUrl : response . data . url } ;
54- } else if ( response . data ?. shortUrl ) {
55- return { longUrl, shortUrl : response . data . shortUrl } ;
56- } else if ( response . data ?. shortenedUrl ) {
57- return { longUrl, shortUrl : response . data . shortenedUrl } ;
58- } else if ( response . data ?. short ) {
59- return { longUrl, shortUrl : response . data . short } ;
60- } else if ( response . data ?. short_url ) {
61- return { longUrl, shortUrl : response . data . short_url } ;
62- } else if ( response . data ?. result_url ) {
63- return { longUrl, shortUrl : response . data . result_url } ;
64- }
65- }
66-
67- if ( [ 'haha' , 'pwm' , 'cya' ] . includes ( provider ) ) {
68-
69-
70- return { longUrl, shortUrl : `https://${ provider } .se/${ response . data } ` } ;
71-
72- }
73-
74-
75- return { longUrl, shortUrl : response . data } ;
76-
77-
78-
79-
57+ function responseMap ( response : Record < any , any > , longUrl : string , provider : providers ) : IResponse {
58+ const responseType = response . headers [ 'content-type' ] . split ( ';' ) [ 0 ]
59+ if ( ! [ 'text/plain' , 'text/html' ] . includes ( responseType ) ) {
60+ if ( response . data ?. url )
61+ return { longUrl, shortUrl : response . data . url }
62+ else if ( response . data ?. shortUrl )
63+ return { longUrl, shortUrl : response . data . shortUrl }
64+ else if ( response . data ?. shortenedUrl )
65+ return { longUrl, shortUrl : response . data . shortenedUrl }
66+ else if ( response . data ?. short )
67+ return { longUrl, shortUrl : response . data . short }
68+ else if ( response . data ?. short_url )
69+ return { longUrl, shortUrl : response . data . short_url }
70+ else if ( response . data ?. result_url )
71+ return { longUrl, shortUrl : response . data . result_url }
72+ }
73+
74+ if ( [ 'haha' , 'pwm' , 'cya' ] . includes ( provider ) )
75+ return { longUrl, shortUrl : `https://${ provider } .se/${ response . data } ` }
76+
77+ return { longUrl, shortUrl : response . data }
8078}
8179
82-
83-
8480/**
8581 *
8682 *
@@ -89,83 +85,84 @@ function responseMap(response: Record<any, any>, longUrl: string,provider: provi
8985 * @return {* }
9086 */
9187const getAxios = ( provider : IProviders , longUrl : string , option : IOptions ) => {
92- if ( provider . method === 'get' ) {
93- return axios . get ( provider . url + longUrl , { timeout : option . timeout } ) ;
94- } else {
95- return axios . post ( provider . url , provider . body ( longUrl ) , {
96- timeout : option . timeout ,
97- } ) ;
98- }
99- } ;
88+ if ( provider . method === 'get' ) {
89+ return axios . get ( provider . url + longUrl , { timeout : option . timeout } )
90+ }
91+ else {
92+ return axios . post ( provider . url , provider . body ( longUrl ) , {
93+ timeout : option . timeout ,
94+ } )
95+ }
96+ }
10097
10198const ValidProviders : Record < string , IProviders > = {
102- isgd : {
103- url : 'https://is.gd/create.php?format=simple&url=' ,
104- method : 'get' ,
105- } ,
106- cdpt : {
107- url : 'https://cdpt.in/shorten?url=' ,
108- method : 'get' ,
109- } ,
110- vgd : {
111- url : 'https://v.gd/create.php?format=simple&url=' ,
112- method : 'get' ,
113- } ,
114- haha : {
115- url : 'https://www.haha.se/lank.php?dataLank=' ,
116- method : 'get' ,
117- } ,
118-
119- cya : {
120- url : 'https://www.haha.se/lank.php?dataLank=' ,
121- method : 'get' ,
122- } ,
123-
124- pwm : {
125- url : 'https://www.haha.se/lank.php?dataLank=' ,
126- method : 'get' ,
127- } ,
128-
129- tnyim : {
130- url : 'http://tny.im/yourls-api.php?action=shorturl&format=simple&url=' ,
131- method : 'get' ,
132- } ,
133-
134- '4hnet' : {
135- url : 'https://4h.net/api/?url=' ,
136- method : 'get' ,
137- } ,
138-
139- vurl : {
140- url : 'https://vurl.com/api.php?url=' ,
141- method : 'get' ,
142- } ,
143- // POST APIS
144-
145- tinube : {
146- url : 'https://api.tinu.be/api/shorten' ,
147- method : 'post' ,
148- body : ( val : string ) => {
149- return { longUrl : val } ;
150- } ,
151- } ,
152-
153- hideuri : {
154- url : 'https://hideuri.com/api/v1/shorten' ,
155- method : 'post' ,
156- body : ( val : string ) => {
157- return { url : val } ;
158- } ,
159- } ,
160-
161- rbgy : {
162- url : 'https://free-url-shortener.rb.gy/shorten' ,
163- method : 'post' ,
164- body : ( val : string ) => {
165- return { destination : val , " dryRun" : true }
166- } ,
167- } ,
168- } ;
99+ ' isgd' : {
100+ url : 'https://is.gd/create.php?format=simple&url=' ,
101+ method : 'get' ,
102+ } ,
103+ ' cdpt' : {
104+ url : 'https://cdpt.in/shorten?url=' ,
105+ method : 'get' ,
106+ } ,
107+ ' vgd' : {
108+ url : 'https://v.gd/create.php?format=simple&url=' ,
109+ method : 'get' ,
110+ } ,
111+ ' haha' : {
112+ url : 'https://www.haha.se/lank.php?dataLank=' ,
113+ method : 'get' ,
114+ } ,
115+
116+ ' cya' : {
117+ url : 'https://www.haha.se/lank.php?dataLank=' ,
118+ method : 'get' ,
119+ } ,
120+
121+ ' pwm' : {
122+ url : 'https://www.haha.se/lank.php?dataLank=' ,
123+ method : 'get' ,
124+ } ,
125+
126+ ' tnyim' : {
127+ url : 'http://tny.im/yourls-api.php?action=shorturl&format=simple&url=' ,
128+ method : 'get' ,
129+ } ,
130+
131+ '4hnet' : {
132+ url : 'https://4h.net/api/?url=' ,
133+ method : 'get' ,
134+ } ,
135+
136+ ' vurl' : {
137+ url : 'https://vurl.com/api.php?url=' ,
138+ method : 'get' ,
139+ } ,
140+ // POST APIS
141+
142+ ' tinube' : {
143+ url : 'https://api.tinu.be/api/shorten' ,
144+ method : 'post' ,
145+ body : ( val : string ) => {
146+ return { longUrl : val }
147+ } ,
148+ } ,
149+
150+ ' hideuri' : {
151+ url : 'https://hideuri.com/api/v1/shorten' ,
152+ method : 'post' ,
153+ body : ( val : string ) => {
154+ return { url : val }
155+ } ,
156+ } ,
157+
158+ ' rbgy' : {
159+ url : 'https://free-url-shortener.rb.gy/shorten' ,
160+ method : 'post' ,
161+ body : ( val : string ) => {
162+ return { destination : val , dryRun : true }
163+ } ,
164+ } ,
165+ }
169166
170167/**
171168 *
@@ -175,21 +172,12 @@ const ValidProviders: Record<string, IProviders> = {
175172 * @param {IOptions } option
176173 * @return {IResponse }
177174 */
178- const minify = async (
179- longUrl : string ,
180- option : IOptions = { provider : 'isgd' , timeout : 2000 } ,
175+ const minify = async (
176+ longUrl : string ,
177+ option : IOptions = { provider : 'isgd' , timeout : 2000 } ,
181178) : Promise < IResponse > => {
182- try {
183- const response = await getAxios (
184- ValidProviders [ option . provider ] ,
185- longUrl ,
186- option ,
187- ) ;
188- return responseMap ( response , longUrl , option . provider ) ;
189- } catch ( error ) {
190- throw error ;
191- }
192- } ;
193-
194-
195- export default minify ;
179+ const response = await getAxios ( ValidProviders [ option . provider ] , longUrl , option )
180+ return responseMap ( response , longUrl , option . provider )
181+ }
182+
183+ export default minify
0 commit comments