Skip to content

Commit

Permalink
add error handlers and zerialization
Browse files Browse the repository at this point in the history
  • Loading branch information
icedevera committed May 3, 2024
1 parent ce22f4a commit 2f8b236
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/components/sections/contest/GithubFileSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const GithubFileSelect = () => {
const {data: publicReposData, isLoading: publicReposIsLoading} =
useGetPublicRepos()
const {data: repoFilesData, isLoading: repoFilesIsLoading} = useGetRepoFiles(
selectedRepo ?? undefined,
selectedRepo ?? {defaultBranch: '', owner: '', repo: ''},
{enabled: selectedRepo !== null},
)

if (session.data?.user.provider !== 'github') {
Expand Down
13 changes: 8 additions & 5 deletions src/lib/queries/github/getGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import {
getRepoFiles,
} from '@/server/actions/github/getGithub'
import {useUserId} from '@/lib/hooks/useUserId'
import {withApiErrorHandler} from '@/lib/utils/common/error'

const getPublicReposQueryOptions = (userId: string | undefined) => ({
queryKey: queryKeys.gitHub.publicRepos(userId).queryKey,
queryFn: () => getPublicRepos(),
queryFn: withApiErrorHandler(() => getPublicRepos()),
})

export const useGetPublicRepos = () => {
Expand All @@ -20,10 +21,12 @@ export const useGetPublicRepos = () => {
return useQuery({...getPublicReposQueryOptions(userId), enabled: !!userId})
}

const getRepoFilesQueryOptions = (params: GetRepoFilesParams | undefined) => ({
const getRepoFilesQueryOptions = (params: GetRepoFilesParams) => ({
queryKey: queryKeys.gitHub.repoFiles(params).queryKey,
queryFn: () => (params ? getRepoFiles(params) : null),
queryFn: withApiErrorHandler(() => getRepoFiles(params)),
})

export const useGetRepoFiles = (params: GetRepoFilesParams | undefined) =>
useQuery({...getRepoFilesQueryOptions(params), enabled: !!params})
export const useGetRepoFiles = (
params: GetRepoFilesParams,
options?: {enabled?: boolean},
) => useQuery({...options, ...getRepoFilesQueryOptions(params)})
2 changes: 1 addition & 1 deletion src/lib/queries/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ export const queryKeys = createQueryKeyStore({
},
gitHub: {
publicRepos: (userId: string | undefined) => [userId],
repoFiles: (params: GetRepoFilesParams | undefined) => [params],
repoFiles: (params: GetRepoFilesParams) => [params],
},
})
30 changes: 17 additions & 13 deletions src/server/actions/github/getGithub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import {z} from 'zod'

import {requireGitHubAuth} from '@/server/utils/auth'
import {db} from '@/server/db'
import {ServerError} from '@/lib/types/error'
import {serializeServerErrors} from '@/lib/utils/common/error'

export type GetPublicReposResponse = Awaited<ReturnType<typeof getPublicRepos>>
export type GetPublicReposResponse = Awaited<
ReturnType<typeof getPublicReposAction>
>

export const getPublicRepos = async () => {
const getPublicReposAction = async () => {
const session = await requireGitHubAuth()

const account = await db.query.accounts.findFirst({
Expand All @@ -18,7 +22,7 @@ export const getPublicRepos = async () => {
})

if (!account) {
throw new Error('GitHub account not found')
throw new ServerError('GitHub account not found')
}

const octokit = new Octokit({
Expand All @@ -39,7 +43,11 @@ export const getPublicRepos = async () => {
}))
}

export type GetRepoFilesResponse = Awaited<ReturnType<typeof getRepoFiles>>
export const getPublicRepos = serializeServerErrors(getPublicReposAction)

export type GetRepoFilesResponse = Awaited<
ReturnType<typeof getRepoFilesAction>
>

const getRepoFilesParamsSchema = z.object({
repo: z.string(),
Expand All @@ -49,16 +57,10 @@ const getRepoFilesParamsSchema = z.object({

export type GetRepoFilesParams = z.infer<typeof getRepoFilesParamsSchema>

export const getRepoFiles = async (request: GetRepoFilesParams) => {
const getRepoFilesAction = async (request: GetRepoFilesParams) => {
const session = await requireGitHubAuth()

const result = getRepoFilesParamsSchema.safeParse(request)

if (!result.success) {
throw new Error('Invalid request')
}

const {repo, owner, defaultBranch} = result.data
const {repo, owner, defaultBranch} = getRepoFilesParamsSchema.parse(request)

const account = await db.query.accounts.findFirst({
columns: {access_token: true},
Expand All @@ -67,7 +69,7 @@ export const getRepoFiles = async (request: GetRepoFilesParams) => {
})

if (!account) {
throw new Error('GitHub account not found')
throw new ServerError('GitHub account not found')
}

const octokit = new Octokit({
Expand Down Expand Up @@ -95,3 +97,5 @@ export const getRepoFiles = async (request: GetRepoFilesParams) => {

return data.tree
}

export const getRepoFiles = serializeServerErrors(getRepoFilesAction)

0 comments on commit 2f8b236

Please sign in to comment.