From be78ef130f3a2950f57b99bbb1fb93c4d50b1d1d Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Mon, 2 Jul 2018 19:02:25 +0200 Subject: [PATCH] [koa-websocket] Expose KoaWebsocket namespace (#26811) * Expose KoaWebsocket namespace * Lint fixes --- types/koa-websocket/index.d.ts | 45 ++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/types/koa-websocket/index.d.ts b/types/koa-websocket/index.d.ts index ebbfeee8f2..38fb0c0994 100644 --- a/types/koa-websocket/index.d.ts +++ b/types/koa-websocket/index.d.ts @@ -1,6 +1,7 @@ // Type definitions for koa-websocket 5.0 // Project: https://github.com/kudos/koa-websocket // Definitions by: Maƫl Lavault +// Jaco Greeff // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 @@ -9,26 +10,32 @@ 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; -interface KoaWebsocketMiddlewareContext extends Koa.Context { - websocket: ws; - path: string; +declare namespace KoaWebsocket { + type ConnectionHandler = (socket: ws) => void; + + type Middleware = (this: MiddlewareContext, context: Koa.Context, next: () => Promise) => any; + + interface MiddlewareContext extends Koa.Context { + websocket: ws; + path: string; + } + + class Server { + app: Koa; + middleware: Koa.Middleware[]; + + constructor(app: Koa); + + listen(options: ws.ServerOptions): ws.Server; + onConnection(handler: ConnectionHandler): void; + use(middleware: Middleware): this; + } + + interface App extends Koa { + ws: Server; + } } -declare class KoaWebsocketServer { - app: Koa; - middleware: Koa.Middleware[]; +declare function websockets(app: Koa): KoaWebsocket.App; - 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;