Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Refactor to use `dub` TypeScript SDK

Steven Tey8bb9b8358c6b
diff --git a/apps/web/app/api/desktop/video/create/route.ts b/apps/web/app/api/desktop/video/create/route.ts
index c7bd9ac46..fa65d9794 100644
--- a/apps/web/app/api/desktop/video/create/route.ts
+++ b/apps/web/app/api/desktop/video/create/route.ts
@@ -4,6 +4,7 @@ import { videos } from "@cap/database/schema";
import { getCurrentUser } from "@cap/database/auth/session";
import { nanoId } from "@cap/database/helpers";
import { cookies } from "next/headers";
+import { dub } from "@/utils/dub";
const allowedOrigins = [
process.env.NEXT_PUBLIC_URL,
@@ -91,23 +92,11 @@ export async function GET(req: NextRequest) {
process.env.NEXT_PUBLIC_IS_CAP &&
process.env.NEXT_PUBLIC_ENVIRONMENT === "production"
) {
- const dubOptions = {
- method: "POST",
- headers: {
- Authorization: `Bearer ${process.env.DUB_API_KEY}`,
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- url: process.env.NEXT_PUBLIC_URL + "/s/" + id,
- key: id,
- domain: "cap.link",
- }),
- };
-
- await fetch("https://api.dub.co/links?projectSlug=cap", dubOptions)
- .then((response) => response.json())
- .then((response) => console.log(response))
- .catch((err) => console.error(err));
+ await dub.links.create({
+ url: process.env.NEXT_PUBLIC_URL + "/s/" + id,
+ domain: "cap.link",
+ key: id,
+ });
}
return new Response(
diff --git a/apps/web/app/api/video/analytics/route.ts b/apps/web/app/api/video/analytics/route.ts
index 699832e99..ac861fe7b 100644
--- a/apps/web/app/api/video/analytics/route.ts
+++ b/apps/web/app/api/video/analytics/route.ts
@@ -1,3 +1,5 @@
+import { dub } from "@/utils/dub";
+import { ClicksCount } from "dub/models/components";
import { NextRequest } from "next/server";
export const revalidate = 300;
@@ -15,18 +17,10 @@ export async function GET(request: NextRequest) {
});
}
- const dubOptions = {
- method: "GET",
- headers: {
- Authorization: `Bearer ${process.env.DUB_API_KEY}`,
- "Content-Type": "application/json",
- },
- };
-
- const analytics = await fetch(
- `https://api.dub.co/analytics/clicks?projectSlug=cap&domain=cap.link&key=${videoId}`,
- dubOptions
- ).then((response) => response.json());
+ const { clicks: analytics } = (await dub.analytics.retrieve({
+ domain: "cap.link",
+ key: videoId,
+ })) as ClicksCount;
if (typeof analytics !== "number") {
return new Response(JSON.stringify({ error: true }), {
diff --git a/apps/web/package.json b/apps/web/package.json
index eb3b5661a..7121c8d46 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -33,6 +33,7 @@
"date-fns": "^3.2.0",
"dotenv": "^16.3.1",
"drizzle-orm": "0.30.9",
+ "dub": "^0.33.1",
"hls.js": "^1.5.3",
"lodash": "^4.17.21",
"lucide-react": "^0.294.0",
diff --git a/apps/web/utils/dub.ts b/apps/web/utils/dub.ts
new file mode 100644
index 000000000..bd35ce65d
--- /dev/null
+++ b/apps/web/utils/dub.ts
@@ -0,0 +1,5 @@
+import { Dub } from "dub";
+
+export const dub = new Dub({
+ token: process.env.DUB_WORKSPACE_API_KEY,
+});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 292cd5411..4331d5acc 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -368,6 +368,9 @@ importers:
drizzle-orm:
specifier: 0.30.9
+ dub:
+ specifier: ^0.33.1
+ version: 0.33.1([email protected])
hls.js:
specifier: ^1.5.3
version: 1.5.3
@@ -9291,6 +9294,14 @@ packages:
react: 18.2.0
dev: false
+ resolution: {integrity: sha512-R0X0z9+nPCLs7J62kH2K3tX5L6fNigsPsw/Ch6zvfMy3hgSuSrA6idGQ474e/kJQdXXSi8T4hZsAPy1UzTZHAA==}
+ peerDependencies:
+ zod: '>= 3'
+ dependencies:
+ zod: 3.22.4
+ dev: false
+
resolution: {integrity: sha512-I88TYZWc9XiYHRQ4/3c5rjjfgkjhLyW2luGIheGERbNQ6OY7yTybanSpDXZa8y7VUP9YmDcYa+eyq4ca7iLqWA==}
@@ -10007,7 +10018,7 @@ packages:
doctrine: 2.1.0
eslint: 8.57.0
eslint-import-resolver-node: 0.3.9
- eslint-module-utils: 2.8.0(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])
+ eslint-module-utils: 2.8.0(@typescript-eslint/[email protected])([email protected])([email protected])([email protected])
hasown: 2.0.0
is-core-module: 2.13.1
is-glob: 4.0.3