@@ -7,6 +7,10 @@ export interface FetchOptions {
77 noResolveJson ?: boolean
88}
99
10+ export interface FetchParameters {
11+ signal ?: AbortSignal
12+ }
13+
1014export type RequestMethodType = 'GET' | 'POST' | 'PUT' | 'DELETE'
1115
1216const _getErrorMessage = ( err : any ) : string =>
@@ -24,7 +28,12 @@ const handleError = (error: any, reject: any) => {
2428 } )
2529}
2630
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+ ) => {
2837 const params : { [ k : string ] : any } = { method, headers : options ?. headers || { } }
2938
3039 if ( method === 'GET' ) {
@@ -34,17 +43,18 @@ const _getRequestParams = (method: RequestMethodType, options?: FetchOptions, bo
3443 params . headers = { 'Content-Type' : 'application/json' , ...options ?. headers }
3544 params . body = JSON . stringify ( body )
3645
37- return params
46+ return { ... params , ... parameters }
3847}
3948
4049async function _handleRequest (
4150 method : RequestMethodType ,
4251 url : string ,
4352 options ?: FetchOptions ,
53+ parameters ?: FetchParameters ,
4454 body ?: object
4555) : Promise < any > {
4656 return new Promise ( ( resolve , reject ) => {
47- fetch ( url , _getRequestParams ( method , options , body ) )
57+ fetch ( url , _getRequestParams ( method , options , parameters , body ) )
4858 . then ( ( result ) => {
4959 if ( ! result . ok ) throw result
5060 if ( options ?. noResolveJson ) return resolve ( result )
@@ -55,18 +65,37 @@ async function _handleRequest(
5565 } )
5666}
5767
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 )
6074}
6175
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 )
6483}
6584
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 )
6892}
6993
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 )
72101}
0 commit comments