Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/injected transport proofzero #716

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/components/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
PostMessageIframeTransport,
WebSocketTransport,
HTTPTransport,
Transport,
Client,
JSONRPCError
} from "@open-rpc/client-js";
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -198,7 +209,11 @@ const hooks: IHooks = {
afterCopyStatic: [
async (dest, frm, component): Promise<void> => {
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 }
);
}
},
],
Expand All @@ -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,
},
Expand Down
37 changes: 25 additions & 12 deletions src/custom-test-component.js
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;

Expand All @@ -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);
Expand Down Expand Up @@ -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 }
);
}
},
],
Expand Down Expand Up @@ -193,8 +207,7 @@ const hooks = {
},
};


module.exports = {
hooks,
staticPath: getDefaultComponentTemplatePath
}
staticPath: getDefaultComponentTemplatePath,
};
90 changes: 71 additions & 19 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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" },
Expand All @@ -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);
});
Expand All @@ -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`,
},
],
});

Expand All @@ -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`,
},
],
});

Expand Down