Skip to content

Commit

Permalink
✨ feat: fetch 추상화 작업 #23
Browse files Browse the repository at this point in the history
  • Loading branch information
froggy1014 committed Aug 15, 2024
1 parent 2430425 commit 00c794b
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/apis/error.ts
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");
}
98 changes: 98 additions & 0 deletions src/apis/instance.ts
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;

0 comments on commit 00c794b

Please sign in to comment.