Skip to content

Commit

Permalink
fix: leetcode api proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Aug 23, 2024
1 parent 7df7448 commit 0649f97
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 34 deletions.
16 changes: 16 additions & 0 deletions src/app/api/leetcode/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

export const POST = async (req: NextRequest) => {
const requestBody = await req.json()

const response = await fetch('https://leetcode.cn/graphql/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
})

return NextResponse.json(await response.json())
}
60 changes: 26 additions & 34 deletions src/components/ui/link-card/LinkCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import uniqolor from 'uniqolor'
import { LazyLoad } from '~/components/common/Lazyload'
import { MingcuteStarHalfFill } from '~/components/icons/star'
import { usePeek } from '~/components/modules/peek/usePeek'
import { API_URL } from '~/constants/env'
import { LanguageToColorMap } from '~/constants/language'
import { useIsClientTransition } from '~/hooks/common/use-is-client'
import useIsCommandOrControlPressed from '~/hooks/common/use-is-command-or-control-pressed'
Expand Down Expand Up @@ -526,7 +525,7 @@ const fetchLeetCodeQuestionData: FetchObject = {
query: `query questionData($titleSlug: String!) {\n question(titleSlug: $titleSlug) {translatedTitle\n difficulty\n likes\n topicTags { translatedName\n }\n stats\n }\n}\n`,
variables: { titleSlug: id },
}
const questionData = await fetch(`${API_URL}/fn/leetcode/shiro`, {
const questionData = await fetch(`/api/leetcode`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -588,38 +587,31 @@ const fetchLeetCodeQuestionData: FetchObject = {
console.error('Error fetching LeetCode question data:', err)
throw err
}
},
}

// 映射难度到颜色的函数
function getDifficultyColor(difficulty: string) {
switch (difficulty) {
case 'Easy':
return '#00BFA5'
case 'Medium':
return '#FFA726'
case 'Hard':
return '#F44336'
default:
return '#757575'
}
}

// 难度字体颜色className
function getDifficultyColorClass(difficulty: string) {
switch (difficulty) {
case 'Easy':
return 'text-green-500'
case 'Medium':
return 'text-yellow-500'
case 'Hard':
return 'text-red-500'
default:
return 'text-gray-500'
}
}
function getDifficultyColor(difficulty: string) {
switch (difficulty) {
case 'Easy':
return '#00BFA5'
case 'Medium':
return '#FFA726'
case 'Hard':
return '#F44336'
default:
return '#757575'
}
}

interface LeetCodeResponse {
query: string
variables: Record<string, any>
function getDifficultyColorClass(difficulty: string) {
switch (difficulty) {
case 'Easy':
return 'text-green-500'
case 'Medium':
return 'text-yellow-500'
case 'Hard':
return 'text-red-500'
default:
return 'text-gray-500'
}
}
},
}

0 comments on commit 0649f97

Please sign in to comment.