Skip to content

Commit

Permalink
feat: fetcher 공통 API 호출 모듈 구현 #442
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgksqkr committed Dec 26, 2024
1 parent 83c1c5a commit 9db2b40
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions frontend-admin/src/apis/fetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
interface RequestProps {
method: "GET" | "POST" | "DELETE" | "PATCH" | "PUT";
body?: Record<string, string | number>;
headers?: Record<string, string>;
}

type FetchProps = Omit<RequestProps, "method">;

const fetcher = {
async request(url: string, { method, body, headers }: RequestProps) {
try {
const response = await fetch(url, {
method,
body: body && JSON.stringify(body),
headers: headers && { ...headers, "Content-Type": "application/json" },
});

return response;
} catch (error) {
throw new Error((error as Error).message);
}
},

get(url: string, options: FetchProps = {}) {
return this.request(url, { method: "GET", headers: options.headers });
},
post(url: string, options: FetchProps = {}) {
return this.request(url, {
method: "POST",
body: options.body,
headers: options.headers,
});
},
delete(url: string, options: FetchProps = {}) {
return this.request(url, { method: "DELETE", headers: options.headers });
},
patch(url: string, options: FetchProps = {}) {
return this.request(url, {
method: "PATCH",
body: options.body,
headers: options.headers,
});
},
put(url: string, options: FetchProps = {}) {
return this.request(url, { method: "PUT", headers: options.headers });
},
};

export default fetcher;

0 comments on commit 9db2b40

Please sign in to comment.