Skip to content

Commit 3f35111

Browse files
committed
Revert to io-ts
1 parent 9c2e216 commit 3f35111

File tree

4 files changed

+100
-95
lines changed

4 files changed

+100
-95
lines changed

package-lock.json

Lines changed: 13 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@
7979
"eslint": "^6.8.0",
8080
"eslint-config-prettier": "^6.9.0",
8181
"eslint-plugin-prettier": "^3.1.2",
82+
"fp-ts": "^2.4.0",
8283
"husky": "^4.2.3",
84+
"io-ts": "^2.0.4",
8385
"jest": "^25.1.0",
8486
"lint-staged": "^10.0.8",
8587
"prettier": "^1.19.1",
@@ -88,7 +90,8 @@
8890
"ts-jest": "^25.2.1",
8991
"typescript": "^3.7.4"
9092
},
91-
"dependencies": {
92-
"zod": "^1.0.9"
93+
"peerDependencies": {
94+
"io-ts": "^2.0.4",
95+
"fp-ts": "^2.4.0"
9396
}
9497
}

src/index.spec.ts

Lines changed: 52 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import * as t from "io-ts";
2+
import { isLeft, isRight } from "fp-ts/lib/Either";
13
import { expectType, TypeEqual } from "ts-expect";
24
import {
3-
schema,
45
createServer,
56
parse,
67
createClient,
@@ -13,12 +14,12 @@ import {
1314
describe("json rpc", () => {
1415
const methods = {
1516
hello: {
16-
request: schema.object({}),
17-
response: schema.string()
17+
request: t.undefined,
18+
response: t.string
1819
},
1920
echo: {
20-
request: schema.object({ arg: schema.string() }),
21-
response: schema.string()
21+
request: t.type({ arg: t.string }),
22+
response: t.string
2223
}
2324
};
2425

@@ -28,7 +29,7 @@ describe("json rpc", () => {
2829
};
2930

3031
const server = createServer(methods, resolvers);
31-
const client = createClient(methods, server);
32+
const client = createClient(methods, x => server(x, undefined));
3233

3334
describe("types", () => {
3435
type Requests = ClientRequests<typeof methods>;
@@ -39,11 +40,23 @@ describe("json rpc", () => {
3940

4041
describe("parse", () => {
4142
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+
}
4350
});
4451

4552
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+
}
4760
});
4861
});
4962

@@ -219,25 +232,7 @@ describe("json rpc", () => {
219232
id: "test",
220233
error: {
221234
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 }"
241236
}
242237
});
243238
});
@@ -254,8 +249,8 @@ describe("json rpc", () => {
254249
jsonrpc: "2.0",
255250
id: "test",
256251
error: {
257-
code: -32600,
258-
message: "Invalid request"
252+
code: -32602,
253+
message: 'Invalid value "test" supplied to : { arg: string }'
259254
}
260255
});
261256
});
@@ -287,28 +282,28 @@ describe("json rpc", () => {
287282
describe("client", () => {
288283
describe("request", () => {
289284
it("should make a request", async () => {
290-
const result = await client({ method: "hello", params: {} });
285+
const result = await client({ method: "hello", params: undefined });
291286

292287
expect(result).toEqual("Hello World!");
293288
});
294289

295290
it("should make a notification request", async () => {
296291
const result = await client({
297292
method: "hello",
298-
params: {},
293+
params: undefined,
299294
async: true
300295
});
301296

302297
expect(result).toEqual(undefined);
303298
});
304299

305-
it("should throw on invalid argument", async () => {
300+
it("should throw rpc errors", async () => {
306301
await expect(
307302
client({
308303
method: "echo",
309304
params: {} as any
310305
})
311-
).rejects.toBeInstanceOf(Error);
306+
).rejects.toBeInstanceOf(RpcError);
312307
});
313308

314309
describe("without type checking", () => {
@@ -318,7 +313,7 @@ describe("json rpc", () => {
318313
});
319314

320315
it("should succeed", async () => {
321-
const result = await client({ method: "hello", params: {} });
316+
const result = await client({ method: "hello", params: undefined });
322317

323318
expect(result).toEqual("Hello World!");
324319
});
@@ -330,7 +325,10 @@ describe("json rpc", () => {
330325
);
331326

332327
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+
);
334332

335333
expect(result).toEqual("Test");
336334
});
@@ -340,7 +338,7 @@ describe("json rpc", () => {
340338
describe("many", () => {
341339
it("should make a many request", async () => {
342340
const result = await client.many([
343-
{ method: "hello", params: {} },
341+
{ method: "hello", params: undefined },
344342
{ method: "echo", params: { arg: "test" } }
345343
]);
346344

@@ -349,7 +347,7 @@ describe("json rpc", () => {
349347

350348
it("should make a many notification request", async () => {
351349
const result = await client.many([
352-
{ method: "hello", params: {}, async: true },
350+
{ method: "hello", params: undefined, async: true },
353351
{ method: "echo", params: { arg: "test" }, async: true }
354352
]);
355353

@@ -358,35 +356,36 @@ describe("json rpc", () => {
358356

359357
it("should handle mixed many responses", async () => {
360358
const result = await client.many([
361-
{ method: "hello", params: {}, async: true },
359+
{ method: "hello", params: undefined, async: true },
362360
{ method: "echo", params: { arg: "test" } },
363-
{ method: "hello", params: {}, async: true }
361+
{ method: "hello", params: undefined, async: true }
364362
]);
365363

366364
expect(result).toEqual([undefined, "test", undefined]);
367365
});
368366

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);
378377
});
379378
});
380379
});
381380

382381
describe("intersection types", () => {
383382
const methods = {
384383
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
390389
}
391390
};
392391

0 commit comments

Comments
 (0)