diff --git a/pr_description.txt b/pr_description.txt new file mode 100644 index 00000000..28f2edce --- /dev/null +++ b/pr_description.txt @@ -0,0 +1,13 @@ +🎯 **What:** The code health issue addressed +Replaced the use of magic numbers (`404`, `504`) in `src/lib/cardDataFetcher.ts` with explicit, descriptive constants. We introduced a new `HTTP_STATUS` constant object in `src/lib/types.ts` that includes common HTTP status codes and exported it for general application usage. + +💡 **Why:** How this improves maintainability +Magic numbers obscure the meaning of code by hiding intent behind arbitrary numbers. By extracting these into a centralized `HTTP_STATUS` constant object, the code's readability and maintainability are significantly improved. It provides better context, avoids typos, and standardizes how status codes are referenced across the application. + +✅ **Verification:** How you confirmed the change is safe +- Executed `npm run lint` and resolved any new issues (there were none). +- Ran the full Vitest suite (`npm run test`) and confirmed that all 551 tests across 71 files passed successfully. +- Manually verified the changes in both `src/lib/types.ts` and `src/lib/cardDataFetcher.ts`. + +✨ **Result:** The improvement achieved +The `fetchCardData` and `getJson` logic in `src/lib/cardDataFetcher.ts` is cleaner and easier to understand at a glance, with a robust baseline for other files to adopt standard HTTP status code references in the future. diff --git a/src/lib/cardDataFetcher.ts b/src/lib/cardDataFetcher.ts index 5bdc6b00..3f0f584b 100644 --- a/src/lib/cardDataFetcher.ts +++ b/src/lib/cardDataFetcher.ts @@ -1,6 +1,6 @@ import "server-only"; -import { GitHubApiError } from "@/lib/types"; +import { GitHubApiError, HTTP_STATUS } from "@/lib/types"; type GitHubUser = { login: string; @@ -89,15 +89,15 @@ async function getJson(url: string): Promise<{ status: number; data: T | null }); } catch (error) { if ((error as Error).name === "AbortError") { - throw new GitHubApiError("GitHub API request timed out", 504); + throw new GitHubApiError("GitHub API request timed out", HTTP_STATUS.GATEWAY_TIMEOUT); } throw error; } finally { clearTimeout(timeoutId); } - if (response.status === 404) { - return { status: 404, data: null }; + if (response.status === HTTP_STATUS.NOT_FOUND) { + return { status: HTTP_STATUS.NOT_FOUND, data: null }; } if (!response.ok) { @@ -229,7 +229,7 @@ export async function fetchCardData(username: string): Promise getJson(reposUrl), ]); - if (!profileResult.data || profileResult.status === 404) { + if (!profileResult.data || profileResult.status === HTTP_STATUS.NOT_FOUND) { return null; } diff --git a/src/lib/types.ts b/src/lib/types.ts index e55b3d26..27492067 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -159,6 +159,22 @@ export const DEFAULT_CARD_LAYOUT: CardLayout = { ], }; + +export const HTTP_STATUS = { + OK: 200, + BAD_REQUEST: 400, + UNAUTHORIZED: 401, + FORBIDDEN: 403, + NOT_FOUND: 404, + UNPROCESSABLE_ENTITY: 422, + INTERNAL_SERVER_ERROR: 500, + BAD_GATEWAY: 502, + SERVICE_UNAVAILABLE: 503, + GATEWAY_TIMEOUT: 504, +} as const; + +export type HttpStatus = typeof HTTP_STATUS[keyof typeof HTTP_STATUS]; + // ===== カスタムエラー ===== export class UserNotFoundError extends Error {