@@ -40,27 +40,24 @@ const CONTENT_TYPES: Record<ContentTypes, string> = {
40
40
blob : '*/*' ,
41
41
}
42
42
43
- class ResponseError extends Error {
44
- response : Response
45
- constructor ( response : Response ) {
46
- super ( response . statusText )
47
- this . name = 'ResponseError'
48
- this . response = response
49
- }
43
+ function ResponseError ( response : Response ) {
44
+ return Object . assign ( new Error ( response . statusText ) , {
45
+ name : 'ResponseError' ,
46
+ response : response ,
47
+ } )
50
48
}
51
49
52
- class TimeoutError extends Error {
53
- constructor ( ) {
54
- super ( 'Request timed out' )
55
- this . name = 'TimeoutError'
56
- }
50
+ function TimeoutError ( ) {
51
+ return Object . assign ( new Error ( 'Request timed out' ) , {
52
+ name : 'TimeoutError' ,
53
+ } )
57
54
}
58
55
59
- const mergeOptions = ( left : Options = { } , right : Options = { } ) => ( {
60
- ... left ,
61
- ... right ,
62
- headers : { ... left . headers , ... right . headers } ,
63
- } )
56
+ const merge = ( a ?: unknown , b ?: unknown , c ?: unknown , d ?: unknown ) =>
57
+ Object . assign ( { } , a , b , c , d )
58
+
59
+ const mergeOptions = ( left : Options = { } , right : Options = { } ) : Options =>
60
+ merge ( left , right , { headers : merge ( left . headers , right . headers ) } )
64
61
65
62
function isAborted ( error : Error ) {
66
63
return error . name === 'AbortError'
@@ -82,7 +79,7 @@ const DEFAULT_OPTIONS: Options = {
82
79
return response
83
80
}
84
81
85
- throw new ResponseError ( response )
82
+ throw ResponseError ( response )
86
83
} ,
87
84
onSuccess ( response ) {
88
85
return response
@@ -93,6 +90,8 @@ const DEFAULT_OPTIONS: Options = {
93
90
}
94
91
95
92
function request ( baseResource : string , baseInit : Options ) : Request {
93
+ const options = mergeOptions ( DEFAULT_OPTIONS , baseInit )
94
+
96
95
const {
97
96
json,
98
97
params,
@@ -103,14 +102,13 @@ function request(baseResource: string, baseInit: Options): Request {
103
102
onResponse,
104
103
onSuccess,
105
104
onFailure,
106
- ...options
107
- } = mergeOptions ( DEFAULT_OPTIONS , baseInit )
105
+ } = options
108
106
109
107
const query = params == null ? '' : '?' + serialize ( params )
110
108
const resource = prefixUrl + baseResource + query
111
109
112
110
const headers = new Headers ( options . headers )
113
- const init : RequestInit = { ... options , headers }
111
+ const init : RequestInit = merge ( options , { headers } )
114
112
115
113
if ( json != null ) {
116
114
init . body = JSON . stringify ( json )
@@ -129,7 +127,7 @@ function request(baseResource: string, baseInit: Options): Request {
129
127
const controller = new AbortController ( )
130
128
131
129
timerID = setTimeout ( ( ) => {
132
- reject ( new TimeoutError ( ) )
130
+ reject ( TimeoutError ( ) )
133
131
controller . abort ( )
134
132
} , timeout )
135
133
@@ -142,7 +140,7 @@ function request(baseResource: string, baseInit: Options): Request {
142
140
143
141
init . signal = controller . signal
144
142
} else {
145
- timerID = setTimeout ( ( ) => reject ( new TimeoutError ( ) ) , timeout )
143
+ timerID = setTimeout ( ( ) => reject ( TimeoutError ( ) ) , timeout )
146
144
}
147
145
}
148
146
@@ -177,7 +175,7 @@ function create(baseOptions?: Options) {
177
175
const createMethod = ( method : RequestMethods ) => (
178
176
resource : string ,
179
177
options ?: Options
180
- ) => request ( resource , mergeOptions ( baseOptions , { method, ... options } ) )
178
+ ) => request ( resource , mergeOptions ( baseOptions , merge ( { method } , options ) ) )
181
179
182
180
const intance = {
183
181
create,
0 commit comments