|
1 |
| -import * as t from "io-ts"; |
2 |
| -import { isLeft, isRight } from "fp-ts/lib/Either"; |
3 | 1 | import { expectType, TypeEqual } from "ts-expect";
|
4 | 2 | import {
|
5 | 3 | createServer,
|
6 |
| - parse, |
| 4 | + parseJSON, |
7 | 5 | createClient,
|
8 | 6 | RpcError,
|
9 | 7 | Resolvers,
|
10 |
| - ClientRequests, |
| 8 | + ClientRequest, |
11 | 9 | ClientResponse
|
12 | 10 | } from "./index";
|
13 | 11 |
|
14 | 12 | describe("json rpc", () => {
|
15 |
| - const methods = { |
| 13 | + type Methods = { |
16 | 14 | hello: {
|
17 |
| - request: t.undefined, |
18 |
| - response: t.string |
19 |
| - }, |
| 15 | + request: undefined; |
| 16 | + response: string; |
| 17 | + }; |
20 | 18 | echo: {
|
21 |
| - request: t.type({ arg: t.string }), |
22 |
| - response: t.string |
23 |
| - } |
| 19 | + request: { arg: string }; |
| 20 | + response: string; |
| 21 | + }; |
24 | 22 | };
|
25 | 23 |
|
26 |
| - const resolvers: Resolvers<typeof methods> = { |
| 24 | + const resolvers: Resolvers<Methods> = { |
27 | 25 | hello: _ => "Hello World!",
|
28 | 26 | echo: ({ arg }) => arg
|
29 | 27 | };
|
30 | 28 |
|
31 |
| - const server = createServer(methods, resolvers); |
32 |
| - const client = createClient(methods, x => server(x, undefined)); |
| 29 | + const server = createServer(resolvers); |
| 30 | + const client = createClient(x => server(x, undefined)); |
33 | 31 |
|
34 | 32 | describe("types", () => {
|
35 |
| - type Requests = ClientRequests<typeof methods>; |
36 |
| - type Responses = ClientResponse<typeof methods, Requests>; |
| 33 | + type Requests = ClientRequest<Methods>; |
| 34 | + type Responses = ClientResponse<Methods, Requests>; |
37 | 35 |
|
38 | 36 | expectType<TypeEqual<Responses, string>>(true);
|
39 | 37 | });
|
40 | 38 |
|
41 | 39 | describe("parse", () => {
|
42 | 40 | it("should parse json", () => {
|
43 |
| - const result = parse("{}"); |
44 |
| - |
45 |
| - expect(isRight(result)).toEqual(true); |
46 |
| - |
47 |
| - if (isRight(result)) { |
48 |
| - expect(result.right).toEqual({}); |
49 |
| - } |
| 41 | + expect(() => parseJSON("{}")).not.toThrow(); |
50 | 42 | });
|
51 | 43 |
|
52 | 44 | it("should fail to parse malformed json", () => {
|
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 |
| - } |
| 45 | + expect(() => parseJSON("[")).toThrow(RpcError); |
60 | 46 | });
|
61 | 47 | });
|
62 | 48 |
|
@@ -219,63 +205,6 @@ describe("json rpc", () => {
|
219 | 205 | }
|
220 | 206 | });
|
221 | 207 | });
|
222 |
| - |
223 |
| - it("should fail on missing parameters", async () => { |
224 |
| - const res = await server({ |
225 |
| - jsonrpc: "2.0", |
226 |
| - id: "test", |
227 |
| - method: "echo" |
228 |
| - }); |
229 |
| - |
230 |
| - expect(res).toEqual({ |
231 |
| - jsonrpc: "2.0", |
232 |
| - id: "test", |
233 |
| - error: { |
234 |
| - code: -32602, |
235 |
| - message: "Invalid value undefined supplied to : { arg: string }" |
236 |
| - } |
237 |
| - }); |
238 |
| - }); |
239 |
| - |
240 |
| - it("should fail on invalid parameters", async () => { |
241 |
| - const res = await server({ |
242 |
| - jsonrpc: "2.0", |
243 |
| - id: "test", |
244 |
| - method: "echo", |
245 |
| - params: "test" |
246 |
| - }); |
247 |
| - |
248 |
| - expect(res).toEqual({ |
249 |
| - jsonrpc: "2.0", |
250 |
| - id: "test", |
251 |
| - error: { |
252 |
| - code: -32602, |
253 |
| - message: 'Invalid value "test" supplied to : { arg: string }' |
254 |
| - } |
255 |
| - }); |
256 |
| - }); |
257 |
| - |
258 |
| - describe("without type checking", () => { |
259 |
| - const server = createServer(methods, resolvers, { |
260 |
| - encode: false, |
261 |
| - decode: false |
262 |
| - }); |
263 |
| - |
264 |
| - it("should succeed", async () => { |
265 |
| - const result = await server({ |
266 |
| - jsonrpc: "2.0", |
267 |
| - id: 1, |
268 |
| - method: "hello", |
269 |
| - params: {} |
270 |
| - }); |
271 |
| - |
272 |
| - expect(result).toEqual({ |
273 |
| - jsonrpc: "2.0", |
274 |
| - id: 1, |
275 |
| - result: "Hello World!" |
276 |
| - }); |
277 |
| - }); |
278 |
| - }); |
279 | 208 | });
|
280 | 209 | });
|
281 | 210 |
|
@@ -306,23 +235,10 @@ describe("json rpc", () => {
|
306 | 235 | ).rejects.toBeInstanceOf(RpcError);
|
307 | 236 | });
|
308 | 237 |
|
309 |
| - describe("without type checking", () => { |
310 |
| - const client = createClient(methods, x => server(x), { |
311 |
| - encode: false, |
312 |
| - decode: false |
313 |
| - }); |
314 |
| - |
315 |
| - it("should succeed", async () => { |
316 |
| - const result = await client({ method: "hello", params: undefined }); |
317 |
| - |
318 |
| - expect(result).toEqual("Hello World!"); |
319 |
| - }); |
320 |
| - }); |
321 |
| - |
322 |
| - describe("with send options", () => { |
323 |
| - const client = createClient(methods, (_, options: string) => |
324 |
| - Promise.resolve({ result: options }) |
325 |
| - ); |
| 238 | + describe("with send context", () => { |
| 239 | + const client = createClient(async (_, context: string) => ({ |
| 240 | + result: context |
| 241 | + })); |
326 | 242 |
|
327 | 243 | it("should accept options", async () => {
|
328 | 244 | const result = await client(
|
@@ -377,40 +293,4 @@ describe("json rpc", () => {
|
377 | 293 | });
|
378 | 294 | });
|
379 | 295 | });
|
380 |
| - |
381 |
| - describe("intersection types", () => { |
382 |
| - const methods = { |
383 |
| - test: { |
384 |
| - request: t.intersection([ |
385 |
| - t.type({ url: t.string }), |
386 |
| - t.partial({ accept: t.string }) |
387 |
| - ]), |
388 |
| - response: t.string |
389 |
| - } |
390 |
| - }; |
391 |
| - |
392 |
| - const server = createServer(methods, { |
393 |
| - test: ({ url, accept }) => `${url}#${accept}` |
394 |
| - }); |
395 |
| - |
396 |
| - const client = createClient(methods, server); |
397 |
| - |
398 |
| - it("should support intersection types", async () => { |
399 |
| - const result = await client({ |
400 |
| - method: "test", |
401 |
| - params: { url: "http://example.com", accept: "json" } |
402 |
| - }); |
403 |
| - |
404 |
| - expect(result).toEqual(`http://example.com#json`); |
405 |
| - }); |
406 |
| - |
407 |
| - it("should support intersection type with optional key", async () => { |
408 |
| - const result = await client({ |
409 |
| - method: "test", |
410 |
| - params: { url: "http://example.com" } |
411 |
| - }); |
412 |
| - |
413 |
| - expect(result).toEqual(`http://example.com#undefined`); |
414 |
| - }); |
415 |
| - }); |
416 | 296 | });
|
0 commit comments