diff --git a/src/components/client.ts b/src/components/client.ts index 00c80c86..5a23f15f 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; @@ -39,12 +40,21 @@ export class <%= className %> { if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } - const {type, host, port, protocol} = options.transport; + + const {type, host, port, protocol, injected} = options.transport; + + if (type === "injected" && injected === undefined) { + throw new Error("Missing injected transport"); + } + let path = options.transport.path || ""; if(path && path[0] !== "/") { path = "/" + path; } switch (type) { + case 'injected': + this.transport = injected; + break; case 'http': case 'https': this.transport = new HTTPTransport((protocol || type) + "://" + host + ":" + port + path); @@ -176,7 +186,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 +216,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, }, diff --git a/src/custom-test-component.js b/src/custom-test-component.js index f68a9b7c..ca353ea8 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,18 +16,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; + 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; @@ -36,12 +37,22 @@ export class <%= className %> { if (options.transport === undefined || options.transport.type === undefined) { throw new Error("Invalid constructor params"); } - const {type, host, port, protocol} = options.transport; + + const {type, host, port, protocol, injected} = options.transport; + + if (type === "injected" && injected === undefined) { + throw new Error("Missing injected transport"); + } + let path = options.transport.path || ""; if(path && path[0] !== "/") { path = "/" + path; } + switch (type) { + case 'injected': + this.transport = injected + break; case 'http': case 'https': this.transport = new HTTPTransport((protocol || type) + "://" + host + ":" + port + path); @@ -158,12 +169,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 +207,7 @@ const hooks = { }, }; - module.exports = { hooks, - staticPath: getDefaultComponentTemplatePath -} + staticPath: getDefaultComponentTemplatePath, +}; diff --git a/src/index.test.ts b/src/index.test.ts index 96f1d59d..86d08f95 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -4,7 +4,10 @@ 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"; const stat = promisify(fs.stat); @@ -34,9 +37,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 +62,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 +90,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 +136,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`, + }, ], });