Skip to content

Commit 402878c

Browse files
authored
Merge pull request #751 from eddywashere/selfhosted-dub-support
feat(short-linking): make Dub provider configurable for self-hosting
2 parents afd39b0 + 93464bf commit 402878c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,8 @@ POSTIZ_OAUTH_USERINFO_URL="https://authentik.example.com/application/o/userinfo"
100100
POSTIZ_OAUTH_CLIENT_ID=""
101101
POSTIZ_OAUTH_CLIENT_SECRET=""
102102
# POSTIZ_OAUTH_SCOPE="openid profile email" # default values
103+
104+
# Short Link Service Settings
105+
# DUB_TOKEN="" # Your self-hosted Dub API token
106+
# DUB_API_ENDPOINT="https://api.dub.co" # Your self-hosted Dub API endpoint
107+
# DUB_SHORT_LINK_DOMAIN="dub.sh" # Your self-hosted Dub domain

libraries/nestjs-libraries/src/short-linking/providers/dub.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { 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

1013
export 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

Comments
 (0)