@@ -7,6 +7,10 @@ export interface FetchOptions {
7
7
noResolveJson ?: boolean
8
8
}
9
9
10
+ export interface FetchParameters {
11
+ signal ?: AbortSignal
12
+ }
13
+
10
14
export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
11
15
12
16
const _getErrorMessage = ( err : any ) : string =>
@@ -27,7 +31,7 @@ const handleError = (error: any, reject: any) => {
27
31
const _getRequestParams = (
28
32
method : RequestMethodType ,
29
33
options ?: FetchOptions ,
30
- signal ?: AbortSignal ,
34
+ parameters ?: FetchParameters ,
31
35
body ?: object
32
36
) => {
33
37
const params : { [ k : string ] : any } = { method, headers : options ?. headers || { } }
@@ -39,22 +43,18 @@ const _getRequestParams = (
39
43
params . headers = { 'Content-Type' : 'application/json' , ...options ?. headers }
40
44
params . body = JSON . stringify ( body )
41
45
42
- if ( signal ) {
43
- params . signal = signal
44
- }
45
-
46
- return params
46
+ return { ...params , ...parameters }
47
47
}
48
48
49
49
async function _handleRequest (
50
50
method : RequestMethodType ,
51
51
url : string ,
52
52
options ?: FetchOptions ,
53
- signal ?: AbortSignal ,
53
+ parameters ?: FetchParameters ,
54
54
body ?: object
55
55
) : Promise < any > {
56
56
return new Promise ( ( resolve , reject ) => {
57
- fetch ( url , _getRequestParams ( method , options , signal , body ) )
57
+ fetch ( url , _getRequestParams ( method , options , parameters , body ) )
58
58
. then ( ( result ) => {
59
59
if ( ! result . ok ) throw result
60
60
if ( options ?. noResolveJson ) return resolve ( result )
@@ -65,33 +65,37 @@ async function _handleRequest(
65
65
} )
66
66
}
67
67
68
- export async function get ( url : string , options ?: FetchOptions , signal ?: AbortSignal ) : Promise < any > {
69
- return _handleRequest ( 'GET' , url , options , signal )
68
+ export async function get (
69
+ url : string ,
70
+ options ?: FetchOptions ,
71
+ parameters ?: FetchParameters
72
+ ) : Promise < any > {
73
+ return _handleRequest ( 'GET' , url , options , parameters )
70
74
}
71
75
72
76
export async function post (
73
77
url : string ,
74
78
body : object ,
75
79
options ?: FetchOptions ,
76
- signal ?: AbortSignal
80
+ parameters ?: FetchParameters
77
81
) : Promise < any > {
78
- return _handleRequest ( 'POST' , url , options , signal , body )
82
+ return _handleRequest ( 'POST' , url , options , parameters , body )
79
83
}
80
84
81
85
export async function put (
82
86
url : string ,
83
87
body : object ,
84
88
options ?: FetchOptions ,
85
- signal ?: AbortSignal
89
+ parameters ?: FetchParameters
86
90
) : Promise < any > {
87
- return _handleRequest ( 'PUT' , url , options , signal , body )
91
+ return _handleRequest ( 'PUT' , url , options , parameters , body )
88
92
}
89
93
90
94
export async function remove (
91
95
url : string ,
92
96
body : object ,
93
97
options ?: FetchOptions ,
94
- signal ?: AbortSignal
98
+ parameters ?: FetchParameters
95
99
) : Promise < any > {
96
- return _handleRequest ( 'DELETE' , url , options , signal , body )
100
+ return _handleRequest ( 'DELETE' , url , options , parameters , body )
97
101
}
0 commit comments