mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-16 23:10:29 +00:00
Add typings for express-ws (#24984)
* Add types for express-ws * strictFunctionTypes=true * dtslint compliant * union types for application and router * allow https server * applyTo accepts object * fix lint error * applyTo accepts router-like obj * websocket method returns this
This commit is contained in:
committed by
Mohamed Hegazy
parent
a8637a544d
commit
ffbf344eb0
@@ -0,0 +1,72 @@
|
||||
import http = require('http');
|
||||
import https = require('https');
|
||||
import express = require('express');
|
||||
import expressWs = require('express-ws');
|
||||
|
||||
const dummyApp = express();
|
||||
const httpServer = http.createServer(dummyApp);
|
||||
const httpsServer = https.createServer({}, dummyApp);
|
||||
|
||||
expressWs(dummyApp); // optional server argument
|
||||
expressWs(dummyApp, httpsServer); // https server allowed
|
||||
expressWs(dummyApp, httpServer, {
|
||||
leaveRouterUntouched: false,
|
||||
// ws server options
|
||||
wsOptions: {
|
||||
clientTracking: true
|
||||
}
|
||||
});
|
||||
|
||||
const { app, getWss, applyTo } = expressWs(express());
|
||||
|
||||
/**
|
||||
* applyTo accepts router object
|
||||
*/
|
||||
applyTo(express.Router());
|
||||
|
||||
/**
|
||||
* applyTo accepts router-like objects
|
||||
*/
|
||||
applyTo({
|
||||
get() { return this; }
|
||||
});
|
||||
|
||||
/**
|
||||
* getWss function returns ws server
|
||||
*/
|
||||
getWss().clients.forEach(ws => {
|
||||
if (ws.readyState !== ws.OPEN) {
|
||||
ws.terminate();
|
||||
return;
|
||||
}
|
||||
ws.ping();
|
||||
});
|
||||
|
||||
/**
|
||||
* ws method is added to express app instance
|
||||
*/
|
||||
app.ws('/', (ws, req) => {
|
||||
ws.on('message', msg => {
|
||||
console.log(msg);
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* ws method is added to express.Router prototype
|
||||
*/
|
||||
const router = express.Router();
|
||||
|
||||
router.ws(
|
||||
'/:id',
|
||||
(ws, req, next) => { next(); },
|
||||
(ws, req, next) => {
|
||||
ws.send(req.params.id);
|
||||
|
||||
ws.on('close', (code, reason) => {
|
||||
console.log('code:', code);
|
||||
console.log('reason:', reason);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
app.use(router);
|
||||
Vendored
+47
@@ -0,0 +1,47 @@
|
||||
// Type definitions for express-ws 3.0
|
||||
// Project: https://github.com/HenningM/express-ws
|
||||
// Definitions by: AJ Livingston <https://github.com/ajliv>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.3
|
||||
|
||||
import * as core from 'express-serve-static-core';
|
||||
import * as express from 'express';
|
||||
import * as http from 'http';
|
||||
import * as https from 'https';
|
||||
import * as ws from 'ws';
|
||||
|
||||
declare module 'express' {
|
||||
function Router(options?: RouterOptions): expressWs.Router;
|
||||
}
|
||||
|
||||
declare function expressWs(app: express.Application, server?: http.Server | https.Server, options?: expressWs.Options): expressWs.Instance;
|
||||
declare namespace expressWs {
|
||||
type Application = express.Application & WithWebsocketMethod;
|
||||
type Router = express.Router & WithWebsocketMethod;
|
||||
|
||||
interface Options {
|
||||
leaveRouterUntouched?: boolean;
|
||||
wsOptions?: ws.ServerOptions;
|
||||
}
|
||||
|
||||
interface RouterLike {
|
||||
get: express.IRouterMatcher<this>;
|
||||
[key: string]: any;
|
||||
[key: number]: any;
|
||||
}
|
||||
|
||||
interface Instance {
|
||||
app: Application;
|
||||
applyTo(target: RouterLike): void;
|
||||
getWss(): ws.Server;
|
||||
}
|
||||
|
||||
type WebsocketRequestHandler = (ws: ws, req: express.Request, next: express.NextFunction) => void;
|
||||
type WebsocketMethod<T> = (route: core.PathParams, ...middlewares: WebsocketRequestHandler[]) => T;
|
||||
|
||||
interface WithWebsocketMethod {
|
||||
ws: WebsocketMethod<this>;
|
||||
}
|
||||
}
|
||||
|
||||
export = expressWs;
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictFunctionTypes": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"express-ws-tests.ts"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user