Skip to content

Commit

Permalink
Feature: Add bit.ly as a shortening link - 545
Browse files Browse the repository at this point in the history
  • Loading branch information
pritam1322 committed Feb 9, 2025
1 parent 092ea21 commit 2083da0
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
88 changes: 88 additions & 0 deletions libraries/nestjs-libraries/src/short-linking/providers/bitly.ts
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))];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Empty } from '@gitroom/nestjs-libraries/short-linking/providers/empty';
import { ShortLinking } from '@gitroom/nestjs-libraries/short-linking/short-linking.interface';
import { Injectable } from '@nestjs/common';
import { ShortIo } from './providers/short.io';
import { Bitly } from '@gitroom/nestjs-libraries/short-linking/providers/bitly';

const getProvider = (): ShortLinking => {
if (process.env.DUB_TOKEN) {
Expand All @@ -13,6 +14,10 @@ const getProvider = (): ShortLinking => {
return new ShortIo();
}

if(process.env.BITLY_TOKEN){
return new Bitly();
}

return new Empty();
};

Expand Down

0 comments on commit 2083da0

Please sign in to comment.