From 7f8f7853566ad7d73e0e7c8ffafb463c3fa72313 Mon Sep 17 00:00:00 2001 From: Ben Salili-James Date: Fri, 11 Oct 2019 01:22:00 +0100 Subject: [PATCH] Add socket.io-emitter types (#39038) --- types/socket.io-emitter/index.d.ts | 53 +++++++++++++++++++ .../socket.io-emitter-tests.ts | 17 ++++++ types/socket.io-emitter/tsconfig.json | 23 ++++++++ types/socket.io-emitter/tslint.json | 1 + 4 files changed, 94 insertions(+) create mode 100644 types/socket.io-emitter/index.d.ts create mode 100644 types/socket.io-emitter/socket.io-emitter-tests.ts create mode 100644 types/socket.io-emitter/tsconfig.json create mode 100644 types/socket.io-emitter/tslint.json diff --git a/types/socket.io-emitter/index.d.ts b/types/socket.io-emitter/index.d.ts new file mode 100644 index 0000000000..679acb8d36 --- /dev/null +++ b/types/socket.io-emitter/index.d.ts @@ -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 +// 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; + } + + 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; diff --git a/types/socket.io-emitter/socket.io-emitter-tests.ts b/types/socket.io-emitter/socket.io-emitter-tests.ts new file mode 100644 index 0000000000..81adc21566 --- /dev/null +++ b/types/socket.io-emitter/socket.io-emitter-tests.ts @@ -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; diff --git a/types/socket.io-emitter/tsconfig.json b/types/socket.io-emitter/tsconfig.json new file mode 100644 index 0000000000..1d119e12f9 --- /dev/null +++ b/types/socket.io-emitter/tsconfig.json @@ -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" + ] +} diff --git a/types/socket.io-emitter/tslint.json b/types/socket.io-emitter/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/socket.io-emitter/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }