Skip to content

Commit

Permalink
refactor: (#158) try-catch 구문으로 변경
Browse files Browse the repository at this point in the history
Co-authored-by: 김영길/KIM YOUNG GIL <Gilpop8663@users.noreply.github.com>
Co-authored-by: chsua <chsua@users.noreply.github.com>
  • Loading branch information
inyeong-kang committed Aug 8, 2023
1 parent 63678c3 commit a73eb64
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 77 deletions.
2 changes: 1 addition & 1 deletion frontend/src/api/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const createCart = async () => {
};

export const editCart = async () => {
return await putFetch<Cart, { id: number }>('api/cart', { id: 12, text: '생성' });
return await putFetch<{ id: number }>('api/cart', { id: 12 });
};

// remove or delete
Expand Down
179 changes: 103 additions & 76 deletions frontend/src/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,102 +23,129 @@ const makeFetchMultiHeaders = () => {
};

export const getFetch = async <T>(url: string): Promise<T> => {
const response = await fetch(url, {
method: 'GET',
headers: makeFetchHeaders(),
});

if (!response.ok) {
throw new Error('에러');
try {
const response = await fetch(url, {
method: 'GET',
headers: makeFetchHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}

const data = await response.json();

return data;
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

const data = await response.json();
return data;
};

export const postFetch = async <T>(url: string, body: T): Promise<void> => {
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: makeFetchHeaders(),
});

if (!response.ok) {
throw new Error('에러');
export const postFetch = async <T>(url: string, body: T) => {
try {
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: makeFetchHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

// const data = await response.json();

return;
};

export const putFetch = async <T, R>(url: string, body: T): Promise<R | void> => {
const response = await fetch(url, {
method: 'PUT',
body: JSON.stringify(body),
headers: makeFetchHeaders(),
});

// const data = await response.json();

if (!response.ok) {
throw new Error('error');
export const putFetch = async <T>(url: string, body: T) => {
try {
const response = await fetch(url, {
method: 'PUT',
body: JSON.stringify(body),
headers: makeFetchHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

return;
};

export const patchFetch = async <T>(url: string, body?: T) => {
const response = await fetch(url, {
method: 'PATCH',
headers: makeFetchHeaders(),
body: JSON.stringify(body),
});

if (!response.ok) {
throw new Error('에러');
try {
const response = await fetch(url, {
method: 'PATCH',
headers: makeFetchHeaders(),
body: JSON.stringify(body),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

return response;
};

export const deleteFetch = async (url: string) => {
const response = await fetch(url, {
method: 'DELETE',
headers: makeFetchHeaders(),
});

return response;
try {
const response = await fetch(url, {
method: 'DELETE',
headers: makeFetchHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}
};

export const multiPostFetch = async (url: string, body: FormData) => {
const response = await fetch(url, {
method: 'POST',
body,
headers: makeFetchMultiHeaders(),
});

// const data = await response.json();

if (!response.ok) {
throw new Error('error');
try {
const response = await fetch(url, {
method: 'POST',
body,
headers: makeFetchMultiHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

return;
};

export const multiPutFetch = async (url: string, body: FormData) => {
const response = await fetch(url, {
method: 'PUT',
body,
headers: makeFetchMultiHeaders(),
});

const data = await response.json();

if (!response.ok) {
throw new Error(data.message);
try {
const response = await fetch(url, {
method: 'PUT',
body,
headers: makeFetchMultiHeaders(),
});

if (!response.ok) {
const errorMessage = await response.text();
throw new Error(errorMessage);
}
} catch (e) {
const error = e as Error;
throw new Error(error.message);
}

return data;
};

0 comments on commit a73eb64

Please sign in to comment.