-
Notifications
You must be signed in to change notification settings - Fork 1
[refactor] API 오류 정규화 및 인증 오류 처리 개선 #20
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0f27155
refactor: add common API error types and classification logic
bom0320 878b39d
refactor: normalize errors in my info API
bom0320 bdecbcc
refactor: separate authentication and API error handling
bom0320 da902d6
refactor: centralize authentication session cleanup
bom0320 c7cba6d
refactor: normalize query and mutation API errors
bom0320 02cd143
refactor: validate reservation history response
bom0320 95e323e
refactor: reuse session cleanup in logout flow
bom0320 4970b88
style: apply Biome formatting
bom0320 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,12 +1,20 @@ | ||
| import { dashboardUrl, get } from "@/shared/api"; | ||
| import { | ||
| dashboardUrl, | ||
| get, | ||
| normalizeApiError, | ||
| } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import type { DashboardSummaryDTO } from "../model/types"; | ||
| import { dashboardSummarySchema } from "./schemas"; | ||
|
|
||
| export async function getDashboardSummary(): Promise<DashboardSummaryDTO> { | ||
| const response = await get<BaseResponseType<unknown>>( | ||
| dashboardUrl.getSummary(), | ||
| ); | ||
| try { | ||
| const response = await get<BaseResponseType<unknown>>( | ||
| dashboardUrl.getSummary(), | ||
| ); | ||
|
|
||
| return dashboardSummarySchema.parse(response.data); | ||
| return dashboardSummarySchema.parse(response.data); | ||
| } catch (error) { | ||
| throw normalizeApiError(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,13 @@ | ||
| import { | ||
| del, | ||
| machineUrl, | ||
| normalizeApiError, | ||
| } from "@/shared/api"; | ||
|
|
||
| export async function deleteMachine(id: number): Promise<void> { | ||
| try { | ||
| await del(machineUrl.deleteMachine(id)); | ||
| } catch (error) { | ||
| throw normalizeApiError(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,39 +1,47 @@ | ||
| import { get, machineUrl } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import { mapMachines } from "../lib/mapMachine"; | ||
| import type { | ||
| MachineItem, | ||
| MachineParamsType, | ||
| MachineType, | ||
| } from "../model/types"; | ||
| import { machineResponseSchema } from "./schemas"; | ||
|
|
||
| async function getMachinesByType( | ||
| machineType: MachineType, | ||
| params?: MachineParamsType, | ||
| ): Promise<MachineItem[]> { | ||
| const response = await get<BaseResponseType<unknown>>( | ||
| machineUrl.getMachines(), | ||
| { | ||
| params: { | ||
| ...params, | ||
| type: machineType, | ||
| import { | ||
| get, | ||
| machineUrl, | ||
| normalizeApiError, | ||
| } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import { mapMachines } from "../lib/mapMachine"; | ||
| import type { | ||
| MachineItem, | ||
| MachineParamsType, | ||
| MachineType, | ||
| } from "../model/types"; | ||
| import { machineResponseSchema } from "./schemas"; | ||
|
|
||
| async function getMachinesByType( | ||
| machineType: MachineType, | ||
| params?: MachineParamsType, | ||
| ): Promise<MachineItem[]> { | ||
| const response = await get<BaseResponseType<unknown>>( | ||
| machineUrl.getMachines(), | ||
| { | ||
| params: { | ||
| ...params, | ||
| type: machineType, | ||
| }, | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| const parsedData = machineResponseSchema.parse(response.data); | ||
|
|
||
| return mapMachines(parsedData.machines); | ||
| } | ||
|
|
||
| export async function getMachines( | ||
| params?: MachineParamsType, | ||
| ): Promise<MachineItem[]> { | ||
| const [washers, dryers] = await Promise.all([ | ||
| getMachinesByType("WASHER", params), | ||
| getMachinesByType("DRYER", params), | ||
| ]); | ||
|
|
||
| return [...washers, ...dryers]; | ||
| } | ||
| ); | ||
|
|
||
| const parsedData = machineResponseSchema.parse(response.data); | ||
|
|
||
| return mapMachines(parsedData.machines); | ||
| } | ||
|
|
||
| export async function getMachines( | ||
| params?: MachineParamsType, | ||
| ): Promise<MachineItem[]> { | ||
| try { | ||
| const [washers, dryers] = await Promise.all([ | ||
| getMachinesByType("WASHER", params), | ||
| getMachinesByType("DRYER", params), | ||
| ]); | ||
|
|
||
| return [...washers, ...dryers]; | ||
| } catch (error) { | ||
| throw normalizeApiError(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,24 @@ | ||
| import { | ||
| machineUrl, | ||
| normalizeApiError, | ||
| put, | ||
| } from "@/shared/api"; | ||
| import type { MachineConditionStatusDTO } from "../model/types"; | ||
|
|
||
| type UpdateMachineStatusParams = { | ||
| id: number; | ||
| status: MachineConditionStatusDTO; | ||
| }; | ||
|
|
||
| export async function updateMachineStatus({ | ||
| id, | ||
| status, | ||
| }: UpdateMachineStatusParams): Promise<void> { | ||
| try { | ||
| await put(machineUrl.updateMachineStatus(id), { | ||
| status, | ||
| }); | ||
| } catch (error) { | ||
| throw normalizeApiError(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,15 +1,16 @@ | ||
| import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import { del, machineQueryKeys, machineUrl } from "@/shared/api"; | ||
| import { machineQueryKeys } from "@/shared/api"; | ||
| import { deleteMachine } from "./deleteMachine"; | ||
|
|
||
| export const useDeleteMachine = () => { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: (id: number) => del(machineUrl.deleteMachine(id)), | ||
| mutationFn: deleteMachine, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ | ||
| queryKey: machineQueryKeys.all, | ||
| }); | ||
| }, | ||
| }); | ||
| }; | ||
| }; |
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,22 +1,16 @@ | ||
| import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import { machineQueryKeys, machineUrl, put } from "@/shared/api"; | ||
| import type { MachineConditionStatusDTO } from "../model/types"; | ||
| import { machineQueryKeys } from "@/shared/api"; | ||
| import { updateMachineStatus } from "./updateMachineStatus"; | ||
|
|
||
| export const useUpdateMachineStatus = () => { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: ({ | ||
| id, | ||
| status, | ||
| }: { | ||
| id: number; | ||
| status: MachineConditionStatusDTO; | ||
| }) => put(machineUrl.updateMachineStatus(id), { status }), | ||
| mutationFn: updateMachineStatus, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ | ||
| queryKey: machineQueryKeys.all, | ||
| }); | ||
| }, | ||
| }); | ||
| }; | ||
| }; |
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
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,28 @@ | ||
| import { | ||
| normalizeApiError, | ||
| put, | ||
| reportUrl, | ||
| } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import type { ReportStatusType } from "../model/types"; | ||
|
|
||
| type UpdateReportStatusParams = { | ||
| id: number; | ||
| status: ReportStatusType; | ||
| }; | ||
|
|
||
| export async function updateReportStatus({ | ||
| id, | ||
| status, | ||
| }: UpdateReportStatusParams): Promise<void> { | ||
| try { | ||
| await put<BaseResponseType<null>>( | ||
| reportUrl.updateMalfunctionReportStatus(id), | ||
| { | ||
| status, | ||
| }, | ||
| ); | ||
| } catch (error) { | ||
| throw normalizeApiError(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,25 +1,25 @@ | ||
| import { useMutation, useQueryClient } from "@tanstack/react-query"; | ||
| import { put, reportUrl, reportQueryKeys } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import type { ReportStatusType } from "../model/types"; | ||
| import { reportQueryKeys } from "@/shared/api"; | ||
| import { updateReportStatus } from "./updateReportStatus"; | ||
|
|
||
| export function useUpdateReportStatus() { | ||
| const queryClient = useQueryClient(); | ||
|
|
||
| return useMutation({ | ||
| mutationFn: async ({ id, status }: { id: number; status: ReportStatusType }) => { | ||
| await put<BaseResponseType<null>>(reportUrl.updateMalfunctionReportStatus(id), { | ||
| status, | ||
| }); | ||
| }, | ||
| mutationFn: updateReportStatus, | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ | ||
| queryKey: reportQueryKeys.all, | ||
| }); | ||
|
|
||
| alert("신고 상태가 변경되었습니다."); | ||
| }, | ||
| onError: () => { | ||
| alert("신고 상태 변경 중 오류가 발생했습니다."); | ||
| onError: (error) => { | ||
| alert( | ||
| error instanceof Error | ||
| ? error.message | ||
| : "신고 상태 변경 중 오류가 발생했습니다.", | ||
| ); | ||
| }, | ||
| }); | ||
| } | ||
| } |
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,5 +1,13 @@ | ||
| import { del, reservationUrl } from "@/shared/api"; | ||
| import { | ||
| del, | ||
| normalizeApiError, | ||
| reservationUrl, | ||
| } from "@/shared/api"; | ||
|
|
||
| export async function deleteReservation(id: number) { | ||
| await del(reservationUrl.deleteReservation(id)); | ||
| } | ||
| export async function deleteReservation(id: number): Promise<void> { | ||
| try { | ||
| await del(reservationUrl.deleteReservation(id)); | ||
| } catch (error) { | ||
| throw normalizeApiError(error); | ||
| } | ||
| } |
34 changes: 23 additions & 11 deletions
34
src/entities/reservation/api/getMachineReservationHistory.ts
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,36 @@ | ||
| import { get, reservationUrl } from "@/shared/api"; | ||
| import { | ||
| get, | ||
| normalizeApiError, | ||
| reservationUrl, | ||
| } from "@/shared/api"; | ||
| import type { BaseResponseType } from "@/shared/api/types"; | ||
| import { mapMachineReservationHistory } from "../lib/mapReservationHistory"; | ||
| import type { | ||
| MachineReservationHistory, | ||
| MachineReservationHistoryParamsType, | ||
| MachineReservationHistoryResponseType, | ||
| } from "../model/historyTypes"; | ||
| import { machineReservationHistoryResponseSchema } from "./schemas"; | ||
|
|
||
| export async function getMachineReservationHistory( | ||
| params?: MachineReservationHistoryParamsType, | ||
| ): Promise<MachineReservationHistory | null> { | ||
| const response = await get< | ||
| BaseResponseType<MachineReservationHistoryResponseType> | ||
| >(reservationUrl.getMachineReservationHistory(), { | ||
| params, | ||
| }); | ||
| try { | ||
| const response = await get<BaseResponseType<unknown>>( | ||
| reservationUrl.getMachineReservationHistory(), | ||
| { | ||
| params, | ||
| }, | ||
| ); | ||
|
|
||
| const machine = response.data.machines[0]; | ||
| const parsedData = | ||
| machineReservationHistoryResponseSchema.parse(response.data); | ||
|
|
||
| if (!machine) return null; | ||
| const machine = parsedData.machines[0]; | ||
|
|
||
| return mapMachineReservationHistory(machine); | ||
| } | ||
| if (!machine) return null; | ||
|
|
||
| return mapMachineReservationHistory(machine); | ||
| } catch (error) { | ||
| throw normalizeApiError(error); | ||
| } | ||
| } |
Oops, something went wrong.
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.