Skip to content
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

feat(core): HTTP 요청 처리를 위한 기본 인스턴스 구현 #26

Merged
merged 1 commit into from
Feb 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions src/configs/fetch/http.api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
interface ResponseData<T> {
code: string;
isSuccess: boolean;
result: T;
}

type FetchOptions = RequestInit & {
params?: Record<string, string>;
revalidate?: number | false;
};

const baseURL = process.env.NEXT_PUBLIC_API_URL ?? '';

const defaultHeaders = {
'Content-Type': 'application/json',
Accept: 'application/json',
};

const handleError = (error: unknown) => {
const errorMessage = error instanceof Error ? error.message : 'An unknown error occurred';
console.error('API Error:', errorMessage);
};

const createFetchInstance = (baseUrl: string) => {
const fetchInstance = async (url: string, options: FetchOptions = {}) => {
const { params, revalidate, ...init } = options;
const requestUrl = `${baseUrl}${url}`;
const headers = new Headers(defaultHeaders);

const queryString = params ? `?${new URLSearchParams(params).toString()}` : '';
const fullUrl = `${requestUrl}${queryString}`;

const nextConfig = revalidate !== undefined ? { next: { revalidate } } : {};

const config: RequestInit = {
...init,
headers,
...nextConfig,
};

try {
const response = await fetch(fullUrl, config);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const responseData = await response.json();
return responseData;
} catch (error) {
handleError(error);
return Promise.reject(error);
}
};
return fetchInstance;
};

export const fetchApi = createFetchInstance(baseURL);

const createApiMethods = (instance: (url: string, options?: FetchOptions) => Promise<any>) => ({
Get: async <T>(url: string, params = {}, options: { revalidate?: number | false } = {}): Promise<ResponseData<T>> => {
try {
return await instance(url, {
method: 'GET',
params,
...options,
});
} catch (error) {
return Promise.reject(error);
}
},

Post: async <T, D = unknown>(url: string, data?: D, options = {}): Promise<ResponseData<T>> => {
try {
return await instance(url, {
method: 'POST',
body: JSON.stringify(data),
cache: 'no-store',
...options,
});
} catch (error) {
return Promise.reject(error);
}
},

Put: async <T, D = unknown>(url: string, data?: D, options = {}): Promise<ResponseData<T>> => {
try {
return await instance(url, {
method: 'PUT',
body: JSON.stringify(data),
cache: 'no-store',
...options,
});
} catch (error) {
return Promise.reject(error);
}
},

Delete: async <T>(url: string, options = {}): Promise<ResponseData<T>> => {
try {
return await instance(url, {
method: 'DELETE',
cache: 'no-store',
...options,
});
} catch (error) {
return Promise.reject(error);
}
},

Patch: async <T, D = unknown>(url: string, data?: D, options = {}): Promise<ResponseData<T>> => {
try {
return await instance(url, {
method: 'PATCH',
body: JSON.stringify(data),
cache: 'no-store',
...options,
});
} catch (error) {
return Promise.reject(error);
}
},
});

export const { Get, Post, Put, Delete, Patch } = createApiMethods(fetchApi);