From 319d31b4e5fa85ed46b6b97b06665d2cd49608be Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:08:10 -0500 Subject: [PATCH 1/5] feat(client): inject transport --- src/components/client.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index 00c80c86..f7763bcc 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -10,7 +10,7 @@ import { template } from "lodash"; const tsTemplate = template(` // Code generated by @open-rpc/generator DO NOT EDIT. -import { RequestManager, PostMessageWindowTransport, PostMessageIframeTransport, WebSocketTransport, HTTPTransport, Client, JSONRPCError } from "@open-rpc/client-js"; +import { RequestManager, PostMessageWindowTransport, PostMessageIframeTransport, WebSocketTransport, HTTPTransport, Transport, Client, JSONRPCError } from "@open-rpc/client-js"; import _ from "lodash"; import { OpenrpcDocument as OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema"; import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils-js"; @@ -19,18 +19,19 @@ import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils export interface Options { transport: { - type: "websocket" | "http" | "https" | "postmessagewindow" | "postmessageiframe"; + type: "websocket" | "http" | "https" | "postmessagewindow" | "postmessageiframe" | "injected"; host: string; port: number; path?: string; protocol?: string; + injected: Transport; }, } export class <%= className %> { public rpc: Client; public static openrpcDocument: OpenRPC = <%= JSON.stringify(openrpcDocument) %> ; - public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport; + public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport | Transport; private validator: MethodCallValidator; private timeout: number | undefined; @@ -45,6 +46,9 @@ export class <%= className %> { path = "/" + path; } switch (type) { + case 'injected': + this.transport = options.transport.injected; + break; case 'http': case 'https': this.transport = new HTTPTransport((protocol || type) + "://" + host + ":" + port + path); @@ -176,7 +180,11 @@ const hooks: IHooks = { afterCopyStatic: [ async (dest, frm, component): Promise => { if (component.language === "typescript") { - return await move(path.join(dest, "_package.json"), path.join(dest, "package.json"), { overwrite: true }); + return await move( + path.join(dest, "_package.json"), + path.join(dest, "package.json"), + { overwrite: true } + ); } }, ], @@ -202,7 +210,7 @@ const hooks: IHooks = { const updatedCargo = TOML.stringify({ ...cargoTOML, package: { - ...cargoTOML.package as object, + ...(cargoTOML.package as object), name: component.name, version: openrpcDocument.info.version, }, From b1501d66a1c5762a0239de850447db1a1780d6c8 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:28:14 -0500 Subject: [PATCH 2/5] wip: add check and update test component --- src/components/client.ts | 4 ++ src/custom-test-component.js | 33 +++++++---- src/index.test.ts | 105 +++++++++++++++++++++++++++-------- 3 files changed, 107 insertions(+), 35 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index f7763bcc..bf1cb283 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -37,6 +37,10 @@ export class <%= className %> { constructor(options: Options) { + if (type === "injected" && options.transport.injected === undefined) { + throw new Error("Missing injected transport"); + } + if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } diff --git a/src/custom-test-component.js b/src/custom-test-component.js index f68a9b7c..bb9e422c 100644 --- a/src/custom-test-component.js +++ b/src/custom-test-component.js @@ -1,13 +1,13 @@ const path = require("path"); -const { move, readFile, writeFile } =require("fs-extra"); -const _ = require('lodash') -const components = require("./components") +const { move, readFile, writeFile } = require("fs-extra"); +const _ = require("lodash"); +const components = require("./components"); const { getDefaultComponentTemplatePath } = components; const { template } = _; const tsTemplate = template(` // Code generated by @custom-test generator DO NOT EDIT. -import { RequestManager, PostMessageWindowTransport, PostMessageIframeTransport, WebSocketTransport, HTTPTransport, Client, JSONRPCError } from "@open-rpc/client-js"; +import { RequestManager, PostMessageWindowTransport, PostMessageIframeTransport, WebSocketTransport, HTTPTransport, Transport, Client, JSONRPCError } from "@open-rpc/client-js"; import _ from "lodash"; import { OpenrpcDocument as OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema"; import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils-js"; @@ -16,22 +16,27 @@ import { MethodCallValidator, MethodNotFoundError } from "@open-rpc/schema-utils export interface Options { transport: { - type: "websocket" | "http" | "https" | "postmessagewindow" | "postmessageiframe"; + type: "websocket" | "http" | "https" | "postmessagewindow" | "postmessageiframe" | "injected"; host: string; port: number; path?: string; protocol?: string; + transport?: Transport; }, } export class <%= className %> { public rpc: Client; public static openrpcDocument: OpenRPC = <%= JSON.stringify(openrpcDocument) %> ; - public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport; + public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport | Transport; private validator: MethodCallValidator; private timeout: number | undefined; constructor(options: Options) { + + if (type === "injected" && options.transport.injected === undefined) { + throw new Error("Missing injected transport"); + } if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); @@ -41,7 +46,11 @@ export class <%= className %> { if(path && path[0] !== "/") { path = "/" + path; } + switch (type) { + case 'injected': + this.transport = options.transport.injected + break; case 'http': case 'https': this.transport = new HTTPTransport((protocol || type) + "://" + host + ":" + port + path); @@ -158,12 +167,15 @@ export class <%= className %> { export default <%= className %>; `); - const hooks = { afterCopyStatic: [ async (dest, frm, component) => { if (component.language === "typescript") { - return await move(path.join(dest, "_package.json"), path.join(dest, "package.json"), { overwrite: true }); + return await move( + path.join(dest, "_package.json"), + path.join(dest, "package.json"), + { overwrite: true } + ); } }, ], @@ -193,8 +205,7 @@ const hooks = { }, }; - module.exports = { hooks, - staticPath: getDefaultComponentTemplatePath -} + staticPath: getDefaultComponentTemplatePath, +}; diff --git a/src/index.test.ts b/src/index.test.ts index 96f1d59d..03bfbf14 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -4,8 +4,12 @@ import fsx, { emptyDir } from "fs-extra"; import examples from "@open-rpc/examples"; import { promisify } from "util"; import { forEach } from "lodash"; -import { parseOpenRPCDocument, OpenRPCDocumentDereferencingError } from "@open-rpc/schema-utils-js"; +import { + parseOpenRPCDocument, + OpenRPCDocumentDereferencingError, +} from "@open-rpc/schema-utils-js"; import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema"; +import { HTTPTransport } from "@open-rpc/client-js"; const stat = promisify(fs.stat); const rmdir = promisify(fs.rmdir); @@ -34,9 +38,7 @@ describe(`Examples to generate Js clients`, () => { methods: [ { name: "foo", - params: [ - { $ref: "#/components/contentDescriptors/LeFoo" }, - ], + params: [{ $ref: "#/components/contentDescriptors/LeFoo" }], result: { name: "bar", schema: { $ref: "#/components/contentDescriptors/LeFoo" }, @@ -61,19 +63,20 @@ describe(`Examples to generate Js clients`, () => { }; const genProm = clientGen(testDocument); - return expect(genProm).rejects.toBeInstanceOf(OpenRPCDocumentDereferencingError); + return expect(genProm).rejects.toBeInstanceOf( + OpenRPCDocumentDereferencingError + ); }); - forEach(examples, (example: OpenRPC, exampleName: string) => { - it(`rejects configurations without outDir or outPath`, async ()=>{ - const promGen = clientGen({ - openrpcDocument: await parseOpenRPCDocument(example), - components: [ - { type: "client", language: "typescript", name: "testclient-ts" }, - ] - }); - expect(promGen).rejects.toBeInstanceOf(Error); + it(`rejects configurations without outDir or outPath`, async () => { + const promGen = clientGen({ + openrpcDocument: await parseOpenRPCDocument(example), + components: [ + { type: "client", language: "typescript", name: "testclient-ts" }, + ], + }); + expect(promGen).rejects.toBeInstanceOf(Error); }); it(`creates a new client for example: ${exampleName} and regenerates after`, async () => { @@ -88,11 +91,38 @@ describe(`Examples to generate Js clients`, () => { { type: "client", language: "typescript", name: "testclient-ts" }, { type: "server", language: "typescript", name: "testserver-ts" }, { type: "docs", language: "gatsby", name: "testserver-gatsby" }, - { type: "custom", language: "typescript", name: "custom-stuff", "customComponent":"./src/custom-test-component.js", customType:"client"}, - { type: "custom", language: "typescript", name: "custom-stuff2", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: null}, - { type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"}, - { type: "custom", language: "typescript", name: "custom-stuff4", "customComponent":"./src/custom-test-component.js", customType:"client", - openRPCPath: "tmpy", outPath: `${exampleOutDir}/special`} + { + type: "custom", + language: "typescript", + name: "custom-stuff", + customComponent: "./src/custom-test-component.js", + customType: "client", + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff2", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: null, + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff3", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: "tmpz", + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff4", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: "tmpy", + outPath: `${exampleOutDir}/special`, + }, ], }); @@ -107,11 +137,38 @@ describe(`Examples to generate Js clients`, () => { { type: "client", language: "typescript", name: "testclient-ts" }, { type: "server", language: "typescript", name: "testserver-ts" }, { type: "docs", language: "gatsby", name: "testserver-gatsby" }, - { type: "custom", language: "typescript", name: "custom-stuff", "customComponent":"./src/custom-test-component.js", customType:"client"}, - { type: "custom", language: "typescript", name: "custom-stuff2", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: null}, - { type: "custom", language: "typescript", name: "custom-stuff3", "customComponent":"./src/custom-test-component.js", customType:"client", openRPCPath: "tmpz"}, - { type: "custom", language: "typescript", name: "custom-stuff4", "customComponent":"./src/custom-test-component.js", customType:"client", - openRPCPath: "tmpy", outPath: `${exampleOutDir}/special`} + { + type: "custom", + language: "typescript", + name: "custom-stuff", + customComponent: "./src/custom-test-component.js", + customType: "client", + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff2", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: null, + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff3", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: "tmpz", + }, + { + type: "custom", + language: "typescript", + name: "custom-stuff4", + customComponent: "./src/custom-test-component.js", + customType: "client", + openRPCPath: "tmpy", + outPath: `${exampleOutDir}/special`, + }, ], }); From e4005c02dbf6a0595cfad04e5c9cfb2f571b3157 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:28:56 -0500 Subject: [PATCH 3/5] wip: revert test change --- src/index.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/index.test.ts b/src/index.test.ts index 03bfbf14..86d08f95 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -9,7 +9,6 @@ import { OpenRPCDocumentDereferencingError, } from "@open-rpc/schema-utils-js"; import { OpenrpcDocument as OpenRPC } from "@open-rpc/meta-schema"; -import { HTTPTransport } from "@open-rpc/client-js"; const stat = promisify(fs.stat); const rmdir = promisify(fs.rmdir); From 5c294de16b2260d40c709279f903e8267a7ebbc3 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:34:29 -0500 Subject: [PATCH 4/5] fix: test --- src/components/client.ts | 10 ++++++---- src/custom-test-component.js | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index bf1cb283..5d6ff83b 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -37,14 +37,16 @@ export class <%= className %> { constructor(options: Options) { - if (type === "injected" && options.transport.injected === undefined) { - throw new Error("Missing injected transport"); - } - if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } + const {type, host, port, protocol} = options.transport; + + if (type === "injected" && options.transport.injected === undefined) { + throw new Error("Missing injected transport"); + } + let path = options.transport.path || ""; if(path && path[0] !== "/") { path = "/" + path; diff --git a/src/custom-test-component.js b/src/custom-test-component.js index bb9e422c..6e112a0d 100644 --- a/src/custom-test-component.js +++ b/src/custom-test-component.js @@ -33,15 +33,17 @@ export class <%= className %> { private timeout: number | undefined; constructor(options: Options) { - - if (type === "injected" && options.transport.injected === undefined) { - throw new Error("Missing injected transport"); - } if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } + const {type, host, port, protocol} = options.transport; + + if (type === "injected" && options.transport.injected === undefined) { + throw new Error("Missing injected transport"); + } + let path = options.transport.path || ""; if(path && path[0] !== "/") { path = "/" + path; From bcc11cab22d2358cb7b896183ea67d4b8f26ece6 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:36:08 -0500 Subject: [PATCH 5/5] wip: destructure injected prop --- src/components/client.ts | 6 +++--- src/custom-test-component.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index 5d6ff83b..5a23f15f 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -41,9 +41,9 @@ export class <%= className %> { throw new Error("Invalid constructor params"); } - const {type, host, port, protocol} = options.transport; + const {type, host, port, protocol, injected} = options.transport; - if (type === "injected" && options.transport.injected === undefined) { + if (type === "injected" && injected === undefined) { throw new Error("Missing injected transport"); } @@ -53,7 +53,7 @@ export class <%= className %> { } switch (type) { case 'injected': - this.transport = options.transport.injected; + this.transport = injected; break; case 'http': case 'https': diff --git a/src/custom-test-component.js b/src/custom-test-component.js index 6e112a0d..ca353ea8 100644 --- a/src/custom-test-component.js +++ b/src/custom-test-component.js @@ -38,9 +38,9 @@ export class <%= className %> { throw new Error("Invalid constructor params"); } - const {type, host, port, protocol} = options.transport; + const {type, host, port, protocol, injected} = options.transport; - if (type === "injected" && options.transport.injected === undefined) { + if (type === "injected" && injected === undefined) { throw new Error("Missing injected transport"); } @@ -51,7 +51,7 @@ export class <%= className %> { switch (type) { case 'injected': - this.transport = options.transport.injected + this.transport = injected break; case 'http': case 'https':