Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions api/create.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const sdk = require("node-appwrite");

// Initialize Appwrite client
const client = new sdk.Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT);

const database = new sdk.Databases(client);

export default async function handler(req, res) {
const { shortCode, longURL } = req.query;

console.log("Create URL: ", shortCode, "\n", "Long URL: ", longURL);

if (!shortCode && !longURL) {
Copy link

Copilot AI May 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition uses '&&' instead of '||'. This may allow one of the required parameters to be missing. Update the condition to 'if (!shortCode || !longURL)' to ensure both parameters are provided.

Suggested change
if (!shortCode && !longURL) {
if (!shortCode || !longURL) {

Copilot uses AI. Check for mistakes.
return res
.status(400)
.json({ error: "shortCode and longURL are required" });
}

try {
// Retrieve the document by shortCode
const result = await database.createDocument(
process.env.URL_DB,
process.env.URL_COL,
shortCode,
{
longURL: longURL,
}
);

// Redirect to the original URL
return res.status(201).json({
message: "URL created successfully",
data: result,
url: "https://go.hutotpn.live/" + shortCode,
});
} catch (error) {
return res.status(500).json({ error: error.message });
}
}
14 changes: 7 additions & 7 deletions api/redirect.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
const sdk = require("node-appwrite");

// Initialize Appwrite client
const client = new sdk.Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT);

const database = new sdk.Databases(client);

export default async function handler(req, res) {
const { shortCode } = req.query;

Expand All @@ -9,13 +16,6 @@ export default async function handler(req, res) {
return res.status(400).json({ error: "shortCode is required" });
}

// Initialize Appwrite client
const client = new sdk.Client()
.setEndpoint(process.env.APPWRITE_ENDPOINT)
.setProject(process.env.APPWRITE_PROJECT);

const database = new sdk.Databases(client);

try {
// Retrieve the document by shortCode
const result = await database.getDocument(
Expand Down
4 changes: 4 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
}
],
"routes": [
{
"src": "/api/(.*)",
"dest": "/api/$1"
},
{
"src": "/(.*)",
"dest": "/api/redirect.js?shortCode=$1"
Expand Down