@open-rpc/client-js
Advanced tools
Comparing version 1.3.2 to 1.3.3
@@ -10,3 +10,2 @@ "use strict"; | ||
return { | ||
id: null, | ||
jsonrpc: "2.0", | ||
@@ -13,0 +12,0 @@ method: method, |
@@ -10,2 +10,4 @@ "use strict"; | ||
var events_1 = require("events"); | ||
var eventEmitter_1 = require("./__mocks__/eventEmitter"); | ||
var requestData_1 = require("./__mocks__/requestData"); | ||
describe("client-js", function () { | ||
@@ -29,2 +31,9 @@ it("can be constructed", function () { | ||
}); | ||
it("can recieve notifications", function (done) { | ||
var emitter = new events_1.EventEmitter(); | ||
var c = new _1.default(new RequestManager_1.default([new EventEmitterTransport_1.default(emitter, "from1", "to1")])); | ||
eventEmitter_1.addMockServerTransport(emitter, "from1", "to1://asdf/rpc-notification"); | ||
c.onNotification(function () { return done(); }); | ||
emitter.emit("to1", JSON.stringify(requestData_1.generateMockNotificationRequest("foo", ["bar"]))); | ||
}); | ||
it("can register error and subscription handlers", function () { | ||
@@ -31,0 +40,0 @@ var emitter = new events_1.EventEmitter(); |
@@ -129,3 +129,3 @@ "use strict"; | ||
RequestManager.prototype.handleNotification = function (data) { | ||
this.requestChannel.emit(data); | ||
this.requestChannel.emit("notification", data); | ||
}; | ||
@@ -132,0 +132,0 @@ return RequestManager; |
@@ -80,3 +80,3 @@ "use strict"; | ||
} | ||
return payload.every(function (datum) { return (datum.result !== undefined || datum.error !== undefined); }); | ||
return payload.every(function (datum) { return (datum.result !== undefined || datum.error !== undefined || datum.method !== undefined); }); | ||
}; | ||
@@ -83,0 +83,0 @@ TransportRequestManager.prototype.processResult = function (payload, prom) { |
@@ -0,1 +1,11 @@ | ||
## [1.3.3](https://github.com/open-rpc/client-js/compare/1.3.2...1.3.3) (2020-07-17) | ||
### Bug Fixes | ||
* **README:** add collapsed examples ([588bcba](https://github.com/open-rpc/client-js/commit/588bcbaa7bbb6f019fff6ed654c122212b8e360f)) | ||
* **README:** example formatting ([840857f](https://github.com/open-rpc/client-js/commit/840857f02c064bd9f8a96227c628eaabca7a8b29)) | ||
* add failing notification test ([a638062](https://github.com/open-rpc/client-js/commit/a638062d3870b2c93307db6dccc778265596f4f8)) | ||
* onNotification not firing bug ([0b62f9c](https://github.com/open-rpc/client-js/commit/0b62f9cdb431e1cd2d79c6b2ff0fadd38426e918)) | ||
## [1.3.2](https://github.com/open-rpc/client-js/compare/1.3.1...1.3.2) (2020-07-17) | ||
@@ -2,0 +12,0 @@ |
{ | ||
"name": "@open-rpc/client-js", | ||
"version": "1.3.2", | ||
"version": "1.3.3", | ||
"description": " A browser-compatible JSON-RPC client with multiple transports.", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -29,4 +29,93 @@ # OpenRPC Client JS | ||
#### Examples | ||
<details> | ||
<summary>EventEmitter</summary> | ||
```javascript | ||
import { EventEmitter } from "events"; | ||
import { RequestManager, EventEmitterTransport, Client } from "@open-rpc/client-js"; | ||
const chan1 = "chan1"; | ||
const chan2 = "chan2"; | ||
const emitter = new EventEmitter(); | ||
const transport = new EventEmitterTransport(emitter, chan1, chan2); | ||
const requestManager = new RequestManager([transport]); | ||
const client = new Client(requestManager); | ||
// event emitter server code | ||
emitter.on(chan1, (jsonrpcRequest) => { | ||
const res = { | ||
jsonrpc: "2.0", | ||
result: "potato", | ||
id: jsonrpcRequest.id, | ||
}; | ||
emitter.emit(chan2, JSON.stringify(res)); | ||
}); | ||
const main = async () => { | ||
const result = await client.request("addition", [2, 2]); | ||
console.log(result); | ||
}; | ||
main().then(() => { | ||
console.log("DONE"); | ||
}); | ||
``` | ||
</details> | ||
<details> | ||
<summary>HTTP</summary> | ||
```javascript | ||
import { EventEmitter } from "events"; | ||
import { RequestManager, Client, HTTPTransport } from "@open-rpc/client-js"; | ||
const emitter = new EventEmitter(); | ||
const transport = new HTTPTransport("http://localhost:3333"); | ||
const requestManager = new RequestManager([transport]); | ||
const client = new Client(requestManager); | ||
const main = async () => { | ||
const result = await client.request("addition", [2, 2]); | ||
console.log(result); | ||
}; | ||
main().then(() => { | ||
console.log("DONE"); | ||
}); | ||
``` | ||
</details> | ||
<details> | ||
<summary>Websocket</summary> | ||
```javascript | ||
import { RequestManager, Client, WebSocketTransport } from "@open-rpc/client-js"; | ||
const transport = new WebSocketTransport("ws://localhost:3333"); | ||
const requestManager = new RequestManager([transport]); | ||
const client = new Client(requestManager); | ||
const main = async () => { | ||
const result = await client.request("addition", [2, 2]); | ||
console.log(result); | ||
}; | ||
main().then(() => { | ||
console.log("DONE"); | ||
}); | ||
``` | ||
</details> | ||
### Contributing | ||
How to contribute, build and release are outlined in [CONTRIBUTING.md](CONTRIBUTING.md), [BUILDING.md](BUILDING.md) and [RELEASING.md](RELEASING.md) respectively. Commits in this repository follow the [CONVENTIONAL_COMMITS.md](CONVENTIONAL_COMMITS.md) specification. |
@@ -6,3 +6,2 @@ import * as req from "../Request"; | ||
return { | ||
id: null, | ||
jsonrpc: "2.0", | ||
@@ -9,0 +8,0 @@ method, |
@@ -5,2 +5,4 @@ import Client from "."; | ||
import { EventEmitter } from "events"; | ||
import { addMockServerTransport } from "./__mocks__/eventEmitter"; | ||
import { generateMockNotificationRequest } from "./__mocks__/requestData"; | ||
@@ -28,2 +30,10 @@ describe("client-js", () => { | ||
it("can recieve notifications", (done) => { | ||
const emitter = new EventEmitter(); | ||
const c = new Client(new RequestManager([new EventEmitterTransport(emitter, "from1", "to1")])); | ||
addMockServerTransport(emitter, "from1", "to1://asdf/rpc-notification"); | ||
c.onNotification(() => done()); | ||
emitter.emit("to1", JSON.stringify(generateMockNotificationRequest("foo", ["bar"]))); | ||
}); | ||
it("can register error and subscription handlers", () => { | ||
@@ -30,0 +40,0 @@ const emitter = new EventEmitter(); |
@@ -105,3 +105,3 @@ import { Transport } from "./transports/Transport"; | ||
private handleNotification(data: any) { | ||
this.requestChannel.emit(data); | ||
this.requestChannel.emit("notification", data); | ||
} | ||
@@ -108,0 +108,0 @@ |
@@ -92,3 +92,3 @@ import { | ||
} | ||
return payload.every((datum) => (datum.result !== undefined || datum.error !== undefined)); | ||
return payload.every((datum) => (datum.result !== undefined || datum.error !== undefined || datum.method !== undefined)); | ||
} | ||
@@ -95,0 +95,0 @@ |
260643
4660
121