// Type definitions for koa-websocket 2.1
// Project: https://github.com/kudos/koa-websocket
// Definitions by: My Self <https://github.com/me>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import Koa = require('koa');
import * as ws from 'ws';
import * as http from 'http';
import * as https from 'https';

type KoaWebsocketConnectionHandler = (socket: ws) => void;
type KoaWebsocketMiddleware = (this: KoaWebsocketMiddlewareContext, context: Koa.Context, next: () => Promise<any>) => any;
interface KoaWebsocketMiddlewareContext extends Koa.Context {
    websocket: ws;
    path: string;
}

declare class KoaWebsocketServer {
    app: Koa;
    middleware: Koa.Middleware[];

    constructor(app: Koa);
    listen(server: http.Server | https.Server): ws.Server;
    onConnection(handler: KoaWebsocketConnectionHandler): void;
    use(middleware: KoaWebsocketMiddleware): this;
}

interface KoaWebsocketApp extends Koa {
    ws: KoaWebsocketServer;
}

declare function websockets(app: Koa): KoaWebsocketApp;
export = websockets;

// Type definitions for koa-websocket 5.0
// Project: https://github.com/kudos/koa-websocket
// Definitions by: Maël Lavault <https://github.com/moimael>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3

import Koa = require('koa');
import * as ws from 'ws';
import * as http from 'http';
import * as https from 'https';

type KoaWebsocketConnectionHandler = (socket: ws) => void;
type KoaWebsocketMiddleware = (this: KoaWebsocketMiddlewareContext, context: Koa.Context, next: () => Promise<any>) => any;
interface KoaWebsocketMiddlewareContext extends Koa.Context {
    websocket: ws;
    path: string;
}

declare class KoaWebsocketServer {
    app: Koa;
    middleware: Koa.Middleware[];

    constructor(app: Koa);
    listen(options: ws.ServerOptions): ws.Server;
    onConnection(handler: KoaWebsocketConnectionHandler): void;
    use(middleware: KoaWebsocketMiddleware): this;
}

interface KoaWebsocketApp extends Koa {
    ws: KoaWebsocketServer;
}

declare function websockets(app: Koa): KoaWebsocketApp;
export = websockets;

Support passing ws options to listen.
Support passing ws options to listen.

Fix ts breaking when passing anything to listen.

Signed-off-by: Maël Lavault <80a5833c0cb45e7ff3bf6d5c5214e811c56ae7c5@izettle.com>
