-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathcustom-test-component.js
211 lines (193 loc) · 7.1 KB
/
custom-test-component.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const path = require("path");
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, 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";
<%= methodTypings.toString("typescript") %>
export interface Options {
transport: {
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 | 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");
}
const {type, host, port, protocol} = options.transport;
let path = options.transport.path || "";
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);
break;
case 'websocket':
this.transport = new WebSocketTransport((protocol || "ws://") + host + ":" + port + path);
break;
case 'postmessageiframe':
this.transport = new PostMessageIframeTransport(protocol + "://" + host + ":" + port + path);
break;
case 'postmessagewindow':
this.transport = new PostMessageWindowTransport(protocol + "://" + host + ":" + port + path);
break;
default:
throw new Error("unsupported transport");
break;
}
this.rpc = new Client(new RequestManager([this.transport]));
this.validator = new MethodCallValidator(<%= className %>.openrpcDocument);
}
/**
* Adds a JSONRPC notification handler to handle receiving notifications.
* @example
* myClient.onNotification((data)=>console.log(data));
*/
public onNotification(callback: (data: any) => void) {
this.rpc.onNotification(callback);
}
/**
* Adds an optional JSONRPCError handler to handle receiving errors that cannot be resolved to a specific request
* @example
* myClient.onError((err: JSONRPCError)=>console.log(err.message));
*/
public onError(callback: (data: JSONRPCError) => void) {
this.rpc.onError(callback);
}
/**
* Sets a default timeout in ms for all requests excluding notifications.
* @example
* // 20s timeout
* myClient.setDefaultTimeout(20000);
* // Removes timeout from request
* myClient.setDefaultTimeout(undefined);
*/
public setDefaultTimeout(ms?: number) {
this.timeout = ms;
}
/**
* Initiates [[<%= className %>.startBatch]] in order to build a batch call.
*
* Subsequent calls to [[<%= className %>.request]] will be added to the batch.
* Once [[<%= className %>.stopBatch]] is called, the promises for the [[<%= className %>.request]]
* will then be resolved. If there is already a batch in progress this method is a noop.
*
* @example
* myClient.startBatch();
* myClient.foo().then(() => console.log("foobar"))
* myClient.bar().then(() => console.log("foobarbaz"))
* myClient.stopBatch();
*/
public startBatch(): void {
return this.rpc.startBatch();
}
/**
* Initiates [[Client.stopBatch]] in order to finalize and send the batch to the underlying transport.
*
* stopBatch will send the [[<%= className %>]] calls made since the last [[<%= className %>.startBatch]] call. For
* that reason, [[<%= className %>.startBatch]] MUST be called before [[<%= className %>.stopBatch]].
*
* @example
* myClient.startBatch();
* myClient.foo().then(() => console.log("foobar"))
* myClient.bar().then(() => console.log("foobarbaz"))
* myClient.stopBatch();
*/
public stopBatch(): void {
return this.rpc.stopBatch();
}
private request(methodName: string, params: any[]): Promise<any> {
const methodObject = _.find((<%= className %>.openrpcDocument.methods as MethodObject[]), ({name}) => name === methodName) as MethodObject;
const notification = methodObject.result ? false : true;
const openRpcMethodValidationErrors = this.validator.validate(methodName, params);
if ( openRpcMethodValidationErrors instanceof MethodNotFoundError || openRpcMethodValidationErrors.length > 0) {
return Promise.reject(openRpcMethodValidationErrors);
}
let rpcParams;
if (methodObject.paramStructure && methodObject.paramStructure === "by-name") {
rpcParams = _.zipObject(params, _.map(methodObject.params, "name"));
} else {
rpcParams = params;
}
if (notification) {
return this.rpc.notify({method: methodName, params: rpcParams});
}
return this.rpc.request({method: methodName, params: rpcParams}, this.timeout);
}
<% openrpcDocument.methods.forEach((method) => { %>
/**
* <%= method.summary %>
*/
// tslint:disable-next-line:max-line-length
public <%= method.name %>: <%= methodTypings.getTypingNames("typescript", method).method %> = (...params) => {
return this.request("<%= method.name %>", params);
}
<% }); %>
}
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 }
);
}
},
],
afterCompileTemplate: [
async (dest, frm, component, openrpcDocument) => {
if (component.language === "typescript") {
const packagePath = path.join(dest, "package.json");
const fileContents = await readFile(packagePath);
const pkg = JSON.parse(fileContents.toString());
const updatedPkg = JSON.stringify({
...pkg,
name: component.name,
version: openrpcDocument.info.version,
});
return await writeFile(packagePath, updatedPkg);
}
},
],
templateFiles: {
typescript: [
{
path: "src/index.ts",
template: tsTemplate,
},
],
},
};
module.exports = {
hooks,
staticPath: getDefaultComponentTemplatePath,
};