@@ -3,9 +3,9 @@ import fetch from 'cross-fetch';
33import { DefaultError , ErrorResponse } from '../errors/defaultError' ;
44
55type DefaultRequest = {
6- url : string ,
7- method : string
8- }
6+ url : string ;
7+ method : string ;
8+ } ;
99
1010/**
1111 * 공용 API 클라이언트 함수
@@ -14,27 +14,34 @@ type DefaultRequest = {
1414 * @param request API URI, HTTP method 정의
1515 * @param data API에 요청할 request body 데이터
1616 */
17- export default async function defaultFetcher < T , R > ( authParameter : AuthenticationParameter , request : DefaultRequest , data ?: T ) : Promise < R > {
18- const authorizationHeaderData = getAuthInfo ( authParameter ) ;
19- return await fetch ( request . url , {
20- headers : {
21- 'Authorization' : authorizationHeaderData ,
22- 'Content-Type' : 'application/json' ,
23- } ,
24- body : JSON . stringify ( data ) ,
25- method : request . method
26- } ) . then < R > ( async ( res ) => {
27- if ( res . status >= 400 && res . status < 500 ) {
28- const errorResponse : ErrorResponse = await res . json ( ) ;
29- throw new DefaultError ( errorResponse . errorCode , errorResponse . errorMessage ) ;
30- } else if ( res . status >= 500 ) {
31- const responseText = await res . text ( ) ;
32- throw new DefaultError ( 'UnknownException' , responseText ) ;
33- }
34- try {
35- return res . json ( ) ;
36- } catch ( exception ) {
37- throw new Error ( await res . text ( ) ) ;
38- }
39- } ) ;
17+ export default async function defaultFetcher < T , R > (
18+ authParameter : AuthenticationParameter ,
19+ request : DefaultRequest ,
20+ data ?: T ,
21+ ) : Promise < R > {
22+ const authorizationHeaderData = getAuthInfo ( authParameter ) ;
23+ return await fetch ( request . url , {
24+ headers : {
25+ Authorization : authorizationHeaderData ,
26+ 'Content-Type' : 'application/json' ,
27+ } ,
28+ body : JSON . stringify ( data ) ,
29+ method : request . method ,
30+ } ) . then < R > ( async res => {
31+ if ( res . status >= 400 && res . status < 500 ) {
32+ const errorResponse : ErrorResponse = await res . json ( ) ;
33+ throw new DefaultError (
34+ errorResponse . errorCode ,
35+ errorResponse . errorMessage ,
36+ ) ;
37+ } else if ( res . status >= 500 ) {
38+ const responseText = await res . text ( ) ;
39+ throw new DefaultError ( 'UnknownException' , responseText ) ;
40+ }
41+ try {
42+ return res . json ( ) ;
43+ } catch ( exception ) {
44+ throw new Error ( await res . text ( ) ) ;
45+ }
46+ } ) ;
4047}
0 commit comments