1
+ import * as t from "io-ts" ;
2
+ import { isLeft , isRight } from "fp-ts/lib/Either" ;
1
3
import { expectType , TypeEqual } from "ts-expect" ;
2
4
import {
3
- schema ,
4
5
createServer ,
5
6
parse ,
6
7
createClient ,
@@ -13,12 +14,12 @@ import {
13
14
describe ( "json rpc" , ( ) => {
14
15
const methods = {
15
16
hello : {
16
- request : schema . object ( { } ) ,
17
- response : schema . string ( )
17
+ request : t . undefined ,
18
+ response : t . string
18
19
} ,
19
20
echo : {
20
- request : schema . object ( { arg : schema . string ( ) } ) ,
21
- response : schema . string ( )
21
+ request : t . type ( { arg : t . string } ) ,
22
+ response : t . string
22
23
}
23
24
} ;
24
25
@@ -28,7 +29,7 @@ describe("json rpc", () => {
28
29
} ;
29
30
30
31
const server = createServer ( methods , resolvers ) ;
31
- const client = createClient ( methods , server ) ;
32
+ const client = createClient ( methods , x => server ( x , undefined ) ) ;
32
33
33
34
describe ( "types" , ( ) => {
34
35
type Requests = ClientRequests < typeof methods > ;
@@ -39,11 +40,23 @@ describe("json rpc", () => {
39
40
40
41
describe ( "parse" , ( ) => {
41
42
it ( "should parse json" , ( ) => {
42
- expect ( parse ( "{}" ) ) . toEqual ( { } ) ;
43
+ const result = parse ( "{}" ) ;
44
+
45
+ expect ( isRight ( result ) ) . toEqual ( true ) ;
46
+
47
+ if ( isRight ( result ) ) {
48
+ expect ( result . right ) . toEqual ( { } ) ;
49
+ }
43
50
} ) ;
44
51
45
52
it ( "should fail to parse malformed json" , ( ) => {
46
- expect ( ( ) => parse ( "[" ) ) . toThrow ( SyntaxError ) ;
53
+ const result = parse ( "[" ) ;
54
+
55
+ expect ( isLeft ( result ) ) . toEqual ( true ) ;
56
+
57
+ if ( isLeft ( result ) ) {
58
+ expect ( result . left ) . toEqual ( { code : - 32700 , message : "Parse error" } ) ;
59
+ }
47
60
} ) ;
48
61
} ) ;
49
62
@@ -219,25 +232,7 @@ describe("json rpc", () => {
219
232
id : "test" ,
220
233
error : {
221
234
code : - 32602 ,
222
- message : "arg: Non-string type: undefined"
223
- }
224
- } ) ;
225
- } ) ;
226
-
227
- it ( "should fail on invalid parameters" , async ( ) => {
228
- const res = await server ( {
229
- jsonrpc : "2.0" ,
230
- id : "test" ,
231
- method : "echo" ,
232
- params : "test"
233
- } ) ;
234
-
235
- expect ( res ) . toEqual ( {
236
- jsonrpc : "2.0" ,
237
- id : "test" ,
238
- error : {
239
- code : - 32600 ,
240
- message : "Invalid request"
235
+ message : "Invalid value undefined supplied to : { arg: string }"
241
236
}
242
237
} ) ;
243
238
} ) ;
@@ -254,8 +249,8 @@ describe("json rpc", () => {
254
249
jsonrpc : "2.0" ,
255
250
id : "test" ,
256
251
error : {
257
- code : - 32600 ,
258
- message : " Invalid request"
252
+ code : - 32602 ,
253
+ message : ' Invalid value "test" supplied to : { arg: string }'
259
254
}
260
255
} ) ;
261
256
} ) ;
@@ -287,28 +282,28 @@ describe("json rpc", () => {
287
282
describe ( "client" , ( ) => {
288
283
describe ( "request" , ( ) => {
289
284
it ( "should make a request" , async ( ) => {
290
- const result = await client ( { method : "hello" , params : { } } ) ;
285
+ const result = await client ( { method : "hello" , params : undefined } ) ;
291
286
292
287
expect ( result ) . toEqual ( "Hello World!" ) ;
293
288
} ) ;
294
289
295
290
it ( "should make a notification request" , async ( ) => {
296
291
const result = await client ( {
297
292
method : "hello" ,
298
- params : { } ,
293
+ params : undefined ,
299
294
async : true
300
295
} ) ;
301
296
302
297
expect ( result ) . toEqual ( undefined ) ;
303
298
} ) ;
304
299
305
- it ( "should throw on invalid argument " , async ( ) => {
300
+ it ( "should throw rpc errors " , async ( ) => {
306
301
await expect (
307
302
client ( {
308
303
method : "echo" ,
309
304
params : { } as any
310
305
} )
311
- ) . rejects . toBeInstanceOf ( Error ) ;
306
+ ) . rejects . toBeInstanceOf ( RpcError ) ;
312
307
} ) ;
313
308
314
309
describe ( "without type checking" , ( ) => {
@@ -318,7 +313,7 @@ describe("json rpc", () => {
318
313
} ) ;
319
314
320
315
it ( "should succeed" , async ( ) => {
321
- const result = await client ( { method : "hello" , params : { } } ) ;
316
+ const result = await client ( { method : "hello" , params : undefined } ) ;
322
317
323
318
expect ( result ) . toEqual ( "Hello World!" ) ;
324
319
} ) ;
@@ -330,7 +325,10 @@ describe("json rpc", () => {
330
325
) ;
331
326
332
327
it ( "should accept options" , async ( ) => {
333
- const result = await client ( { method : "hello" , params : { } } , "Test" ) ;
328
+ const result = await client (
329
+ { method : "hello" , params : undefined } ,
330
+ "Test"
331
+ ) ;
334
332
335
333
expect ( result ) . toEqual ( "Test" ) ;
336
334
} ) ;
@@ -340,7 +338,7 @@ describe("json rpc", () => {
340
338
describe ( "many" , ( ) => {
341
339
it ( "should make a many request" , async ( ) => {
342
340
const result = await client . many ( [
343
- { method : "hello" , params : { } } ,
341
+ { method : "hello" , params : undefined } ,
344
342
{ method : "echo" , params : { arg : "test" } }
345
343
] ) ;
346
344
@@ -349,7 +347,7 @@ describe("json rpc", () => {
349
347
350
348
it ( "should make a many notification request" , async ( ) => {
351
349
const result = await client . many ( [
352
- { method : "hello" , params : { } , async : true } ,
350
+ { method : "hello" , params : undefined , async : true } ,
353
351
{ method : "echo" , params : { arg : "test" } , async : true }
354
352
] ) ;
355
353
@@ -358,35 +356,36 @@ describe("json rpc", () => {
358
356
359
357
it ( "should handle mixed many responses" , async ( ) => {
360
358
const result = await client . many ( [
361
- { method : "hello" , params : { } , async : true } ,
359
+ { method : "hello" , params : undefined , async : true } ,
362
360
{ method : "echo" , params : { arg : "test" } } ,
363
- { method : "hello" , params : { } , async : true }
361
+ { method : "hello" , params : undefined , async : true }
364
362
] ) ;
365
363
366
364
expect ( result ) . toEqual ( [ undefined , "test" , undefined ] ) ;
367
365
} ) ;
368
366
369
- it ( "should throw on invalid argument" , async ( ) => {
370
- expect (
371
- client . many ( [
372
- {
373
- method : "echo" ,
374
- params : { } as any
375
- }
376
- ] )
377
- ) . rejects . toBeInstanceOf ( Error ) ;
367
+ it ( "should return rpc errors" , async ( ) => {
368
+ const result = await client . many ( [
369
+ {
370
+ method : "echo" ,
371
+ params : { } as any
372
+ }
373
+ ] ) ;
374
+
375
+ expect ( result . length ) . toEqual ( 1 ) ;
376
+ expect ( result [ 0 ] ) . toBeInstanceOf ( RpcError ) ;
378
377
} ) ;
379
378
} ) ;
380
379
} ) ;
381
380
382
381
describe ( "intersection types" , ( ) => {
383
382
const methods = {
384
383
test : {
385
- request : schema . intersection (
386
- schema . object ( { url : schema . string ( ) } ) ,
387
- schema . object ( { accept : schema . string ( ) . optional ( ) } )
388
- ) ,
389
- response : schema . string ( )
384
+ request : t . intersection ( [
385
+ t . type ( { url : t . string } ) ,
386
+ t . partial ( { accept : t . string } )
387
+ ] ) ,
388
+ response : t . string
390
389
}
391
390
} ;
392
391
0 commit comments