diff --git a/src/components/client.ts b/src/components/client.ts index 434a7c2c..f6dfe52c 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; @@ -48,12 +50,21 @@ public dereffedDocument: OpenRPC | undefined; 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); @@ -198,7 +209,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 +239,7 @@ const hooks: IHooks = { const updatedCargo = TOML.stringify({ ...cargoTOML, package: { - ...cargoTOML.package as any, + ...(cargoTOML.package as any), 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 5fdebcde..bb63c87d 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 { 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,17 +62,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 +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`, + }, ], }); @@ -109,13 +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`, + }, ], });