Skip to content

Commit 90bf027

Browse files
committed
refactor: typescript WIP
1 parent 3e02144 commit 90bf027

21 files changed

+9060
-14385
lines changed

dist/cjs/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
"test": "mocha",
77
"coverage": "nyc --reporter=lcov --reporter=text mocha",
88
"clean": "rm -rf ./dist",
9-
"build": "npm run clean && npm run build:types && npm run build:esm && npm run build:cjs",
9+
"build": "npm run clean && npm run schemas2ts && npm run build:types && npm run build:esm && npm run build:cjs",
1010
"build:types": "tsc -p ./tsconfig/types.json",
1111
"build:esm": "tsc -p ./tsconfig/node-esm.json && mv dist/esm/src/index.js dist/esm/src/index.mjs",
1212
"build:cjs": "tsc -p ./tsconfig/node-cjs.json",
13+
"schemas2ts": "node scripts/gen-schema-types.mjs src/lib/schemas",
1314
"prepack": "npm run build"
1415
},
1516
"engines": {
@@ -53,6 +54,7 @@
5354
"@types/mocha": "^10.0.1",
5455
"@types/node": "^18.14.1",
5556
"@types/ws": "^8.5.4",
57+
"json-schema-to-typescript": "^12.0.0",
5658
"mocha": "^10.2.0",
5759
"nyc": "^15.1.0",
5860
"ts-node": "^10.9.1",

dist/cjs/src/lib/baseclient.js

+17-12
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class RPCBaseClient extends node_events_1.EventEmitter {
4141
this._lastPingTime = 0;
4242
this._closePromise = undefined;
4343
this._protocolOptions = [];
44-
this._protocol = undefined;
44+
this._protocol = '';
4545
this._strictProtocols = [];
4646
this._strictValidators = new Map();
4747
this._pendingCalls = new Map();
@@ -77,6 +77,9 @@ class RPCBaseClient extends node_events_1.EventEmitter {
7777
get state() {
7878
return this._state;
7979
}
80+
isProtocol(protocol) {
81+
return this._protocol === protocol;
82+
}
8083
reconfigure(options) {
8184
var _a;
8285
const newOpts = Object.assign(this._options, options);
@@ -192,7 +195,7 @@ class RPCBaseClient extends node_events_1.EventEmitter {
192195
this._wildcardHandler = undefined;
193196
this._handlers.clear();
194197
}
195-
async call(method, params = {}, options = {}) {
198+
async call(method, params, options = {}) {
196199
return await this._callQueue.push(this._call.bind(this, method, params, options));
197200
}
198201
async _call(method, params, options = {}) {
@@ -201,18 +204,20 @@ class RPCBaseClient extends node_events_1.EventEmitter {
201204
if ([StateEnum.CLOSED, StateEnum.CLOSING].includes(this._state)) {
202205
throw Error(`Cannot make call while socket not open`);
203206
}
207+
const methodName = method;
208+
const paramsObj = params;
204209
const msgId = (0, node_crypto_1.randomUUID)();
205-
const payload = [MsgType.CALL, msgId, method, params];
210+
const payload = [MsgType.CALL, msgId, methodName, paramsObj];
206211
if (typeof this._protocol === 'string' && this._strictProtocols.includes(this._protocol)) {
207212
const validator = this._strictValidators.get(this._protocol);
208213
try {
209-
validator.validate(`urn:${method}.req`, params);
214+
validator.validate(`urn:${methodName}.req`, paramsObj);
210215
}
211216
catch (error) {
212217
this.emit('strictValidationFailure', {
213218
messageId: msgId,
214-
method,
215-
params,
219+
method: methodName,
220+
params: paramsObj,
216221
result: null,
217222
error,
218223
outbound: true,
@@ -223,8 +228,8 @@ class RPCBaseClient extends node_events_1.EventEmitter {
223228
}
224229
const pendingCall = {
225230
msgId,
226-
method,
227-
params
231+
method: methodName,
232+
params: paramsObj
228233
};
229234
if (!options.noReply) {
230235
const timeoutAc = new AbortController();
@@ -275,8 +280,8 @@ class RPCBaseClient extends node_events_1.EventEmitter {
275280
this.emit('callResult', {
276281
outbound: true,
277282
messageId: msgId,
278-
method,
279-
params,
283+
method: methodName,
284+
params: paramsObj,
280285
result,
281286
});
282287
return result;
@@ -285,8 +290,8 @@ class RPCBaseClient extends node_events_1.EventEmitter {
285290
this.emit('callError', {
286291
outbound: true,
287292
messageId: msgId,
288-
method,
289-
params,
293+
method: methodName,
294+
params: paramsObj,
290295
error: err,
291296
});
292297
throw err;

dist/cjs/src/lib/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class RPCClient extends baseclient_1.RPCBaseClient {
2121
this._identity = options.identity;
2222
this._state = StateEnum.CLOSED;
2323
this._ws = undefined;
24-
this._protocol = undefined;
24+
this._protocol = '';
2525
this._options = {
2626
identity: '',
2727
endpoint: 'ws://localhost',
@@ -55,7 +55,7 @@ class RPCClient extends baseclient_1.RPCBaseClient {
5555
async connect() {
5656
var _a;
5757
this._protocolOptions = (_a = this._options.protocols) !== null && _a !== void 0 ? _a : [];
58-
this._protocol = undefined;
58+
this._protocol = '';
5959
this._identity = this._options.identity;
6060
let connUrl = this._options.endpoint + '/' + encodeURIComponent(this._options.identity);
6161
if (this._options.query) {

dist/cjs/src/lib/protocols.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

dist/cjs/src/lib/schemas/ocpp1_6.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2373,4 +2373,4 @@
23732373
"properties": {},
23742374
"type": "object"
23752375
}
2376-
]
2376+
]

0 commit comments

Comments
 (0)