-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathdub.ts
More file actions
56 lines (51 loc) · 978 Bytes
/
dub.ts
File metadata and controls
56 lines (51 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import prisma from "@/lib/prisma";
import { nanoid } from "@dub/utils";
import { Link } from "@prisma/client";
import { Dub } from "dub";
import { getUrlWithRef } from "./utils";
export const dub = new Dub();
export async function shortenAndCreateLink({
url,
type,
projectId,
}: {
url: string;
type: "GITHUB" | "WEBSITE";
projectId: string;
}) {
const linkId = nanoid(24);
const { shortLink } = await dub.links.create({
url: getUrlWithRef(url),
externalId: linkId,
});
return await prisma.link.create({
data: {
id: linkId,
type,
url,
shortLink,
projectId,
},
});
}
export async function editShortLink({
link,
newUrl,
}: {
link: Link;
newUrl: string;
}) {
return await Promise.all([
dub.links.update(`ext_${link.id}`, {
url: getUrlWithRef(newUrl),
}),
prisma.link.update({
where: {
id: link.id,
},
data: {
url: newUrl,
},
}),
]);
}