diff --git a/types/koa-websocket/index.d.ts b/types/koa-websocket/index.d.ts new file mode 100644 index 0000000000..115dbb43f6 --- /dev/null +++ b/types/koa-websocket/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for koa-websocket 2.1 +// Project: https://github.com/kudos/koa-websocket +// Definitions by: My Self +// 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; +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; diff --git a/types/koa-websocket/koa-websocket-tests.ts b/types/koa-websocket/koa-websocket-tests.ts new file mode 100644 index 0000000000..66bd4bf4d2 --- /dev/null +++ b/types/koa-websocket/koa-websocket-tests.ts @@ -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); diff --git a/types/koa-websocket/tsconfig.json b/types/koa-websocket/tsconfig.json new file mode 100644 index 0000000000..effcb59356 --- /dev/null +++ b/types/koa-websocket/tsconfig.json @@ -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" + ] +} diff --git a/types/koa-websocket/tslint.json b/types/koa-websocket/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/koa-websocket/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }