From 40d600ab5bdf9e32cfa3077aba3502ffa87704a2 Mon Sep 17 00:00:00 2001 From: smfahim25 Date: Wed, 19 Mar 2025 10:45:42 +0600 Subject: [PATCH] update 00000 --- src/api/auth/auth.route.ts | 130 +++++++++++++++++++++++++++++-------- src/api/index.ts | 35 +++++++--- src/app.ts | 37 ++++++----- 3 files changed, 148 insertions(+), 54 deletions(-) diff --git a/src/api/auth/auth.route.ts b/src/api/auth/auth.route.ts index ab00a43..7632cd4 100644 --- a/src/api/auth/auth.route.ts +++ b/src/api/auth/auth.route.ts @@ -2,21 +2,36 @@ import Elysia, { t } from "elysia"; import { generateToken, getUserData, updateUser } from "./auth.controller"; import { verifyAuth } from "../../middlewares/auth.middlewares"; -export const authRoute = new Elysia({ - prefix: "/api/auth", - tags: ["Auth"], - detail: { - description: "Routes for managing users", - }, -}); +export const authRoute = new Elysia({ prefix: "/auth" }); authRoute.get( "/user/:userId", async ({ params: { userId } }) => await getUserData(userId), { + detail: { + tags: ["Auth"], + summary: "Get user data", + description: "Retrieve user data by user ID", + }, params: t.Object({ - userId: t.String(), + userId: t.String({ description: "The user ID" }), }), + response: { + 200: t.Object({ + status: t.Number(), + message: t.String(), + data: t.Object({ + // Define user data properties here + // For example: + id: t.String(), + // Add other fields + }), + }), + 404: t.Object({ + status: t.Number(), + message: t.String(), + }), + }, } ); @@ -24,37 +39,96 @@ authRoute.post( "/user/update/:userId", async ({ params: { userId }, body }) => await updateUser(userId, body), { + detail: { + tags: ["Auth"], + summary: "Update user data", + description: "Update user payment status and expiration date", + }, params: t.Object({ - userId: t.String(), + userId: t.String({ description: "The user ID to update" }), }), body: t.Object({ - paid_status: t.String(), - package_expire_date: t.String(), + paid_status: t.String({ description: "User payment status" }), + package_expire_date: t.String({ description: "Package expiration date" }), }), + response: { + 200: t.Object({ + status: t.Number(), + message: t.String(), + // Add other expected response properties + }), + 400: t.Object({ + status: t.Number(), + message: t.String(), + }), + }, } ); authRoute.get( "/generate-token/:userId", - async (context) => await generateToken(context) + async (context) => await generateToken(context), + { + detail: { + tags: ["Auth"], + summary: "Generate authentication token", + description: "Generate a new auth token for the specified user", + }, + params: t.Object({ + userId: t.String({ description: "The user ID" }), + }), + response: { + 200: t.Object({ + status: t.Number(), + message: t.String(), + token: t.String(), + }), + 400: t.Object({ + status: t.Number(), + message: t.String(), + }), + }, + } ); -authRoute.get("/user/me", async ({ cookie }) => { - const authData = await verifyAuth(cookie); - if (authData.status !== 200) { - return authData; - } else { - const userId: string | any = authData.userId; - const response = await getUserData(userId); - if (response?.status === 200) { - return { - ...response.data, - token: authData.token, - status: 200, - message: "User data fetched successfully", - }; +authRoute.get( + "/user/me", + async ({ cookie }) => { + const authData = await verifyAuth(cookie); + if (authData.status !== 200) { + return authData; } else { - return response; + const userId: string | any = authData.userId; + const response = await getUserData(userId); + if (response?.status === 200) { + return { + ...response.data, + token: authData.token, + status: 200, + message: "User data fetched successfully", + }; + } else { + return response; + } } + }, + { + detail: { + tags: ["Auth"], + summary: "Get current user data", + description: "Get currently authenticated user data from cookie", + }, + response: { + 200: t.Object({ + status: t.Number(), + message: t.String(), + token: t.String(), + // Add other user data properties + }), + 401: t.Object({ + status: t.Number(), + message: t.String(), + }), + }, } -}); +); diff --git a/src/api/index.ts b/src/api/index.ts index 05c586d..7f0bef9 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,14 +1,33 @@ -import Elysia from "elysia"; +import { Elysia } from "elysia"; import { projectRoutes } from "./project/project.route"; import { uploadRoutes } from "./upload/upload.route"; import { authRoute } from "./auth/auth.route"; import { downloadRoute } from "./downloadCount/download.count.route"; -export const api = new Elysia({}); -api.get("/", () => { - return "Hello from PlanPostAI Canvas API"; +export const api = new Elysia({ prefix: "/api" }) + .get("/", () => { + console.log("Root endpoint accessed"); + return "Hello from PlanPostAI Canvas API"; + }) + .get("/test", () => { + console.log("Test endpoint accessed"); + return "Test endpoint works!"; + }) + .use(authRoute) + .use(projectRoutes) + .use(uploadRoutes) + .use(downloadRoute) + .onError(({ code, error, set }) => { + console.error(`API Error: ${code}`, error); + if (code === "NOT_FOUND") { + set.status = 404; + return "API Endpoint Not Found"; + } + return "API Error Occurred"; + }); + +// Log all registered routes for debugging +console.log("API Routes registered:"); +api.routes.forEach((route) => { + console.log(`${route.method} ${api.prefix}${route.path}`); }); -api.use(authRoute); -api.use(projectRoutes); -api.use(uploadRoutes); -api.use(downloadRoute); diff --git a/src/app.ts b/src/app.ts index eedb6f3..9a0ed09 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,8 +1,7 @@ import { Elysia } from "elysia"; import swagger from "@elysiajs/swagger"; - -import { ENV } from "./config/env"; import cors from "@elysiajs/cors"; +import { ENV } from "./config/env"; import { api } from "./api"; const allowedOrigins = [ @@ -12,9 +11,7 @@ const allowedOrigins = [ "https://canvas.planpostai.com", ]; -const app = new Elysia({ - tags: ["Default"], -}) +const app = new Elysia() .use( cors({ origin: allowedOrigins, @@ -40,27 +37,31 @@ const app = new Elysia({ description: "Canvas API Documentation", }, tags: [ - { - name: "Projects", - description: "All APIs related to Projects", - }, - { - name: "Uploads", - description: "All APIs related to Uploads", - }, + { name: "Default", description: "Default endpoints" }, + { name: "Projects", description: "All APIs related to Projects" }, + { name: "Uploads", description: "All APIs related to Uploads" }, + { name: "Auth", description: "Authentication related endpoints" }, + { name: "Download", description: "Download count related endpoints" }, ], }, }) ) + .get("/", () => "PlanPostAI Canvas Server is running") + .use(api) .onError(({ code, error }) => { + console.error(`App Error: ${code}`, error); if (code === "NOT_FOUND") return "Not Found :("; - console.log("hello from app.ts under error"); - console.error(error); + return "An error occurred"; }); -// all routes here -app.use(api); - app.listen(ENV.SERVER_PORT, () => { console.log(`🦊 Elysia is running at ${ENV.SERVER_URL}:${ENV.SERVER_PORT}`); + console.log( + `Swagger docs available at ${ENV.SERVER_URL}:${ENV.SERVER_PORT}/docs` + ); + + console.log("All registered routes:"); + app.routes.forEach((route) => { + console.log(`${route.method} ${route.path}`); + }); });