Skip to content

Commit d7f9eb7

Browse files
committed
Remove io-ts from package
1 parent d65a6e6 commit d7f9eb7

File tree

5 files changed

+127
-317
lines changed

5 files changed

+127
-317
lines changed

README.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
[![Build status][travis-image]][travis-url]
66
[![Test coverage][coveralls-image]][coveralls-url]
77

8-
> Tiny, type-safe [JSON-RPC 2.0](https://www.jsonrpc.org/specification) implementation.
8+
> Tiny, mostly compliant [JSON-RPC 2.0](https://www.jsonrpc.org/specification) implementation.
9+
10+
This package intentionally doesn't implement the "arguments" form of request parameters. This is when the input `params` can be an object or an ordered array representing the object. Instead, you can pass _any_ JSON params over the wire.
911

1012
## Installation
1113

1214
```sh
1315
npm install @borderlesslabs/json-rpc --save
14-
15-
# Peer dependencies.
16-
npm install io-ts fp-ts --save
1716
```
1817

1918
## Usage
@@ -22,33 +21,27 @@ This package makes no assumptions about the transportation layer, for client or
2221

2322
### Methods
2423

25-
The methods definition uses [`io-ts`](https://github.com/gcanti/io-ts) to encode and decode requests or responses.
26-
2724
```ts
28-
import * as t from "io-ts";
29-
30-
const methods = {
25+
type Methods = {
3126
hello: {
32-
// `request` is required, even when empty.
33-
request: t.type({}),
34-
response: t.string
35-
},
27+
request: {};
28+
response: string;
29+
};
3630
echo: {
37-
// Specify `request` parameters as keys of the object.
38-
request: t.type({ arg: t.string }),
39-
response: t.string
40-
}
31+
request: { arg: string };
32+
response: string;
33+
};
4134
};
4235
```
4336

4437
### Server
4538

46-
The server takes the methods and a dictionary of matching resolvers.
39+
The server accepts a dictionary of resolvers.
4740

4841
```ts
4942
import { createClient } from "@borderlesslabs/json-rpc";
5043

51-
const server = createServer(methods, {
44+
const server = createServer<Methods>({
5245
hello: _ => "Hello World!",
5346
echo: ({ arg }) => arg
5447
});
@@ -62,15 +55,15 @@ const res = await server({
6255

6356
### Client
6457

65-
The client takes the methods and a function to `send` the JSON-RPC request.
58+
The client accepts a function to `send` the JSON-RPC request.
6659

6760
```ts
6861
import { createClient } from "@borderlesslabs/json-rpc";
6962

70-
const client = createClient(methods, async x => {
63+
const client = createClient(async payload => {
7164
const res = await fetch("...", {
7265
method: "POST",
73-
body: JSON.stringify(x),
66+
body: JSON.stringify(payload),
7467
headers: {
7568
"Content-Type": "application/json"
7669
}

package-lock.json

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

package.json

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,13 @@
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",
8382
"husky": "^4.2.3",
84-
"io-ts": "^2.0.4",
8583
"jest": "^25.1.0",
8684
"lint-staged": "^10.0.8",
8785
"prettier": "^1.19.1",
8886
"rimraf": "^3.0.0",
8987
"ts-expect": "^1.1.0",
9088
"ts-jest": "^25.2.1",
9189
"typescript": "^3.7.4"
92-
},
93-
"peerDependencies": {
94-
"io-ts": "^2.0.4",
95-
"fp-ts": "^2.4.0"
9690
}
9791
}

src/index.spec.ts

Lines changed: 20 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
1-
import * as t from "io-ts";
2-
import { isLeft, isRight } from "fp-ts/lib/Either";
31
import { expectType, TypeEqual } from "ts-expect";
42
import {
53
createServer,
6-
parse,
4+
parseJSON,
75
createClient,
86
RpcError,
97
Resolvers,
10-
ClientRequests,
8+
ClientRequest,
119
ClientResponse
1210
} from "./index";
1311

1412
describe("json rpc", () => {
15-
const methods = {
13+
type Methods = {
1614
hello: {
17-
request: t.undefined,
18-
response: t.string
19-
},
15+
request: undefined;
16+
response: string;
17+
};
2018
echo: {
21-
request: t.type({ arg: t.string }),
22-
response: t.string
23-
}
19+
request: { arg: string };
20+
response: string;
21+
};
2422
};
2523

26-
const resolvers: Resolvers<typeof methods> = {
24+
const resolvers: Resolvers<Methods> = {
2725
hello: _ => "Hello World!",
2826
echo: ({ arg }) => arg
2927
};
3028

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));
3331

3432
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>;
3735

3836
expectType<TypeEqual<Responses, string>>(true);
3937
});
4038

4139
describe("parse", () => {
4240
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();
5042
});
5143

5244
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);
6046
});
6147
});
6248

@@ -219,63 +205,6 @@ describe("json rpc", () => {
219205
}
220206
});
221207
});
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-
});
279208
});
280209
});
281210

@@ -306,23 +235,10 @@ describe("json rpc", () => {
306235
).rejects.toBeInstanceOf(RpcError);
307236
});
308237

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+
}));
326242

327243
it("should accept options", async () => {
328244
const result = await client(
@@ -377,40 +293,4 @@ describe("json rpc", () => {
377293
});
378294
});
379295
});
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-
});
416296
});

0 commit comments

Comments
 (0)