11import { ShortLinking } from '@gitroom/nestjs-libraries/short-linking/short-linking.interface' ;
22
3- const options = {
3+ const DUB_API_ENDPOINT = process . env . DUB_API_ENDPOINT || 'https://api.dub.co' ;
4+ const DUB_SHORT_LINK_DOMAIN = process . env . DUB_SHORT_LINK_DOMAIN || 'dub.sh' ;
5+
6+ const getOptions = ( ) => ( {
47 headers : {
58 Authorization : `Bearer ${ process . env . DUB_TOKEN } ` ,
69 'Content-Type' : 'application/json' ,
710 } ,
8- } ;
11+ } ) ;
912
1013export class Dub implements ShortLinking {
11- shortLinkDomain = 'dub.sh' ;
14+ shortLinkDomain = DUB_SHORT_LINK_DOMAIN ;
1215
1316 async linksStatistics ( links : string [ ] ) {
1417 return Promise . all (
1518 links . map ( async ( link ) => {
1619 const response = await (
17- await fetch ( `https://api.dub.co/ links/info?domain=${ this . shortLinkDomain } &key=${ link . split ( '/' ) . pop ( ) } ` , options )
20+ await fetch ( `${ DUB_API_ENDPOINT } / links/info?domain=${ this . shortLinkDomain } &key=${ link . split ( '/' ) . pop ( ) } ` , getOptions ( ) )
1821 ) . json ( ) ;
1922
2023 return {
@@ -29,8 +32,8 @@ export class Dub implements ShortLinking {
2932 async convertLinkToShortLink ( id : string , link : string ) {
3033 return (
3134 await (
32- await fetch ( `https://api.dub.co /links` , {
33- ...options ,
35+ await fetch ( `${ DUB_API_ENDPOINT } /links` , {
36+ ...getOptions ( ) ,
3437 method : 'POST' ,
3538 body : JSON . stringify ( {
3639 url : link ,
@@ -46,8 +49,8 @@ export class Dub implements ShortLinking {
4649 return await (
4750 await (
4851 await fetch (
49- `https://api.dub.co /links/info?domain=${ shortLink } ` ,
50- options
52+ `${ DUB_API_ENDPOINT } /links/info?domain=${ shortLink } ` ,
53+ getOptions ( )
5154 )
5255 ) . json ( )
5356 ) . url ;
@@ -60,8 +63,8 @@ export class Dub implements ShortLinking {
6063 ) : Promise < { short : string ; original : string ; clicks : string } [ ] > {
6164 const response = await (
6265 await fetch (
63- `https://api.dub.co /links?tenantId=${ id } &page=${ page } &pageSize=100` ,
64- options
66+ `${ DUB_API_ENDPOINT } /links?tenantId=${ id } &page=${ page } &pageSize=100` ,
67+ getOptions ( )
6568 )
6669 ) . json ( ) ;
6770
0 commit comments