Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @types for TypeScript. #20

Open
lygstate opened this issue Jan 3, 2019 · 1 comment
Open

Add @types for TypeScript. #20

lygstate opened this issue Jan 3, 2019 · 1 comment

Comments

@lygstate
Copy link

lygstate commented Jan 3, 2019

index.d.ts

import { Duplexify } from 'duplexify';
import { Stream, Duplex } from 'stream'

export = Connection;

type ConnectionCallback = ()=>void

interface ConnectionConstructor {
    (duplex?: Duplex, opts?: any | ConnectionCallback, cb?: ConnectionCallback): Connection.Connection;
    new(duplex?: Duplex, opts?: any | ConnectionCallback, cb?: ConnectionCallback): Connection.Connection;
}
declare var Connection: ConnectionConstructor;
declare namespace Connection {
    interface Connection extends Duplexify {
        connect(opts:any, cb?:ConnectionCallback):void;
        connack(opts:any, cb?:ConnectionCallback):void;
        publish(opts:any, cb?:ConnectionCallback):void;
        puback(opts:any, cb?:ConnectionCallback):void;
        pubrec(opts:any, cb?:ConnectionCallback):void;
        pubrel(opts:any, cb?:ConnectionCallback):void;
        pubcomp(opts:any, cb?:ConnectionCallback):void;
        subscribe(opts:any, cb?:ConnectionCallback):void;
        suback(opts:any, cb?:ConnectionCallback):void;
        unsubscribe(opts:any, cb?:ConnectionCallback):void;
        unsuback(opts:any, cb?:ConnectionCallback):void;
        pingreq(opts:any, cb?:ConnectionCallback):void;
        pingresp(opts:any, cb?:ConnectionCallback):void;
        disconnect(opts:any, cb?:ConnectionCallback):void;
        auth(opts:any, cb?:ConnectionCallback):void;

        destroy():void;
        setOptions(opts:any):void;
    }
}
@lygstate lygstate changed the title Add @types Add @types for TypeScript. Jan 3, 2019
@JohnCMcDonough
Copy link

I'm still working on these, but I think this might be slightly better?

  import { EventEmitter } from 'events';
  import { Duplexify } from 'duplexify';
  import * as mqtt from 'mqtt-packet';
  import { Stream, Duplex } from 'stream'

  export = Connection;

  type ConnectionCallback = () => void

  interface ConnectionConstructor {
    (duplex?: Duplex, opts?: any | ConnectionCallback, cb?: ConnectionCallback): Connection.Connection;
    new(duplex?: Duplex, opts?: any | ConnectionCallback, cb?: ConnectionCallback): Connection.Connection;
  }
  declare  var Connection: ConnectionConstructor;
  declare namespace Connection {
    interface Connection extends Duplexify, EventEmitter {
      connect(opts: Partial<mqtt.IConnectPacket>, cb?: ConnectionCallback): void;
      connack(opts: Partial<mqtt.IConnackPacket>, cb?: ConnectionCallback): void;
      publish(opts: Partial<mqtt.IPubackPacket>, cb?: ConnectionCallback): void;
      puback(opts: Partial<mqtt.IPubackPacket>, cb?: ConnectionCallback): void;
      pubrec(opts: Partial<mqtt.IPubrecPacket>, cb?: ConnectionCallback): void;
      pubrel(opts: Partial<mqtt.IPubrelPacket>, cb?: ConnectionCallback): void;
      pubcomp(opts: Partial<mqtt.IPubcompPacket>, cb?: ConnectionCallback): void;
      subscribe(opts: Partial<mqtt.ISubscribePacket>, cb?: ConnectionCallback): void;
      suback(opts: Partial<mqtt.ISubackPacket>, cb?: ConnectionCallback): void;
      unsubscribe(opts: Partial<mqtt.IUnsubscribePacket>, cb?: ConnectionCallback): void;
      unsuback(opts: Partial<mqtt.IUnsubackPacket>, cb?: ConnectionCallback): void;
      pingreq(opts: Partial<mqtt.IPingreqPacket>, cb?: ConnectionCallback): void;
      pingresp(opts: Partial<mqtt.IPingrespPacket>, cb?: ConnectionCallback): void;
      disconnect(opts: Partial<mqtt.IDisconnectPacket>, cb?: ConnectionCallback): void;
      auth(opts: any, cb?: ConnectionCallback): void;

      on(event: "connack", cb: (packet: mqtt.IConnackPacket) => void);
      on(event: "connect", cb: (packet: mqtt.IConnectPacket) => void);
      on(event: "disconnect", cb: (packet: mqtt.IDisconnectPacket) => void);
      on(event: "pingreq", cb: (packet: mqtt.IPingreqPacket) => void);
      on(event: "pingresp", cb: (packet: mqtt.IPingrespPacket) => void);
      on(event: "puback", cb: (packet: mqtt.IPubackPacket) => void);
      on(event: "pubcomp", cb: (packet: mqtt.IPubcompPacket) => void);
      on(event: "publish", cb: (packet: mqtt.IPublishPacket) => void);
      on(event: "pubrel", cb: (packet: mqtt.IPubrelPacket) => void);
      on(event: "pubrec", cb: (packet: mqtt.IPubrecPacket) => void);
      on(event: "suback", cb: (packet: mqtt.ISubackPacket) => void);
      on(event: "subscribe", cb: (packet: mqtt.ISubscribePacket & mqtt.ISubscription) => void); // why is this packet wrong without the union?
      on(event: "unsuback", cb: (packet: mqtt.IUnsubackPacket) => void);
      on(event: "unsubscribe", cb: (packet: mqtt.IUnsubscribePacket) => void);


      on(event: 'close', cb: () => void)
      on(event: 'error', cb: () => void)
      on(event: 'disconnect', cb: () => void)

      destroy(): void;
      setOptions(opts: any): void;
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants