@eduardorothdev/rxjs-mqtt
Advanced tools
Comparing version 1.0.7 to 1.0.8
import { IClientOptions, IClientReconnectOptions, ISubscriptionMap, MqttClient, Packet, PacketCallback } from "mqtt"; | ||
import { Observable } from "rxjs"; | ||
import { MessagePacket } from "@eduardorothdev/rxjs-mqtt/types"; | ||
import { typedMessage } from "@eduardorothdev/rxjs-mqtt/parsers"; | ||
declare class ObsClient { | ||
@@ -25,5 +26,3 @@ _client: MqttClient; | ||
on(event: string): Observable<MessagePacket>; | ||
onJsonMessage<T>(): Observable<Omit<MessagePacket, "payload"> & { | ||
payload: string | JSON | T; | ||
}>; | ||
onJsonMessage<T>(): Observable<typedMessage<T>>; | ||
once(event: string): Observable<unknown>; | ||
@@ -30,0 +29,0 @@ prependListener(event: string, listener: (...args: any[]) => void): MqttClient; |
import { OperatorFunction } from "rxjs"; | ||
import { MessagePacket } from "@eduardorothdev/rxjs-mqtt/types"; | ||
declare function parsePayload<T>(parser?: (buffer: Buffer) => T | string): OperatorFunction<MessagePacket, Omit<MessagePacket, "payload"> & { | ||
export type typedMessage<T> = Omit<MessagePacket, "payload"> & { | ||
payload: T | string; | ||
}>; | ||
declare function parsePayloadToJSON<T>(): OperatorFunction<MessagePacket, Omit<MessagePacket, "payload"> & { | ||
payload: string | T | JSON; | ||
}>; | ||
}; | ||
declare function parsePayload<T>(parser?: (buffer: Buffer) => T | string): OperatorFunction<MessagePacket, typedMessage<T>>; | ||
declare function parsePayloadToJSON<T>(): OperatorFunction<MessagePacket, typedMessage<T>>; | ||
export { parsePayload, parsePayloadToJSON }; |
{ | ||
"name": "@eduardorothdev/rxjs-mqtt", | ||
"version": "1.0.7", | ||
"version": "1.0.8", | ||
"description": "RxJS wrapper for MQTT. Based on async-mqtt", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -15,3 +15,6 @@ import { | ||
import { MessagePacket } from "@eduardorothdev/rxjs-mqtt/types"; | ||
import { parsePayloadToJSON } from "@eduardorothdev/rxjs-mqtt/parsers"; | ||
import { | ||
parsePayloadToJSON, | ||
typedMessage, | ||
} from "@eduardorothdev/rxjs-mqtt/parsers"; | ||
@@ -18,0 +21,0 @@ class ObsClient { |
import { map, OperatorFunction, pipe } from "rxjs"; | ||
import { MessagePacket } from "@eduardorothdev/rxjs-mqtt/types"; | ||
export type typedMessage<T> = Omit<MessagePacket, "payload"> & { | ||
payload: T | string; | ||
}; | ||
function parsePayload<T>( | ||
parser: (buffer: Buffer) => T | string = (buffer: Buffer) => | ||
buffer.toString(), | ||
): OperatorFunction< | ||
MessagePacket, | ||
Omit<MessagePacket, "payload"> & { payload: T | string } | ||
> { | ||
): OperatorFunction<MessagePacket, typedMessage<T>> { | ||
return pipe( | ||
map((v) => ({ | ||
...v, | ||
payload: parser(v.payload), | ||
})), | ||
map( | ||
(v): typedMessage<T> => ({ | ||
...v, | ||
payload: parser(v.payload), | ||
}), | ||
), | ||
); | ||
} | ||
function parsePayloadToJSON<T>() { | ||
return parsePayload<T | JSON>((buffer) => JSON.parse(buffer.toString())); | ||
function parsePayloadToJSON<T>(): OperatorFunction< | ||
MessagePacket, | ||
typedMessage<T> | ||
> { | ||
return parsePayload<T>((buffer) => JSON.parse(buffer.toString())); | ||
} | ||
export { parsePayload, parsePayloadToJSON }; |
22106
544