Skip to content

Commit

Permalink
Merge pull request #194 from zcstarr/master
Browse files Browse the repository at this point in the history
fix: service runner to support new clientjs
  • Loading branch information
zcstarr committed Sep 30, 2019
2 parents 70c6728 + 86f4611 commit a9f537c
Show file tree
Hide file tree
Showing 4 changed files with 716 additions and 115 deletions.
51 changes: 43 additions & 8 deletions fixtures/src/testService/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

// Code generated by @open-rpc/client-generator DO NOT EDIT.
import { RequestManager, WebSocketTransport, HTTPTransport, Client } from "@open-rpc/client-js";
import { RequestManager, WebSocketTransport, HTTPTransport, Client, JSONRPCError } from "@open-rpc/client-js";
import _ from "lodash";
import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema";
import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils-js";
Expand All @@ -19,21 +19,21 @@ export interface Options {
}

export class SimpleMath {
public static openrpcDocument: OpenRPC = { "openrpc": "1.0.0-rc1", "info": { "title": "Simple Math", "description": "A simple math example", "version": "1.0.0" }, "servers": [{ "name": "my simple math server", "summary": "simple math server summary", "description": "simple math server description", "url": "http://${username}.simple-math.example.org:${port}/${basePath}/", "variables": { "username": { "default": "demo", "description": "this is applied to the url as the subdomain" }, "port": { "default": "443", "enum": ["8545", "443"] }, "basePath": { "default": "jsonrpc" } } }], "methods": [{ "name": "addition", "params": [{ "name": "a", "schema": { "type": "integer" } }, { "name": "b", "schema": { "type": "integer" } }], "result": { "name": "c", "schema": { "type": "integer" } }, "examples": [{ "name": "simpleMathAdditionTwo", "params": [{ "name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2 }, { "name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2 }], "result": { "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 } }, { "name": "simpleMathAdditionFour", "params": [{ "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 }, { "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 }], "result": { "name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8 } }], "links": [{ "name": "subtractionLink", "description": "use the parameters from addition for subtraction", "method": "subtraction", "params": { "a": "$params.a", "b": "$params.b" } }] }, { "name": "subtraction", "params": [{ "name": "a", "schema": { "type": "integer" } }, { "name": "b", "schema": { "type": "integer" } }], "result": { "name": "c", "schema": { "type": "integer" } }, "examples": [{ "name": "examplesSubtractFourTwo", "params": [{ "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 }, { "name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2 }], "result": { "name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2 } }, { "name": "examplesSubtractEightFour", "params": [{ "name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8 }, { "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 }], "result": { "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 } }], "links": [{ "name": "additionLink", "description": "use the parameters from subtraction for addition", "method": "addition", "params": { "a": "$params.a", "b": "$params.b" } }] }], "components": { "contentDescriptors": { "c": { "name": "c", "schema": { "type": "integer" } } }, "schemas": { "Integer": { "type": "integer" } }, "examples": { "integerTwo": { "name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2 }, "integerFour": { "name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4 }, "integerEight": { "name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8 } } } };
public static openrpcDocument: OpenRPC = {"openrpc": "1.0.0-rc1", "info": {"title": "Simple Math", "description": "A simple math example", "version": "1.0.0"}, "servers": [{"name": "my simple math server", "summary": "simple math server summary", "description": "simple math server description", "url": "http://${username}.simple-math.example.org:${port}/${basePath}/", "variables": {"username": {"default": "demo", "description": "this is applied to the url as the subdomain"}, "port": {"default": "443", "enum": ["8545", "443"]}, "basePath": {"default": "jsonrpc"}}}], "methods": [{"name": "addition", "params": [{"name": "a", "schema": {"type": "integer"}}, {"name": "b", "schema": {"type": "integer"}}], "result": {"name": "c", "schema": {"type": "integer"}}, "examples": [{"name": "simpleMathAdditionTwo", "params": [{"name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2}, {"name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2}], "result": {"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}}, {"name": "simpleMathAdditionFour", "params": [{"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}, {"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}], "result": {"name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8}}], "links": [{"name": "subtractionLink", "description": "use the parameters from addition for subtraction", "method": "subtraction", "params": {"a": "$params.a", "b": "$params.b"}}]}, {"name": "subtraction", "params": [{"name": "a", "schema": {"type": "integer"}}, {"name": "b", "schema": {"type": "integer"}}], "result": {"name": "c", "schema": {"type": "integer"}}, "examples": [{"name": "examplesSubtractFourTwo", "params": [{"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}, {"name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2}], "result": {"name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2}}, {"name": "examplesSubtractEightFour", "params": [{"name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8}, {"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}], "result": {"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}}], "links": [{"name": "additionLink", "description": "use the parameters from subtraction for addition", "method": "addition", "params": {"a": "$params.a", "b": "$params.b"}}]}], "components": {"contentDescriptors": {"c": {"name": "c", "schema": {"type": "integer"}}}, "schemas": {"Integer": {"type": "integer"}}, "examples": {"integerTwo": {"name": "two", "summary": "its a sample two", "description": "Im not sure how else to say two", "value": 2}, "integerFour": {"name": "four", "summary": "its a sample four", "description": "Im not sure how else to say four", "value": 4}, "integerEight": {"name": "eight", "summary": "its a sample eight", "description": "Im not sure how else to say eight", "value": 8}}}} ;
public rpc: Client;
public transport: HTTPTransport | WebSocketTransport;
private validator: MethodCallValidator;
private timeout: number | undefined;

constructor(options: Options) {
// this.openrpcDocument = {"openrpc":"1.0.0-rc1","info":{"title":"Simple Math","description":"A simple math example","version":"1.0.0"},"servers":[{"name":"my simple math server","summary":"simple math server summary","description":"simple math server description","url":"http://${username}.simple-math.example.org:${port}/${basePath}/","variables":{"username":{"default":"demo","description":"this is applied to the url as the subdomain"},"port":{"default":"443","enum":["8545","443"]},"basePath":{"default":"jsonrpc"}}}],"methods":[{"name":"addition","params":[{"name":"a","schema":{"type":"integer"}},{"name":"b","schema":{"type":"integer"}}],"result":{"name":"c","schema":{"type":"integer"}},"examples":[{"name":"simpleMathAdditionTwo","params":[{"name":"two","summary":"its a sample two","description":"Im not sure how else to say two","value":2},{"name":"two","summary":"its a sample two","description":"Im not sure how else to say two","value":2}],"result":{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4}},{"name":"simpleMathAdditionFour","params":[{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4},{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4}],"result":{"name":"eight","summary":"its a sample eight","description":"Im not sure how else to say eight","value":8}}],"links":[{"name":"subtractionLink","description":"use the parameters from addition for subtraction","method":"subtraction","params":{"a":"$params.a","b":"$params.b"}}]},{"name":"subtraction","params":[{"name":"a","schema":{"type":"integer"}},{"name":"b","schema":{"type":"integer"}}],"result":{"name":"c","schema":{"type":"integer"}},"examples":[{"name":"examplesSubtractFourTwo","params":[{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4},{"name":"two","summary":"its a sample two","description":"Im not sure how else to say two","value":2}],"result":{"name":"two","summary":"its a sample two","description":"Im not sure how else to say two","value":2}},{"name":"examplesSubtractEightFour","params":[{"name":"eight","summary":"its a sample eight","description":"Im not sure how else to say eight","value":8},{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4}],"result":{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4}}],"links":[{"name":"additionLink","description":"use the parameters from subtraction for addition","method":"addition","params":{"a":"$params.a","b":"$params.b"}}]}],"components":{"contentDescriptors":{"c":{"name":"c","schema":{"type":"integer"}}},"schemas":{"Integer":{"type":"integer"}},"examples":{"integerTwo":{"name":"two","summary":"its a sample two","description":"Im not sure how else to say two","value":2},"integerFour":{"name":"four","summary":"its a sample four","description":"Im not sure how else to say four","value":4},"integerEight":{"name":"eight","summary":"its a sample eight","description":"Im not sure how else to say eight","value":8}}}};

if (options.transport === undefined || options.transport.type === undefined) {
throw new Error("Invalid constructor params");
}
const { type, host, port } = options.transport;
const {type, host, port} = options.transport;
let path = options.transport.path || "";
if (path && path[0] !== "/") {
path = "/" + path;
path = "/" + path;
}
switch (type) {
case "http":
Expand All @@ -50,6 +50,35 @@ export class SimpleMath {
this.rpc = new Client(new RequestManager([this.transport]));
this.validator = new MethodCallValidator(SimpleMath.openrpcDocument);
}
/**
* Adds a JSONRPC notification handler to handle receiving notifications.
* @example
* myClient.onNotification((data)=>console.log(data));
*/
public onNotification(callback: (data: any) => void) {
this.rpc.onNotification(callback);
}

/**
* Adds an optional JSONRPCError handler to handle receiving errors that cannot be resolved to a specific request
* @example
* myClient.onError((err: JSONRPCError)=>console.log(err.message));
*/
public onError(callback: (data: JSONRPCError) => void) {
this.rpc.onError(callback);
}

/**
* Sets a default timeout in ms for all requests excluding notifications.
* @example
* // 20s timeout
* myClient.setDefaultTimeout(20000);
* // Removes timeout from request
* myClient.setDefaultTimeout(undefined);
*/
public setDefaultTimeout(ms?: number) {
this.timeout = ms;
}

/**
* Initiates [[SimpleMath.startBatch]] in order to build a batch call.
Expand All @@ -65,7 +94,7 @@ export class SimpleMath {
* myClient.stopBatch();
*/
public startBatch(): void {
// return this.rpc.startBatch();
return this.rpc.startBatch();
}

/**
Expand All @@ -87,19 +116,22 @@ export class SimpleMath {
/**
*
*/
// tslint:disable-next-line:max-line-length
public addition: Addition = (...params) => {
return this.request("addition", params);
}

/**
*
*/
// tslint:disable-next-line:max-line-length
public subtraction: Subtraction = (...params) => {
return this.request("subtraction", params);
}

private request(methodName: string, params: any[]): Promise<any> {
const methodObject = _.find(SimpleMath.openrpcDocument.methods, ({ name }) => name === methodName) as MethodObject;
const methodObject = _.find(SimpleMath.openrpcDocument.methods, ({name}) => name === methodName) as MethodObject;
const notification = methodObject.result ? false : true;
const openRpcMethodValidationErrors = this.validator.validate(methodName, params);
if (openRpcMethodValidationErrors instanceof MethodNotFoundError || openRpcMethodValidationErrors.length > 0) {
return Promise.reject(openRpcMethodValidationErrors);
Expand All @@ -111,7 +143,10 @@ export class SimpleMath {
} else {
rpcParams = params;
}
return this.rpc.request(methodName, rpcParams);
if (notification) {
return this.rpc.notify(methodName, rpcParams);
}
return this.rpc.request(methodName, rpcParams, this.timeout);
}

}
Expand Down
Loading

0 comments on commit a9f537c

Please sign in to comment.