1
1
import type { Serialized } from '../../core-typings/dist' ;
2
2
import type { MatchPathPattern , OperationParams , OperationResult , PathFor } from '../../rest-typings/dist' ;
3
- import type { RestClientInterface } from './RestClientInterface' ;
3
+ import type { Middleware , RestClientInterface } from './RestClientInterface' ;
4
4
5
5
export { RestClientInterface } ;
6
6
7
+ const pipe =
8
+ < T extends ( ...args : any [ ] ) => any > ( fn : T ) =>
9
+ ( ...args : Parameters < T > ) : ReturnType < T > =>
10
+ fn ( ...args ) ;
11
+
7
12
export class RestClient implements RestClientInterface {
8
13
private readonly baseUrl : string ;
9
14
@@ -61,7 +66,9 @@ export class RestClient implements RestClientInterface {
61
66
: Record < string , string > ,
62
67
options ?: Omit < RequestInit , 'method' > ,
63
68
) : Promise < TPath extends PathFor < 'GET' > ? Serialized < OperationResult < 'GET' , MatchPathPattern < TPath > > > : unknown > {
64
- return this . send ( `${ endpoint } ?${ this . getParams ( params ) } ` , 'GET' , options ) ;
69
+ return this . send ( `${ endpoint } ?${ this . getParams ( params ) } ` , 'GET' , options ) . then ( function ( response ) {
70
+ return response . json ( ) ;
71
+ } ) ;
65
72
}
66
73
67
74
post : RestClientInterface [ 'post' ] = ( endpoint , params , { headers, ...options } = { } ) => {
@@ -75,6 +82,8 @@ export class RestClient implements RestClientInterface {
75
82
} ,
76
83
77
84
...options ,
85
+ } ) . then ( function ( response ) {
86
+ return response . json ( ) ;
78
87
} ) ;
79
88
} ;
80
89
@@ -89,11 +98,15 @@ export class RestClient implements RestClientInterface {
89
98
} ,
90
99
91
100
...options ,
101
+ } ) . then ( function ( response ) {
102
+ return response . json ( ) ;
92
103
} ) ;
93
104
} ;
94
105
95
106
delete : RestClientInterface [ 'delete' ] = ( endpoint , params , options ) => {
96
- return this . send ( endpoint , 'DELETE' , options ) ;
107
+ return this . send ( endpoint , 'DELETE' , options ) . then ( function ( response ) {
108
+ return response . json ( ) ;
109
+ } ) ;
97
110
} ;
98
111
99
112
protected getCredentialsAsHeaders ( ) : Record < string , string > {
@@ -106,14 +119,12 @@ export class RestClient implements RestClientInterface {
106
119
: { } ;
107
120
}
108
121
109
- protected send < T > ( endpoint : string , method : string , { headers, ...options } : Omit < RequestInit , 'method' > = { } ) : Promise < T > {
122
+ send ( endpoint : string , method : string , { headers, ...options } : Omit < RequestInit , 'method' > = { } ) : Promise < Response > {
110
123
return fetch ( `${ this . baseUrl } ${ endpoint } ` , {
111
124
...options ,
112
125
headers : { ...this . getCredentialsAsHeaders ( ) , ...this . headers , ...headers } ,
113
126
method,
114
- } ) . then ( function ( response ) {
115
- return response . json ( ) ;
116
- } ) as Promise < T > ;
127
+ } ) ;
117
128
}
118
129
119
130
protected getParams ( data : Record < string , object | number | string | boolean > | void ) : string {
@@ -127,4 +138,10 @@ export class RestClient implements RestClientInterface {
127
138
. join ( '&' )
128
139
: '' ;
129
140
}
141
+
142
+ use ( middleware : Middleware < RestClientInterface [ 'send' ] > ) : void {
143
+ this . send = function ( this : RestClient , ...context : Parameters < RestClientInterface [ 'send' ] > ) : ReturnType < RestClientInterface [ 'send' ] > {
144
+ return middleware ( context , pipe ( this . send . bind ( this ) ) ) ;
145
+ } as RestClientInterface [ 'send' ] ;
146
+ }
130
147
}
0 commit comments