DefinitelyTyped/types/koa-websocket/index.d.ts
Eunchong Yu 2a4a6d66af [koa-websocket] Fix to match with its implementation (#29611)
* Fix koa-websocket to match with its implementation

The major change is about `Middleware`. Koa-websocket doesn't inject
`this` argument to its middlewares. Instead, additional properties are
appended on the Koa context.

https://github.com/kudos/koa-websocket/blob/master/index.js

* Extract Context.websocket

It's necessary to use well with other middlewares,
like koa-router or else.

* Add Context.app to be able to access ctx.app.ws

* Add a test about Context.app

* Remove the target requirement
2018-10-17 08:41:51 -07:00

52 lines
1.5 KiB
TypeScript

// Type definitions for koa-websocket 5.0
// Project: https://github.com/kudos/koa-websocket
// Definitions by: Maël Lavault <https://github.com/moimael>
// Jaco Greeff <https://github.com/jacogr>
// Martin Ždila <https://github.com/zdila>
// Eunchong Yu <https://github.com/Kroisse>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import Koa = require('koa');
import compose = require('koa-compose');
import * as ws from 'ws';
import * as http from 'http';
import * as https from 'https';
declare module "koa" {
interface Context {
websocket: ws;
path: string;
}
}
declare namespace KoaWebsocket {
type Middleware = compose.Middleware<MiddlewareContext>;
interface MiddlewareContext extends Koa.Context {
// Limitation: Declaration merging cannot overwrap existing properties.
// That's why this property is here, not in the merged declaration above.
app: App;
}
class Server {
app: App;
middleware: Middleware[];
server?: ws.Server;
constructor(app: Koa);
listen(options: ws.ServerOptions): ws.Server;
onConnection(socket: ws, request: http.IncomingMessage): void;
use(middleware: Middleware): this;
}
interface App extends Koa {
ws: Server;
}
}
declare function KoaWebsocket(app: Koa, wsOptions?: ws.ServerOptions, httpsOptions?: https.ServerOptions): KoaWebsocket.App;
export = KoaWebsocket;