Skip to content

Commit 9496c6e

Browse files
committed
add example routes for Dub SDKs
1 parent 3d1380c commit 9496c6e

File tree

10 files changed

+2698
-25
lines changed

10 files changed

+2698
-25
lines changed

typescript/next/.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DUB_API_KEY=
2+
DUB_WORKSPACE_ID=

typescript/next/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.next
2+
.env

typescript/next/README.md

+34-21
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
1-
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
1+
# Dub with Next.js
2+
3+
This example shows how to use Dub with [Next.js](https://nextjs.org/).
24

35
## Getting Started
46

5-
First, run the development server:
7+
1. Add your Dub API key and Workspace ID to your `.env` file:
8+
9+
```shell
10+
cp .env.example .env
11+
```
12+
13+
```
14+
DUB_API_KEY=p4z...
15+
DUB_WORKSPACE_ID=ws_clrei...
16+
```
617

7-
```bash
8-
npm run dev
9-
# or
10-
yarn dev
11-
# or
12-
pnpm dev
13-
# or
14-
bun dev
18+
2. Install dependencies:
19+
20+
```shell
21+
pnpm install
1522
```
1623

17-
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
24+
3. Run the Next.js development server:
1825

19-
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
26+
```shell
27+
pnpm run dev
28+
```
2029

21-
This project uses [`next/font`](https://nextjs.org/docs/basic-features/font-optimization) to automatically optimize and load Inter, a custom Google Font.
30+
## Resources
2231

23-
## Learn More
32+
- [Dub API Reference](https://dub.co/docs/api-reference)
33+
- [Create API Key](https://app.dub.co/settings/tokens)
34+
- [Get Workspace ID](https://dub.co/help/article/how-to-get-workspace-id)
2435

25-
To learn more about Next.js, take a look at the following resources:
36+
## Contributing
2637

27-
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
28-
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
38+
Thanks for taking the time to contribute! Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody and are very appreciated.
2939

30-
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js/) - your feedback and contributions are welcome!
40+
Please try to create bug reports that are:
3141

32-
## Deploy on Vercel
42+
- _Reproducible._ Include steps to reproduce the problem.
43+
- _Specific._ Include as much detail as possible: which version, what environment, etc.
44+
- _Unique._ Do not duplicate existing opened issues.
45+
- _Scoped to a Single Bug._ One bug per report.
3346

34-
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
47+
## License
3548

36-
Check out our [Next.js deployment documentation](https://nextjs.org/docs/deployment) for more details.
49+
MIT License
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { dub } from "@/dub";
3+
import { ClicksTimeseries } from "dub/models/components";
4+
5+
export async function GET(req: NextRequest) {
6+
try {
7+
const result = await dub.analytics.retrieve({
8+
linkId: "clv3o9p9q000au1h0mc7r6l63",
9+
interval: "7d",
10+
event: "clicks",
11+
groupBy: "timeseries",
12+
});
13+
14+
const timeseries = result as ClicksTimeseries[];
15+
16+
return NextResponse.json(timeseries);
17+
} catch (error: any) {
18+
console.error(error);
19+
return NextResponse.json({ error: error.message }, { status: 400 });
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { dub } from "@/dub";
3+
4+
export async function POST(req: NextRequest) {
5+
try {
6+
const result = await dub.links.create({
7+
url: "https://www.google.com",
8+
externalId: "12345", // Optional
9+
});
10+
11+
return NextResponse.json(result);
12+
} catch (error: any) {
13+
console.error(error);
14+
return NextResponse.json({ error: error.message }, { status: 400 });
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { dub } from "@/dub";
3+
4+
export async function POST(req: NextRequest) {
5+
try {
6+
// Update a link by its linkId
7+
let result = await dub.links.update("clv3o9p9q000au1h0mc7r6l63", {
8+
url: "https://www.google.uk",
9+
});
10+
11+
// Update a link by its externalId
12+
result = await dub.links.update("ext_12345", {
13+
url: "https://www.google.uk",
14+
});
15+
16+
return NextResponse.json(result);
17+
} catch (error: any) {
18+
console.error(error);
19+
return NextResponse.json({ error: error.message }, { status: 400 });
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { NextRequest, NextResponse } from "next/server";
2+
import { dub } from "@/dub";
3+
4+
export async function POST(req: NextRequest) {
5+
try {
6+
const result = await dub.links.upsert({
7+
url: "https://www.google.com",
8+
});
9+
10+
return NextResponse.json(result);
11+
} catch (error: any) {
12+
console.error(error);
13+
return NextResponse.json({ error: error.message }, { status: 400 });
14+
}
15+
}

typescript/next/dub.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { Dub } from "dub";
2+
3+
export const dub = new Dub({
4+
token: process.env.DUB_API_KEY,
5+
workspaceId: process.env.DUB_WORKSPACE_ID,
6+
});

typescript/next/package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99
"lint": "next lint"
1010
},
1111
"dependencies": {
12+
"dub": "^0.30.1",
13+
"next": "14.2.4",
1214
"react": "^18",
13-
"react-dom": "^18",
14-
"next": "14.2.4"
15+
"react-dom": "^18"
1516
},
1617
"devDependencies": {
17-
"typescript": "^5",
1818
"@types/node": "^20",
1919
"@types/react": "^18",
2020
"@types/react-dom": "^18",
2121
"eslint": "^8",
22-
"eslint-config-next": "14.2.4"
22+
"eslint-config-next": "14.2.4",
23+
"typescript": "^5"
2324
}
2425
}

0 commit comments

Comments
 (0)