Skip to content

Commit 6581a6c

Browse files
committed
add shortener
1 parent 254bc2b commit 6581a6c

3 files changed

Lines changed: 38 additions & 2 deletions

File tree

apps/client/src/app/[id]/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import debounce from "@/helpers/debounce";
1313
import editorLanguageOptions from "@/data/languages";
1414
import editorThemeOptions from "@/data/themes";
1515
import { getSession } from "@/service/api/session";
16+
import { shortenUrl } from "@/service/shortener";
1617
import toast from "react-hot-toast";
1718
import useSocket from "@/hooks/useSocket";
1819

@@ -51,9 +52,12 @@ export default function ShareCode() {
5152
}, [socket, sessionID]);
5253

5354
const handleCopyURL = async () => {
55+
const url = window.location.href;
56+
const shortenedUrl = await shortenUrl(url);
57+
5458
const type = "text/plain";
5559
const clipboardItemData = {
56-
[type]: window.location.href
60+
[type]: shortenedUrl
5761
};
5862
const clipboardItem = new ClipboardItem(clipboardItemData);
5963

apps/client/src/common/env.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ const NODE_ENV = process.env.NODE_ENV;
22
const PORT = Number(process.env.PORT) || 3023;
33
const API_URL = process.env.API_URL || "http://localhost:3123/api";
44
const WS_SERVER_URL = process.env.WS_SERVER_URL || "ws://localhost:3123";
5+
const SHORTENER_API_URL = process.env.SHORTENER_API_URL || "http://localhost:3024/api";
56

67
export const env = {
78
NODE_ENV,
89
PORT,
910
API_URL,
10-
WS_SERVER_URL
11+
WS_SERVER_URL,
12+
SHORTENER_API_URL
1113
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import axios from "axios";
2+
import { env } from "@/common/env";
3+
4+
const shortenerApi = axios.create({
5+
baseURL: env.SHORTENER_API_URL
6+
});
7+
8+
const authenticate = async () => {
9+
const { data, status } = await shortenerApi.get("/auth");
10+
11+
if (status === 200 && data.accessToken) {
12+
shortenerApi.defaults.headers.common["Authorization"] = `Bearer ${data.accessToken}`;
13+
}
14+
};
15+
16+
export const shortenUrl = async (url: string) => {
17+
try {
18+
if (!shortenerApi.defaults.headers.common["Authorization"]) {
19+
await authenticate();
20+
}
21+
22+
const { data, status } = await shortenerApi.post("/url/shorten", { url });
23+
24+
if (status === 200 && data.url) {
25+
return data.url;
26+
}
27+
} catch {
28+
return url;
29+
}
30+
};

0 commit comments

Comments
 (0)