Skip to content

Commit 891b187

Browse files
fix(typings): properly type the adapter attribute
Related: #3796
1 parent b84ed1e commit 891b187

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

lib/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type ParentNspNameMatchFn = (
3737
fn: (err: Error | null, success: boolean) => void
3838
) => void;
3939

40+
type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
41+
4042
interface EngineOptions {
4143
/**
4244
* how many ms without a pong packet to consider the connection closed
@@ -152,7 +154,7 @@ interface ServerOptions extends EngineAttachOptions {
152154
* the adapter to use
153155
* @default the in-memory adapter (https://github.com/socketio/socket.io-adapter)
154156
*/
155-
adapter: any;
157+
adapter: AdapterConstructor;
156158
/**
157159
* the parser to use
158160
* @default the default parser (https://github.com/socketio/socket.io-parser)
@@ -207,7 +209,7 @@ export class Server<
207209
ParentNspNameMatchFn,
208210
ParentNamespace<ListenEvents, EmitEvents, ServerSideEvents>
209211
> = new Map();
210-
private _adapter?: typeof Adapter;
212+
private _adapter?: AdapterConstructor;
211213
private _serveClient: boolean;
212214
private opts: Partial<EngineOptions>;
213215
private eio;
@@ -360,10 +362,11 @@ export class Server<
360362
* @return self when setting or value when getting
361363
* @public
362364
*/
363-
public adapter(): typeof Adapter | undefined;
364-
public adapter(v: typeof Adapter): this;
365-
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this;
366-
public adapter(v?: typeof Adapter): typeof Adapter | undefined | this {
365+
public adapter(): AdapterConstructor | undefined;
366+
public adapter(v: AdapterConstructor): this;
367+
public adapter(
368+
v?: AdapterConstructor
369+
): AdapterConstructor | undefined | this {
367370
if (!arguments.length) return this._adapter;
368371
this._adapter = v;
369372
for (const nsp of this._nsps.values()) {

lib/namespace.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class Namespace<
102102
* @private
103103
*/
104104
_initAdapter(): void {
105+
// @ts-ignore
105106
this.adapter = new (this.server.adapter()!)(this);
106107
}
107108

test/socket.io.test-d.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"use strict";
2-
import { Server, Socket } from "..";
2+
import { Namespace, Server, Socket } from "..";
33
import type { DefaultEventsMap } from "../lib/typed-events";
44
import { createServer } from "http";
55
import { expectError, expectType } from "tsd";
6+
import { Adapter } from "socket.io-adapter";
67

78
// This file is run by tsd, not mocha.
89

@@ -275,4 +276,25 @@ describe("server", () => {
275276
});
276277
});
277278
});
279+
280+
describe("adapter", () => {
281+
it("accepts arguments of the correct types", () => {
282+
const io = new Server({
283+
adapter: (nsp) => new Adapter(nsp),
284+
});
285+
io.adapter(Adapter);
286+
287+
class MyCustomAdapter extends Adapter {
288+
constructor(nsp, readonly opts) {
289+
super(nsp);
290+
}
291+
}
292+
io.adapter((nsp) => new MyCustomAdapter(nsp, { test: "123" }));
293+
});
294+
295+
it("does not accept arguments of wrong types", () => {
296+
const io = new Server();
297+
expectError(io.adapter((nsp) => "nope"));
298+
});
299+
});
278300
});

0 commit comments

Comments
 (0)