dreaction-client-core 1.1.6 → 1.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.d.ts +5 -8
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +16 -3
- package/package.json +2 -2
- package/src/index.ts +24 -14
package/lib/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import WebSocket from 'ws';
|
|
2
|
-
import type { Command, CommandMap, CommandTypeKey, CustomCommandArg } from 'dreaction-protocol';
|
|
2
|
+
import type { Command, CommandMap, CommandTypeKey, CustomCommandArg, CustomCommandRegisterPayload } from 'dreaction-protocol';
|
|
3
3
|
import { ClientOptions } from './client-options';
|
|
4
4
|
export type { ClientOptions };
|
|
5
5
|
export { assertHasLoggerPlugin } from './plugins/logger';
|
|
@@ -35,12 +35,9 @@ type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
|
|
|
35
35
|
export type CustomCommandArgs<Args extends CustomCommandArg[]> = UnionToIntersection<Args extends Array<infer U> ? U extends CustomCommandArg ? {
|
|
36
36
|
[K in U as U['name']]: ArgTypeMap[U['type']];
|
|
37
37
|
} : never : never>;
|
|
38
|
-
export interface CustomCommand<Args extends CustomCommandArg[] = CustomCommandArg[]> {
|
|
38
|
+
export interface CustomCommand<Args extends CustomCommandArg[] = CustomCommandArg[]> extends Omit<CustomCommandRegisterPayload, 'id' | 'args'> {
|
|
39
39
|
id?: number;
|
|
40
|
-
|
|
41
|
-
handler: (args: CustomCommandArgs<Args>) => void;
|
|
42
|
-
title?: string;
|
|
43
|
-
description?: string;
|
|
40
|
+
handler: (args: CustomCommandArgs<Args>) => any | Promise<any>;
|
|
44
41
|
args?: Args;
|
|
45
42
|
}
|
|
46
43
|
type ExtractFeatures<T> = T extends {
|
|
@@ -162,7 +159,7 @@ export declare class DReactionImpl implements Omit<DReactionCore, 'options' | 'p
|
|
|
162
159
|
/**
|
|
163
160
|
* Connect to the DReaction server.
|
|
164
161
|
*/
|
|
165
|
-
connect(): this;
|
|
162
|
+
connect(): this | undefined;
|
|
166
163
|
/**
|
|
167
164
|
* Sends a command to the server
|
|
168
165
|
*/
|
|
@@ -179,7 +176,7 @@ export declare class DReactionImpl implements Omit<DReactionCore, 'options' | 'p
|
|
|
179
176
|
* Adds a plugin to the system
|
|
180
177
|
*/
|
|
181
178
|
use(pluginCreator: PluginCreator<this>): this & PluginFeatures<this, typeof pluginCreator>;
|
|
182
|
-
registerCustomCommand(config: CustomCommand
|
|
179
|
+
registerCustomCommand(config: CustomCommand, optHandler?: () => void): () => void;
|
|
183
180
|
}
|
|
184
181
|
export declare function createClient<Client extends DReactionCore = DReactionCore>(options?: ClientOptions<Client>): Client;
|
|
185
182
|
//# sourceMappingURL=index.d.ts.map
|
package/lib/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,IAAI,CAAC;AAC3B,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,cAAc,EACd,gBAAgB,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,IAAI,CAAC;AAC3B,OAAO,KAAK,EACV,OAAO,EACP,UAAU,EACV,cAAc,EACd,gBAAgB,EAChB,4BAA4B,EAC7B,MAAM,oBAAoB,CAAC;AAW5B,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD,YAAY,EAAE,aAAa,EAAE,CAAC;AAC9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,kBAAkB,CAAC;AACzD,YAAY,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EACL,4BAA4B,EAC5B,sBAAsB,GACvB,MAAM,2BAA2B,CAAC;AACnC,YAAY,EAAE,mBAAmB,EAAE,MAAM,2B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AS,CAAC,CAAC,GACrE,IAAI;IAiCR,KAAK;IAOL;;OAEG;IACH,OAAO;IAuIP;;OAEG;IACH,IAAI,GAAI,IAAI,SAAS,cAAc,QAC3B,IAAI,YACA,UAAU,CAAC,IAAI,CAAC,CAAC,SAAS,CAAC,cACzB,OAAO,UAmCnB;IAEF;;OAEG;IACH,OAAO,CAAC,MAAM,EAAE,aAAa;IAW7B;;OAEG;IACH,WAAW,CAAC,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK;IAInC;;OAEG;IACH,GAAG,CACD,aAAa,EAAE,aAAa,CAAC,IAAI,CAAC,GACjC,IAAI,GAAG,cAAc,CAAC,IAAI,EAAE,OAAO,aAAa,CAAC;IA0DpD,qBAAqB,CACnB,MAAM,EAAE,aAAa,EACrB,UAAU,CAAC,EAAE,MAAM,IAAI,GACtB,MAAM,IAAI;CAwGd;AAGD,wBAAgB,YAAY,CAAC,MAAM,SAAS,aAAa,GAAG,aAAa,EACvE,OAAO,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,GAGyB,MAAM,CAC/D"}
|
package/lib/index.js
CHANGED
|
@@ -123,6 +123,10 @@ class DReactionImpl {
|
|
|
123
123
|
this.connected = true;
|
|
124
124
|
const { createSocket, secure, host, environment, port, name, client = {}, info = {}, getClientId, } = this.options;
|
|
125
125
|
const { onCommand, onConnect, onDisconnect } = this.options;
|
|
126
|
+
if (!host) {
|
|
127
|
+
console.log('host is not config, skip connect.');
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
126
130
|
// establish a connection to the server
|
|
127
131
|
const protocol = secure ? 'wss' : 'ws';
|
|
128
132
|
const socket = createSocket(`${protocol}://${host}:${port}`);
|
|
@@ -185,9 +189,16 @@ class DReactionImpl {
|
|
|
185
189
|
}
|
|
186
190
|
return cc.command === command.payload.command;
|
|
187
191
|
})
|
|
188
|
-
.forEach((cc) =>
|
|
189
|
-
? command.payload.args
|
|
190
|
-
|
|
192
|
+
.forEach(async (cc) => {
|
|
193
|
+
const res = await cc.handler(typeof command.payload === 'object' ? command.payload.args : {});
|
|
194
|
+
if (res) {
|
|
195
|
+
// has return value
|
|
196
|
+
this.send('customCommand.response', {
|
|
197
|
+
command: cc.command,
|
|
198
|
+
payload: res,
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
});
|
|
191
202
|
}
|
|
192
203
|
else if (command.type === 'setClientId') {
|
|
193
204
|
this.options.setClientId && this.options.setClientId(command.payload);
|
|
@@ -371,6 +382,7 @@ class DReactionImpl {
|
|
|
371
382
|
title,
|
|
372
383
|
description,
|
|
373
384
|
args,
|
|
385
|
+
responseViewType: config.responseViewType,
|
|
374
386
|
};
|
|
375
387
|
// Increment our id counter
|
|
376
388
|
this.customCommandLatestId += 1;
|
|
@@ -382,6 +394,7 @@ class DReactionImpl {
|
|
|
382
394
|
title: customHandler.title,
|
|
383
395
|
description: customHandler.description,
|
|
384
396
|
args: customHandler.args,
|
|
397
|
+
responseViewType: customHandler.responseViewType,
|
|
385
398
|
});
|
|
386
399
|
return () => {
|
|
387
400
|
this.customCommands = this.customCommands.filter((cc) => cc.id !== customHandler.id);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dreaction-client-core",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.9",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
"@types/ws": "^8.5.13",
|
|
18
18
|
"eventemitter-strict": "^1.0.1",
|
|
19
19
|
"ws": "^8.18.0",
|
|
20
|
-
"dreaction-protocol": "1.0.
|
|
20
|
+
"dreaction-protocol": "1.0.5"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"typescript": "^5.4.5"
|
package/src/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {
|
|
|
4
4
|
CommandMap,
|
|
5
5
|
CommandTypeKey,
|
|
6
6
|
CustomCommandArg,
|
|
7
|
+
CustomCommandRegisterPayload,
|
|
7
8
|
} from 'dreaction-protocol';
|
|
8
9
|
import validate from './validate';
|
|
9
10
|
import logger from './plugins/logger';
|
|
@@ -72,13 +73,9 @@ export type CustomCommandArgs<Args extends CustomCommandArg[]> =
|
|
|
72
73
|
|
|
73
74
|
export interface CustomCommand<
|
|
74
75
|
Args extends CustomCommandArg[] = CustomCommandArg[]
|
|
75
|
-
> {
|
|
76
|
+
> extends Omit<CustomCommandRegisterPayload, 'id' | 'args'> {
|
|
76
77
|
id?: number;
|
|
77
|
-
|
|
78
|
-
handler: (args: CustomCommandArgs<Args>) => void;
|
|
79
|
-
|
|
80
|
-
title?: string;
|
|
81
|
-
description?: string;
|
|
78
|
+
handler: (args: CustomCommandArgs<Args>) => any | Promise<any>;
|
|
82
79
|
args?: Args;
|
|
83
80
|
}
|
|
84
81
|
|
|
@@ -302,6 +299,11 @@ export class DReactionImpl
|
|
|
302
299
|
} = this.options;
|
|
303
300
|
const { onCommand, onConnect, onDisconnect } = this.options;
|
|
304
301
|
|
|
302
|
+
if (!host) {
|
|
303
|
+
console.log('host is not config, skip connect.');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
|
|
305
307
|
// establish a connection to the server
|
|
306
308
|
const protocol = secure ? 'wss' : 'ws';
|
|
307
309
|
const socket = createSocket!(`${protocol}://${host}:${port}`);
|
|
@@ -378,13 +380,19 @@ export class DReactionImpl
|
|
|
378
380
|
|
|
379
381
|
return cc.command === command.payload.command;
|
|
380
382
|
})
|
|
381
|
-
.forEach((cc) =>
|
|
382
|
-
cc.handler(
|
|
383
|
-
typeof command.payload === 'object'
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
)
|
|
387
|
-
|
|
383
|
+
.forEach(async (cc) => {
|
|
384
|
+
const res = await cc.handler(
|
|
385
|
+
typeof command.payload === 'object' ? command.payload.args : {}
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
if (res) {
|
|
389
|
+
// has return value
|
|
390
|
+
this.send('customCommand.response', {
|
|
391
|
+
command: cc.command,
|
|
392
|
+
payload: res,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
});
|
|
388
396
|
} else if (command.type === 'setClientId') {
|
|
389
397
|
this.options.setClientId && this.options.setClientId(command.payload);
|
|
390
398
|
}
|
|
@@ -539,7 +547,7 @@ export class DReactionImpl
|
|
|
539
547
|
}
|
|
540
548
|
|
|
541
549
|
registerCustomCommand(
|
|
542
|
-
config: CustomCommand
|
|
550
|
+
config: CustomCommand,
|
|
543
551
|
optHandler?: () => void
|
|
544
552
|
): () => void {
|
|
545
553
|
let command: string;
|
|
@@ -616,6 +624,7 @@ export class DReactionImpl
|
|
|
616
624
|
title,
|
|
617
625
|
description,
|
|
618
626
|
args,
|
|
627
|
+
responseViewType: config.responseViewType,
|
|
619
628
|
};
|
|
620
629
|
|
|
621
630
|
// Increment our id counter
|
|
@@ -630,6 +639,7 @@ export class DReactionImpl
|
|
|
630
639
|
title: customHandler.title,
|
|
631
640
|
description: customHandler.description,
|
|
632
641
|
args: customHandler.args,
|
|
642
|
+
responseViewType: customHandler.responseViewType,
|
|
633
643
|
});
|
|
634
644
|
|
|
635
645
|
return () => {
|