From f9345c0b73251de3e6693a7ec9f20bcff1c83b37 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:08:10 -0500 Subject: [PATCH 1/6] feat(client): inject transport --- src/components/client.ts | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index 434a7c2c..3deeba27 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -16,6 +16,7 @@ import { PostMessageIframeTransport, WebSocketTransport, HTTPTransport, + Transport, Client, JSONRPCError } from "@open-rpc/client-js"; @@ -27,19 +28,20 @@ import { MethodCallValidator, MethodNotFoundError, parseOpenRPCDocument } from " 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 dereffedDocument: OpenRPC | undefined; - public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport; + public dereffedDocument: OpenRPC | undefined; + public static openrpcDocument: OpenRPC = <%= JSON.stringify(openrpcDocument) %> ; + public transport: HTTPTransport | WebSocketTransport | PostMessageWindowTransport | PostMessageIframeTransport | Transport; private validator: MethodCallValidator | undefined; private timeout: number | undefined; @@ -54,6 +56,9 @@ public dereffedDocument: OpenRPC | undefined; 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); @@ -198,7 +203,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 } + ); } }, ], @@ -224,7 +233,7 @@ const hooks: IHooks = { const updatedCargo = TOML.stringify({ ...cargoTOML, package: { - ...cargoTOML.package as any, + ...(cargoTOML.package as object), name: component.name, version: openrpcDocument.info.version, }, From 0629747be919fb8587a0dd155c5d89cc9d602c93 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:28:14 -0500 Subject: [PATCH 2/6] wip: add check and update test component --- src/components/client.ts | 4 ++ src/custom-test-component.js | 33 ++++++++----- src/index.test.ts | 91 ++++++++++++++++++++++++++++-------- 3 files changed, 98 insertions(+), 30 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index 3deeba27..eab9c5b8 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -47,6 +47,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 5fdebcde..5babe6e4 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 { 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,17 +63,18 @@ 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: example, components: [ { type: "client", language: "typescript", name: "testclient-ts" }, - ] + ], }); expect(promGen).rejects.toBeInstanceOf(Error); }); @@ -88,13 +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`, + }, ], }); @@ -109,13 +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 23d51ce6399e20a40d656ff3901182542660b608 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:28:56 -0500 Subject: [PATCH 3/6] 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 5babe6e4..bb63c87d 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 31e872f09671ca6fba7d811c49b3b8801ecccd4a Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:34:29 -0500 Subject: [PATCH 4/6] 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 eab9c5b8..75baf79c 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -47,14 +47,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 6d96e2de3fa337bdf88de3ffb135109738d1ea01 Mon Sep 17 00:00:00 2001 From: Adrian Maurer Date: Sat, 17 Dec 2022 08:36:08 -0500 Subject: [PATCH 5/6] 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 75baf79c..6cead1f9 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -51,9 +51,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"); } @@ -63,7 +63,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': From 49a11cfc9e1b164dc78dfc20fba2cca37949c5ca Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Fri, 10 May 2024 14:24:22 -0700 Subject: [PATCH 6/6] fix: lint issue --- src/components/client.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/client.ts b/src/components/client.ts index 6cead1f9..f6dfe52c 100644 --- a/src/components/client.ts +++ b/src/components/client.ts @@ -50,7 +50,7 @@ export class <%= className %> { if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } - + const {type, host, port, protocol, injected} = options.transport; if (type === "injected" && injected === undefined) { @@ -239,7 +239,7 @@ const hooks: IHooks = { const updatedCargo = TOML.stringify({ ...cargoTOML, package: { - ...(cargoTOML.package as object), + ...(cargoTOML.package as any), name: component.name, version: openrpcDocument.info.version, },