Skip to content

Commit

Permalink
Fix linting
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRickyZhang committed Mar 4, 2025
1 parent 93b58a6 commit 7671f99
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/client/api/blogs.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
14 changes: 5 additions & 9 deletions src/client/api/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@ import { apiFetch } from "@shared/utils";

// Fetch ALL Users
export const fetchUsers = async (): Promise<Array<SelectUser>> => {
const response = await apiFetch(
"/api/users",
{ method: "GET" },
selectUserSchema.array()
);
const response = await apiFetch("/api/users", { method: "GET" }, selectUserSchema.array());
return response.data;
};

Expand All @@ -20,7 +16,7 @@ export const fetchUser = async (id: string): Promise<SelectUser> => {
credentials: "include",
headers: { "Content-Type": "application/json" },
},
selectUserSchema
selectUserSchema,
);
return response.data;
};
Expand All @@ -34,7 +30,7 @@ export const createUser = async (newUser: InsertUser): Promise<SelectUser> => {
body: JSON.stringify(newUser),
headers: { "Content-Type": "application/json" },
},
selectUserSchema
selectUserSchema,
);
return response.data;
};
Expand All @@ -48,7 +44,7 @@ export const updateUser = async (updatedUser: UpdateUser): Promise<SelectUser> =
body: JSON.stringify(updatedUser),
headers: { "Content-Type": "application/json" },
},
selectUserSchema
selectUserSchema,
);
return response.data;
};
Expand All @@ -60,7 +56,7 @@ export const deleteUser = async (userId: number): Promise<DeleteUser> => {
method: "DELETE",
headers: { "Content-Type": "application/json" },
},
deleteUserSchema
deleteUserSchema,
);
return response.data;
};
10 changes: 3 additions & 7 deletions src/client/hooks/AuthContext.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -39,7 +39,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
z.object({
id: z.string(),
username: z.string(),
})
}),
);
console.log("ID set to ", user.data.id);
setId(user.data.id);
Expand Down Expand Up @@ -81,11 +81,7 @@ export const AuthProvider: React.FC<AuthProviderProps> = ({ children }) => {
}
};

return (
<AuthContext.Provider value={{ errorMessage, isAuthenticated, id, login, logout, isLoading }}>
{children}
</AuthContext.Provider>
);
return <AuthContext.Provider value={{ errorMessage, isAuthenticated, id, login, logout, isLoading }}>{children}</AuthContext.Provider>;
};

export const useAuth = (): AuthContextType => {
Expand Down
2 changes: 1 addition & 1 deletion src/client/hooks/useProfile.ts
Original file line number Diff line number Diff line change
@@ -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 = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/client/routes/profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <div>Loading...</div>;
Expand Down
15 changes: 3 additions & 12 deletions src/server/api/auth.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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];
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/contact.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand Down
3 changes: 1 addition & 2 deletions src/server/api/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -138,4 +138,3 @@ eventRoutes.get("/events/search/:name", async (c) => {
});

export default eventRoutes;

13 changes: 4 additions & 9 deletions src/server/api/profile.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down Expand Up @@ -54,7 +54,6 @@ profileRoutes.get("/profile", async (c) => {
}
});


// Fetch profile, return JSON object of {data: <profileSchema>, message: <string>}
// 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) => {
Expand Down Expand Up @@ -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];
}
7 changes: 2 additions & 5 deletions src/server/api/saseInfo.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/server/api/tags.ts
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
2 changes: 1 addition & 1 deletion src/server/api/user.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
22 changes: 5 additions & 17 deletions src/server/api/userInfo.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/shared/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ export * from "./professionalInfoSchema";
export * from "./saseInfoSchema";
export * from "./userSchema";
export * from "./responseSchema";
export * from "./profileSchema";
export * from "./profileSchema";
6 changes: 3 additions & 3 deletions src/shared/schema/profileSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
16 changes: 3 additions & 13 deletions src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,12 @@ export function cn(...inputs: Array<ClassValue>) {
return twMerge(clsx(inputs));
}

export const createSuccessResponse = <T>(
c: Context,
result: T,
message: string = "Success",
meta: Record<string, unknown> = {}
) => {
export const createSuccessResponse = <T>(c: Context, result: T, message: string = "Success", meta: Record<string, unknown> = {}) => {
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<SuccessResponse> => {
const response = await fetch(url, options);
Expand Down

0 comments on commit 7671f99

Please sign in to comment.