@@ -24,7 +24,8 @@ import {
24
24
ListItemText ,
25
25
Container ,
26
26
} from "@material-ui/core" ;
27
- import { Client , RequestManager , HTTPTransport , WebSocketTransport } from "@open-rpc/client-js" ;
27
+ import { HTTPTransport , WebSocketTransport } from "@open-rpc/client-js" ;
28
+ import { Transport } from "@open-rpc/client-js/build/transports/Transport" ;
28
29
import Brightness3Icon from "@material-ui/icons/Brightness3" ;
29
30
import WbSunnyIcon from "@material-ui/icons/WbSunny" ;
30
31
import { JSONRPCError } from "@open-rpc/client-js/build/Error" ;
@@ -38,18 +39,36 @@ import useMonacoVimMode from "../hooks/useMonacoVimMode";
38
39
import { addDiagnostics } from "@etclabscore/monaco-add-json-schema-diagnostics" ;
39
40
import openrpcDocumentToJSONRPCSchemaResult from "../helpers/openrpcDocumentToJSONRPCSchemaResult" ;
40
41
41
- const errorToJSON = ( error : JSONRPCError | any ) : any => {
42
+ const errorToJSON = ( error : JSONRPCError | any , id : string | number ) : any => {
42
43
const isError = error instanceof Error ;
43
44
if ( ! isError ) {
44
45
return ;
45
46
}
46
47
if ( ! error ) {
47
48
return ;
48
49
}
50
+ const emptyErrorResponse = {
51
+ jsonrpc : "2.0" ,
52
+ id,
53
+ } ;
54
+ // this is an internal wrapped client-js error
55
+ if ( error . data instanceof Error ) {
56
+ return {
57
+ ...emptyErrorResponse ,
58
+ error : {
59
+ code : error . data . code ,
60
+ message : error . data . message ,
61
+ data : error . data . data ,
62
+ } ,
63
+ } ;
64
+ }
49
65
return {
50
- code : error . code ,
51
- message : error . message ,
52
- data : error . data ,
66
+ ...emptyErrorResponse ,
67
+ error : {
68
+ code : error . code ,
69
+ message : error . message ,
70
+ data : error . data ,
71
+ } ,
53
72
} ;
54
73
} ;
55
74
@@ -62,35 +81,36 @@ interface IProps {
62
81
onToggleDarkMode ?: ( ) => void ;
63
82
}
64
83
65
- const useClient = ( url : string ) : [ Client , JSONRPCError | undefined , Dispatch < JSONRPCError | undefined > ] => {
66
- const [ client , setClient ] = useState ( ) ;
67
- const [ error , setError ] = useState ( ) ;
84
+ type TUseTransport = ( url : string ) =>
85
+ [ Transport | undefined , JSONRPCError | undefined , Dispatch < JSONRPCError | undefined > ] ;
86
+
87
+ const useTransport : TUseTransport = ( url ) => {
88
+ const [ transport , setTransport ] : [ Transport | undefined , Dispatch < Transport | undefined > ] = useState ( ) ;
89
+ const [ error , setError ] : [ JSONRPCError | undefined , Dispatch < JSONRPCError | undefined > ] = useState ( ) ;
68
90
useEffect ( ( ) => {
69
- let transport ;
91
+ let localTransport ;
70
92
if ( url === "" || url === undefined ) {
71
- setClient ( undefined ) ;
93
+ setTransport ( undefined ) ;
72
94
return ;
73
95
}
74
96
if ( url . includes ( "http://" ) || url . includes ( "https://" ) ) {
75
- transport = HTTPTransport ;
97
+ localTransport = HTTPTransport ;
76
98
}
77
99
if ( url . includes ( "ws://" ) ) {
78
- transport = WebSocketTransport ;
100
+ localTransport = WebSocketTransport ;
79
101
}
80
102
try {
81
- const clientTransport = transport || HTTPTransport ;
82
- const t = new clientTransport ( url ) ;
83
- const c = new Client ( new RequestManager ( [ t ] ) ) ;
84
- setClient ( c ) ;
85
- c . onError ( ( e ) => {
86
- console . log ( "onError" , e ) ; //tslint:disable-line
103
+ const transportTransport = localTransport || HTTPTransport ;
104
+ const t = new transportTransport ( url ) ;
105
+ t . connect ( ) . then ( ( ) => {
106
+ setTransport ( t ) ;
87
107
} ) ;
88
108
} catch ( e ) {
89
- setClient ( undefined ) ;
109
+ setTransport ( undefined ) ;
90
110
setError ( e ) ;
91
111
}
92
112
} , [ url ] ) ;
93
- return [ client , error , setError ] ;
113
+ return [ transport , error , setError ] ;
94
114
} ;
95
115
96
116
function useCounter ( defaultValue : number ) : [ number , ( ) => void ] {
@@ -107,7 +127,7 @@ const emptyJSONRPC = {
107
127
jsonrpc : "2.0" ,
108
128
method : "" ,
109
129
params : [ ] ,
110
- id : "0" ,
130
+ id : 0 ,
111
131
} ;
112
132
113
133
const Inspector : React . FC < IProps > = ( props ) => {
@@ -145,8 +165,8 @@ const Inspector: React.FC<IProps> = (props) => {
145
165
const [ results , setResults ] : [ any , Dispatch < any > ] = useState ( ) ;
146
166
const [ url , setUrl ] = useState ( props . url || "" ) ;
147
167
const [ debouncedUrl ] = useDebounce ( url , 1000 ) ;
148
- const [ client , error ] = useClient ( debouncedUrl ) ;
149
- const [ historyOpen , setHistoryOpen ] = useState ( ) ;
168
+ const [ transport , error ] = useTransport ( debouncedUrl ) ;
169
+ const [ historyOpen , setHistoryOpen ] = useState ( false ) ;
150
170
const [ requestHistory , setRequestHistory ] : [ any [ ] , Dispatch < any > ] = useState ( [ ] ) ;
151
171
const [ historySelectedIndex , setHistorySelectedIndex ] = useState ( 0 ) ;
152
172
useEffect ( ( ) => {
@@ -166,7 +186,7 @@ const Inspector: React.FC<IProps> = (props) => {
166
186
const jsonResult = {
167
187
...json ,
168
188
jsonrpc : "2.0" ,
169
- id : id . toString ( ) ,
189
+ id,
170
190
} ;
171
191
setJson ( jsonResult ) ;
172
192
}
@@ -187,7 +207,9 @@ const Inspector: React.FC<IProps> = (props) => {
187
207
const modelName = "inspector-results" ;
188
208
const modelUriString = `inmemory://${ modelName } -${ Math . random ( ) } .json` ;
189
209
const modelUri = monaco . Uri . parse ( modelUriString ) ;
190
- const model = monaco . editor . createModel ( results ? JSON . stringify ( results , null , 4 ) : "" , "json" , modelUri ) ;
210
+ const model = monaco . editor . createModel (
211
+ results ? JSON . stringify ( results , null , 4 ) : ""
212
+ , "json" , modelUri ) ;
191
213
responseEditor . setModel ( model ) ;
192
214
addDiagnostics ( modelUri . toString ( ) , schema , monaco ) ;
193
215
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -210,21 +232,25 @@ const Inspector: React.FC<IProps> = (props) => {
210
232
211
233
const handlePlayButton = async ( ) => {
212
234
clear ( ) ;
213
- if ( client ) {
214
- incrementId ( ) ;
235
+ if ( transport ) {
215
236
try {
216
- const result = await client . request ( json . method , json . params ) ;
237
+ const result = await transport . sendData ( {
238
+ internalID : id ,
239
+ request : json ,
240
+ } ) ;
217
241
const r = { jsonrpc : "2.0" , result, id } ;
218
242
setResults ( r ) ;
219
243
setTabResults ( tabIndex , r ) ;
220
244
const newHistory : any = [ ...requestHistory , { ...tabs [ tabIndex ] } ] ;
221
245
setRequestHistory ( newHistory ) ;
222
246
} catch ( e ) {
247
+ const convertedError = errorToJSON ( e , id ) ;
223
248
const newHistory : any = [ ...requestHistory , { ...tabs [ tabIndex ] } ] ;
224
249
setRequestHistory ( newHistory ) ;
225
- setResults ( e ) ;
226
- setTabResults ( tabIndex , e ) ;
250
+ setResults ( convertedError ) ;
251
+ setTabResults ( tabIndex , convertedError ) ;
227
252
}
253
+ incrementId ( ) ;
228
254
}
229
255
} ;
230
256
function handleResponseEditorDidMount ( __ : any , editor : any ) {
@@ -246,8 +272,19 @@ const Inspector: React.FC<IProps> = (props) => {
246
272
}
247
273
} ;
248
274
const refreshOpenRpcDocument = async ( ) => {
275
+ if ( ! transport ) {
276
+ return ;
277
+ }
249
278
try {
250
- const d = await client . request ( "rpc.discover" , [ ] ) ;
279
+ const d = await transport . sendData ( {
280
+ internalID : id ,
281
+ request : {
282
+ jsonrpc : "2.0" ,
283
+ params : [ ] ,
284
+ id,
285
+ method : "rpc.discover" ,
286
+ } ,
287
+ } ) ;
251
288
const doc = await parseOpenRPCDocument ( d ) ;
252
289
setOpenRpcDocument ( doc ) ;
253
290
setTabOpenRPCDocument ( tabIndex , doc ) ;
@@ -261,7 +298,7 @@ const Inspector: React.FC<IProps> = (props) => {
261
298
useEffect ( ( ) => {
262
299
refreshOpenRpcDocument ( ) ;
263
300
// eslint-disable-next-line react-hooks/exhaustive-deps
264
- } , [ client , tabIndex ] ) ;
301
+ } , [ transport , tabIndex ] ) ;
265
302
266
303
useEffect ( ( ) => {
267
304
if ( tabs [ tabIndex ] ) {
@@ -516,7 +553,7 @@ const Inspector: React.FC<IProps> = (props) => {
516
553
height = "93vh"
517
554
editorDidMount = { handleResponseEditorDidMount }
518
555
language = "json"
519
- value = { JSON . stringify ( errorToJSON ( results ) || results , null , 4 ) || "" }
556
+ value = { JSON . stringify ( results , null , 4 ) || "" }
520
557
/>
521
558
: < Grid container justify = "center" style = { { paddingTop : "20px" } } direction = "column" alignItems = "center" >
522
559
< Typography variant = "body1" gutterBottom color = "textSecondary" style = { { paddingBottom : "15px" } } >
0 commit comments