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