@@ -2,20 +2,22 @@ import fetch from 'cross-fetch'
22import Client from '../base/Client'
33import Request from '../base/Request'
44import { HttpAddress , TcpAddressInitializer } from '../base/Transports'
5+ import Response from '../base/Response'
6+ import JsonRpcError from '../base/Error'
57
68/**
79 * A `Client` using HTTP/S for communication.
810 */
911export default class HttpClient extends Client {
10- private readonly address : HttpAddress
12+ public readonly url : string
1113
12- public constructor ( address ?: TcpAddressInitializer ) {
14+ private readonly jwt ?: string
15+
16+ public constructor ( address : HttpAddress = new HttpAddress ( ) ) {
1317 super ( )
14- this . address = new HttpAddress ( address )
15- }
1618
17- public get url ( ) {
18- return this . address . toString ( )
19+ this . url = address . toString ( )
20+ this . jwt = address . jwt
1921 }
2022
2123 protected send ( request : Request ) : Promise < void > {
@@ -26,12 +28,25 @@ export default class HttpClient extends Client {
2628 credentials : 'same-origin' , // include, *same-origin, omit
2729 headers : {
2830 'Content-Type' : 'application/json; charset=utf-8' ,
29- Accept : 'application/json; charset=utf-8'
31+ Accept : 'application/json; charset=utf-8' ,
32+ ...( this . jwt !== undefined
33+ ? { Authorization : `Bearer ${ this . jwt } ` }
34+ : { } )
3035 } ,
3136 body : JSON . stringify ( request )
3237 } )
33- . then ( response => response . json ( ) )
34- . then ( response => this . receive ( response ) )
38+ . then ( async reply => {
39+ if ( reply . status >= 400 ) {
40+ // Translate the HTTP error into JSON-RPC error
41+ const message = await reply . text ( )
42+ const error = new JsonRpcError ( - 32600 , message )
43+ return new Response ( request . id , undefined , error )
44+ }
45+ return reply . json ( )
46+ } )
47+ . then ( ( response : Response ) => {
48+ this . receive ( response )
49+ } )
3550 }
3651
3752 // Additional methods for getting and posting to server
0 commit comments