From d8008160af2b3b1c7702b10df404f554ae302071 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Tue, 14 May 2019 18:36:12 -0700 Subject: [PATCH] fix: use open-rpc client-js instead of Jayson fixes #123 fixes #96 --- package-lock.json | 43 ++++++------------- src/index.ts | 2 +- templates/typescript/static/_package.json | 3 +- .../templated/exported-class.template.ts | 30 +++++++++++-- 4 files changed, 41 insertions(+), 37 deletions(-) diff --git a/package-lock.json b/package-lock.json index e5338f20..5be8bc17 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2000,8 +2000,7 @@ "ansi-regex": { "version": "2.1.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "aproba": { "version": "1.2.0", @@ -2022,14 +2021,12 @@ "balanced-match": { "version": "1.0.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "brace-expansion": { "version": "1.1.11", "bundled": true, "dev": true, - "optional": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -2044,20 +2041,17 @@ "code-point-at": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "concat-map": { "version": "0.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "console-control-strings": { "version": "1.1.0", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "core-util-is": { "version": "1.0.2", @@ -2174,8 +2168,7 @@ "inherits": { "version": "2.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "ini": { "version": "1.3.5", @@ -2187,7 +2180,6 @@ "version": "1.0.0", "bundled": true, "dev": true, - "optional": true, "requires": { "number-is-nan": "^1.0.0" } @@ -2202,7 +2194,6 @@ "version": "3.0.4", "bundled": true, "dev": true, - "optional": true, "requires": { "brace-expansion": "^1.1.7" } @@ -2210,14 +2201,12 @@ "minimist": { "version": "0.0.8", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "minipass": { "version": "2.3.5", "bundled": true, "dev": true, - "optional": true, "requires": { "safe-buffer": "^5.1.2", "yallist": "^3.0.0" @@ -2236,7 +2225,6 @@ "version": "0.5.1", "bundled": true, "dev": true, - "optional": true, "requires": { "minimist": "0.0.8" } @@ -2317,8 +2305,7 @@ "number-is-nan": { "version": "1.0.1", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "object-assign": { "version": "4.1.1", @@ -2330,7 +2317,6 @@ "version": "1.4.0", "bundled": true, "dev": true, - "optional": true, "requires": { "wrappy": "1" } @@ -2416,8 +2402,7 @@ "safe-buffer": { "version": "5.1.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "safer-buffer": { "version": "2.1.2", @@ -2453,7 +2438,6 @@ "version": "1.0.2", "bundled": true, "dev": true, - "optional": true, "requires": { "code-point-at": "^1.0.0", "is-fullwidth-code-point": "^1.0.0", @@ -2473,7 +2457,6 @@ "version": "3.0.1", "bundled": true, "dev": true, - "optional": true, "requires": { "ansi-regex": "^2.0.0" } @@ -2517,14 +2500,12 @@ "wrappy": { "version": "1.0.2", "bundled": true, - "dev": true, - "optional": true + "dev": true }, "yallist": { "version": "3.0.3", "bundled": true, - "dev": true, - "optional": true + "dev": true } } }, @@ -5091,7 +5072,7 @@ "string-to-stream": "^1.1.0", "typescript": "~2.8.3", "typescript-json-schema": "github:quicktype/typescript-json-schema#d16083d29c8b6702c666a981fa6b21113300c059", - "unicode-properties": "github:quicktype/unicode-properties#dist", + "unicode-properties": "github:quicktype/unicode-properties#d5fddfea1ef9d05c6479a979e225476063e13f52", "universal-analytics": "^0.4.16", "urijs": "^1.19.1", "uuid": "^3.2.1" diff --git a/src/index.ts b/src/index.ts index 69d6e99b..776f830a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,7 +24,7 @@ const cleanBuildDir = async (destinationDirectoryName: string): Promise => const compileTemplate = async ( openrpcDocument: OpenRPC, language: string, - methodTypings: MethodTypings + methodTypings: MethodTypings, ): Promise => { const template = language === "rust" ? rsTemplate : jsTemplate; return template({ diff --git a/templates/typescript/static/_package.json b/templates/typescript/static/_package.json index d2e3ea8b..b1d35f15 100644 --- a/templates/typescript/static/_package.json +++ b/templates/typescript/static/_package.json @@ -16,11 +16,12 @@ "dependencies": { "@open-rpc/meta-schema": "^1.3.1", "@open-rpc/schema-utils-js": "^1.8.0", - "jayson": "^2.1.2", + "@open-rpc/client-js": "0.0.0-development", "lodash": "4.17.11" }, "devDependencies": { "typescript": "^3.2.4", + "@types/ws": "^6.0.1", "@types/json-schema": "7.0.3", "tslint": "^5.13.1", "typedoc": "^0.14.2" diff --git a/templates/typescript/templated/exported-class.template.ts b/templates/typescript/templated/exported-class.template.ts index 52ec0ac3..3fe9bdaf 100644 --- a/templates/typescript/templated/exported-class.template.ts +++ b/templates/typescript/templated/exported-class.template.ts @@ -2,7 +2,7 @@ import { template } from "lodash"; export default template(` // Code generated by @open-rpc/client-generator DO NOT EDIT. -import * as jayson from "jayson/promise"; +import { RequestManager, WebSocketTransport, HTTPTransport, Client } from '@open-rpc/client-js'; import ajv from "ajv"; import _ from "lodash"; import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema"; @@ -10,18 +10,40 @@ import { MethodCallValidator } from "@open-rpc/schema-utils-js"; <%= methodTypings.getAllUniqueTypings("typescript") %> +interface IOptions { + transport: { + type: "websocket" | "http" | "https"; + host: string; + port: number; + } +} + export default class <%= className %> { - public rpc: jayson.Client; + public rpc: Client; private validator: MethodCallValidator; private openrpcDocument: OpenRPC; - constructor(options: any) { + constructor(options: IOptions) { this.openrpcDocument = <%= JSON.stringify(openrpcDocument) %>; if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } - this.rpc = (jayson.Client as any)[options.transport.type](options.transport); + + let transport; + switch (options.transport.type) { + case 'http': + case 'https': + transport = new HTTPTransport(options.transport.type + "://" + options.transport.host + ":" + options.transport.port) + break; + case 'websocket': + transport = new WebSocketTransport("ws://" + options.transport.host + ":" + options.transport.port) + break; + default: + throw new Error("unsupported transport"); + break; + } + this.rpc = new Client(new RequestManager([transport])); this.validator = new MethodCallValidator(this.openrpcDocument); }