Add type definitions for koa-websocket (#16221)

This commit is contained in:
Mike Cook 2017-05-01 20:34:37 +01:00 committed by Mohamed Hegazy
parent 0a29ae08a9
commit 0cefb71e50
4 changed files with 73 additions and 0 deletions

36
types/koa-websocket/index.d.ts vendored Normal file
View File

@ -0,0 +1,36 @@
// 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.2
import * as Koa from '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;
}
type KoaWebsockets = (app: Koa) => KoaWebsocketApp;
declare const websockets: KoaWebsockets;
export = websockets;

View File

@ -0,0 +1,14 @@
import * as Koa from 'koa';
import * as websocket from 'koa-websocket';
const app = websocket(new Koa());
app.ws.use(async function(context, next) {
this.websocket.on('message', (message) => {
console.log(message);
});
this.websocket.send('Hello world');
await next();
});
app.listen(3000);

View File

@ -0,0 +1,22 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"koa-websocket-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }