-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2430425
commit 00c794b
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,58 @@ | ||
export class FiestaError extends Error { | ||
public statusCode: number; | ||
public code: string; | ||
|
||
constructor(statusCode: number, code: string, message: string) { | ||
super(message); | ||
this.statusCode = statusCode; | ||
this.code = code; | ||
this.name = this.constructor.name; | ||
} | ||
} | ||
|
||
// 서버 에러 클래스 | ||
export class ServerError extends FiestaError { | ||
constructor(code: string, message: string) { | ||
super(500, code, message); | ||
} | ||
} | ||
|
||
// 클라이언트 에러 클래스 | ||
export class ClientError extends FiestaError { | ||
constructor(statusCode: number, code: string, message: string) { | ||
super(statusCode, code, message); | ||
} | ||
} | ||
// 페스티벌 에러 클래스 | ||
export class FestivalError extends FiestaError { | ||
constructor(statusCode: number, code: string, message: string) { | ||
super(statusCode, code, message); | ||
} | ||
} | ||
// 방문일지 에러 클래스 | ||
export class LogsError extends FiestaError { | ||
constructor(statusCode: number, code: string, message: string) { | ||
super(statusCode, code, message); | ||
} | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export function createFiestaError(error: any) { | ||
const { code, message, statusCode } = error; | ||
const initial = error.code.charAt(0); | ||
|
||
if (initial === "S") { | ||
return new ServerError(code, message); | ||
} | ||
if (initial === "C") { | ||
return new ClientError(statusCode, code, message); | ||
} | ||
if (initial === "F") { | ||
return new FestivalError(statusCode, code, message); | ||
} | ||
if (initial === "L") { | ||
return new ClientError(statusCode, code, message); | ||
} | ||
|
||
return new Error("Something went wrong"); | ||
} |
This file contains 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,98 @@ | ||
import { env } from "@/env"; | ||
|
||
import { | ||
ClientError, | ||
createFiestaError, | ||
FestivalError, | ||
LogsError, | ||
ServerError, | ||
} from "./error"; | ||
|
||
export type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE"; | ||
|
||
export interface FiestaResponse<T> { | ||
statusCode: number; | ||
status: string; | ||
message: string; | ||
data: T; | ||
} | ||
|
||
const fiestaFetch = async ( | ||
url: string, | ||
method: Method, | ||
options: RequestInit, | ||
) => { | ||
const baseUrl = env.NEXT_PUBLIC_BASE_URL; | ||
|
||
const _defaultOptions: RequestInit = { | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}; | ||
|
||
const finalOptions = { | ||
method, | ||
..._defaultOptions, | ||
...options, | ||
headers: { | ||
..._defaultOptions.headers, | ||
...options?.headers, | ||
}, | ||
}; | ||
|
||
try { | ||
const response = await fetch(baseUrl + url, finalOptions); | ||
if (!response.ok) { | ||
const errorData = await response.json(); | ||
const fiestaError = createFiestaError(errorData); | ||
throw fiestaError; | ||
} | ||
return await response.json(); | ||
} catch (error: unknown) { | ||
console.error(error); | ||
|
||
if (error instanceof ServerError) { | ||
// TODO: Server Error | ||
throw error; | ||
} | ||
if (error instanceof ClientError) { | ||
// TODO: Client Error | ||
throw error; | ||
} | ||
if (error instanceof FestivalError) { | ||
// TODO: Festival Error | ||
throw error; | ||
} | ||
if (error instanceof LogsError) { | ||
// TODO: Logs Error | ||
throw error; | ||
} | ||
|
||
throw error; | ||
} | ||
}; | ||
|
||
const instance = { | ||
get: async <T>( | ||
url: string, | ||
options: Omit<RequestInit, "body"> = {}, | ||
): Promise<FiestaResponse<T>> => fiestaFetch(url, "GET", options), | ||
post: async <T>( | ||
url: string, | ||
body = {}, | ||
options: Omit<RequestInit, "body"> = {}, | ||
): Promise<FiestaResponse<T>> => | ||
fiestaFetch(url, "POST", { body: JSON.stringify(body), ...options }), | ||
put: async <T>( | ||
url: string, | ||
body = {}, | ||
options: Omit<RequestInit, "body"> = {}, | ||
): Promise<FiestaResponse<T>> => | ||
fiestaFetch(url, "PUT", { body: JSON.stringify(body), ...options }), | ||
delete: async <T>( | ||
url: string, | ||
options: Omit<RequestInit, "body"> = {}, | ||
): Promise<FiestaResponse<T>> => fiestaFetch(url, "DELETE", { ...options }), | ||
}; | ||
|
||
export default instance; |