From a677da4ed518b47d35923018c5728c04b62b17a7 Mon Sep 17 00:00:00 2001 From: uwap Date: Sat, 20 Jan 2018 14:15:16 +0100 Subject: [PATCH] Type mqtt.js a bit --- .flowconfig | 4 ++++ src/connectMqtt.js | 16 +++++++------- types/mqtt.js | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 8 deletions(-) create mode 100644 types/mqtt.js diff --git a/.flowconfig b/.flowconfig index 267709e..b8b683a 100644 --- a/.flowconfig +++ b/.flowconfig @@ -5,8 +5,12 @@ src/ [libs] types/types.js +types/mqtt.js [options] esproposal.export_star_as=enable module.system.node.resolve_dirname=node_modules module.system.node.resolve_dirname=src + +[lints] +all=warn diff --git a/src/connectMqtt.js b/src/connectMqtt.js index 4360734..bf959f0 100644 --- a/src/connectMqtt.js +++ b/src/connectMqtt.js @@ -5,15 +5,15 @@ import _ from "lodash"; // TODO: type mqtt.js export type MqttSettings = { - onReconnect?: (mqtt: Object) => void, - onDisconnect?: (mqtt: Object) => void, - onMessage?: (topic: string, message: Object) => void, - onMessageSent?: (topic: string, message: any) => void, - onConnect?: (mqtt: Object) => void, + onReconnect?: (mqtt: mqtt.Client) => void, + onDisconnect?: (mqtt: mqtt.Client) => void, + onMessage?: (topic: string, message: Buffer) => void, + onMessageSent?: (topic: string, message: Buffer) => void, + onConnect?: (mqtt: mqtt.Client) => void, subscribe?: Array } -export type MessageCallback = (topic: string, message: any) => void; +export type MessageCallback = (topic: string, message: Buffer) => void; export default function connectMqtt( url: string, @@ -28,7 +28,7 @@ export default function connectMqtt( settings.onConnect(client); } }); - client.on("message", (topic, message) => { + client.on("message", (topic: string, message: Buffer) => { if (settings.onMessage != null) { settings.onMessage(topic, message); } @@ -48,7 +48,7 @@ export default function connectMqtt( settings.onReconnect(client); } }); - return (topic: string, message: any) => { + return (topic: string, message: Buffer) => { client.publish(topic, message, null, (error) => { if (error == null && settings.onMessageSent != null) { settings.onMessageSent(topic, message); diff --git a/types/mqtt.js b/types/mqtt.js new file mode 100644 index 0000000..db989bf --- /dev/null +++ b/types/mqtt.js @@ -0,0 +1,55 @@ +declare module 'mqtt' { + declare type QoS = 0 | 1 | 2; + + declare type ClientOptions = { + wsOptions?: any, // TODO: websocket options + keepalive?: number, + reschedulePings?: boolean, + clientId?: string, + protocolId?: 'MQTT' | 'MQIsdp', + protocolVersion?: 3 | 4, + clean?: boolean, + reconnectPeriod?: number, + connectTimeout?: number, + username?: string, + password?: string | Buffer, + incomingStore?: any, // TODO: redux store + outgoingStore?: any, // TODO: store + queueQoSZero?: boolean, + will?: { + topic?: string, + payload?: Buffer, + qos?: QoS, + retain?: boolean + }, + transformWsUrl?: any, // TODO: function + resubscribe?: boolean + }; + + declare type PublishOptions = { + qos?: QoS, + retain?: boolean, + dup?: boolean + }; + + declare type ClientSubscribeOptions = { + qos: QoS + }; + + declare class Client { + constructor(streamBuilder: any, options?: ClientOptions): Client; // TODO: StreamBuilder + + publish(topic: string, message: Buffer | string, options: ?PublishOptions, callback: any): void; // TODO: message: Buffer|string, callback..., topic array and topic object + + subscribe(topic: string | Array, cb: (error: Error, granted: any) => void): void; // TODO: granted + subscribe(topic: string | Array, opts: ClientSubscribeOptions, cb: (error: Error, granted: any) => void): void; // TODO: granted + + on(event: "message", // TODO: packet + cb: (topic: string, payload: Buffer, packet: any) => void): void; + on(event: "offline", cb: () => void): void; + on(event: "reconnect", cb: () => void): void; + on(event: "close", cb: () => void): void; + } + + declare function connect(url: string, options?: ClientOptions): Client; +}