Merge pull request #751 from eddywashere/selfhosted-dub-support

feat(short-linking): make Dub provider configurable for self-hosting
This commit is contained in:
egelhaus 2025-05-23 20:40:14 +02:00 committed by GitHub
commit 402878cf7e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 10 deletions

View File

@ -100,3 +100,8 @@ POSTIZ_OAUTH_USERINFO_URL="https://authentik.example.com/application/o/userinfo"
POSTIZ_OAUTH_CLIENT_ID=""
POSTIZ_OAUTH_CLIENT_SECRET=""
# POSTIZ_OAUTH_SCOPE="openid profile email" # default values
# Short Link Service Settings
# DUB_TOKEN="" # Your self-hosted Dub API token
# DUB_API_ENDPOINT="https://api.dub.co" # Your self-hosted Dub API endpoint
# DUB_SHORT_LINK_DOMAIN="dub.sh" # Your self-hosted Dub domain

View File

@ -1,20 +1,23 @@
import { ShortLinking } from '@gitroom/nestjs-libraries/short-linking/short-linking.interface';
const options = {
const DUB_API_ENDPOINT = process.env.DUB_API_ENDPOINT || 'https://api.dub.co';
const DUB_SHORT_LINK_DOMAIN = process.env.DUB_SHORT_LINK_DOMAIN || 'dub.sh';
const getOptions = () => ({
headers: {
Authorization: `Bearer ${process.env.DUB_TOKEN}`,
'Content-Type': 'application/json',
},
};
});
export class Dub implements ShortLinking {
shortLinkDomain = 'dub.sh';
shortLinkDomain = DUB_SHORT_LINK_DOMAIN;
async linksStatistics(links: string[]) {
return Promise.all(
links.map(async (link) => {
const response = await (
await fetch(`https://api.dub.co/links/info?domain=${this.shortLinkDomain}&key=${link.split('/').pop()}`, options)
await fetch(`${DUB_API_ENDPOINT}/links/info?domain=${this.shortLinkDomain}&key=${link.split('/').pop()}`, getOptions())
).json();
return {
@ -29,8 +32,8 @@ export class Dub implements ShortLinking {
async convertLinkToShortLink(id: string, link: string) {
return (
await (
await fetch(`https://api.dub.co/links`, {
...options,
await fetch(`${DUB_API_ENDPOINT}/links`, {
...getOptions(),
method: 'POST',
body: JSON.stringify({
url: link,
@ -46,8 +49,8 @@ export class Dub implements ShortLinking {
return await (
await (
await fetch(
`https://api.dub.co/links/info?domain=${shortLink}`,
options
`${DUB_API_ENDPOINT}/links/info?domain=${shortLink}`,
getOptions()
)
).json()
).url;
@ -60,8 +63,8 @@ export class Dub implements ShortLinking {
): Promise<{ short: string; original: string; clicks: string }[]> {
const response = await (
await fetch(
`https://api.dub.co/links?tenantId=${id}&page=${page}&pageSize=100`,
options
`${DUB_API_ENDPOINT}/links?tenantId=${id}&page=${page}&pageSize=100`,
getOptions()
)
).json();