-
Notifications
You must be signed in to change notification settings - Fork 0
🧹 [コードの健全性向上] Dashboard APIルートの共通処理をリファクタリング #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
is0692vs
wants to merge
2
commits into
main
Choose a base branch
from
jules-12487929405010469319-17cad98c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+56
−53
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,25 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { getAuthenticatedUser, handleErrorResponse } from "@/lib/apiUtils"; | ||
|
|
||
| import { authOptions } from "@/lib/auth"; | ||
| import { fetchViewerLogin } from "@/lib/githubViewer"; | ||
| import { fetchCommitActivityHeatmap } from "@/lib/githubYearInReview"; | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const session = await getServerSession(authOptions); | ||
| const token = session?.accessToken; | ||
| try { | ||
| const user = await getAuthenticatedUser(); | ||
| if (!user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| if (!session || !token) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
| const yearParam = request.nextUrl.searchParams.get("year"); | ||
| const year = yearParam ? Number.parseInt(yearParam, 10) : new Date().getUTCFullYear(); | ||
|
|
||
| const yearParam = request.nextUrl.searchParams.get("year"); | ||
| const year = yearParam ? Number.parseInt(yearParam, 10) : new Date().getUTCFullYear(); | ||
| if (!Number.isFinite(year) || year < 2008 || year > new Date().getUTCFullYear()) { | ||
| return NextResponse.json({ error: "Invalid year" }, { status: 400 }); | ||
| } | ||
|
|
||
| if (!Number.isFinite(year) || year < 2008 || year > new Date().getUTCFullYear()) { | ||
| return NextResponse.json({ error: "Invalid year" }, { status: 400 }); | ||
| } | ||
|
|
||
| try { | ||
| const username = session.user?.login ?? (await fetchViewerLogin(token)); | ||
| const heatmap = await fetchCommitActivityHeatmap(username, year, token); | ||
| const heatmap = await fetchCommitActivityHeatmap(user.username, year, user.token); | ||
| return NextResponse.json({ year, heatmap }); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : "Unknown error"; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| return handleErrorResponse(error); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,18 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { getAuthenticatedUser, handleErrorResponse } from "@/lib/apiUtils"; | ||
|
|
||
| import { authOptions } from "@/lib/auth"; | ||
| import { fetchUserSummary } from "@/lib/github"; | ||
| import { fetchViewerLogin } from "@/lib/githubViewer"; | ||
|
|
||
| export async function GET() { | ||
| const session = await getServerSession(authOptions); | ||
| const token = session?.accessToken; | ||
|
|
||
| if (!session || !token) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| try { | ||
| const username = session.user?.login ?? (await fetchViewerLogin(token)); | ||
| const summary = await fetchUserSummary(username, token); | ||
| return NextResponse.json({ username, summary }); | ||
| const user = await getAuthenticatedUser(); | ||
| if (!user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| const summary = await fetchUserSummary(user.username, user.token); | ||
| return NextResponse.json({ username: user.username, summary }); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : "Unknown error"; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| return handleErrorResponse(error); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,25 @@ | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { getAuthenticatedUser, handleErrorResponse } from "@/lib/apiUtils"; | ||
|
|
||
| import { authOptions } from "@/lib/auth"; | ||
| import { fetchViewerLogin } from "@/lib/githubViewer"; | ||
| import { fetchYearInReviewData } from "@/lib/githubYearInReview"; | ||
|
|
||
| export async function GET(request: NextRequest) { | ||
| const session = await getServerSession(authOptions); | ||
| const token = session?.accessToken; | ||
| try { | ||
| const user = await getAuthenticatedUser(); | ||
| if (!user) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
|
|
||
| if (!session || !token) { | ||
| return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); | ||
| } | ||
| const yearParam = request.nextUrl.searchParams.get("year"); | ||
| const year = yearParam ? Number.parseInt(yearParam, 10) : new Date().getUTCFullYear(); | ||
|
|
||
| const yearParam = request.nextUrl.searchParams.get("year"); | ||
| const year = yearParam ? Number.parseInt(yearParam, 10) : new Date().getUTCFullYear(); | ||
| if (!Number.isFinite(year) || year < 2008 || year > new Date().getUTCFullYear()) { | ||
| return NextResponse.json({ error: "Invalid year" }, { status: 400 }); | ||
| } | ||
|
|
||
| if (!Number.isFinite(year) || year < 2008 || year > new Date().getUTCFullYear()) { | ||
| return NextResponse.json({ error: "Invalid year" }, { status: 400 }); | ||
| } | ||
|
|
||
| try { | ||
| const username = session.user?.login ?? (await fetchViewerLogin(token)); | ||
| const data = await fetchYearInReviewData(username, year, token); | ||
| const data = await fetchYearInReviewData(user.username, year, user.token); | ||
| return NextResponse.json(data); | ||
| } catch (error) { | ||
| const message = error instanceof Error ? error.message : "Unknown error"; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| return handleErrorResponse(error); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { NextResponse } from "next/server"; | ||
| import { getServerSession } from "next-auth"; | ||
| import { authOptions } from "@/lib/auth"; | ||
| import { fetchViewerLogin } from "@/lib/githubViewer"; | ||
|
|
||
| export async function getAuthenticatedUser() { | ||
| const session = await getServerSession(authOptions); | ||
| const token = session?.accessToken; | ||
|
|
||
| if (!session || !token) { | ||
| return null; | ||
| } | ||
|
|
||
| const username = session.user?.login ?? (await fetchViewerLogin(token)); | ||
| return { username, token }; | ||
| } | ||
|
|
||
| export function handleErrorResponse(error: unknown) { | ||
| const message = error instanceof Error ? error.message : "Unknown error"; | ||
| return NextResponse.json({ error: message }, { status: 500 }); | ||
| } | ||
|
is0692vs marked this conversation as resolved.
is0692vs marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.