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