Skip to content

Commit

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

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

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

if (!isFormData) {
headers['Content-Type'] = 'application/json';
}

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

Expand Down

0 comments on commit f8c47af

Please sign in to comment.