Add socket.io-emitter types (#39038)

This commit is contained in:
Ben Salili-James
2019-10-10 17:21:59 -07:00
committed by Armando Aguirre
parent dc72e8ed3b
commit 7f8f785356
4 changed files with 94 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
// Type definitions for socket.io-emitter 3.1
// Project: https://github.com/socketio/socket.io-emitter#readme
// Definitions by: Ben Salili-James <https://github.com/benhjames>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace init {
/**
* The Redis client passed to `socket.io-emitter` only requires the `publish`
* method, so accept any client that implements this.
*/
interface RedisClient {
publish: (channel: string, message: string) => Promise<any>;
}
class SocketIOEmitter {
redis: RedisClient;
prefix: string;
nsp: string;
channel: string;
private _rooms: string[];
private _flags: Record<'broadcast' | 'json' | 'volatile', boolean | undefined>;
// TypeScript Version: 3.6
get broadcast(): this;
// TypeScript Version: 3.6
get json(): this;
// TypeScript Version: 3.6
get volatile(): this;
in(room: string): this;
to(room: string): this;
of(nsp: string): SocketIOEmitter;
emit(...data: any[]): this;
}
type SocketIORedisOptions = { host: string; port: number } | { socket: string };
interface SocketIOEmitterOptions {
key?: string;
}
}
declare function init(
redisOptions: init.RedisClient | init.SocketIORedisOptions | string,
options?: init.SocketIOEmitterOptions,
): init.SocketIOEmitter;
export = init;
@@ -0,0 +1,17 @@
import ioEmitter = require('socket.io-emitter');
const io1 = ioEmitter('localhost:6379');
const io2 = ioEmitter('localhost:6379', { key: 'my-key' });
const io3 = ioEmitter({ host: 'localhost', port: 6379 }, { key: 'my-key' });
io1.emit('broadcast', 'hello');
io2.to('game').emit('new-game', 'testing');
const nsp = io3.of('/admin');
nsp.emit('namespace', 'hello namespace');
nsp.to('notifications').emit('namespace', 'hello notifications in namespace');
io1.broadcast.json.volatile;
+23
View File
@@ -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",
"socket.io-emitter-tests.ts"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }