Skip to content

Commit

Permalink
fix: improve code quality based on SonarLint analysis
Browse files Browse the repository at this point in the history
- Replace nanoid with uuid for better compatibility
- Add proper error handling in auth service
- Remove console.log statements from axios interceptors
- Fix potential null reference issues
- Add proper type annotations for error objects
  • Loading branch information
jiaah committed Jan 8, 2025
1 parent 820fed1 commit e386e93
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 58 deletions.
4 changes: 3 additions & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"@supabase/supabase-js": "^2.39.0",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2"
"express": "^4.18.2",
"uuid": "^11.0.4"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/uuid": "^10.0.0",
"nodemon": "^3.0.1",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0"
Expand Down
3 changes: 3 additions & 0 deletions packages/api/src/config/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { createClient } from "@supabase/supabase-js";
import dotenv from "dotenv";
import path from "path";

/**
* @todo 반복적으로 사용되는 조건문 통합
*/
const env = process.env.NODE_ENV || "development";
const envPath = path.resolve(__dirname, `../../../../.env.${env}`);

Expand Down
2 changes: 1 addition & 1 deletion packages/api/src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NextFunction, Request, Response } from "express";
import { AuthService } from "../services/auth.service";

export class AuthController {
private authService: AuthService;
readonly authService: AuthService;

constructor() {
this.authService = new AuthService();
Expand Down
5 changes: 2 additions & 3 deletions packages/api/src/middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NextFunction, Request, Response } from "express";

import { v4 as uuidv4 } from 'uuid';
export interface ApiError {
success: false;
error: {
Expand All @@ -15,8 +15,7 @@ export interface ApiError {

const generateId = () => {
const timestamp = new Date().toISOString().replace(/[-:.]/g, "");
const random = Math.random().toString(36).substring(2, 8);
return `${timestamp}-${random}`;
return `${timestamp}-${uuidv4()}`;
};

export const errorMiddleware = (
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AuthProvider } from "@shared/types/authTypes";
import { supabase } from "../config/supabase";
import { UserModel } from "../models/user.model";
export class AuthService {
private userModel: UserModel;
private readonly userModel: UserModel;

constructor() {
this.userModel = new UserModel();
Expand Down Expand Up @@ -72,7 +72,8 @@ export class AuthService {

async createUser(userData: { id: string }) {
try {
return this.userModel.createUser(userData.id);
const result = await this.userModel.createUser(userData.id);
return result;
} catch (error) {
throw error;
}
Expand Down
94 changes: 47 additions & 47 deletions packages/shared/utils/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
import { logger } from "../index";

describe("logger", () => {
let consoleLogSpy: jest.SpyInstance;

beforeEach(() => {
// console.log를 모킹하여 실제 콘솔 출력을 방지
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
});

afterEach(() => {
// 각 테스트 후 모킹 초기화
consoleLogSpy.mockRestore();
});

it("should log message with correct format", () => {
logger("Test message");
expect(consoleLogSpy).toHaveBeenCalledWith("[Logger] Test message", "");
});

it("should log message with data when provided", () => {
const testData = { count: 1, text: "hello" };
logger("Test message", testData);
expect(consoleLogSpy).toHaveBeenCalledWith(
"[Logger] Test message",
testData,
);
});

it("should handle undefined data correctly", () => {
logger("Test message", undefined);
expect(consoleLogSpy).toHaveBeenCalledWith("[Logger] Test message", "");
});

it("should handle multiple calls correctly", () => {
logger("First message");
logger("Second message", { value: 123 });

expect(consoleLogSpy).toHaveBeenCalledTimes(2);
expect(consoleLogSpy).toHaveBeenNthCalledWith(
1,
"[Logger] First message",
"",
);
expect(consoleLogSpy).toHaveBeenNthCalledWith(
2,
"[Logger] Second message",
{ value: 123 },
);
});
let consoleLogSpy: jest.SpyInstance;

beforeEach(() => {
// console.log를 모킹하여 실제 콘솔 출력을 방지
consoleLogSpy = jest.spyOn(console, "log").mockImplementation();
});

afterEach(() => {
// 각 테스트 후 모킹 초기화
consoleLogSpy.mockRestore();
});

it("should log message with correct format", () => {
logger("Test message");
expect(consoleLogSpy).toHaveBeenCalledWith("[Logger] Test message", "");
});

it("should log message with data when provided", () => {
const testData = { count: 1, text: "hello" };
logger("Test message", testData);
expect(consoleLogSpy).toHaveBeenCalledWith(
"[Logger] Test message",
testData,
);
});

it("should handle undefined data correctly", () => {
logger("Test message");
expect(consoleLogSpy).toHaveBeenCalledWith("[Logger] Test message", "");
});

it("should handle multiple calls correctly", () => {
logger("First message");
logger("Second message", { value: 123 });

expect(consoleLogSpy).toHaveBeenCalledTimes(2);
expect(consoleLogSpy).toHaveBeenNthCalledWith(
1,
"[Logger] First message",
"",
);
expect(consoleLogSpy).toHaveBeenNthCalledWith(
2,
"[Logger] Second message",
{ value: 123 },
);
});
});
1 change: 1 addition & 0 deletions packages/web/app/(routes)/auth/callback/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default async function AuthCallbackPage({
*/
if (params.error) {
const errorData = JSON.parse(params.error as string);
console.log(errorData);
return <ErrorFeedback />;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ export default function RootLayout({
</body>
</html>
);
}
}
10 changes: 7 additions & 3 deletions packages/web/src/shared/config/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ export const createAxiosInstance = (baseURL: string) => {
},
(error) => {
console.error("Request Error:", error);
return Promise.reject(error);
return Promise.reject(
error instanceof Error ? error : new Error('Request failed')
);
},
);
instance.interceptors.response.use(
Expand All @@ -33,8 +35,10 @@ export const createAxiosInstance = (baseURL: string) => {
},
(error) => {
console.error("Response Error:", error);
return Promise.reject(error);
return Promise.reject(
error instanceof Error ? error : new Error('Request failed')
);
},
);
return instance;
};
};

0 comments on commit e386e93

Please sign in to comment.