-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add bit.ly as a shortening link - 545
- Loading branch information
1 parent
092ea21
commit 2083da0
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
libraries/nestjs-libraries/src/short-linking/providers/bitly.ts
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,88 @@ | ||
import { ShortLinking } from '@gitroom/nestjs-libraries/short-linking/short-linking.interface'; | ||
|
||
const options = { | ||
headers: { | ||
Authorization: `Bearer ${process.env.BITLY_TOKEN}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
|
||
export class Bitly implements ShortLinking { | ||
shortLinkDomain = 'bit.ly'; | ||
|
||
async linksStatistics(links: string[]) { | ||
return Promise.all( | ||
links.map(async (link) => { | ||
const linkId = link.split('/').pop(); | ||
const response = await ( | ||
await fetch(`https://api-ssl.bitly.com/v4/bitlinks/${linkId}`, options) | ||
).json(); | ||
|
||
const clicksResponse = await ( | ||
await fetch(`https://api-ssl.bitly.com/v4/bitlinks/${linkId}/clicks/summary`, options) | ||
).json(); | ||
|
||
return { | ||
short: link, | ||
original: response.long_url, | ||
clicks: clicksResponse.total_clicks || 0, | ||
}; | ||
}) | ||
); | ||
} | ||
|
||
async convertLinkToShortLink(id: string, link: string) { | ||
return ( | ||
await ( | ||
await fetch(`https://api-ssl.bitly.com/v4/shorten`, { | ||
...options, | ||
method: 'POST', | ||
body: JSON.stringify({ | ||
long_url: link, | ||
group_guid: id, | ||
domain: this.shortLinkDomain, | ||
}), | ||
}) | ||
).json() | ||
).link; | ||
} | ||
|
||
async convertShortLinkToLink(shortLink: string) { | ||
const linkId = shortLink.split('/').pop(); | ||
const response = await fetch(`https://api-ssl.bitly.com/v4/bitlinks/${linkId}`, options); | ||
const data = await response.json(); | ||
return data.long_url; | ||
} | ||
|
||
async getAllLinksStatistics( | ||
groupId: string, | ||
page = 1 | ||
): Promise<{ short: string; original: string; clicks: string }[]> { | ||
const response = await ( | ||
await fetch( | ||
`https://api-ssl.bitly.com/v4/groups/${groupId}/bitlinks?page=${page}&size=100`, | ||
options | ||
) | ||
).json(); | ||
|
||
const mapLinks = await Promise.all( | ||
response.links.map(async (link: any) => { | ||
const clicksResponse = await ( | ||
await fetch(`https://api-ssl.bitly.com/v4/bitlinks/${link.id}/clicks/summary`, options) | ||
).json(); | ||
|
||
return { | ||
short: link.link, | ||
original: link.long_url, | ||
clicks: clicksResponse.total_clicks || 0, | ||
}; | ||
}) | ||
); | ||
|
||
if (mapLinks.length < 100) { | ||
return mapLinks; | ||
} | ||
|
||
return [...mapLinks, ...(await this.getAllLinksStatistics(groupId, page + 1))]; | ||
} | ||
} |
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