mqtt-plus 0.9.0 → 0.9.2

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/README.md CHANGED
@@ -22,28 +22,26 @@ $ npm install mqtt mqtt-plus
22
22
  About
23
23
  -----
24
24
 
25
- This is **MQTT+**, an addon JavaScript/TypeScript API for the excellent
26
- [MQTT.js](https://www.npmjs.com/package/mqtt), for additional
27
- communication patterns like [Remote Procedure
28
- Call](https://en.wikipedia.org/wiki/Remote_procedure_call) (RPC).
29
- This allows a bi-directional request/response-style
30
- communication over the technically uni-directional message protocol
31
- [MQTT](http://mqtt.org).
32
-
33
- Conceptually, the **MQTT+** API provides two types of communication patterns:
25
+ This is **MQTT+**, an addon API for the excellent
26
+ [MQTT](http://mqtt.org/) client TypeScript/JavaScript API
27
+ [MQTT.js](https://www.npmjs.com/package/mqtt), provoding additional
28
+ communication patterns with optional type safety:
34
29
 
35
30
  - **Event Emission**:
31
+
36
32
  Event Emission is a *uni-directional* communication pattern.
37
33
  An Event is the combination of an event name and optionally zero or more arguments.
38
34
  You *subscribe* to events.
39
35
  When an event is *emitted*, either a single particular subscriber (in case of
40
36
  a directed event emission) or all subscribers are called and receive the
41
37
  arguments as extra information.
38
+
42
39
  In contrast to the regular MQTT message publish/subscribe, this
43
40
  pattern allows to direct the event to particular subscribers and
44
41
  provides optional information about the sender to subscribers.
45
42
 
46
43
  - **Service Call**:
44
+
47
45
  Service Call is a *bi-directional* communication pattern.
48
46
  A Service is the combination of a service name and optionally zero or more arguments.
49
47
  You *register* for a service.
@@ -52,6 +50,11 @@ Conceptually, the **MQTT+** API provides two types of communication patterns:
52
50
  receives the arguments as the request. The registrator then has to
53
51
  provide the service response.
54
52
 
53
+ In contrast to the regular uni-directional MQTT message
54
+ publish/subscribe communication, this allows a bi-directional [Remote
55
+ Procedure Call](https://en.wikipedia.org/wiki/Remote_procedure_call)
56
+ (RPC) style communication.
57
+
55
58
  > [!Note]
56
59
  > **MQTT+** is similar to and derived from
57
60
  > [MQTT-JSON-RPC](https://github.com/rse/mqtt-json-rpc) of the same
@@ -115,11 +118,11 @@ mqtt.on("connect", () => {
115
118
  Application Programming Interface
116
119
  ---------------------------------
117
120
 
118
- The MQTT+ API provides the following methods:
121
+ The **MQTT+** API provides the following methods:
119
122
 
120
123
  - **Construction**:<br/>
121
124
 
122
- constructor(
125
+ constructor<API>(
123
126
  mqtt: MqttClient,
124
127
  options?: {
125
128
  id: string
@@ -134,8 +137,14 @@ The MQTT+ API provides the following methods:
134
137
  }
135
138
  )
136
139
 
140
+ The `API` is an optional TypeScript type `Record<string, ((...args:
141
+ any[]) => void) | ((...args: any[]) => any)>`, describing the
142
+ available events and services. Callbacks without return values
143
+ describe events, callbacks with return values describe services.
144
+
137
145
  The `mqtt` is the [MQTT.js](https://www.npmjs.com/package/mqtt) instance,
138
- which has to be establish separately.
146
+ which has to be established separately.
147
+
139
148
  The optional `options` object supports the following fields:
140
149
  - `id`: Custom MQTT peer identifier (default: auto-generated NanoID).
141
150
  - `codec`: Encoding format (default: `cbor`).
@@ -159,7 +168,7 @@ The MQTT+ API provides the following methods:
159
168
  subscribe(
160
169
  event: string,
161
170
  options?: MQTT::IClientSubscribeOptions
162
- callback: (...params: any[], info?: sender: string, receiver?: string) => void
171
+ callback: (...params: any[], info: { sender: string, receiver?: string }) => void
163
172
  ): Promise<Subscription>
164
173
 
165
174
  Subscribe to an event.
@@ -179,7 +188,7 @@ The MQTT+ API provides the following methods:
179
188
  register(
180
189
  service: string,
181
190
  options?: MQTT::IClientSubscribeOptions
182
- callback: (...params: any[], info?: sender: string, receiver?: string) => any
191
+ callback: (...params: any[], info: { sender: string, receiver?: string }) => any
183
192
  ): Promise<Registration>
184
193
 
185
194
  Register a service.
@@ -247,7 +256,7 @@ The MQTT+ API provides the following methods:
247
256
  Internals
248
257
  ---------
249
258
 
250
- In the following, assume that an MQTT+ instance is created with:
259
+ In the following, we assume that an **MQTT+** instance is created with:
251
260
 
252
261
  ```ts
253
262
  import MQTT from "mqtt"
@@ -272,8 +281,8 @@ ones):
272
281
 
273
282
  ```json
274
283
  {
275
- "id": "RRRRRRRRRRRRRRRRRRRRR",
276
- "sender": "SSSSSSSSSSSSSSSSSSSSS",
284
+ "id": "vwLzfQDu2uEeOdOfIlT42",
285
+ "sender": "2IBMSk0NPnrz1AeTERoea",
277
286
  "method": "example/hello",
278
287
  "params": [ "world", 42 ]
279
288
  }
@@ -290,12 +299,12 @@ mqttp.register("example/hello", (a1, a2) => {
290
299
  ...and then its result, in the above `mqttp.call()` example `"world:42"`, is then
291
300
  sent back as the following success response
292
301
  message to the temporary (client-specific) MQTT topic
293
- `example/hello/service-response/SSSSSSSSSSSSSSSSSSSSS`:
302
+ `example/hello/service-response/2IBMSk0NPnrz1AeTERoea`:
294
303
 
295
304
  ```json
296
305
  {
297
- "id": "RRRRRRRRRRRRRRRRRRRRR",
298
- "sender": "SSSSSSSSSSSSSSSSSSSSS",
306
+ "id": "vwLzfQDu2uEeOdOfIlT42",
307
+ "sender": "2IBMSk0NPnrz1AeTERoea",
299
308
  "result": "world:42"
300
309
  }
301
310
  ```
@@ -422,8 +431,6 @@ The output will be:
422
431
  ```
423
432
  $ node sample.ts
424
433
  CONNECT
425
- example/sample: info: world 42 undefined
426
- RECEIVED example/sample/event-notice {"id":"GraDZnA4BLrF66g9qmpPX","sender":"2IBMSk0NPnrz1AeTERoea","event":"example/sample","params":["world",42]}
427
434
  RECEIVED example/hello/service-request {"id":"vwLzfQDu2uEeOdOfIlT42","sender":"2IBMSk0NPnrz1AeTERoea","service":"example/hello","params":["world",42]}
428
435
  example/hello: request: world 42 undefined
429
436
  RECEIVED example/hello/service-response/2IBMSk0NPnrz1AeTERoea {"id":"vwLzfQDu2uEeOdOfIlT42","sender":"2IBMSk0NPnrz1AeTERoea","receiver":"2IBMSk0NPnrz1AeTERoea","result":"world:42"}
@@ -30,7 +30,7 @@ export interface Info {
30
30
  receiver?: string;
31
31
  }
32
32
  export type WithInfo<F> = F extends (...args: infer P) => infer R ? (...args: [...P, info: Info]) => R : never;
33
- export type APISchema = Record<string, (...args: any[]) => any>;
33
+ export type APISchema = Record<string, ((...args: any[]) => void) | ((...args: any[]) => any)>;
34
34
  export type EventKeys<T> = string extends keyof T ? string : {
35
35
  [K in keyof T]: T[K] extends (...args: any[]) => infer R ? [R] extends [void] ? K : never : never;
36
36
  }[keyof T];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mqtt-plus",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "MQTT Communication Patterns",
5
5
  "keywords": [ "mqtt", "event", "service", "rpc", "request", "response" ],
6
6
  "license": "MIT",
package/src/mqtt-plus.ts CHANGED
@@ -78,7 +78,11 @@ export type WithInfo<F> = F extends (...args: infer P) => infer R
78
78
  : never
79
79
 
80
80
  /* type utilities for generic API */
81
- export type APISchema = Record<string, (...args: any[]) => any>
81
+ export type APISchema = Record<
82
+ string,
83
+ ((...args: any[]) => void) |
84
+ ((...args: any[]) => any)
85
+ >
82
86
 
83
87
  /* extract event keys where return type IS void (events: subscribe/notify/control) */
84
88
  export type EventKeys<T> = string extends keyof T ? string : {