Skip to content

Commit

Permalink
feat: params 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
eonseok-jeon committed Sep 18, 2024
1 parent 4eac3f9 commit 9094850
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/common/apis/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ export class CustomError extends Error {
}
}

interface FetchOptions extends RequestInit {
interface FetchOptions extends Omit<RequestInit, 'body'> {
method?: RequestMethod;
headers?: Record<StandardHeaders, string>;
body?: Record<string, unknown>;
params?: Record<string, any>;
}

const instance = async (url: string, options: FetchOptions = {}) => {
const response = await fetch(`${baseURL}${url}`, {
const { body, params, headers, ...rest } = options;
const urlWithParams = params ? `${url}?${new URLSearchParams(params).toString()}` : url;

const response = await fetch(`${baseURL}${urlWithParams}`, {
headers: {
'Content-Type': 'application/json',
...options.headers,
...headers,
},
...options,
body: JSON.stringify(body),
...rest,
});

if (!response.ok) {
Expand Down

0 comments on commit 9094850

Please sign in to comment.