Skip to content

Commit

Permalink
fix: write methods overload
Browse files Browse the repository at this point in the history
  • Loading branch information
robertsLando committed Jul 21, 2023
1 parent 960caab commit 1c56921
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,18 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
* client.publish('topic', 'message', {qos: 1, retain: true, dup: true});
* @example client.publish('topic', 'message', console.log);
*/
public publish(topic: string, message: string | Buffer): MqttClient
public publish(
topic: string,
message: string | Buffer,
callback?: DoneCallback,
): MqttClient
public publish(
topic: string,
message: string | Buffer,
opts?: IClientPublishOptions,
callback?: DoneCallback,
): MqttClient
public publish(
topic: string,
message: string | Buffer,
Expand Down Expand Up @@ -1058,6 +1070,22 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
* @example client.subscribe({'topic': {qos: 0}, 'topic2': {qos: 1}}, console.log);
* @example client.subscribe('topic', console.log);
*/
public subscribe(
topicObject: string | string[] | ISubscriptionMap,
): MqttClient
public subscribe(
topicObject: string | string[] | ISubscriptionMap,
callback?: ClientSubscribeCallback,
): MqttClient
public subscribe(
topicObject: string | string[] | ISubscriptionMap,
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
): MqttClient
public subscribe(
topicObject: string | string[] | ISubscriptionMap,
opts?: IClientSubscribeOptions | IClientSubscribeProperties,
callback?: ClientSubscribeCallback,
): MqttClient
public subscribe(
topicObject: string | string[] | ISubscriptionMap,
opts?:
Expand Down Expand Up @@ -1259,6 +1287,20 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
* @example client.unsubscribe('topic');
* @example client.unsubscribe('topic', console.log);
*/
public unsubscribe(topic: string | string[]): MqttClient
public unsubscribe(
topic: string | string[],
opts?: IClientSubscribeOptions,
): MqttClient
public unsubscribe(
topic: string | string[],
callback?: PacketCallback,
): MqttClient
public unsubscribe(
topic: string | string[],
opts?: IClientSubscribeOptions,
callback?: PacketCallback,
): MqttClient
public unsubscribe(
topic: string | string[],
opts?: IClientSubscribeOptions | PacketCallback,
Expand Down Expand Up @@ -1348,6 +1390,15 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
*
* @api public
*/
public end(cb?: DoneCallback): MqttClient
public end(force?: boolean): MqttClient
public end(opts?: Partial<IDisconnectPacket>, cb?: DoneCallback): MqttClient
public end(force?: boolean, cb?: DoneCallback): MqttClient
public end(
force?: boolean,
opts?: Partial<IDisconnectPacket>,
cb?: DoneCallback,
): MqttClient
public end(
force?: boolean | Partial<IDisconnectPacket> | DoneCallback,
opts?: Partial<IDisconnectPacket> | DoneCallback,
Expand Down
8 changes: 7 additions & 1 deletion src/lib/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ function parseAuthOptions(opts: IClientOptions) {
/**
* connect - connect to an MQTT broker.
*/
export default function connect(
function connect(brokerUrl: string): MqttClient
function connect(opts: IClientOptions): MqttClient
function connect(brokerUrl: string, opts?: IClientOptions): MqttClient
function connect(
brokerUrl: string | IClientOptions,
opts?: IClientOptions,
): MqttClient {
Expand Down Expand Up @@ -170,9 +173,12 @@ export default function connect(
client.on('error', () => {
/* Automatically set up client error handling */
})

return client
}

export default connect

export {
Store,
MqttClient,
Expand Down
2 changes: 1 addition & 1 deletion test/abstract_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export default function abstractTest(server, config) {
if (typeof opts === 'string') {
opts = { host: opts }
}
opts = { ...config, ...opts }
opts = { ...config, ...opts } as IClientOptions
return mqtt.connect(opts)
}

Expand Down

0 comments on commit 1c56921

Please sign in to comment.