diff --git a/src/client/api/blogs.ts b/src/client/api/blogs.ts index c6b1b94..f1c7dea 100644 --- a/src/client/api/blogs.ts +++ b/src/client/api/blogs.ts @@ -1,6 +1,6 @@ // libapi/blogs.ts import type { Blog, CreateBlog, UpdateBlog } from "@shared/schema/blogSchema"; -import { blogSchema, createBlogSchema, updateBlogSchema, blogSearchResponseSchema, blogTitleSchema } from "@shared/schema/blogSchema"; +import { blogSchema, blogSearchResponseSchema, blogTitleSchema, updateBlogSchema } from "@shared/schema/blogSchema"; import { apiFetch } from "@shared/utils"; import { z } from "zod"; diff --git a/src/client/api/users.ts b/src/client/api/users.ts index 4f0c328..5cae14c 100644 --- a/src/client/api/users.ts +++ b/src/client/api/users.ts @@ -4,11 +4,7 @@ import { apiFetch } from "@shared/utils"; // Fetch ALL Users export const fetchUsers = async (): Promise> => { - const response = await apiFetch( - "/api/users", - { method: "GET" }, - selectUserSchema.array() - ); + const response = await apiFetch("/api/users", { method: "GET" }, selectUserSchema.array()); return response.data; }; @@ -20,7 +16,7 @@ export const fetchUser = async (id: string): Promise => { credentials: "include", headers: { "Content-Type": "application/json" }, }, - selectUserSchema + selectUserSchema, ); return response.data; }; @@ -34,7 +30,7 @@ export const createUser = async (newUser: InsertUser): Promise => { body: JSON.stringify(newUser), headers: { "Content-Type": "application/json" }, }, - selectUserSchema + selectUserSchema, ); return response.data; }; @@ -48,7 +44,7 @@ export const updateUser = async (updatedUser: UpdateUser): Promise = body: JSON.stringify(updatedUser), headers: { "Content-Type": "application/json" }, }, - selectUserSchema + selectUserSchema, ); return response.data; }; @@ -60,7 +56,7 @@ export const deleteUser = async (userId: number): Promise => { method: "DELETE", headers: { "Content-Type": "application/json" }, }, - deleteUserSchema + deleteUserSchema, ); return response.data; }; diff --git a/src/client/hooks/AuthContext.tsx b/src/client/hooks/AuthContext.tsx index 236062a..0d948d5 100644 --- a/src/client/hooks/AuthContext.tsx +++ b/src/client/hooks/AuthContext.tsx @@ -1,6 +1,6 @@ +import { apiFetch } from "@/shared/utils"; import React, { createContext, useContext, useEffect, useState } from "react"; import type { ReactNode } from "react"; -import { apiFetch } from "@/shared/utils"; import { z } from "zod"; export interface AuthContextType { @@ -39,7 +39,7 @@ export const AuthProvider: React.FC = ({ children }) => { z.object({ id: z.string(), username: z.string(), - }) + }), ); console.log("ID set to ", user.data.id); setId(user.data.id); @@ -81,11 +81,7 @@ export const AuthProvider: React.FC = ({ children }) => { } }; - return ( - - {children} - - ); + return {children}; }; export const useAuth = (): AuthContextType => { diff --git a/src/client/hooks/useProfile.ts b/src/client/hooks/useProfile.ts index 5859298..f3b6579 100644 --- a/src/client/hooks/useProfile.ts +++ b/src/client/hooks/useProfile.ts @@ -1,6 +1,6 @@ -import { apiFetch } from "@/shared/utils"; import { profileSchema } from "@/shared/schema/profileSchema"; import type { Profile } from "@/shared/schema/profileSchema"; +import { apiFetch } from "@/shared/utils"; import { useQuery } from "@tanstack/react-query"; export const useProfile = () => { diff --git a/src/client/routes/profile.tsx b/src/client/routes/profile.tsx index 1124945..da4d55e 100644 --- a/src/client/routes/profile.tsx +++ b/src/client/routes/profile.tsx @@ -18,7 +18,7 @@ export const Route = createFileRoute("/profile")({ resources: false, settings: false, }); - const { errorMessage, isAuthenticated, logout } = useAuth(); + const { errorMessage, logout } = useAuth(); const navigate = useNavigate(); if (isLoading) return
Loading...
; diff --git a/src/server/api/auth.ts b/src/server/api/auth.ts index 92044b8..7459d67 100644 --- a/src/server/api/auth.ts +++ b/src/server/api/auth.ts @@ -1,10 +1,10 @@ import { db } from "@/server/db/db"; +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import * as Schema from "@db/tables"; import bcrypt from "bcryptjs"; import { eq } from "drizzle-orm"; import { Hono } from "hono"; import { generateIdFromEntropySize } from "lucia"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; const { compare, genSalt, hash } = bcrypt; @@ -99,15 +99,10 @@ authRoutes.post("/auth/login", async (c) => { createSession(session_id, user[0].id); // Set cookie here c.header("Set-Cookie", `sessionId=${session_id}; Path=/; HttpOnly; Secure; Max-Age=3600; SameSite=Strict`); - return createSuccessResponse( - c, - { sessionId: session_id }, - "Successfully logged in" - ); + return createSuccessResponse(c, { sessionId: session_id }, "Successfully logged in"); } }); - // Logout route authRoutes.post("/auth/logout", async (c) => { const sessionId = c.req.header("Cookie")?.match(/sessionId=([^;]*)/)?.[1]; @@ -120,11 +115,7 @@ authRoutes.post("/auth/logout", async (c) => { // delete the session id row from the table await db.delete(Schema.sessions).where(eq(Schema.sessions.id, sessionId)); - return createSuccessResponse( - c, - { success: true }, - "Successfully logged out" - ); + return createSuccessResponse(c, { success: true }, "Successfully logged out"); } catch (error) { console.log(error); return createErrorResponse(c, "LOGOUT_ERROR", "Error logging out", 500); diff --git a/src/server/api/contact.ts b/src/server/api/contact.ts index 77da2a0..97f23d6 100644 --- a/src/server/api/contact.ts +++ b/src/server/api/contact.ts @@ -1,5 +1,5 @@ +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import { Hono } from "hono"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; // Sleep function that behaves synchronously in an async function const sleep = (delay: number) => new Promise((resolve) => setTimeout(resolve, delay)); diff --git a/src/server/api/events.ts b/src/server/api/events.ts index 9dc5ce7..505cd4a 100644 --- a/src/server/api/events.ts +++ b/src/server/api/events.ts @@ -61,7 +61,7 @@ eventRoutes.post("/events", async (c) => { end_time: endTime, }) .returning(); - + return createSuccessResponse(c, result[0], `Inserted event with ID: ${result[0].id}`); } catch (error) { console.log(error); @@ -138,4 +138,3 @@ eventRoutes.get("/events/search/:name", async (c) => { }); export default eventRoutes; - diff --git a/src/server/api/profile.ts b/src/server/api/profile.ts index 8f76486..f73e34d 100644 --- a/src/server/api/profile.ts +++ b/src/server/api/profile.ts @@ -1,8 +1,8 @@ import { db } from "@/server/db/db"; +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import * as Schema from "@db/tables"; -import { getTableColumns, eq } from "drizzle-orm"; +import { eq, getTableColumns } from "drizzle-orm"; import { Hono } from "hono"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; const profileRoutes = new Hono(); @@ -54,7 +54,6 @@ profileRoutes.get("/profile", async (c) => { } }); - // Fetch profile, return JSON object of {data: , message: } // This route expects a session ID in the cookie, make sure a user is signed in when calling this endpoint profileRoutes.get("/profile", async (c) => { @@ -142,11 +141,7 @@ profileRoutes.patch("/profile", async (c) => { export default profileRoutes; function generateColumns() { - const personalInfoColumns = new Set( - Object.values(getTableColumns(Schema.personalInfo)).map((col) => col.name) - ); - const professionalInfoColumns = new Set( - Object.values(getTableColumns(Schema.professionalInfo)).map((col) => col.name) - ); + const personalInfoColumns = new Set(Object.values(getTableColumns(Schema.personalInfo)).map((col) => col.name)); + const professionalInfoColumns = new Set(Object.values(getTableColumns(Schema.professionalInfo)).map((col) => col.name)); return [personalInfoColumns, professionalInfoColumns]; } diff --git a/src/server/api/saseInfo.ts b/src/server/api/saseInfo.ts index 5e4d1ae..8442571 100644 --- a/src/server/api/saseInfo.ts +++ b/src/server/api/saseInfo.ts @@ -1,9 +1,9 @@ import { db } from "@/server/db/db"; +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import * as Schema from "@db/tables"; import { saseInfoSchema } from "@schema/saseInfoSchema"; import { eq } from "drizzle-orm"; import { Hono } from "hono"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; const saseRoutes = new Hono(); @@ -21,10 +21,7 @@ saseRoutes.post("/users/sase", async (c) => { saseRoutes.get("/users/sase/:id", async (c) => { try { const user_id = c.req.param("id"); - const sase_info = await db - .select() - .from(Schema.saseInfo) - .where(eq(Schema.saseInfo.user_id, user_id)); + const sase_info = await db.select().from(Schema.saseInfo).where(eq(Schema.saseInfo.user_id, user_id)); return createSuccessResponse(c, sase_info, "SASE info retrieved successfully"); } catch (error) { console.error("Error retrieving SASE info:", error); diff --git a/src/server/api/tags.ts b/src/server/api/tags.ts index 9d230f0..0f0842a 100644 --- a/src/server/api/tags.ts +++ b/src/server/api/tags.ts @@ -1,9 +1,9 @@ import { db } from "@/server/db/db"; +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import * as Schema from "@db/tables"; import { blogTagsSchema } from "@schema/blogTagSchema"; import { eq, inArray } from "drizzle-orm"; import { Hono } from "hono"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; const tagRoutes = new Hono(); diff --git a/src/server/api/user.ts b/src/server/api/user.ts index 91405bc..874d974 100644 --- a/src/server/api/user.ts +++ b/src/server/api/user.ts @@ -1,7 +1,7 @@ import { db } from "@/server/db/db"; +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import * as Schema from "@db/tables"; import { updateUserSchema } from "@shared/schema/userSchema"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; import bcrypt from "bcryptjs"; import { eq } from "drizzle-orm"; import { Hono } from "hono"; diff --git a/src/server/api/userInfo.ts b/src/server/api/userInfo.ts index 8bb3138..876b4de 100644 --- a/src/server/api/userInfo.ts +++ b/src/server/api/userInfo.ts @@ -1,6 +1,6 @@ +import { createErrorResponse, createSuccessResponse } from "@/shared/utils"; import { personalInfoInsertSchema, personalInfoUpdateSchema } from "@schema/personalInfoSchema"; import { professionalInfoInsertSchema, professionalInfoUpdateSchema } from "@schema/professionalInfoSchema"; -import { createSuccessResponse, createErrorResponse } from "@/shared/utils"; import { eq } from "drizzle-orm"; import { Hono } from "hono"; import { db } from "../db/db"; @@ -23,10 +23,7 @@ infoRoutes.post("/users/personal", async (c) => { infoRoutes.get("/users/personal/:id", async (c) => { try { const user_id = c.req.param("id"); - const personal_info = await db - .select() - .from(Schema.personalInfo) - .where(eq(Schema.personalInfo.user_id, user_id)); + const personal_info = await db.select().from(Schema.personalInfo).where(eq(Schema.personalInfo.user_id, user_id)); return createSuccessResponse(c, personal_info, "Personal info retrieved successfully"); } catch (error) { console.log(error); @@ -39,10 +36,7 @@ infoRoutes.patch("/users/personal/:id", async (c) => { const user_id = c.req.param("id"); const payload = await c.req.json(); const updateInfo = personalInfoUpdateSchema.parse(payload); - const personal_info = await db - .update(Schema.personalInfo) - .set(updateInfo) - .where(eq(Schema.personalInfo.user_id, user_id)); + const personal_info = await db.update(Schema.personalInfo).set(updateInfo).where(eq(Schema.personalInfo.user_id, user_id)); return createSuccessResponse(c, personal_info, "Personal info updated successfully"); } catch (error) { console.log(error); @@ -65,10 +59,7 @@ infoRoutes.post("/users/professional", async (c) => { infoRoutes.get("/users/professional/:id", async (c) => { try { const user_id = c.req.param("id"); - const professional_info = await db - .select() - .from(Schema.professionalInfo) - .where(eq(Schema.professionalInfo.user_id, user_id)); + const professional_info = await db.select().from(Schema.professionalInfo).where(eq(Schema.professionalInfo.user_id, user_id)); return createSuccessResponse(c, professional_info, "Professional info retrieved successfully"); } catch (error) { console.log(error); @@ -81,10 +72,7 @@ infoRoutes.patch("/users/professional/:id", async (c) => { const user_id = c.req.param("id"); const payload = await c.req.json(); const updateInfo = professionalInfoUpdateSchema.parse(payload); - const professional_info = await db - .update(Schema.professionalInfo) - .set(updateInfo) - .where(eq(Schema.professionalInfo.user_id, user_id)); + const professional_info = await db.update(Schema.professionalInfo).set(updateInfo).where(eq(Schema.professionalInfo.user_id, user_id)); return createSuccessResponse(c, professional_info, "Professional info updated successfully"); } catch (error) { console.log(error); diff --git a/src/shared/schema/index.ts b/src/shared/schema/index.ts index dd025c2..68a9775 100644 --- a/src/shared/schema/index.ts +++ b/src/shared/schema/index.ts @@ -8,4 +8,4 @@ export * from "./professionalInfoSchema"; export * from "./saseInfoSchema"; export * from "./userSchema"; export * from "./responseSchema"; -export * from "./profileSchema"; \ No newline at end of file +export * from "./profileSchema"; diff --git a/src/shared/schema/profileSchema.ts b/src/shared/schema/profileSchema.ts index cb538fd..2d16428 100644 --- a/src/shared/schema/profileSchema.ts +++ b/src/shared/schema/profileSchema.ts @@ -6,15 +6,15 @@ export const profileSchema = z.object({ email: z.string().email("Invalid email address."), time_added: z.preprocess( (val) => (typeof val === "string" ? Date.parse(val) : val), - z.number().int().min(0, "Time added must be a valid timestamp.") + z.number().int().min(0, "Time added must be a valid timestamp."), ), time_updated: z.preprocess( (val) => (typeof val === "string" ? Date.parse(val) : val), - z.number().int().min(0, "Time updated must be a valid timestamp.") + z.number().int().min(0, "Time updated must be a valid timestamp."), ), first_name: z.string(), // allow empty string if needed - last_name: z.string(), // allow empty string if needed + last_name: z.string(), // allow empty string if needed phone: z.string().optional(), resume: z.string().optional(), diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 9e4b120..b2a4e66 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -11,22 +11,12 @@ export function cn(...inputs: Array) { return twMerge(clsx(inputs)); } -export const createSuccessResponse = ( - c: Context, - result: T, - message: string = "Success", - meta: Record = {} -) => { +export const createSuccessResponse = (c: Context, result: T, message: string = "Success", meta: Record = {}) => { return c.json({ data: result, message, meta }); }; -export const createErrorResponse = ( - c: Context, - errCode: string, - errMsg: string, - statusCode: StatusCode = 500 -) => c.json({ error: { errCode, errMsg } }, statusCode); - +export const createErrorResponse = (c: Context, errCode: string, errMsg: string, statusCode: StatusCode = 500) => + c.json({ error: { errCode, errMsg } }, statusCode); export const apiFetch = async (url: string, options: RequestInit = {}, dataSuccessSchema: ZodTypeAny): Promise => { const response = await fetch(url, options);